3 Defines `boost::hana::group`.
5 @copyright Louis Dionne 2013-2016
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)
10 #ifndef BOOST_HANA_GROUP_HPP
11 #define BOOST_HANA_GROUP_HPP
13 #include <boost/hana/fwd/group.hpp>
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/detail/algorithm.hpp>
21 #include <boost/hana/detail/array.hpp>
22 #include <boost/hana/detail/nested_by.hpp> // required by fwd decl
23 #include <boost/hana/equal.hpp>
24 #include <boost/hana/length.hpp>
30 BOOST_HANA_NAMESPACE_BEGIN
32 template <typename Xs>
33 constexpr auto group_t::operator()(Xs&& xs) const {
34 using S = typename hana::tag_of<Xs>::type;
35 using Group = BOOST_HANA_DISPATCH_IF(group_impl<S>,
36 hana::Sequence<S>::value
39 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
40 static_assert(hana::Sequence<S>::value,
41 "hana::group(xs) requires 'xs' to be a Sequence");
44 return Group::apply(static_cast<Xs&&>(xs));
47 template <typename Xs, typename Predicate>
48 constexpr auto group_t::operator()(Xs&& xs, Predicate&& pred) const {
49 using S = typename hana::tag_of<Xs>::type;
50 using Group = BOOST_HANA_DISPATCH_IF(group_impl<S>,
51 hana::Sequence<S>::value
54 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
55 static_assert(hana::Sequence<S>::value,
56 "hana::group(xs, predicate) requires 'xs' to be a Sequence");
59 return Group::apply(static_cast<Xs&&>(xs),
60 static_cast<Predicate&&>(pred));
65 template <typename Xs, std::size_t ...i>
66 constexpr auto get_subsequence_(Xs&& xs, std::index_sequence<i...>) {
67 using S = typename hana::tag_of<Xs>::type;
68 return hana::make<S>(hana::at_c<i>(static_cast<Xs&&>(xs))...);
71 template <std::size_t offset, typename Indices>
74 template <std::size_t offset, std::size_t ...i>
75 struct offset_by<offset, std::index_sequence<i...>> {
76 using type = std::index_sequence<(offset + i)...>;
80 struct group_indices {
81 static constexpr bool bs[] = {b...};
82 static constexpr std::size_t n_groups =
83 detail::count(bs, bs + sizeof(bs), false) + 1;
85 static constexpr auto compute_info() {
86 detail::array<std::size_t, n_groups> sizes{}, offsets{};
87 for (std::size_t g = 0, i = 0, offset = 0; g < n_groups; ++g) {
91 while (i < sizeof...(b) && bs[i++])
96 return std::make_pair(offsets, sizes);
99 static constexpr auto info = compute_info();
100 static constexpr auto offsets = info.first;
101 static constexpr auto group_sizes = info.second;
103 template <typename S, typename Xs, std::size_t ...i>
104 static constexpr auto finish(Xs&& xs, std::index_sequence<i...>) {
105 return hana::make<S>(
106 detail::get_subsequence_(
107 static_cast<Xs&&>(xs),
109 offsets[i], std::make_index_sequence<group_sizes[i]>
115 } // end namespace detail
117 template <typename S, bool condition>
118 struct group_impl<S, when<condition>> : default_ {
119 template <typename Xs, typename Pred, std::size_t ...i>
120 static constexpr auto
121 group_helper(Xs&& xs, Pred&& pred, std::index_sequence<0, i...>) {
122 using info = detail::group_indices<static_cast<bool>(decltype(
123 pred(hana::at_c<i - 1>(static_cast<Xs&&>(xs)),
124 hana::at_c<i>(static_cast<Xs&&>(xs)))
126 return info::template finish<S>(static_cast<Xs&&>(xs),
127 std::make_index_sequence<info::n_groups>{}
131 template <typename Xs, typename Pred>
132 static constexpr auto
133 group_helper(Xs&& xs, Pred&&, std::index_sequence<0>) {
134 return hana::make<S>(static_cast<Xs&&>(xs));
137 template <typename Xs, typename Pred>
138 static constexpr auto
139 group_helper(Xs&&, Pred&&, std::index_sequence<>) {
140 return hana::make<S>();
143 template <typename Xs, typename Pred>
144 static constexpr auto apply(Xs&& xs, Pred&& pred) {
145 constexpr std::size_t len = decltype(hana::length(xs))::value;
146 return group_helper(static_cast<Xs&&>(xs),
147 static_cast<Pred&&>(pred),
148 std::make_index_sequence<len>{});
151 template <typename Xs>
152 static constexpr auto apply(Xs&& xs)
153 { return group_impl::apply(static_cast<Xs&&>(xs), hana::equal); }
155 BOOST_HANA_NAMESPACE_END
157 #endif // !BOOST_HANA_GROUP_HPP