set.hpp 12 KB

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