find

find needle in haystack using predictive search

Nullable!size_t
find
(
R
T
)
if (
is(ReturnType!(
(
R r
,
T t
,
size_t index
)
=> r[index] == t
) : bool) &&
is(ReturnType!(
(
R r
,
T t
)
=> r[0] < t
) : bool)
&&
is(ReturnType!(
(
R r
,
T t
)
=> r[0] > t
) : bool)
&&
is(ReturnType!(
(
R r
)
=> r.length
) : size_t)
&&
is(ReturnType!(
(
R r
,
size_t i1
,
size_t i2
)
=> cast(size_t)(i2 + (r[0] - r[1]).to!double / (r[2] - r[1]).to!double * (i1 - i2))
) : size_t)
)

Examples

auto array = [1, 2, 3, 4];
auto res = find(array, 1);
assert(!res.isNull);
assert(res.get == 0);
res = find(array, 4UL);
assert(!res.isNull);
assert(res.get == 3);
assert(find(array, 5).isNull);
auto array = [4, 5, 41, 44, 45, 48, 60, 72, 76,
               93, 102, 130, 132, 155, 174, 213, 230,
               247, 258, 266, 266, 272, 282, 304, 315,
               325, 334, 344, 350, 357, 369, 372, 376,
               382, 386, 426, 429, 454, 466, 472, 476,
               477, 478, 484, 508, 529, 539, 551, 571,
               574, 575, 581, 596, 619, 624, 629, 634,
               638, 653, 679, 680, 684, 684, 688, 698,
               729, 731, 746, 761, 762, 769, 779, 791,
               800, 806, 808, 818, 824, 828, 831, 833,
               835, 846, 857, 864, 867, 873, 885, 898,
               900, 918, 926, 936, 938, 941, 960, 977,
               981, 986, 989];
auto res = find(array, 155);
assert(!res.isNull);
assert(res.get == 13);
assert(find(array, 819).isNull);

Meta