Debian Codegraph @

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

raw
1 /// \file
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 #ifndef RANGES_V3_ALGORITHM_REPLACE_COPY_HPP
14 #define RANGES_V3_ALGORITHM_REPLACE_COPY_HPP
15
16 #include <meta/meta.hpp>
17
18 #include <range/v3/range_fwd.hpp>
19
20 #include <range/v3/algorithm/result_types.hpp>
21 #include <range/v3/functional/identity.hpp>
22 #include <range/v3/functional/invoke.hpp>
23 #include <range/v3/iterator/concepts.hpp>
24 #include <range/v3/iterator/traits.hpp>
25 #include <range/v3/range/access.hpp>
26 #include <range/v3/range/concepts.hpp>
27 #include <range/v3/range/dangling.hpp>
28 #include <range/v3/range/traits.hpp>
29 #include <range/v3/utility/static_const.hpp>
30
31 #include <range/v3/detail/prologue.hpp>
32
33 namespace ranges
34 {
35 /// \addtogroup group-algorithms
36 /// @{
37 template<typename I, typename O>
38 using replace_copy_result = detail::in_out_result<I, O>;
39
40 RANGES_FUNC_BEGIN(replace_copy)
41
42 /// \brief function template \c replace_copy
43 template(typename I,
44 typename S,
45 typename O,
46 typename T1,
47 typename T2,
48 typename P = identity)(
49 requires input_iterator<I> AND sentinel_for<S, I> AND
50 output_iterator<O, T2 const &> AND indirectly_copyable<I, O> AND
51 indirect_relation<equal_to, projected<I, P>, T1 const *>)
52 constexpr replace_copy_result<I, O> RANGES_FUNC(replace_copy)(I first,
53 S last,
54 O out,
55 T1 const & old_value,
56 T2 const & new_value,
57 P proj = {}) //
58 {
59 for(; first != last; ++first, ++out)
60 {
61 auto && x = *first;
62 if(invoke(proj, x) == old_value)
63 *out = new_value;
64 else
65 *out = (decltype(x) &&)x;
66 }
67 return {first, out};
68 }
69
70 /// \overload
71 template(typename Rng,
72 typename O,
73 typename T1,
74 typename T2,
75 typename P = identity)(
76 requires input_range<Rng> AND output_iterator<O, T2 const &> AND
77 indirectly_copyable<iterator_t<Rng>, O> AND
78 indirect_relation<equal_to, projected<iterator_t<Rng>, P>, T1 const *>)
79 constexpr replace_copy_result<borrowed_iterator_t<Rng>, O> RANGES_FUNC(replace_copy)(
80 Rng && rng, O out, T1 const & old_value, T2 const & new_value, P proj = {}) //
81 {
82 return (*this)(begin(rng),
83 end(rng),
84 std::move(out),
85 old_value,
86 new_value,
87 std::move(proj));
88 }
89
90 RANGES_FUNC_END(replace_copy)
91
92 namespace cpp20
93 {
94 using ranges::replace_copy;
95 using ranges::replace_copy_result;
96 } // namespace cpp20
97 /// @}
98 } // namespace ranges
99
100 #include <range/v3/detail/epilogue.hpp>
101
102 #endif