in include/range/v3/algorithm/search_n.hpp [89:118]
static I impl(I begin, S end, D count, V const &val, C &pred, P &proj)
{
while(true)
{
// Find begin element in sequence 1 that matches val, with a mininum of loop checks
while(true)
{
if(begin == end) // return end if no element matches val
return begin;
if(pred(proj(*begin), val))
break;
++begin;
}
// *begin matches val, now match elements after here
I m = begin;
D c = 0;
while(true)
{
if(++c == count) // If pattern exhausted, begin is the answer (works for 1 element pattern)
return begin;
if(++m == end) // Otherwise if source exhausted, pattern not found
return m;
if(!pred(proj(*m), val)) // if there is a mismatch, restart with a new begin
{
begin = next(std::move(m));
break;
} // else there is a match, check next elements
}
}
}