Horizon
replace_copy.hpp
Go to the documentation of this file.
1 // 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>
29 #include <range/v3/utility/static_const.hpp>
30 
31 #include <range/v3/detail/prologue.hpp>
32 
33 namespace 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)(
76  requires input_range<Rng> AND output_iterator<O, T2 const &> 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
98 } // namespace ranges
99 
100 #include <range/v3/detail/epilogue.hpp>
101 
102 #endif
template(typename Rng, typename O, typename T1, typename T2, typename P=identity)(requires input_range< Rng > AND output_iterator< O
This is an overloaded member function, provided for convenience. It differs from the above function o...
CPP_concept sentinel_for
\concept sentinel_for
Definition: concepts.hpp:306
CPP_concept output_iterator
\concept output_iterator
Definition: concepts.hpp:347
CPP_concept input_iterator
\concept input_iterator
Definition: concepts.hpp:362
CPP_concept indirect_relation
\concept indirect_relation
Definition: concepts.hpp:670
CPP_concept indirectly_copyable
\concept indirectly_copyable
Definition: concepts.hpp:782
decltype(begin(declval(Rng &))) iterator_t
Definition: access.hpp:698
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.
Definition: comparisons.hpp:28
Definition: identity.hpp:25