Skip to content

Commit

Permalink
Merge pull request #8845 from ntrel/canFind-needles
Browse files Browse the repository at this point in the history
Fix Issue 11111 - std.algorithm.canFind should support Needles...
  • Loading branch information
dkorpel authored Nov 20, 2023
2 parents 585ddbe + edc5bbd commit 7417040
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions std/algorithm/searching.d
Original file line number Diff line number Diff line change
Expand Up @@ -2376,9 +2376,9 @@ is considered to be 1.) The strategy used in searching several
subranges at once maximizes cache usage by moving in `haystack` as
few times as possible.
*/
Tuple!(Range, size_t) find(alias pred = "a == b", Range, Ranges...)
(Range haystack, Ranges needles)
if (Ranges.length > 1 && is(typeof(startsWith!pred(haystack, needles))))
Tuple!(Range, size_t) find(alias pred = "a == b", Range, Needles...)
(Range haystack, Needles needles)
if (Needles.length > 1 && is(typeof(startsWith!pred(haystack, needles))))
{
for (;; haystack.popFront())
{
Expand Down Expand Up @@ -2572,9 +2572,8 @@ template canFind(alias pred="a == b")
without having to deal with the tuple that $(LREF find) returns for the
same operation.
+/
size_t canFind(Range, Ranges...)(Range haystack, scope Ranges needles)
if (Ranges.length > 1 &&
allSatisfy!(isForwardRange, Ranges) &&
size_t canFind(Range, Needles...)(Range haystack, scope Needles needles)
if (Needles.length > 1 &&
is(typeof(find!pred(haystack, needles))))
{
return find!pred(haystack, needles)[1];
Expand All @@ -2589,6 +2588,10 @@ template canFind(alias pred="a == b")
assert(!canFind(arr, 4));

// find one of several needles
assert(arr.canFind(3, 2));
assert(arr.canFind(3, 2) == 2); // second needle found
assert(arr.canFind([1, 3], 2) == 2);

assert(canFind(arr, [1, 2], [2, 3]));
assert(canFind(arr, [1, 2], [2, 3]) == 1);
assert(canFind(arr, [1, 7], [2, 3]));
Expand Down

0 comments on commit 7417040

Please sign in to comment.