Skip to content

Instantly share code, notes, and snippets.

@goldsborough
Created December 11, 2015 23:39
Show Gist options
  • Save goldsborough/e066cf600496f52d1822 to your computer and use it in GitHub Desktop.
Save goldsborough/e066cf600496f52d1822 to your computer and use it in GitHub Desktop.
Generic TMP lower_bound_if, upper_bound_if and equal_range_if for any predicates and conditions.
template<
typename Iterator,
typename Predicate,
typename Condition
>
Iterator lower_bound_if(Iterator begin,
Iterator end,
Predicate predicate,
Condition condition)
{
if (begin == end) return end;
auto pivot = begin;
std::advance(pivot, std::distance(begin, end)/2);
if (predicate(*pivot) || condition(*pivot))
{
return lower_bound_if(begin, pivot, predicate, condition);
}
else return lower_bound_if(++pivot, end, predicate, condition);
}
template<
typename Iterator,
typename Predicate,
typename Condition
>
Iterator upper_bound_if(Iterator begin,
Iterator end,
Predicate predicate,
Condition condition)
{
if (begin == end) return end;
auto pivot = begin;
std::advance(pivot, std::distance(begin, end)/2);
if (! predicate(*pivot) && condition(*pivot))
{
return upper_bound_if(begin, pivot, predicate, condition);
}
else return upper_bound_if(++pivot, end, predicate, condition);
}
template<
typename Iterator,
typename Predicate,
typename Condition
>
std::pair<Iterator, Iterator> equal_range_if(Iterator begin,
Iterator end,
Predicate predicate,
Condition condition)
{
return {
lower_bound_if(begin, end, predicate, condition),
upper_bound_if(begin, end, predicate, condition)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment