Horizon
binary_search.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2013-present
5 //
6 // Use, modification and distribution is subject to the
7 // Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // Project home: https://github.com/ericniebler/range-v3
12 //
13 #ifndef RANGES_V3_ALGORITHM_BINARY_SEARCH_HPP
14 #define RANGES_V3_ALGORITHM_BINARY_SEARCH_HPP
15 
16 #include <functional>
17 #include <utility>
18 
19 #include <range/v3/range_fwd.hpp>
20 
30 #include <range/v3/utility/static_const.hpp>
31 
32 #include <range/v3/detail/prologue.hpp>
33 
34 namespace ranges
35 {
38  RANGES_FUNC_BEGIN(binary_search)
44  template(typename I,
45  typename S,
46  typename V,
47  typename C = less,
48  typename P = identity)(
49  requires forward_iterator<I> AND sentinel_for<S, I> AND
50  indirect_strict_weak_order<C, V const *, projected<I, P>>)
51  constexpr bool RANGES_FUNC(binary_search)(
52  I first, S last, V const & val, C pred = C{}, P proj = P{})
53  {
54  first =
55  lower_bound(std::move(first), last, val, ranges::ref(pred), ranges::ref(proj));
56  return first != last && !invoke(pred, val, invoke(proj, *first));
57  }
58 
60  template(typename Rng, typename V, typename C = less, typename P = identity)(
61  requires forward_range<Rng> AND
62  indirect_strict_weak_order<C, V const *, projected<iterator_t<Rng>, P>>)
63  constexpr bool RANGES_FUNC(binary_search)(
64  Rng && rng, V const & val, C pred = C{}, P proj = P{}) //
65  {
66  static_assert(!is_infinite<Rng>::value,
67  "Trying to binary search an infinite range");
68  return (*this)(begin(rng), end(rng), val, std::move(pred), std::move(proj));
69  }
70  RANGES_FUNC_END(binary_search)
71 
72  namespace cpp20
73  {
74  using ranges::binary_search;
75  }
77 } // namespace ranges
78 
79 #include <range/v3/detail/epilogue.hpp>
80 
81 #endif
template(typename Rng, typename V, typename C=less, typename P=identity)(requires forward_range< Rng > AND indirect_strict_weak_order< C
This is an overloaded member function, provided for convenience. It differs from the above function o...
CPP_concept sentinel_for
\concept sentinel_for
Definition: concepts.hpp:306
CPP_concept forward_iterator
\concept forward_iterator
Definition: concepts.hpp:370
CPP_concept indirect_strict_weak_order
\concept indirect_strict_weak_order
Definition: concepts.hpp:689
decltype(begin(declval(Rng &))) iterator_t
Definition: access.hpp:698
typename Fn::template invoke< Args... > invoke
Evaluate the invocable Fn with the arguments Args.
Definition: meta.hpp:541
front< Pair > first
Retrieve the first element of the pair Pair.
Definition: meta.hpp:2251
bool_<(T::type::value< U::type::value)> less
A Boolean integral constant wrapper around true if T::type::value is less than U::type::value; false,...
Definition: meta.hpp:255
Definition: identity.hpp:25
Definition: comparisons.hpp:50