/// \file // Range v3 library // // Copyright Eric Niebler 2021-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_ALGORITHM_FOLD_RIGHT_HPP #define RANGES_V3_ALGORITHM_FOLD_RIGHT_HPP #include #include #include #include #include #include #include #include #include namespace ranges { namespace detail { template struct flipped { F f; template(class T, class U)(requires invocable) // invoke_result_t operator()(T &&, U &&); }; } // namespace detail // clang-format off /// \concept indirectly_binary_right_foldable /// \brief The \c indirectly_binary_right_foldable concept template CPP_concept indirectly_binary_right_foldable = indirectly_binary_left_foldable, T, I>; // clang-format on /// \addtogroup group-algorithms /// @{ RANGES_FUNC_BEGIN(fold_right) template(typename I, typename S, typename T, typename Op)( requires sentinel_for AND bidirectional_iterator AND indirectly_binary_right_foldable) // constexpr auto RANGES_FUNC(fold_right)(I first, S last, T init, Op op) { using U = std::decay_t, T>>; if(first == last) { return U(std::move(init)); } I tail = next(first, last); U accum = invoke(op, *--tail, std::move(init)); while(first != tail) { accum = invoke(op, *--tail, std::move(accum)); } return accum; } template(typename Rng, typename T, typename Op)( requires bidirectional_range AND indirectly_binary_right_foldable>) // constexpr auto RANGES_FUNC(fold_right)(Rng && rng, T init, Op op) { return (*this)(begin(rng), end(rng), std::move(init), std::move(op)); } RANGES_FUNC_END(fold_right) RANGES_FUNC_BEGIN(fold_right_last) template(typename I, typename S, typename Op)( requires sentinel_for AND bidirectional_iterator AND indirectly_binary_right_foldable, I> AND constructible_from, iter_reference_t>) // constexpr auto RANGES_FUNC(fold_right_last)(I first, S last, Op op) { using U = invoke_result_t, Op>; if(first == last) { return optional(); } I tail = prev(next(first, std::move(last))); return optional( in_place, fold_right_fn{}( std::move(first), tail, iter_value_t(*tail), std::move(op))); } template(typename R, typename Op)( requires bidirectional_range AND indirectly_binary_right_foldable, iterator_t> AND constructible_from, range_reference_t>) // constexpr auto RANGES_FUNC(fold_right_last)(R && rng, Op op) { return (*this)(begin(rng), end(rng), std::move(op)); } RANGES_FUNC_END(fold_right_last) /// @} } // namespace ranges #include #endif