Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:27:54

0001 /// \file
0002 // Range v3 library
0003 //
0004 //  Copyright Eric Niebler 2014-present
0005 //
0006 //  Use, modification and distribution is subject to the
0007 //  Boost Software License, Version 1.0. (See accompanying
0008 //  file LICENSE_1_0.txt or copy at
0009 //  http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 // Project home: https://github.com/ericniebler/range-v3
0012 //
0013 #ifndef RANGES_V3_ALGORITHM_REPLACE_COPY_HPP
0014 #define RANGES_V3_ALGORITHM_REPLACE_COPY_HPP
0015 
0016 #include <meta/meta.hpp>
0017 
0018 #include <range/v3/range_fwd.hpp>
0019 
0020 #include <range/v3/algorithm/result_types.hpp>
0021 #include <range/v3/functional/identity.hpp>
0022 #include <range/v3/functional/invoke.hpp>
0023 #include <range/v3/iterator/concepts.hpp>
0024 #include <range/v3/iterator/traits.hpp>
0025 #include <range/v3/range/access.hpp>
0026 #include <range/v3/range/concepts.hpp>
0027 #include <range/v3/range/dangling.hpp>
0028 #include <range/v3/range/traits.hpp>
0029 #include <range/v3/utility/static_const.hpp>
0030 
0031 #include <range/v3/detail/prologue.hpp>
0032 
0033 namespace ranges
0034 {
0035     /// \addtogroup group-algorithms
0036     /// @{
0037     template<typename I, typename O>
0038     using replace_copy_result = detail::in_out_result<I, O>;
0039 
0040     RANGES_FUNC_BEGIN(replace_copy)
0041 
0042         /// \brief function template \c replace_copy
0043         template(typename I,
0044                  typename S,
0045                  typename O,
0046                  typename T1,
0047                  typename T2,
0048                  typename P = identity)(
0049             requires input_iterator<I> AND sentinel_for<S, I> AND
0050                 output_iterator<O, T2 const &> AND indirectly_copyable<I, O> AND
0051                 indirect_relation<equal_to, projected<I, P>, T1 const *>)
0052         constexpr replace_copy_result<I, O> RANGES_FUNC(replace_copy)(I first,
0053                                                                       S last,
0054                                                                       O out,
0055                                                                       T1 const & old_value,
0056                                                                       T2 const & new_value,
0057                                                                       P proj = {}) //
0058         {
0059             for(; first != last; ++first, ++out)
0060             {
0061                 auto && x = *first;
0062                 if(invoke(proj, x) == old_value)
0063                     *out = new_value;
0064                 else
0065                     *out = (decltype(x) &&)x;
0066             }
0067             return {first, out};
0068         }
0069 
0070         /// \overload
0071         template(typename Rng,
0072                  typename O,
0073                  typename T1,
0074                  typename T2,
0075                  typename P = identity)(
0076             requires input_range<Rng> AND output_iterator<O, T2 const &> AND
0077                 indirectly_copyable<iterator_t<Rng>, O> AND
0078                 indirect_relation<equal_to, projected<iterator_t<Rng>, P>, T1 const *>)
0079         constexpr replace_copy_result<borrowed_iterator_t<Rng>, O> RANGES_FUNC(replace_copy)(
0080             Rng && rng, O out, T1 const & old_value, T2 const & new_value, P proj = {}) //
0081         {
0082             return (*this)(begin(rng),
0083                            end(rng),
0084                            std::move(out),
0085                            old_value,
0086                            new_value,
0087                            std::move(proj));
0088         }
0089 
0090     RANGES_FUNC_END(replace_copy)
0091 
0092     namespace cpp20
0093     {
0094         using ranges::replace_copy;
0095         using ranges::replace_copy_result;
0096     } // namespace cpp20
0097     /// @}
0098 } // namespace ranges
0099 
0100 #include <range/v3/detail/epilogue.hpp>
0101 
0102 #endif