1 /*! 2 @file 3 Defines `boost::hana::scan_left`. 4 5 @copyright Louis Dionne 2013-2017 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_LEFT_HPP 11 #define BOOST_HANA_SCAN_LEFT_HPP 12 13 #include <boost/hana/fwd/scan_left.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/length.hpp> 22 #include <boost/hana/prepend.hpp> 23 24 #include <cstddef> 25 #include <utility> 26 27 28 BOOST_HANA_NAMESPACE_BEGIN 29 //! @cond 30 template <typename Xs, typename F> operator ()(Xs && xs,F const & f) const31 constexpr auto scan_left_t::operator()(Xs&& xs, F const& f) const { 32 using S = typename hana::tag_of<Xs>::type; 33 using ScanLeft = BOOST_HANA_DISPATCH_IF(scan_left_impl<S>, 34 hana::Sequence<S>::value 35 ); 36 37 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS 38 static_assert(hana::Sequence<S>::value, 39 "hana::scan_left(xs, f) requires 'xs' to be a Sequence"); 40 #endif 41 42 return ScanLeft::apply(static_cast<Xs&&>(xs), f); 43 } 44 45 template <typename Xs, typename State, typename F> operator ()(Xs && xs,State && state,F const & f) const46 constexpr auto scan_left_t::operator()(Xs&& xs, State&& state, F const& f) const { 47 using S = typename hana::tag_of<Xs>::type; 48 using ScanLeft = BOOST_HANA_DISPATCH_IF(scan_left_impl<S>, 49 hana::Sequence<S>::value 50 ); 51 52 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS 53 static_assert(hana::Sequence<S>::value, 54 "hana::scan_left(xs, state, f) requires 'xs' to be a Sequence"); 55 #endif 56 57 return ScanLeft::apply(static_cast<Xs&&>(xs), 58 static_cast<State&&>(state), f); 59 } 60 //! @endcond 61 62 template <typename S, bool condition> 63 struct scan_left_impl<S, when<condition>> : default_ { 64 // Without initial state 65 template <typename Xs, typename F, std::size_t n1, std::size_t n2, std::size_t ...ns> 66 static constexpr auto apply1_implscan_left_impl67 apply1_impl(Xs&& xs, F const& f, std::index_sequence<n1, n2, ns...>) { 68 static_assert(n1 == 0, "logic error in Boost.Hana: file a bug report"); 69 70 // Use scan_left with the first element as an initial state. 71 return scan_left_impl::apply_impl( 72 static_cast<Xs&&>(xs), 73 hana::at_c<0>(static_cast<Xs&&>(xs)), 74 f, std::index_sequence<n2, ns...>{} 75 ); 76 } 77 78 template <typename Xs, typename F, std::size_t n> apply1_implscan_left_impl79 static constexpr auto apply1_impl(Xs&& xs, F const&, std::index_sequence<n>) { 80 return hana::make<S>(hana::at_c<n>(static_cast<Xs&&>(xs))); 81 } 82 83 template <typename Xs, typename F> apply1_implscan_left_impl84 static constexpr auto apply1_impl(Xs&&, F const&, std::index_sequence<>) { 85 return hana::empty<S>(); 86 } 87 88 template <typename Xs, typename F> applyscan_left_impl89 static constexpr auto apply(Xs&& xs, F const& f) { 90 constexpr std::size_t Len = decltype(hana::length(xs))::value; 91 return scan_left_impl::apply1_impl(static_cast<Xs&&>(xs), 92 f, std::make_index_sequence<Len>{}); 93 } 94 95 96 // With initial state 97 template <typename Xs, typename State, typename F, 98 std::size_t n1, std::size_t n2, std::size_t ...ns> 99 static constexpr auto apply_implscan_left_impl100 apply_impl(Xs&& xs, State&& state, F const& f, 101 std::index_sequence<n1, n2, ns...>) 102 { 103 auto rest = scan_left_impl::apply_impl( 104 static_cast<Xs&&>(xs), 105 f(state, hana::at_c<n1>(static_cast<Xs&&>(xs))), 106 f, std::index_sequence<n2, ns...>{}); 107 return hana::prepend(std::move(rest), static_cast<State&&>(state)); 108 } 109 110 template <typename Xs, typename State, typename F, std::size_t n> 111 static constexpr auto apply_implscan_left_impl112 apply_impl(Xs&& xs, State&& state, F const& f, std::index_sequence<n>) { 113 auto new_state = f(state, hana::at_c<n>(static_cast<Xs&&>(xs))); 114 return hana::make<S>(static_cast<State&&>(state), std::move(new_state)); 115 } 116 117 template <typename Xs, typename State, typename F> 118 static constexpr auto apply_implscan_left_impl119 apply_impl(Xs&&, State&& state, F const&, std::index_sequence<>) { 120 return hana::make<S>(static_cast<State&&>(state)); 121 } 122 123 template <typename Xs, typename State, typename F> applyscan_left_impl124 static constexpr auto apply(Xs&& xs, State&& state, F const& f) { 125 constexpr std::size_t Len = decltype(hana::length(xs))::value; 126 return scan_left_impl::apply_impl(static_cast<Xs&&>(xs), 127 static_cast<State&&>(state), 128 f, std::make_index_sequence<Len>{}); 129 } 130 }; 131 BOOST_HANA_NAMESPACE_END 132 133 #endif // !BOOST_HANA_SCAN_LEFT_HPP 134