| 1 | /*! |
| 2 | @file |
| 3 | Defines `boost::hana::scan_right`. |
| 4 | |
| 5 | Copyright Louis Dionne 2013-2022 |
| 6 | Distributed under the Boost Software License, Version 1.0. |
| 7 | (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 8 | */ |
| 9 | |
| 10 | #ifndef BOOST_HANA_SCAN_RIGHT_HPP |
| 11 | #define BOOST_HANA_SCAN_RIGHT_HPP |
| 12 | |
| 13 | #include <boost/hana/fwd/scan_right.hpp> |
| 14 | |
| 15 | #include <boost/hana/at.hpp> |
| 16 | #include <boost/hana/concept/sequence.hpp> |
| 17 | #include <boost/hana/config.hpp> |
| 18 | #include <boost/hana/core/dispatch.hpp> |
| 19 | #include <boost/hana/core/make.hpp> |
| 20 | #include <boost/hana/empty.hpp> |
| 21 | #include <boost/hana/front.hpp> |
| 22 | #include <boost/hana/length.hpp> |
| 23 | #include <boost/hana/prepend.hpp> |
| 24 | |
| 25 | #include <cstddef> |
| 26 | #include <utility> |
| 27 | |
| 28 | |
| 29 | namespace boost { namespace hana { |
| 30 | //! @cond |
| 31 | template <typename Xs, typename F> |
| 32 | constexpr auto scan_right_t::operator()(Xs&& xs, F const& f) const { |
| 33 | using S = typename hana::tag_of<Xs>::type; |
| 34 | using ScanRight = BOOST_HANA_DISPATCH_IF(scan_right_impl<S>, |
| 35 | hana::Sequence<S>::value |
| 36 | ); |
| 37 | |
| 38 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
| 39 | static_assert(hana::Sequence<S>::value, |
| 40 | "hana::scan_right(xs, f) requires 'xs' to be a Sequence" ); |
| 41 | #endif |
| 42 | |
| 43 | return ScanRight::apply(static_cast<Xs&&>(xs), f); |
| 44 | } |
| 45 | |
| 46 | template <typename Xs, typename State, typename F> |
| 47 | constexpr auto scan_right_t::operator()(Xs&& xs, State&& state, F const& f) const { |
| 48 | using S = typename hana::tag_of<Xs>::type; |
| 49 | using ScanRight = BOOST_HANA_DISPATCH_IF(scan_right_impl<S>, |
| 50 | hana::Sequence<S>::value |
| 51 | ); |
| 52 | |
| 53 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
| 54 | static_assert(hana::Sequence<S>::value, |
| 55 | "hana::scan_right(xs, state, f) requires 'xs' to be a Sequence" ); |
| 56 | #endif |
| 57 | |
| 58 | return ScanRight::apply(static_cast<Xs&&>(xs), |
| 59 | static_cast<State&&>(state), f); |
| 60 | } |
| 61 | //! @endcond |
| 62 | |
| 63 | template <typename S, bool condition> |
| 64 | struct scan_right_impl<S, when<condition>> : default_ { |
| 65 | // Without initial state |
| 66 | template <typename Xs, typename F, std::size_t n1, std::size_t n2, std::size_t ...ns> |
| 67 | static constexpr auto |
| 68 | apply1_impl(Xs&& xs, F const& f, std::index_sequence<n1, n2, ns...>) { |
| 69 | auto rest = scan_right_impl::apply1_impl(static_cast<Xs&&>(xs), |
| 70 | f, std::index_sequence<n2, ns...>{}); |
| 71 | auto element = f(hana::at_c<n1>(static_cast<Xs&&>(xs)), hana::front(rest)); |
| 72 | return hana::prepend(std::move(rest), std::move(element)); |
| 73 | } |
| 74 | |
| 75 | template <typename Xs, typename F, std::size_t n> |
| 76 | static constexpr auto apply1_impl(Xs&& xs, F const&, std::index_sequence<n>) { |
| 77 | return hana::make<S>(hana::at_c<n>(static_cast<Xs&&>(xs))); |
| 78 | } |
| 79 | |
| 80 | template <typename Xs, typename F> |
| 81 | static constexpr auto apply1_impl(Xs&&, F const&, std::index_sequence<>) { |
| 82 | return hana::empty<S>(); |
| 83 | } |
| 84 | |
| 85 | template <typename Xs, typename F> |
| 86 | static constexpr auto apply(Xs&& xs, F const& f) { |
| 87 | constexpr std::size_t Len = decltype(hana::length(xs))::value; |
| 88 | return scan_right_impl::apply1_impl(static_cast<Xs&&>(xs), |
| 89 | f, std::make_index_sequence<Len>{}); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | // With initial state |
| 94 | template <typename Xs, typename State, typename F, |
| 95 | std::size_t n1, std::size_t n2, std::size_t ...ns> |
| 96 | static constexpr auto |
| 97 | apply_impl(Xs&& xs, State&& state, F const& f, |
| 98 | std::index_sequence<n1, n2, ns...>) |
| 99 | { |
| 100 | auto rest = scan_right_impl::apply_impl(static_cast<Xs&&>(xs), |
| 101 | static_cast<State&&>(state), |
| 102 | f, std::index_sequence<n2, ns...>{}); |
| 103 | auto element = f(hana::at_c<n1>(static_cast<Xs&&>(xs)), hana::front(rest)); |
| 104 | return hana::prepend(std::move(rest), std::move(element)); |
| 105 | } |
| 106 | |
| 107 | template <typename Xs, typename State, typename F, std::size_t n> |
| 108 | static constexpr auto |
| 109 | apply_impl(Xs&& xs, State&& state, F const& f, std::index_sequence<n>) { |
| 110 | auto element = f(hana::at_c<n>(static_cast<Xs&&>(xs)), state); |
| 111 | return hana::make<S>(std::move(element), static_cast<State&&>(state)); |
| 112 | } |
| 113 | |
| 114 | template <typename Xs, typename State, typename F> |
| 115 | static constexpr auto |
| 116 | apply_impl(Xs&&, State&& state, F const&, std::index_sequence<>) { |
| 117 | return hana::make<S>(static_cast<State&&>(state)); |
| 118 | } |
| 119 | |
| 120 | template <typename Xs, typename State, typename F> |
| 121 | static constexpr auto apply(Xs&& xs, State&& state, F const& f) { |
| 122 | constexpr std::size_t Len = decltype(hana::length(xs))::value; |
| 123 | return scan_right_impl::apply_impl(static_cast<Xs&&>(xs), |
| 124 | static_cast<State&&>(state), |
| 125 | f, std::make_index_sequence<Len>{}); |
| 126 | } |
| 127 | }; |
| 128 | }} // end namespace boost::hana |
| 129 | |
| 130 | #endif // !BOOST_HANA_SCAN_RIGHT_HPP |
| 131 | |