www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - generic indexOf() for arrays ?

reply "M.Gore" <mgoremeier googlemail.com> writes:
I'd like to know if there's a generic function over arrays to 
find the index of a specific elemnt therein, something like, say:

int indexOf(S) (in S[] arr, S elem);

which works the same way the std.string.indexOf() function 
works... couldn't find anything in the std.array module for this 
scenario. Would be nice to have this functionality built-in 
somehow.

Or is there a completely different / better approach to this in D?
Thx, M.
Apr 27 2012
parent reply "Brad Anderson" <eco gnuk.net> writes:
On Friday, 27 April 2012 at 19:49:33 UTC, M.Gore wrote:
 I'd like to know if there's a generic function over arrays to 
 find the index of a specific elemnt therein, something like, 
 say:

 int indexOf(S) (in S[] arr, S elem);

 which works the same way the std.string.indexOf() function 
 works... couldn't find anything in the std.array module for 
 this scenario. Would be nice to have this functionality 
 built-in somehow.

 Or is there a completely different / better approach to this in 
 D?
 Thx, M.
countUntil in std.algorithm should work fine. http://dlang.org/phobos/std_algorithm.html#countUntil Regards, Brad Anderson
Apr 27 2012
next sibling parent "M.Gore" <mgoremeier googlemail.com> writes:
 countUntil in std.algorithm should work fine.

 http://dlang.org/phobos/std_algorithm.html#countUntil
Yup, it does! Just didn't see it there in the algorithm collection. Thanks a lot.
Apr 27 2012
prev sibling parent reply Ary Manzana <ary esperanto.org.ar> writes:
On 4/28/12 3:51 AM, Brad Anderson wrote:
 On Friday, 27 April 2012 at 19:49:33 UTC, M.Gore wrote:
 I'd like to know if there's a generic function over arrays to find the
 index of a specific elemnt therein, something like, say:

 int indexOf(S) (in S[] arr, S elem);

 which works the same way the std.string.indexOf() function works...
 couldn't find anything in the std.array module for this scenario.
 Would be nice to have this functionality built-in somehow.

 Or is there a completely different / better approach to this in D?
 Thx, M.
countUntil in std.algorithm should work fine. http://dlang.org/phobos/std_algorithm.html#countUntil Regards, Brad Anderson
--- sizediff_t indexOf(alias pred = "a == b", R1, R2)(R1 haystack, R2 needle); Scheduled for deprecation. Please use std.algorithm.countUntil instead. Same as countUntil. This symbol has been scheduled for deprecation because it is easily confused with the homonym function in std.string. --- But isn't it the same funcionality? Why use a different name for the same funcionality?
Apr 27 2012
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, April 28, 2012 12:18:57 Ary Manzana wrote:
 But isn't it the same funcionality? Why use a different name for the
 same funcionality?
It's _not_ the same functionality, even if it's similar. std.string.indexOf varies drastically from std.algorithm.countUntil and in a way that easily leads to confusion if they're both named indexOf. std.array.indexOf returns the index of the string that you passed in. That number doesn't say _anything_ about how many characters are before that sub- string. If str.indexOf("hello") returns 6, that's index 6, but it could be the third character rather than the sixth, because strings are UTF-8 and therefore variably-lengthed encoded. std.algorithm.countUntil returns the number of elements in the range up to the range passed in and does not necessarily have any relation with any index (particularly if the range isn't random access). In the case of str.countUntil("hello"), it will return one minus the number of the character that "hello" starts at. So, if it's the third character, then the result will be 2, whereas the result of std.string.indexOf could have been 6. Also, the result of indexOf isn't necessarily an index (particularly in the case of strings - but also in the case where a range is not random access), so calling countUntil indexOf is arguably a poor choice. - Jonathan M Davis
Apr 27 2012