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.

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