binarySearch

This function returns the index of the value if it exist among values, size_t.max otherwise.

TODO: Should we extend to isRandomAccessRange support? In that case we don't get static array support by default.

size_t
binarySearch
(
R
E
)
(
const R[] values
,
in E value
)
if (
is(typeof(values[0].init == E.init))
)

Examples

const int[9] x = [1, 3, 5, 6, 8, 9, 10, 13, 15];
assert(x.binarySearch(0) == size_t.max);
assert(x.binarySearch(1) == 0);
assert(x.binarySearch(2) == size_t.max);
assert(x.binarySearch(3) == 1);
assert(x.binarySearch(4) == size_t.max);
assert(x.binarySearch(5) == 2);
assert(x.binarySearch(6) == 3);
assert(x.binarySearch(7) == size_t.max);
assert(x.binarySearch(8) == 4);
assert(x.binarySearch(9) == 5);
assert(x.binarySearch(10) == 6);
assert(x.binarySearch(11) == size_t.max);
assert(x.binarySearch(12) == size_t.max);
assert(x.binarySearch(13) == 7);
assert(x.binarySearch(14) == size_t.max);
assert(x.binarySearch(15) == 8);

Meta