]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::set`. | |
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_SET_HPP | |
11 | #define BOOST_HANA_SET_HPP | |
12 | ||
13 | #include <boost/hana/fwd/set.hpp> | |
14 | ||
15 | #include <boost/hana/at.hpp> | |
16 | #include <boost/hana/bool.hpp> | |
17 | #include <boost/hana/concept/comparable.hpp> | |
18 | #include <boost/hana/concept/constant.hpp> | |
19 | #include <boost/hana/concept/hashable.hpp> | |
20 | #include <boost/hana/config.hpp> | |
21 | #include <boost/hana/contains.hpp> | |
22 | #include <boost/hana/core/make.hpp> | |
23 | #include <boost/hana/core/to.hpp> | |
24 | #include <boost/hana/detail/decay.hpp> | |
25 | #include <boost/hana/detail/fast_and.hpp> | |
26 | #include <boost/hana/detail/has_duplicates.hpp> | |
27 | #include <boost/hana/detail/operators/adl.hpp> | |
28 | #include <boost/hana/detail/operators/comparable.hpp> | |
29 | #include <boost/hana/detail/operators/searchable.hpp> | |
30 | #include <boost/hana/equal.hpp> | |
31 | #include <boost/hana/erase_key.hpp> | |
32 | #include <boost/hana/find_if.hpp> | |
33 | #include <boost/hana/fold_left.hpp> | |
34 | #include <boost/hana/fwd/any_of.hpp> | |
35 | #include <boost/hana/fwd/core/to.hpp> | |
36 | #include <boost/hana/fwd/difference.hpp> | |
37 | #include <boost/hana/fwd/intersection.hpp> | |
38 | #include <boost/hana/fwd/union.hpp> | |
39 | #include <boost/hana/insert.hpp> | |
40 | #include <boost/hana/is_subset.hpp> | |
41 | #include <boost/hana/length.hpp> | |
42 | #include <boost/hana/or.hpp> | |
43 | #include <boost/hana/remove.hpp> | |
44 | #include <boost/hana/tuple.hpp> | |
45 | #include <boost/hana/unpack.hpp> | |
46 | #include <boost/hana/value.hpp> | |
47 | ||
48 | #include <cstddef> | |
49 | #include <type_traits> | |
50 | #include <utility> | |
51 | ||
52 | ||
53 | BOOST_HANA_NAMESPACE_BEGIN | |
54 | ////////////////////////////////////////////////////////////////////////// | |
55 | // set | |
56 | ////////////////////////////////////////////////////////////////////////// | |
57 | //! @cond | |
58 | template <typename ...Xs> | |
59 | struct set | |
60 | : detail::operators::adl<set<Xs...>> | |
61 | , detail::searchable_operators<set<Xs...>> | |
62 | { | |
63 | tuple<Xs...> storage; | |
64 | using hana_tag = set_tag; | |
65 | static constexpr std::size_t size = sizeof...(Xs); | |
66 | ||
67 | explicit constexpr set(tuple<Xs...> const& xs) | |
68 | : storage(xs) | |
69 | { } | |
70 | ||
71 | explicit constexpr set(tuple<Xs...>&& xs) | |
72 | : storage(static_cast<tuple<Xs...>&&>(xs)) | |
73 | { } | |
74 | ||
75 | constexpr set(set const& other) = default; | |
76 | constexpr set(set&& other) = default; | |
77 | }; | |
78 | //! @endcond | |
79 | ||
80 | ////////////////////////////////////////////////////////////////////////// | |
81 | // Operators | |
82 | ////////////////////////////////////////////////////////////////////////// | |
83 | namespace detail { | |
84 | template <> | |
85 | struct comparable_operators<set_tag> { | |
86 | static constexpr bool value = true; | |
87 | }; | |
88 | } | |
89 | ||
90 | ////////////////////////////////////////////////////////////////////////// | |
91 | // make<set_tag> | |
92 | ////////////////////////////////////////////////////////////////////////// | |
93 | template <> | |
94 | struct make_impl<set_tag> { | |
95 | template <typename ...Xs> | |
96 | static constexpr auto apply(Xs&& ...xs) { | |
97 | #if defined(BOOST_HANA_CONFIG_ENABLE_DEBUG_MODE) | |
98 | static_assert(detail::fast_and<hana::Comparable<Xs>::value...>::value, | |
99 | "hana::make_set(xs...) requires all the 'xs' to be Comparable"); | |
100 | ||
101 | static_assert(detail::fast_and<hana::Hashable<Xs>::value...>::value, | |
102 | "hana::make_set(xs...) requires all the 'xs' to be Hashable"); | |
103 | ||
104 | static_assert(detail::fast_and< | |
105 | Constant<decltype(hana::equal(xs, xs))>::value... | |
106 | >::value, | |
107 | "hana::make_set(xs...) requires all the 'xs' to be " | |
108 | "Comparable at compile-time"); | |
109 | ||
110 | static_assert(!detail::has_duplicates<Xs&&...>::value, | |
111 | "hana::make_set(xs...) requires all the 'xs' to be unique"); | |
112 | #endif | |
113 | ||
114 | return set<typename detail::decay<Xs>::type...>{ | |
115 | hana::make_tuple(static_cast<Xs&&>(xs)...) | |
116 | }; | |
117 | } | |
118 | }; | |
119 | ||
120 | ////////////////////////////////////////////////////////////////////////// | |
121 | // Comparable | |
122 | ////////////////////////////////////////////////////////////////////////// | |
123 | template <> | |
124 | struct equal_impl<set_tag, set_tag> { | |
125 | template <typename S1, typename S2> | |
126 | static constexpr auto equal_helper(S1 const& s1, S2 const& s2, hana::true_) | |
127 | { return hana::is_subset(s1, s2); } | |
128 | ||
129 | template <typename S1, typename S2> | |
130 | static constexpr auto equal_helper(S1 const&, S2 const&, hana::false_) | |
131 | { return hana::false_c; } | |
132 | ||
133 | template <typename S1, typename S2> | |
134 | static constexpr decltype(auto) apply(S1&& s1, S2&& s2) { | |
135 | return equal_impl::equal_helper(s1, s2, hana::bool_c< | |
136 | decltype(hana::length(s1.storage))::value == | |
137 | decltype(hana::length(s2.storage))::value | |
138 | >); | |
139 | } | |
140 | }; | |
141 | ||
142 | ////////////////////////////////////////////////////////////////////////// | |
143 | // Foldable | |
144 | ////////////////////////////////////////////////////////////////////////// | |
145 | template <> | |
146 | struct unpack_impl<set_tag> { | |
147 | template <typename Set, typename F> | |
148 | static constexpr decltype(auto) apply(Set&& set, F&& f) { | |
149 | return hana::unpack(static_cast<Set&&>(set).storage, | |
150 | static_cast<F&&>(f)); | |
151 | } | |
152 | }; | |
153 | ||
154 | ////////////////////////////////////////////////////////////////////////// | |
155 | // Searchable | |
156 | ////////////////////////////////////////////////////////////////////////// | |
157 | template <> | |
158 | struct find_if_impl<set_tag> { | |
159 | template <typename Xs, typename Pred> | |
160 | static constexpr auto apply(Xs&& xs, Pred&& pred) { | |
161 | return hana::find_if(static_cast<Xs&&>(xs).storage, static_cast<Pred&&>(pred)); | |
162 | } | |
163 | }; | |
164 | ||
165 | template <> | |
166 | struct any_of_impl<set_tag> { | |
167 | template <typename Pred> | |
168 | struct any_of_helper { | |
169 | Pred const& pred; | |
170 | template <typename ...X> | |
171 | constexpr auto operator()(X const& ...x) const { | |
172 | return hana::or_(pred(x)...); | |
173 | } | |
174 | constexpr auto operator()() const { | |
175 | return hana::false_c; | |
176 | } | |
177 | }; | |
178 | ||
179 | template <typename Xs, typename Pred> | |
180 | static constexpr auto apply(Xs const& xs, Pred const& pred) { | |
181 | return hana::unpack(xs.storage, any_of_helper<Pred>{pred}); | |
182 | } | |
183 | }; | |
184 | ||
185 | template <> | |
186 | struct is_subset_impl<set_tag, set_tag> { | |
187 | template <typename Ys> | |
188 | struct all_contained { | |
189 | Ys const& ys; | |
190 | template <typename ...X> | |
191 | constexpr auto operator()(X const& ...x) const { | |
192 | return hana::bool_c<detail::fast_and< | |
193 | hana::value<decltype(hana::contains(ys, x))>()... | |
194 | >::value>; | |
195 | } | |
196 | }; | |
197 | ||
198 | template <typename Xs, typename Ys> | |
199 | static constexpr auto apply(Xs const& xs, Ys const& ys) { | |
200 | return hana::unpack(xs, all_contained<Ys>{ys}); | |
201 | } | |
202 | }; | |
203 | ||
204 | ////////////////////////////////////////////////////////////////////////// | |
205 | // Conversions | |
206 | ////////////////////////////////////////////////////////////////////////// | |
207 | template <typename F> | |
208 | struct to_impl<set_tag, F, when<hana::Foldable<F>::value>> { | |
209 | template <typename Xs> | |
210 | static constexpr decltype(auto) apply(Xs&& xs) { | |
211 | return hana::fold_left(static_cast<Xs&&>(xs), | |
212 | hana::make_set(), | |
213 | hana::insert); | |
214 | } | |
215 | }; | |
216 | ||
217 | ////////////////////////////////////////////////////////////////////////// | |
218 | // insert | |
219 | ////////////////////////////////////////////////////////////////////////// | |
220 | template <> | |
221 | struct insert_impl<set_tag> { | |
222 | template <typename Xs, typename X, typename Indices> | |
223 | static constexpr auto | |
224 | insert_helper(Xs&& xs, X&&, hana::true_, Indices) { | |
225 | return static_cast<Xs&&>(xs); | |
226 | } | |
227 | ||
228 | template <typename Xs, typename X, std::size_t ...n> | |
229 | static constexpr auto | |
230 | insert_helper(Xs&& xs, X&& x, hana::false_, std::index_sequence<n...>) { | |
231 | return hana::make_set( | |
232 | hana::at_c<n>(static_cast<Xs&&>(xs).storage)..., static_cast<X&&>(x) | |
233 | ); | |
234 | } | |
235 | ||
236 | template <typename Xs, typename X> | |
237 | static constexpr auto apply(Xs&& xs, X&& x) { | |
238 | constexpr bool c = hana::value<decltype(hana::contains(xs, x))>(); | |
239 | constexpr std::size_t size = std::remove_reference<Xs>::type::size; | |
240 | return insert_helper(static_cast<Xs&&>(xs), static_cast<X&&>(x), | |
241 | hana::bool_c<c>, std::make_index_sequence<size>{}); | |
242 | } | |
243 | }; | |
244 | ||
245 | ////////////////////////////////////////////////////////////////////////// | |
246 | // erase_key | |
247 | ////////////////////////////////////////////////////////////////////////// | |
248 | template <> | |
249 | struct erase_key_impl<set_tag> { | |
250 | template <typename Xs, typename X> | |
251 | static constexpr decltype(auto) apply(Xs&& xs, X&& x) { | |
252 | return hana::unpack( | |
253 | hana::remove(static_cast<Xs&&>(xs).storage, | |
254 | static_cast<X&&>(x)), | |
255 | hana::make_set | |
256 | ); | |
257 | } | |
258 | }; | |
259 | ||
260 | ////////////////////////////////////////////////////////////////////////// | |
261 | // intersection | |
262 | ////////////////////////////////////////////////////////////////////////// | |
263 | namespace detail { | |
264 | template <typename Ys> | |
265 | struct set_insert_if_contains { | |
266 | Ys const& ys; | |
267 | ||
268 | template <typename Result, typename Key> | |
269 | static constexpr auto helper(Result&& result, Key&& key, hana::true_) { | |
270 | return hana::insert(static_cast<Result&&>(result), static_cast<Key&&>(key)); | |
271 | } | |
272 | ||
273 | template <typename Result, typename Key> | |
274 | static constexpr auto helper(Result&& result, Key&&, hana::false_) { | |
275 | return static_cast<Result&&>(result); | |
276 | } | |
277 | ||
278 | template <typename Result, typename Key> | |
279 | constexpr auto operator()(Result&& result, Key&& key) const { | |
280 | constexpr bool keep = hana::value<decltype(hana::contains(ys, key))>(); | |
281 | return set_insert_if_contains::helper(static_cast<Result&&>(result), | |
282 | static_cast<Key&&>(key), | |
283 | hana::bool_c<keep>); | |
284 | } | |
285 | }; | |
286 | } | |
287 | ||
288 | template <> | |
289 | struct intersection_impl<set_tag> { | |
290 | template <typename Xs, typename Ys> | |
291 | static constexpr auto apply(Xs&& xs, Ys const& ys) { | |
292 | return hana::fold_left(static_cast<Xs&&>(xs), hana::make_set(), | |
293 | detail::set_insert_if_contains<Ys>{ys}); | |
294 | } | |
295 | }; | |
296 | ||
297 | ////////////////////////////////////////////////////////////////////////// | |
298 | // union_ | |
299 | ////////////////////////////////////////////////////////////////////////// | |
300 | template <> | |
301 | struct union_impl<set_tag> { | |
302 | template <typename Xs, typename Ys> | |
303 | static constexpr auto apply(Xs&& xs, Ys&& ys) { | |
304 | return hana::fold_left(static_cast<Xs&&>(xs), static_cast<Ys&&>(ys), | |
305 | hana::insert); | |
306 | } | |
307 | }; | |
308 | ||
309 | ////////////////////////////////////////////////////////////////////////// | |
310 | // difference | |
311 | ////////////////////////////////////////////////////////////////////////// | |
312 | template <> | |
313 | struct difference_impl<set_tag> { | |
314 | template <typename Xs, typename Ys> | |
315 | static constexpr auto apply(Xs&& xs, Ys&& ys) { | |
316 | return hana::fold_left(static_cast<Ys&&>(ys), static_cast<Xs&&>(xs), | |
317 | hana::erase_key); | |
318 | } | |
319 | }; | |
320 | BOOST_HANA_NAMESPACE_END | |
321 | ||
322 | #endif // !BOOST_HANA_SET_HPP |