Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary Search in D 2.0 (Phobos)?

Is it just me, or is there no binary search function in Phobos? I have a pre-sorted array that I want to search with my own comparator function, but I can't find anything in std.algorithms or std.containers.

Thanks!

like image 723
user541686 Avatar asked Jan 07 '11 03:01

user541686


1 Answers

Use SortedRange from std.range:

Cribbed from http://www.digitalmars.com/d/2.0/phobos/std_range.html#SortedRange:

auto a = [ 1, 2, 3, 42, 52, 64 ];
auto r = assumeSorted(a);
assert(r.canFind(3));
assert(!r.canFind(32));
like image 104
Matt Curtis Avatar answered Sep 23 '22 18:09

Matt Curtis