Horizon
Loading...
Searching...
No Matches
partition.hpp
Go to the documentation of this file.
1
2// Range v3 library
3//
4// Copyright Eric Niebler 2014-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//===-------------------------- algorithm ---------------------------------===//
14//
15// The LLVM Compiler Infrastructure
16//
17// This file is dual licensed under the MIT and the University of Illinois Open
18// Source Licenses. See LICENSE.TXT for details.
19//
20//===----------------------------------------------------------------------===//
21#ifndef RANGES_V3_ALGORITHM_PARTITION_HPP
22#define RANGES_V3_ALGORITHM_PARTITION_HPP
23
24#include <meta/meta.hpp>
25
27
37#include <range/v3/utility/static_const.hpp>
39
40#include <range/v3/detail/prologue.hpp>
41
42namespace ranges
43{
46
48 namespace detail
49 {
50 template<typename I, typename S, typename C, typename P>
51 constexpr I partition_impl(I first, S last, C pred, P proj, std::forward_iterator_tag)
52 {
53 while(true)
54 {
55 if(first == last)
56 return first;
57 if(!invoke(pred, invoke(proj, *first)))
58 break;
59 ++first;
60 }
61 for(I p = first; ++p != last;)
62 {
63 if(invoke(pred, invoke(proj, *p)))
64 {
65 ranges::iter_swap(first, p);
66 ++first;
67 }
68 }
69 return first;
70 }
71
72 template<typename I, typename S, typename C, typename P>
73 constexpr I partition_impl(I first, S end_, C pred, P proj, std::bidirectional_iterator_tag)
74 {
75 I last = ranges::next(first, end_);
76 while(true)
77 {
78 while(true)
79 {
80 if(first == last)
81 return first;
82 if(!invoke(pred, invoke(proj, *first)))
83 break;
84 ++first;
85 }
86 do
87 {
88 if(first == --last)
89 return first;
90 } while(!invoke(pred, invoke(proj, *last)));
91 ranges::iter_swap(first, last);
92 ++first;
93 }
94 }
95 } // namespace detail
97
98 RANGES_FUNC_BEGIN(partition)
99
100
101 template(typename I, typename S, typename C, typename P = identity)(
102 requires permutable<I> AND sentinel_for<S, I> AND
103 indirect_unary_predicate<C, projected<I, P>>)
104 constexpr I RANGES_FUNC(partition)(I first, S last, C pred, P proj = P{})
105 {
106 return detail::partition_impl(std::move(first),
107 std::move(last),
108 std::move(pred),
109 std::move(proj),
110 iterator_tag_of<I>());
111 }
112
114 template(typename Rng, typename C, typename P = identity)(
115 requires forward_range<Rng> AND permutable<iterator_t<Rng>> AND
116 indirect_unary_predicate<C, projected<iterator_t<Rng>, P>>)
117 constexpr borrowed_iterator_t<Rng> RANGES_FUNC(partition)(Rng && rng, C pred, P proj = P{})
118 {
119 return detail::partition_impl(begin(rng),
120 end(rng),
121 std::move(pred),
122 std::move(proj),
123 iterator_tag_of<iterator_t<Rng>>());
124 }
125
126 RANGES_FUNC_END(partition)
127
128 namespace cpp20
129 {
130 using ranges::partition;
131 }
133} // namespace ranges
134
135#include <range/v3/detail/epilogue.hpp>
136
137#endif
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.