File indexing completed on 2025-01-30 09:43:39
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_HANA_SPAN_HPP
0011 #define BOOST_HANA_SPAN_HPP
0012
0013 #include <boost/hana/fwd/span.hpp>
0014
0015 #include <boost/hana/at.hpp>
0016 #include <boost/hana/concept/sequence.hpp>
0017 #include <boost/hana/config.hpp>
0018 #include <boost/hana/core/dispatch.hpp>
0019 #include <boost/hana/core/make.hpp>
0020 #include <boost/hana/detail/nested_by.hpp> // required by fwd decl
0021 #include <boost/hana/detail/first_unsatisfied_index.hpp>
0022 #include <boost/hana/length.hpp>
0023 #include <boost/hana/pair.hpp>
0024 #include <boost/hana/unpack.hpp>
0025
0026 #include <cstddef>
0027 #include <utility>
0028
0029
0030 namespace boost { namespace hana {
0031
0032 template <typename Xs, typename Pred>
0033 constexpr auto span_t::operator()(Xs&& xs, Pred&& pred) const {
0034 using S = typename hana::tag_of<Xs>::type;
0035 using Span = BOOST_HANA_DISPATCH_IF(span_impl<S>,
0036 hana::Sequence<S>::value
0037 );
0038
0039 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0040 static_assert(hana::Sequence<S>::value,
0041 "hana::span(xs, pred) requires 'xs' to be a Sequence");
0042 #endif
0043
0044 return Span::apply(static_cast<Xs&&>(xs), static_cast<Pred&&>(pred));
0045 }
0046
0047
0048 template <typename S, bool condition>
0049 struct span_impl<S, when<condition>> : default_ {
0050 template <typename Xs, std::size_t ...before, std::size_t ...after>
0051 static constexpr auto span_helper(Xs&& xs, std::index_sequence<before...>,
0052 std::index_sequence<after...>)
0053 {
0054 return hana::make_pair(
0055 hana::make<S>(hana::at_c<before>(static_cast<Xs&&>(xs))...),
0056 hana::make<S>(hana::at_c<sizeof...(before) + after>(static_cast<Xs&&>(xs))...)
0057 );
0058 }
0059
0060 template <typename Xs, typename Pred>
0061 static constexpr auto apply(Xs&& xs, Pred&&) {
0062 using FirstUnsatisfied = decltype(
0063 hana::unpack(static_cast<Xs&&>(xs),
0064 detail::first_unsatisfied_index<Pred&&>{})
0065 );
0066 constexpr std::size_t breakpoint = FirstUnsatisfied::value;
0067 constexpr std::size_t N = decltype(hana::length(xs))::value;
0068 return span_helper(static_cast<Xs&&>(xs),
0069 std::make_index_sequence<breakpoint>{},
0070 std::make_index_sequence<N - breakpoint>{});
0071 }
0072 };
0073 }}
0074
0075 #endif