/*! @file Defines `boost::hana::scan_left`. @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_LEFT_HPP #define BOOST_HANA_SCAN_LEFT_HPP #include #include #include #include #include #include #include #include #include #include #include BOOST_HANA_NAMESPACE_BEGIN //! @cond template constexpr auto scan_left_t::operator()(Xs&& xs, F const& f) const { using S = typename hana::tag_of::type; using ScanLeft = BOOST_HANA_DISPATCH_IF(scan_left_impl, hana::Sequence::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Sequence::value, "hana::scan_left(xs, f) requires 'xs' to be a Sequence"); #endif return ScanLeft::apply(static_cast(xs), f); } template constexpr auto scan_left_t::operator()(Xs&& xs, State&& state, F const& f) const { using S = typename hana::tag_of::type; using ScanLeft = BOOST_HANA_DISPATCH_IF(scan_left_impl, hana::Sequence::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Sequence::value, "hana::scan_left(xs, state, f) requires 'xs' to be a Sequence"); #endif return ScanLeft::apply(static_cast(xs), static_cast(state), f); } //! @endcond template struct scan_left_impl> : default_ { // Without initial state template static constexpr auto apply1_impl(Xs&& xs, F const& f, std::index_sequence) { static_assert(n1 == 0, "logic error in Boost.Hana: file a bug report"); // Use scan_left with the first element as an initial state. return scan_left_impl::apply_impl( static_cast(xs), hana::at_c<0>(static_cast(xs)), f, std::index_sequence{} ); } 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_left_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_left_impl::apply_impl( static_cast(xs), f(state, hana::at_c(static_cast(xs))), f, std::index_sequence{}); return hana::prepend(std::move(rest), static_cast(state)); } template static constexpr auto apply_impl(Xs&& xs, State&& state, F const& f, std::index_sequence) { auto new_state = f(state, hana::at_c(static_cast(xs))); return hana::make(static_cast(state), std::move(new_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_left_impl::apply_impl(static_cast(xs), static_cast(state), f, std::make_index_sequence{}); } }; BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_SCAN_LEFT_HPP