Debian Codegraph @

3rd_party/range-v3/range/v3/algorithm/binary_search.hpp

raw
1 /// \file
2 // 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
21 #include <range/v3/algorithm/lower_bound.hpp>
22 #include <range/v3/functional/comparisons.hpp>
23 #include <range/v3/functional/identity.hpp>
24 #include <range/v3/functional/invoke.hpp>
25 #include <range/v3/iterator/concepts.hpp>
26 #include <range/v3/iterator/traits.hpp>
27 #include <range/v3/range/access.hpp>
28 #include <range/v3/range/concepts.hpp>
29 #include <range/v3/range/traits.hpp>
30 #include <range/v3/utility/static_const.hpp>
31
32 #include <range/v3/detail/prologue.hpp>
33
34 namespace ranges
35 {
36 /// \addtogroup group-algorithms
37 /// @{
38 RANGES_FUNC_BEGIN(binary_search)
39 /// \brief function template \c binary_search
40 ///
41 /// range-based version of the \c binary_search std algorithm
42 ///
43 /// \pre `Rng` is a model of the `range` concept
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
59 /// \overload
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 }
76 /// @}
77 } // namespace ranges
78
79 #include <range/v3/detail/epilogue.hpp>
80
81 #endif