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.

73 lines
2.3KB

  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_IF_HPP
  14. #define RANGES_V3_ALGORITHM_REPLACE_IF_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_if)
  31. /// \brief function template \c replace_if
  32. template<typename I, typename S, typename C, typename T, typename P = identity>
  33. auto RANGES_FUN_NIEBLOID(replace_if)(
  34. I first, S last, C pred, T const & new_value, P proj = P{}) //
  35. ->CPP_ret(I)( //
  36. requires input_iterator<I> && sentinel_for<S, I> &&
  37. indirect_unary_predicate<C, projected<I, P>> && writable<I, T const &>)
  38. {
  39. for(; first != last; ++first)
  40. if(invoke(pred, invoke(proj, *first)))
  41. *first = new_value;
  42. return first;
  43. }
  44. /// \overload
  45. template<typename Rng, typename C, typename T, typename P = identity>
  46. auto RANGES_FUN_NIEBLOID(replace_if)(
  47. Rng && rng, C pred, T const & new_value, P proj = P{}) //
  48. ->CPP_ret(safe_iterator_t<Rng>)( //
  49. requires input_range<Rng> &&
  50. indirect_unary_predicate<C, projected<iterator_t<Rng>, P>> &&
  51. writable<iterator_t<Rng>, T const &>)
  52. {
  53. return (*this)(
  54. begin(rng), end(rng), std::move(pred), new_value, std::move(proj));
  55. }
  56. RANGES_END_NIEBLOID(replace_if)
  57. namespace cpp20
  58. {
  59. using ranges::replace_if;
  60. }
  61. /// @}
  62. } // namespace ranges
  63. #endif // include guard