/*! @file Defines `boost::hana::scan_right`. @copyright Louis Dionne 2013-2016 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_HANA_SCAN_RIGHT_HPP #define BOOST_HANA_SCAN_RIGHT_HPP #include #include #include #include #include #include #include #include #include #include #include #include BOOST_HANA_NAMESPACE_BEGIN //! @cond template constexpr auto scan_right_t::operator()(Xs&& xs, F const& f) const { using S = typename hana::tag_of::type; using ScanRight = BOOST_HANA_DISPATCH_IF(scan_right_impl, hana::Sequence::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Sequence::value, "hana::scan_right(xs, f) requires 'xs' to be a Sequence"); #endif return ScanRight::apply(static_cast(xs), f); } template constexpr auto scan_right_t::operator()(Xs&& xs, State&& state, F const& f) const { using S = typename hana::tag_of::type; using ScanRight = BOOST_HANA_DISPATCH_IF(scan_right_impl, hana::Sequence::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Sequence::value, "hana::scan_right(xs, state, f) requires 'xs' to be a Sequence"); #endif return ScanRight::apply(static_cast(xs), static_cast(state), f); } //! @endcond template struct scan_right_impl> : default_ { // Without initial state template static constexpr auto apply1_impl(Xs&& xs, F const& f, std::index_sequence) { auto rest = scan_right_impl::apply1_impl(static_cast(xs), f, std::index_sequence{}); auto element = f(hana::at_c(static_cast(xs)), hana::front(rest)); return hana::prepend(std::move(rest), std::move(element)); } template static constexpr auto apply1_impl(Xs&& xs, F const&, std::index_sequence) { return hana::make(hana::at_c(static_cast(xs))); } template static constexpr auto apply1_impl(Xs&&, F const&, std::index_sequence<>) { return hana::empty(); } template static constexpr auto apply(Xs&& xs, F const& f) { constexpr std::size_t Len = decltype(hana::length(xs))::value; return scan_right_impl::apply1_impl(static_cast(xs), f, std::make_index_sequence{}); } // With initial state template static constexpr auto apply_impl(Xs&& xs, State&& state, F const& f, std::index_sequence) { auto rest = scan_right_impl::apply_impl(static_cast(xs), static_cast(state), f, std::index_sequence{}); auto element = f(hana::at_c(static_cast(xs)), hana::front(rest)); return hana::prepend(std::move(rest), std::move(element)); } template static constexpr auto apply_impl(Xs&& xs, State&& state, F const& f, std::index_sequence) { auto element = f(hana::at_c(static_cast(xs)), state); return hana::make(std::move(element), static_cast(state)); } template static constexpr auto apply_impl(Xs&&, State&& state, F const&, std::index_sequence<>) { return hana::make(static_cast(state)); } template static constexpr auto apply(Xs&& xs, State&& state, F const& f) { constexpr std::size_t Len = decltype(hana::length(xs))::value; return scan_right_impl::apply_impl(static_cast(xs), static_cast(state), f, std::make_index_sequence{}); } }; BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_SCAN_RIGHT_HPP