/*! @file Defines `boost::hana::group`. @copyright Louis Dionne 2013-2017 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_GROUP_HPP #define BOOST_HANA_GROUP_HPP #include #include #include #include #include #include #include #include #include // required by fwd decl #include #include #include #include BOOST_HANA_NAMESPACE_BEGIN //! @cond template constexpr auto group_t::operator()(Xs&& xs) const { using S = typename hana::tag_of::type; using Group = BOOST_HANA_DISPATCH_IF(group_impl, hana::Sequence::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Sequence::value, "hana::group(xs) requires 'xs' to be a Sequence"); #endif return Group::apply(static_cast(xs)); } template constexpr auto group_t::operator()(Xs&& xs, Predicate&& pred) const { using S = typename hana::tag_of::type; using Group = BOOST_HANA_DISPATCH_IF(group_impl, hana::Sequence::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Sequence::value, "hana::group(xs, predicate) requires 'xs' to be a Sequence"); #endif return Group::apply(static_cast(xs), static_cast(pred)); } //! @endcond namespace detail { template constexpr auto get_subsequence_(Xs&& xs, std::index_sequence) { using S = typename hana::tag_of::type; return hana::make(hana::at_c(static_cast(xs))...); } template struct offset_by; template struct offset_by> { using type = std::index_sequence<(offset + i)...>; }; template struct group_indices { static constexpr bool bs[] = {b...}; static constexpr std::size_t n_groups = detail::count(bs, bs + sizeof(bs), false) + 1; static constexpr auto compute_info() { detail::array sizes{}, offsets{}; for (std::size_t g = 0, i = 0, offset = 0; g < n_groups; ++g) { offsets[g] = offset; sizes[g] = 1; while (i < sizeof...(b) && bs[i++]) ++sizes[g]; offset += sizes[g]; } return std::make_pair(offsets, sizes); } static constexpr auto info = compute_info(); static constexpr auto group_offsets = info.first; static constexpr auto group_sizes = info.second; template static constexpr auto finish(Xs&& xs, std::index_sequence) { return hana::make( detail::get_subsequence_( static_cast(xs), typename offset_by< group_offsets[i], std::make_index_sequence >::type{} )... ); } }; } // end namespace detail template struct group_impl> : default_ { template static constexpr auto group_helper(Xs&& xs, Pred&& pred, std::index_sequence<0, i...>) { using info = detail::group_indices(decltype( pred(hana::at_c(static_cast(xs)), hana::at_c(static_cast(xs))) )::value)...>; return info::template finish(static_cast(xs), std::make_index_sequence{} ); } template static constexpr auto group_helper(Xs&& xs, Pred&&, std::index_sequence<0>) { return hana::make(static_cast(xs)); } template static constexpr auto group_helper(Xs&&, Pred&&, std::index_sequence<>) { return hana::make(); } template static constexpr auto apply(Xs&& xs, Pred&& pred) { constexpr std::size_t len = decltype(hana::length(xs))::value; return group_helper(static_cast(xs), static_cast(pred), std::make_index_sequence{}); } template static constexpr auto apply(Xs&& xs) { return group_impl::apply(static_cast(xs), hana::equal); } }; BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_GROUP_HPP