Horizon
partition_point.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2014-present
5 // Copyright Casey Carter 2016
6 //
7 // Use, modification and distribution is subject to the
8 // Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
11 //
12 // Project home: https://github.com/ericniebler/range-v3
13 //
14 //===-------------------------- algorithm ---------------------------------===//
15 //
16 // The LLVM Compiler Infrastructure
17 //
18 // This file is dual licensed under the MIT and the University of Illinois Open
19 // Source Licenses. See LICENSE.TXT for details.
20 //
21 //===----------------------------------------------------------------------===//
22 #ifndef RANGES_V3_ALGORITHM_PARTITION_POINT_HPP
23 #define RANGES_V3_ALGORITHM_PARTITION_POINT_HPP
24 
25 #include <meta/meta.hpp>
26 
27 #include <range/v3/range_fwd.hpp>
28 
39 #include <range/v3/utility/static_const.hpp>
40 
41 #include <range/v3/detail/prologue.hpp>
42 
43 namespace ranges
44 {
47 
48  RANGES_FUNC_BEGIN(partition_point)
49 
50 
51  template(typename I, typename S, typename C, typename P = identity)(
52  requires forward_iterator<I> AND sentinel_for<S, I> AND
53  indirect_unary_predicate<C, projected<I, P>>)
54  constexpr I RANGES_FUNC(partition_point)(I first, S last, C pred, P proj = P{})
55  {
56  if(RANGES_CONSTEXPR_IF(sized_sentinel_for<S, I>))
57  {
58  auto len = distance(first, std::move(last));
59  return aux::partition_point_n(
60  std::move(first), len, std::move(pred), std::move(proj));
61  }
62 
63  // Probe exponentially for either last-of-range or an iterator
64  // that is past the partition point (i.e., does not satisfy pred).
65  auto len = iter_difference_t<I>{1};
66  while(true)
67  {
68  auto mid = first;
69  auto d = advance(mid, len, last);
70  if(mid == last || !invoke(pred, invoke(proj, *mid)))
71  {
72  len -= d;
73  return aux::partition_point_n(
74  std::move(first), len, ranges::ref(pred), ranges::ref(proj));
75  }
76  first = std::move(mid);
77  len *= 2;
78  }
79  }
80 
82  template(typename Rng, typename C, typename P = identity)(
83  requires forward_range<Rng> AND
84  indirect_unary_predicate<C, projected<iterator_t<Rng>, P>>)
85  constexpr borrowed_iterator_t<Rng> //
86  RANGES_FUNC(partition_point)(Rng && rng, C pred, P proj = P{}) //
87  {
88  if(RANGES_CONSTEXPR_IF(sized_range<Rng>))
89  {
90  auto len = distance(rng);
91  return aux::partition_point_n(
92  begin(rng), len, std::move(pred), std::move(proj));
93  }
94  return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
95  }
96 
97  RANGES_FUNC_END(partition_point)
98 
99  namespace cpp20
100  {
101  using ranges::partition_point;
102  }
104 } // namespace ranges
105 
106 #include <range/v3/detail/epilogue.hpp>
107 
108 #endif
CPP_concept indirect_unary_predicate
\concept indirect_unary_predicate
Definition: concepts.hpp:632
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
Tiny meta-programming library.