Sprout/sprout/algorithm/upper_bound.hpp

40 lines
1.4 KiB
C++
Raw Normal View History

2012-04-01 13:15:09 +00:00
#ifndef SPROUT_ALGORITHM_UPPER_BOUND_HPP
#define SPROUT_ALGORITHM_UPPER_BOUND_HPP
2012-12-15 14:48:52 +00:00
#include <iterator>
2012-04-01 13:15:09 +00:00
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
2012-12-15 14:48:52 +00:00
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
2012-04-01 13:15:09 +00:00
namespace sprout {
// Copyright (C) 2011 RiSK (sscrisk)
// 25.4.3.2 upper_bound
2012-12-15 14:48:52 +00:00
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
2012-04-01 13:15:09 +00:00
template<typename ForwardIterator, typename T, typename Compare>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR ForwardIterator
upper_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
2012-04-01 13:15:09 +00:00
return first == last ? last
: sprout::next(first) == last ? !comp(value, *first) ? last : first
: !comp(value, *sprout::next(first, sprout::distance(first, last) / 2))
? sprout::upper_bound(sprout::next(first, sprout::distance(first, last) / 2), last, value, comp)
: sprout::upper_bound(first, sprout::next(first, sprout::distance(first, last) / 2), value, comp)
2012-04-01 13:15:09 +00:00
;
}
2012-12-15 14:48:52 +00:00
template<typename ForwardIterator, typename T>
inline SPROUT_CONSTEXPR ForwardIterator
upper_bound(ForwardIterator first, ForwardIterator last, T const& value) {
return sprout::upper_bound(
first, last, value,
NS_SSCRISK_CEL_OR_SPROUT::less<typename std::iterator_traits<ForwardIterator>::value_type>()
);
}
2012-04-01 13:15:09 +00:00
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_UPPER_BOUND_HPP