On 07/24/2017 11:19 AM, Suliman wrote:
> Why D have two function `contains` and `canFind` if C# have only
> contains and it's enough?

std.algorithm.canFind and std.range.SortedRange.contains:

  https://dlang.org/phobos/std_algorithm_searching.html#.canFind

  https://dlang.org/phobos/std_range.html#.SortedRange.contains

canFind() is more general because it works with any input range. For that reason, it has to walk from the beginning to the end. (O(N) complexity)

contains() requires a sorted random access range so that it can use the O(log N) binary search algorithm.

When you have a large sorted array, use contains(). Even if the array is not sorted but there are many searches to be performed, sorting first and then calling contains() many times may be faster than canFind().

Even if the array is not sorted but it's small, you can use canFind() without worrying about performance. Of course it all depends on what small and large mean. :)

Ali

Reply via email to