| 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_HPP |
| 14 |
#define RANGES_V3_ALGORITHM_REPLACE_HPP |
| 15 |
|
| 16 |
#include <meta/meta.hpp> |
| 17 |
|
| 18 |
#include <range/v3/range_fwd.hpp> |
| 19 |
|
| 20 |
#include <range/v3/functional/identity.hpp> |
| 21 |
#include <range/v3/functional/invoke.hpp> |
| 22 |
#include <range/v3/iterator/concepts.hpp> |
| 23 |
#include <range/v3/iterator/traits.hpp> |
| 24 |
#include <range/v3/range/access.hpp> |
| 25 |
#include <range/v3/range/concepts.hpp> |
| 26 |
#include <range/v3/range/dangling.hpp> |
| 27 |
#include <range/v3/range/traits.hpp> |
| 28 |
#include <range/v3/utility/static_const.hpp> |
| 29 |
|
| 30 |
#include <range/v3/detail/prologue.hpp> |
| 31 |
|
| 32 |
namespace ranges |
| 33 |
{ |
| 34 |
/// \addtogroup group-algorithms |
| 35 |
/// @{ |
| 36 |
RANGES_FUNC_BEGIN(replace) |
| 37 |
|
| 38 |
/// \brief function template \c replace |
| 39 |
template(typename I, typename S, typename T1, typename T2, typename P = identity)( |
| 40 |
requires input_iterator<I> AND sentinel_for<S, I> AND |
| 41 |
indirectly_writable<I, T2 const &> AND |
| 42 |
indirect_relation<equal_to, projected<I, P>, T1 const *>) |
| 43 |
constexpr I RANGES_FUNC(replace)( |
| 44 |
I first, S last, T1 const & old_value, T2 const & new_value, P proj = {}) // |
| 45 |
{ |
| 46 |
for(; first != last; ++first) |
| 47 |
if(invoke(proj, *first) == old_value) |
| 48 |
*first = new_value; |
| 49 |
return first; |
| 50 |
} |
| 51 |
|
| 52 |
/// \overload |
| 53 |
template(typename Rng, typename T1, typename T2, typename P = identity)( |
| 54 |
requires input_range<Rng> AND |
| 55 |
indirectly_writable<iterator_t<Rng>, T2 const &> AND |
| 56 |
indirect_relation<equal_to, projected<iterator_t<Rng>, P>, T1 const *>) |
| 57 |
constexpr borrowed_iterator_t<Rng> RANGES_FUNC(replace)( |
| 58 |
Rng && rng, T1 const & old_value, T2 const & new_value, P proj = {}) // |
| 59 |
{ |
| 60 |
return (*this)(begin(rng), end(rng), old_value, new_value, std::move(proj)); |
| 61 |
} |
| 62 |
|
| 63 |
RANGES_FUNC_END(replace) |
| 64 |
|
| 65 |
namespace cpp20 |
| 66 |
{ |
| 67 |
using ranges::replace; |
| 68 |
} |
| 69 |
/// @} |
| 70 |
} // namespace ranges |
| 71 |
|
| 72 |
#include <range/v3/detail/epilogue.hpp> |
| 73 |
|
| 74 |
#endif |