in include/range/v3/algorithm/search_n.hpp [53:86]
static I sized_impl(I const begin_, S end, D const d_, D count,
V const &val, C &pred, P &proj)
{
D d = d_; // always the distance from begin to end
auto begin = uncounted(begin_);
while(true)
{
// Find begin element in sequence 1 that matches val, with a mininum of loop checks
while(true)
{
if(d < count) // return the end if we've run out of room
return ranges::next(recounted(begin_, std::move(begin), d_ - d), std::move(end));
if(pred(proj(*begin), val))
break;
++begin;
--d;
}
// *begin matches val, now match elements after here
auto m = begin;
D c = 0;
while(true)
{
if(++c == count) // If pattern exhausted, begin is the answer (works for 1 element pattern)
return recounted(begin_, std::move(begin), d_ - d);
++m; // No need to check, we know we have room to match successfully
if(!pred(proj(*m), val)) // if there is a mismatch, restart with a new begin
{
begin = next(std::move(m));
d -= (c+1);
break;
} // else there is a match, check next elements
}
}
}