Debian Codegraph @

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

raw
1 /// \file
2 // Range v3 library
3 //
4 // Copyright Eric Niebler
5 // Copyright Christopher Di Bella
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_ADJACENT_REMOVE_IF_HPP
15 #define RANGES_V3_ALGORITHM_ADJACENT_REMOVE_IF_HPP
16
17 #include <functional>
18 #include <utility>
19
20 #include <range/v3/range_fwd.hpp>
21
22 #include <range/v3/algorithm/adjacent_find.hpp>
23 #include <range/v3/algorithm/move.hpp>
24 #include <range/v3/functional/identity.hpp>
25 #include <range/v3/functional/invoke.hpp>
26 #include <range/v3/iterator/concepts.hpp>
27 #include <range/v3/iterator/traits.hpp>
28 #include <range/v3/range/access.hpp>
29 #include <range/v3/range/concepts.hpp>
30 #include <range/v3/range/traits.hpp>
31 #include <range/v3/utility/move.hpp>
32 #include <range/v3/utility/static_const.hpp>
33
34 #include <range/v3/detail/prologue.hpp>
35
36 namespace ranges
37 {
38 /// \addtogroup group-algorithms
39 /// @{
40 RANGES_FUNC_BEGIN(adjacent_remove_if)
41 /// \brief function \c adjacent_remove_if
42 ///
43 /// range-based version of the \c adjacent_remove_if algorithm
44 ///
45 /// \pre `Rng` is a model of the `forward_range` concept.
46 /// \pre `Pred` is a model of the `BinaryPredicate` concept.
47 template(typename I, typename S, typename Pred, typename Proj = identity)(
48 requires permutable<I> AND sentinel_for<S, I> AND
49 indirect_relation<Pred, projected<I, Proj>>)
50 constexpr I RANGES_FUNC(adjacent_remove_if)(I first, S last, Pred pred = {}, Proj proj = {})
51 {
52 first = adjacent_find(std::move(first), last, ranges::ref(pred), ranges::ref(proj));
53 if(first == last)
54 return first;
55
56 auto i = first;
57 for(auto j = ++i; ++j != last; ++i)
58 {
59 if(!invoke(pred, invoke(proj, *i), invoke(proj, *j)))
60 {
61 *first = iter_move(i);
62 ++first;
63 }
64 }
65
66 *first = iter_move(i);
67 ++first;
68 return first;
69 }
70
71 /// \overload
72 template(typename Rng, typename Pred, typename Proj = identity)(
73 requires forward_range<Rng> AND
74 indirect_relation<Pred, projected<iterator_t<Rng>, Proj>> AND
75 permutable<iterator_t<Rng>>)
76 constexpr borrowed_iterator_t<Rng>
77 RANGES_FUNC(adjacent_remove_if)(Rng && rng, Pred pred, Proj proj = {}) //
78 {
79 return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
80 }
81 RANGES_FUNC_END(adjacent_remove_if)
82
83 namespace cpp20
84 {
85 using ranges::adjacent_remove_if;
86 }
87 /// @}
88 } // namespace ranges
89
90 #include <range/v3/detail/epilogue.hpp>
91
92 #endif // RANGES_V3_ALGORITHM_ADJACENT_REMOVE_IF_HPP