Debian Codegraph @

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

raw
1 /// \file
2 // Range v3 library
3 //
4 // Copyright Andrey Diduh 2019
5 // Copyright Casey Carter 2019
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 #ifndef RANGES_V3_ALGORITHM_UNSTABLE_REMOVE_IF_HPP
15 #define RANGES_V3_ALGORITHM_UNSTABLE_REMOVE_IF_HPP
16
17 #include <functional>
18 #include <utility>
19
20 #include <concepts/concepts.hpp>
21
22 #include <range/v3/range_fwd.hpp>
23
24 #include <range/v3/algorithm/find_if.hpp>
25 #include <range/v3/algorithm/find_if_not.hpp>
26 #include <range/v3/functional/identity.hpp>
27 #include <range/v3/iterator/concepts.hpp>
28 #include <range/v3/iterator/operations.hpp>
29 #include <range/v3/iterator/reverse_iterator.hpp>
30 #include <range/v3/range/access.hpp>
31 #include <range/v3/range/concepts.hpp>
32 #include <range/v3/utility/move.hpp>
33 #include <range/v3/utility/static_const.hpp>
34
35 #include <range/v3/detail/prologue.hpp>
36
37 namespace ranges
38 {
39 /// \addtogroup group-algorithms
40 /// @{
41
42 /// unstable_remove have O(1) complexity for each element remove, unlike remove O(n)
43 /// [for worst case]. Each erased element overwritten (moved in) with last one.
44 /// unstable_remove_if does not preserve relative element order.
45 RANGES_FUNC_BEGIN(unstable_remove_if)
46
47 /// \brief function template \c unstable_remove_if
48 template(typename I, typename C, typename P = identity)(
49 requires bidirectional_iterator<I> AND permutable<I> AND
50 indirect_unary_predicate<C, projected<I, P>>)
51 constexpr I RANGES_FUNC(unstable_remove_if)(I first, I last, C pred, P proj = {})
52 {
53 while(true)
54 {
55 first = find_if(std::move(first), last, ranges::ref(pred), ranges::ref(proj));
56 if(first == last)
57 return first;
58
59 last = next(find_if_not(make_reverse_iterator(std::move(last)),
60 make_reverse_iterator(next(first)),
61 ranges::ref(pred),
62 ranges::ref(proj)))
63 .base();
64 if(first == last)
65 return first;
66
67 *first = iter_move(last);
68
69 // discussion here: https://github.com/ericniebler/range-v3/issues/988
70 ++first;
71 }
72 }
73
74 /// \overload
75 template(typename Rng, typename C, typename P = identity)(
76 requires bidirectional_range<Rng> AND common_range<Rng> AND
77 permutable<iterator_t<Rng>> AND
78 indirect_unary_predicate<C, projected<iterator_t<Rng>, P>>)
79 constexpr borrowed_iterator_t<Rng> //
80 RANGES_FUNC(unstable_remove_if)(Rng && rng, C pred, P proj = P{}) //
81 {
82 return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
83 }
84
85 RANGES_FUNC_END(unstable_remove_if)
86 /// @}
87 } // namespace ranges
88
89 #include <range/v3/detail/epilogue.hpp>
90
91 #endif // RANGES_V3_ALGORITHM_UNSTABLE_REMOVE_IF_HPP