You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.4KB

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