]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::group`. | |
4 | ||
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) | |
8 | */ | |
9 | ||
10 | #ifndef BOOST_HANA_GROUP_HPP | |
11 | #define BOOST_HANA_GROUP_HPP | |
12 | ||
13 | #include <boost/hana/fwd/group.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/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> | |
25 | ||
26 | #include <cstddef> | |
27 | #include <utility> | |
28 | ||
29 | ||
30 | BOOST_HANA_NAMESPACE_BEGIN | |
31 | //! @cond | |
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 | |
37 | ); | |
38 | ||
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"); | |
42 | #endif | |
43 | ||
44 | return Group::apply(static_cast<Xs&&>(xs)); | |
45 | } | |
46 | ||
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 | |
52 | ); | |
53 | ||
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"); | |
57 | #endif | |
58 | ||
59 | return Group::apply(static_cast<Xs&&>(xs), | |
60 | static_cast<Predicate&&>(pred)); | |
61 | } | |
62 | //! @endcond | |
63 | ||
64 | namespace detail { | |
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))...); | |
69 | } | |
70 | ||
71 | template <std::size_t offset, typename Indices> | |
72 | struct offset_by; | |
73 | ||
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)...>; | |
77 | }; | |
78 | ||
79 | template <bool ...b> | |
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; | |
84 | ||
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) { | |
88 | offsets[g] = offset; | |
89 | ||
90 | sizes[g] = 1; | |
91 | while (i < sizeof...(b) && bs[i++]) | |
92 | ++sizes[g]; | |
93 | ||
94 | offset += sizes[g]; | |
95 | } | |
96 | return std::make_pair(offsets, sizes); | |
97 | } | |
98 | ||
99 | static constexpr auto info = compute_info(); | |
100 | static constexpr auto offsets = info.first; | |
101 | static constexpr auto group_sizes = info.second; | |
102 | ||
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), | |
108 | typename offset_by< | |
109 | offsets[i], std::make_index_sequence<group_sizes[i]> | |
110 | >::type{} | |
111 | )... | |
112 | ); | |
113 | } | |
114 | }; | |
115 | } // end namespace detail | |
116 | ||
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))) | |
125 | )::value)...>; | |
126 | return info::template finish<S>(static_cast<Xs&&>(xs), | |
127 | std::make_index_sequence<info::n_groups>{} | |
128 | ); | |
129 | } | |
130 | ||
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)); | |
135 | } | |
136 | ||
137 | template <typename Xs, typename Pred> | |
138 | static constexpr auto | |
139 | group_helper(Xs&&, Pred&&, std::index_sequence<>) { | |
140 | return hana::make<S>(); | |
141 | } | |
142 | ||
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>{}); | |
149 | } | |
150 | ||
151 | template <typename Xs> | |
152 | static constexpr auto apply(Xs&& xs) | |
153 | { return group_impl::apply(static_cast<Xs&&>(xs), hana::equal); } | |
154 | }; | |
155 | BOOST_HANA_NAMESPACE_END | |
156 | ||
157 | #endif // !BOOST_HANA_GROUP_HPP |