| 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_IF_HPP |
| 14 |
#define RANGES_V3_ALGORITHM_REPLACE_COPY_IF_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_if_result = detail::in_out_result<I, O>; |
| 39 |
|
| 40 |
RANGES_FUNC_BEGIN(replace_copy_if) |
| 41 |
|
| 42 |
/// \brief function template \c replace_copy_if |
| 43 |
template(typename I, |
| 44 |
typename S, |
| 45 |
typename O, |
| 46 |
typename C, |
| 47 |
typename T, |
| 48 |
typename P = identity)( |
| 49 |
requires input_iterator<I> AND sentinel_for<S, I> AND |
| 50 |
output_iterator<O, T const &> AND |
| 51 |
indirect_unary_predicate<C, projected<I, P>> AND |
| 52 |
indirectly_copyable<I, O>) |
| 53 |
constexpr replace_copy_if_result<I, O> RANGES_FUNC(replace_copy_if)( |
| 54 |
I first, S last, O out, C pred, T const & new_value, P proj = {}) // |
| 55 |
{ |
| 56 |
for(; first != last; ++first, ++out) |
| 57 |
{ |
| 58 |
auto && x = *first; |
| 59 |
if(invoke(pred, invoke(proj, x))) |
| 60 |
*out = new_value; |
| 61 |
else |
| 62 |
*out = (decltype(x) &&)x; |
| 63 |
} |
| 64 |
return {first, out}; |
| 65 |
} |
| 66 |
|
| 67 |
/// \overload |
| 68 |
template(typename Rng, typename O, typename C, typename T, typename P = identity)( |
| 69 |
requires input_range<Rng> AND output_iterator<O, T const &> AND |
| 70 |
indirect_unary_predicate<C, projected<iterator_t<Rng>, P>> AND |
| 71 |
indirectly_copyable<iterator_t<Rng>, O>) |
| 72 |
constexpr replace_copy_if_result<borrowed_iterator_t<Rng>, O> RANGES_FUNC(replace_copy_if)( |
| 73 |
Rng && rng, O out, C pred, T const & new_value, P proj = {}) // |
| 74 |
{ |
| 75 |
return (*this)(begin(rng), |
| 76 |
end(rng), |
| 77 |
std::move(out), |
| 78 |
std::move(pred), |
| 79 |
new_value, |
| 80 |
std::move(proj)); |
| 81 |
} |
| 82 |
|
| 83 |
RANGES_FUNC_END(replace_copy_if) |
| 84 |
|
| 85 |
namespace cpp20 |
| 86 |
{ |
| 87 |
using ranges::replace_copy_if; |
| 88 |
using ranges::replace_copy_if_result; |
| 89 |
} // namespace cpp20 |
| 90 |
/// @} |
| 91 |
} // namespace ranges |
| 92 |
|
| 93 |
#include <range/v3/detail/epilogue.hpp> |
| 94 |
|
| 95 |
#endif |