Horizon
Loading...
Searching...
No Matches
replace_copy.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#ifndef RANGES_V3_ALGORITHM_REPLACE_COPY_HPP
14#define RANGES_V3_ALGORITHM_REPLACE_COPY_HPP
15
16#include <meta/meta.hpp>
17
19
20#include <range/v3/algorithm/result_types.hpp>
29#include <range/v3/utility/static_const.hpp>
30
31#include <range/v3/detail/prologue.hpp>
32
33namespace ranges
34{
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
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
71 template(typename Rng,
72 typename O,
73 typename T1,
74 typename T2,
75 typename P = identity)(
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
98} // namespace ranges
99
100#include <range/v3/detail/epilogue.hpp>
101
102#endif
The indirect_relation concept.
The indirectly_copyable concept.
The input_iterator concept.
The input_range concept.
The output_iterator concept.
The sentinel_for concept.
decltype(begin(declval(Rng &))) iterator_t
Definition access.hpp:698
front< Pair > first
Retrieve the first element of the pair Pair.
Definition meta.hpp:2251
Tiny meta-programming library.
Definition comparisons.hpp:28
Definition identity.hpp:25