3 Defines `boost::hana::set`.
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_SET_HPP
11 #define BOOST_HANA_SET_HPP
13 #include <boost/hana/fwd/set.hpp>
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>
49 #include <type_traits>
53 BOOST_HANA_NAMESPACE_BEGIN
54 //////////////////////////////////////////////////////////////////////////
56 //////////////////////////////////////////////////////////////////////////
58 template <typename ...Xs>
60 : detail::operators::adl<set<Xs...>>
61 , detail::searchable_operators<set<Xs...>>
64 using hana_tag = set_tag;
65 static constexpr std::size_t size = sizeof...(Xs);
67 explicit constexpr set(tuple<Xs...> const& xs)
71 explicit constexpr set(tuple<Xs...>&& xs)
72 : storage(static_cast<tuple<Xs...>&&>(xs))
75 constexpr set(set const& other) = default;
76 constexpr set(set&& other) = default;
80 //////////////////////////////////////////////////////////////////////////
82 //////////////////////////////////////////////////////////////////////////
85 struct comparable_operators<set_tag> {
86 static constexpr bool value = true;
90 //////////////////////////////////////////////////////////////////////////
92 //////////////////////////////////////////////////////////////////////////
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");
101 static_assert(detail::fast_and<hana::Hashable<Xs>::value...>::value,
102 "hana::make_set(xs...) requires all the 'xs' to be Hashable");
104 static_assert(detail::fast_and<
105 Constant<decltype(hana::equal(xs, xs))>::value...
107 "hana::make_set(xs...) requires all the 'xs' to be "
108 "Comparable at compile-time");
110 static_assert(!detail::has_duplicates<Xs&&...>::value,
111 "hana::make_set(xs...) requires all the 'xs' to be unique");
114 return set<typename detail::decay<Xs>::type...>{
115 hana::make_tuple(static_cast<Xs&&>(xs)...)
120 //////////////////////////////////////////////////////////////////////////
122 //////////////////////////////////////////////////////////////////////////
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); }
129 template <typename S1, typename S2>
130 static constexpr auto equal_helper(S1 const&, S2 const&, hana::false_)
131 { return hana::false_c; }
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
142 //////////////////////////////////////////////////////////////////////////
144 //////////////////////////////////////////////////////////////////////////
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));
154 //////////////////////////////////////////////////////////////////////////
156 //////////////////////////////////////////////////////////////////////////
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));
166 struct any_of_impl<set_tag> {
167 template <typename Pred>
168 struct any_of_helper {
170 template <typename ...X>
171 constexpr auto operator()(X const& ...x) const {
172 return hana::or_(pred(x)...);
174 constexpr auto operator()() const {
175 return hana::false_c;
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});
186 struct is_subset_impl<set_tag, set_tag> {
187 template <typename Ys>
188 struct all_contained {
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))>()...
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});
204 //////////////////////////////////////////////////////////////////////////
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),
217 //////////////////////////////////////////////////////////////////////////
219 //////////////////////////////////////////////////////////////////////////
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);
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)
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>{});
245 //////////////////////////////////////////////////////////////////////////
247 //////////////////////////////////////////////////////////////////////////
249 struct erase_key_impl<set_tag> {
250 template <typename Xs, typename X>
251 static constexpr decltype(auto) apply(Xs&& xs, X&& x) {
253 hana::remove(static_cast<Xs&&>(xs).storage,
254 static_cast<X&&>(x)),
260 //////////////////////////////////////////////////////////////////////////
262 //////////////////////////////////////////////////////////////////////////
264 template <typename Ys>
265 struct set_insert_if_contains {
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));
273 template <typename Result, typename Key>
274 static constexpr auto helper(Result&& result, Key&&, hana::false_) {
275 return static_cast<Result&&>(result);
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),
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});
297 //////////////////////////////////////////////////////////////////////////
299 //////////////////////////////////////////////////////////////////////////
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),
309 //////////////////////////////////////////////////////////////////////////
311 //////////////////////////////////////////////////////////////////////////
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),
320 BOOST_HANA_NAMESPACE_END
322 #endif // !BOOST_HANA_SET_HPP