Sprout/sprout/algorithm/binary_search.hpp

40 lines
1.4 KiB
C++
Raw Normal View History

2012-04-01 13:15:09 +00:00
#ifndef SPROUT_ALGORITHM_BINARY_SEARCH_HPP
#define SPROUT_ALGORITHM_BINARY_SEARCH_HPP
#include <sprout/config.hpp>
2012-12-15 14:48:52 +00:00
#include <sprout/algorithm/lower_bound.hpp>
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
2012-04-01 13:15:09 +00:00
namespace sprout {
2012-12-15 14:48:52 +00:00
namespace detail {
template<typename ForwardIterator, typename T, typename Compare>
inline SPROUT_CONSTEXPR bool
binary_search_impl(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
return (first != last && !comp(value, *first));
}
} // namespace detail
2012-04-01 13:15:09 +00:00
// 25.4.3.4 binary_search
2012-12-15 14:48:52 +00:00
//
// recursion depth:
// [first, last) is RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename ForwardIterator, typename T, typename Compare>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR bool
2012-12-15 14:48:52 +00:00
binary_search(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
return sprout::detail::binary_search_impl(sprout::lower_bound(first, last, value), last, value, comp);
2012-04-01 13:15:09 +00:00
}
2012-12-15 14:48:52 +00:00
template<typename ForwardIterator, typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR bool
2012-12-15 14:48:52 +00:00
binary_search(ForwardIterator first, ForwardIterator last, T const& value) {
return sprout::binary_search(
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_BINARY_SEARCH_HPP