2013-08-08 18:54:33 +09:00
|
|
|
/*=============================================================================
|
|
|
|
Copyright (c) 2011-2013 Bolero MURAKAMI
|
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2012-04-01 22:15:09 +09:00
|
|
|
#ifndef SPROUT_ALGORITHM_BINARY_SEARCH_HPP
|
|
|
|
#define SPROUT_ALGORITHM_BINARY_SEARCH_HPP
|
|
|
|
|
2013-01-11 03:17:06 +09:00
|
|
|
#include <iterator>
|
2012-04-01 22:15:09 +09:00
|
|
|
#include <sprout/config.hpp>
|
2012-12-15 23:48:52 +09:00
|
|
|
#include <sprout/algorithm/lower_bound.hpp>
|
2013-09-02 22:47:15 +09:00
|
|
|
#include <sprout/functional/less.hpp>
|
2012-04-01 22:15:09 +09:00
|
|
|
|
|
|
|
namespace sprout {
|
2012-12-15 23:48:52 +09: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 22:15:09 +09:00
|
|
|
|
|
|
|
// 25.4.3.4 binary_search
|
2012-12-15 23:48:52 +09:00
|
|
|
//
|
|
|
|
// recursion depth:
|
2013-01-11 03:17:06 +09:00
|
|
|
// O(log N)
|
2012-12-15 23:48:52 +09:00
|
|
|
//
|
|
|
|
template<typename ForwardIterator, typename T, typename Compare>
|
2012-10-06 13:53:07 +09:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
2012-12-15 23:48:52 +09:00
|
|
|
binary_search(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
|
2013-09-02 22:47:15 +09:00
|
|
|
return sprout::detail::binary_search_impl(sprout::lower_bound(first, last, value, comp), last, value, comp);
|
2012-04-01 22:15:09 +09:00
|
|
|
}
|
|
|
|
|
2012-12-15 23:48:52 +09:00
|
|
|
template<typename ForwardIterator, typename T>
|
2012-10-06 13:53:07 +09:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
2012-12-15 23:48:52 +09:00
|
|
|
binary_search(ForwardIterator first, ForwardIterator last, T const& value) {
|
2013-09-02 22:47:15 +09:00
|
|
|
return sprout::binary_search(first, last, value, sprout::less<>());
|
2012-04-01 22:15:09 +09:00
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_BINARY_SEARCH_HPP
|