/// \file // Range v3 library // // Copyright Eric Niebler 2013-present // // Use, modification and distribution is subject to the // Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // Project home: https://github.com/ericniebler/range-v3 // #ifndef RANGES_V3_VIEW_REPLACE_IF_HPP #define RANGES_V3_VIEW_REPLACE_IF_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace ranges { /// \cond namespace detail { template struct replacer_if_fn : compressed_pair, Val> { private: using base_t = compressed_pair, Val>; using base_t::first; using base_t::second; public: replacer_if_fn() = default; constexpr replacer_if_fn(Pred pred, Val new_value) : base_t{std::move(pred), std::move(new_value)} {} template [[noreturn]] common_type_t>, iter_value_t> & operator()(copy_tag, I const &) const { RANGES_EXPECT(false); } template(typename I)( requires (!invocable>)) common_reference_t, iter_reference_t> // operator()(I const & i) { auto && x = *i; if(invoke(first(), (decltype(x) &&)x)) // return unwrap_reference(second()); return (decltype(x) &&)x; } template(typename I)( requires invocable>) common_reference_t, iter_reference_t> // operator()(I const & i) const { auto && x = *i; if(invoke(first(), (decltype(x) &&)x)) // return unwrap_reference(second()); return (decltype(x) &&)x; } template(typename I)( requires (!invocable>)) common_reference_t< unwrap_reference_t, // iter_rvalue_reference_t> // operator()(move_tag, I const & i) { auto && x = iter_move(i); if(invoke(first(), (decltype(x) &&)x)) // return unwrap_reference(second()); return (decltype(x) &&)x; } template(typename I)( requires invocable>) common_reference_t< // unwrap_reference_t, // iter_rvalue_reference_t> // operator()(move_tag, I const & i) const { auto && x = iter_move(i); if(invoke(first(), (decltype(x) &&)x)) // return unwrap_reference(second()); return (decltype(x) &&)x; } }; } // namespace detail /// \endcond /// \addtogroup group-views /// @{ namespace views { struct replace_if_base_fn { template(typename Rng, typename Pred, typename Val)( requires viewable_range AND input_range AND indirect_unary_predicate> AND common_with>, range_value_t> AND common_reference_with, range_reference_t> AND common_reference_with, range_rvalue_reference_t>) constexpr replace_if_view, Pred, Val> // operator()(Rng && rng, Pred pred, Val new_value) const { return {all(static_cast(rng)), {std::move(pred), std::move(new_value)}}; } }; struct replace_if_fn : replace_if_base_fn { using replace_if_base_fn::operator(); template constexpr auto operator()(Pred pred, Val new_value) const { return make_view_closure(bind_back( replace_if_base_fn{}, std::move(pred), std::move(new_value))); } }; /// \relates replace_if_fn /// \ingroup group-views RANGES_INLINE_VARIABLE(replace_if_fn, replace_if) } // namespace views /// @} } // namespace ranges #include #endif