Created
December 11, 2015 23:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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