Optionally return position in canFindIndex

This commit is contained in:
Stefan Koch 2018-03-01 09:34:37 +01:00 committed by The Dlang Bot
parent c4a7f9cb04
commit dca63110f7
1 changed files with 23 additions and 4 deletions

View File

@ -1849,9 +1849,28 @@ const pure @safe @nogc:
} }
} }
bool canFindIndex(const size_t[] items, size_t index) pure @safe @nogc bool canFindIndex(const size_t[] items, size_t index, size_t* pos = null) pure @safe @nogc
{ {
import std.range : assumeSorted; import std.range : assumeSorted;
if (!pos)
return !assumeSorted(items).equalRange(index).empty; {
return !assumeSorted(items).equalRange(index).empty;
}
else
{
auto trisection_result = assumeSorted(items).trisect(index);
if (trisection_result[1].length == 1)
{
*pos = trisection_result[0].length;
return true;
}
else if (trisection_result[1].length == 0)
{
return false;
}
else
{
assert(0, "the constraint of having unique locations has been violated");
}
}
} }