Skip to content

Commit

Permalink
Optimization in startsWith(Range, Needles...)
Browse files Browse the repository at this point in the history
Basically, makes it stop searching as soon as an element as matched.

This is an important optimization, that avoids a lot of startsWith's other "non-optimzations"...
  • Loading branch information
monarchdodra committed Dec 26, 2012
1 parent cfcdfbb commit d00be2f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions std/algorithm.d
Original file line number Diff line number Diff line change
Expand Up @@ -4603,10 +4603,11 @@ if (isInputRange!Range && Needles.length > 1 &&
// Single-element
if (binaryFun!pred(haystack.front, needles[i]))
{
// found, but continue to account for one-element
// found, but instead of returning, we just stop searching.
// This is to account for one-element
// range matches (consider startsWith("ab", "a",
// 'a') should return 1, not 2).
continue;
break;
}
}
else
Expand All @@ -4625,8 +4626,11 @@ if (isInputRange!Range && Needles.length > 1 &&
}

// If execution reaches this point, then the front matches for all
// needles ranges. What we need to do now is to lop off the front of
// all ranges involved and recurse.
// needle ranges, or a needle element has been matched.
// What we need to do now is iterate, lopping off the front of
// the range and checking if the result is empty, or finding an
// element needle and returning.
// If neither happens, we drop to the end and loop.
foreach (i, Unused; Needles)
{
static if (is(typeof(binaryFun!pred(haystack.front, needles[i])) : bool))
Expand Down

0 comments on commit d00be2f

Please sign in to comment.