set.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*!
  2. @file
  3. Forward declares `boost::hana::set`.
  4. @copyright Louis Dionne 2013-2017
  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_FWD_SET_HPP
  9. #define BOOST_HANA_FWD_SET_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/fwd/core/to.hpp>
  12. #include <boost/hana/fwd/core/make.hpp>
  13. #include <boost/hana/fwd/erase_key.hpp>
  14. BOOST_HANA_NAMESPACE_BEGIN
  15. //! @ingroup group-datatypes
  16. //! Basic unordered container requiring unique, `Comparable` and
  17. //! `Hashable` keys.
  18. //!
  19. //! A set is an unordered container that can hold heterogeneous keys.
  20. //! A set requires (and ensures) that no duplicates are present when
  21. //! inserting new keys.
  22. //!
  23. //! @note
  24. //! The actual representation of a `hana::set` is implementation-defined.
  25. //! In particular, one should not take for granted the order of the
  26. //! template parameters and the presence of any additional constructors
  27. //! or assignment operators than what is documented. The canonical way of
  28. //! creating a `hana::set` is through `hana::make_set`. More details
  29. //! [in the tutorial](@ref tutorial-containers-types).
  30. //!
  31. //!
  32. //! Modeled concepts
  33. //! ----------------
  34. //! 1. `Comparable`\n
  35. //! Two sets are equal iff they contain the same elements, regardless of
  36. //! the order.
  37. //! @include example/set/comparable.cpp
  38. //!
  39. //! 2. Foldable\n
  40. //! Folding a set is equivalent to folding the sequence of its values.
  41. //! However, note that the values are not required to be in any specific
  42. //! order, so using the folds provided here with an operation that is not
  43. //! both commutative and associative will yield non-deterministic behavior.
  44. //! @include example/set/foldable.cpp
  45. //!
  46. //! 3. Searchable\n
  47. //! The elements in a set act as both its keys and its values. Since the
  48. //! elements of a set are unique, searching for an element will return
  49. //! either the only element which is equal to the searched value, or
  50. //! `nothing`. Also note that `operator[]` can be used instead of the
  51. //! `at_key` function.
  52. //! @include example/set/searchable.cpp
  53. //!
  54. //!
  55. //! Conversion from any `Foldable`
  56. //! ------------------------------
  57. //! Any `Foldable` structure can be converted into a `hana::set` with
  58. //! `to<set_tag>`. The elements of the structure must all be compile-time
  59. //! `Comparable`. If the structure contains duplicate elements, only
  60. //! the first occurence will appear in the resulting set. More
  61. //! specifically, conversion from a `Foldable` is equivalent to
  62. //! @code
  63. //! to<set_tag>(xs) == fold_left(xs, make_set(), insert)
  64. //! @endcode
  65. //!
  66. //! __Example__
  67. //! @include example/set/to.cpp
  68. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  69. template <typename implementation_defined>
  70. struct set {
  71. //! Default-construct a set. This constructor only exists when all the
  72. //! elements of the set are default-constructible.
  73. constexpr set() = default;
  74. //! Copy-construct a set from another set. This constructor only
  75. //! exists when all the elements of the set are copy-constructible.
  76. constexpr set(set const& other) = default;
  77. //! Move-construct a set from another set. This constructor only
  78. //! exists when all the elements of the set are move-constructible.
  79. constexpr set(set&& other) = default;
  80. //! Equivalent to `hana::equal`
  81. template <typename X, typename Y>
  82. friend constexpr auto operator==(X&& x, Y&& y);
  83. //! Equivalent to `hana::not_equal`
  84. template <typename X, typename Y>
  85. friend constexpr auto operator!=(X&& x, Y&& y);
  86. //! Equivalent to `hana::at_key`
  87. template <typename Key>
  88. constexpr decltype(auto) operator[](Key&& key);
  89. };
  90. #else
  91. template <typename ...Xs>
  92. struct set;
  93. #endif
  94. //! Tag representing the `hana::set` container.
  95. //! @relates hana::set
  96. struct set_tag { };
  97. //! Function object for creating a `hana::set`.
  98. //! @relates hana::set
  99. //!
  100. //! Given zero or more values `xs...`, `make<set_tag>` returns a `set`
  101. //! containing those values. The values must all be compile-time
  102. //! `Comparable`, and no duplicate values may be provided. To create
  103. //! a `set` from a sequence with possible duplicates, use `to<set_tag>`
  104. //! instead.
  105. //!
  106. //!
  107. //! Example
  108. //! -------
  109. //! @include example/set/make.cpp
  110. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  111. template <>
  112. constexpr auto make<set_tag> = [](auto&& ...xs) {
  113. return set<implementation_defined>{forwarded(xs)...};
  114. };
  115. #endif
  116. //! Equivalent to `make<set_tag>`; provided for convenience.
  117. //! @relates hana::set
  118. //!
  119. //!
  120. //! Example
  121. //! -------
  122. //! @include example/set/make.cpp
  123. constexpr auto make_set = make<set_tag>;
  124. //! Insert an element in a `hana::set`.
  125. //! @relates hana::set
  126. //!
  127. //! If the set already contains an element that compares equal, then
  128. //! nothing is done and the set is returned as is.
  129. //!
  130. //!
  131. //! @param set
  132. //! The set in which to insert a value.
  133. //!
  134. //! @param element
  135. //! The value to insert. It must be compile-time `Comparable`.
  136. //!
  137. //!
  138. //! Example
  139. //! -------
  140. //! @include example/set/insert.cpp
  141. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  142. constexpr auto insert = [](auto&& set, auto&& element) {
  143. return tag-dispatched;
  144. };
  145. #endif
  146. //! Remove an element from a `hana::set`.
  147. //! @relates hana::set
  148. //!
  149. //! Returns a new set containing all the elements of the original,
  150. //! except the one comparing `equal` to the given element. If the set
  151. //! does not contain such an element, a new set equal to the original
  152. //! set is returned.
  153. //!
  154. //!
  155. //! @param set
  156. //! The set in which to remove a value.
  157. //!
  158. //! @param element
  159. //! The value to remove. It must be compile-time `Comparable`.
  160. //!
  161. //!
  162. //! Example
  163. //! -------
  164. //! @include example/set/erase_key.cpp
  165. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  166. constexpr auto erase_key = [](auto&& set, auto&& element) {
  167. return tag-dispatched;
  168. };
  169. #endif
  170. //! Returns the union of two sets.
  171. //! @relates hana::set
  172. //!
  173. //! Given two sets `xs` and `ys`, `union_(xs, ys)` is a new set containing
  174. //! all the elements of `xs` and all the elements of `ys`, without
  175. //! duplicates. For any object `x`, the following holds: `x` is in
  176. //! `hana::union_(xs, ys)` if and only if `x` is in `xs` or `x` is in `ys`.
  177. //!
  178. //!
  179. //! @param xs, ys
  180. //! Two sets to compute the union of.
  181. //!
  182. //!
  183. //! Example
  184. //! -------
  185. //! @include example/set/union.cpp
  186. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  187. constexpr auto union_ = [](auto&& xs, auto&& ys) {
  188. return tag-dispatched;
  189. };
  190. #endif
  191. //! Returns the intersection of two sets.
  192. //! @relates hana::set
  193. //!
  194. //! Given two sets `xs` and `ys`, `intersection(xs, ys)` is a new set
  195. //! containing exactly those elements that are present both in `xs` and
  196. //! in `ys`.
  197. //! In other words, the following holds for any object `x`:
  198. //! @code
  199. //! x ^in^ intersection(xs, ys) if and only if x ^in^ xs && x ^in^ ys
  200. //! @endcode
  201. //!
  202. //!
  203. //! @param xs, ys
  204. //! Two sets to intersect.
  205. //!
  206. //!
  207. //! Example
  208. //! -------
  209. //! @include example/set/intersection.cpp
  210. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  211. constexpr auto intersection = [](auto&& xs, auto&& ys) {
  212. return tag-dispatched;
  213. };
  214. #endif
  215. //! Equivalent to `to<set_tag>`; provided for convenience.
  216. //! @relates hana::set
  217. constexpr auto to_set = to<set_tag>;
  218. //! Returns the set-theoretic difference of two sets.
  219. //! @relates hana::set
  220. //!
  221. //! Given two sets `xs` and `ys`, `difference(xs, ys)` is a new set
  222. //! containing all the elements of `xs` that are _not_ contained in `ys`.
  223. //! For any object `x`, the following holds:
  224. //! @code
  225. //! x ^in^ difference(xs, ys) if and only if x ^in^ xs && !(x ^in^ ys)
  226. //! @endcode
  227. //!
  228. //!
  229. //! This operation is not commutative, i.e. `difference(xs, ys)` is not
  230. //! necessarily the same as `difference(ys, xs)`. Indeed, consider the
  231. //! case where `xs` is empty and `ys` isn't. Then, `difference(xs, ys)`
  232. //! is empty but `difference(ys, xs)` is equal to `ys`. For the symmetric
  233. //! version of this operation, see `symmetric_difference`.
  234. //!
  235. //!
  236. //! @param xs
  237. //! A set param to remove values from.
  238. //!
  239. //! @param ys
  240. //! The set whose values are removed from `xs`.
  241. //!
  242. //!
  243. //! Example
  244. //! -------
  245. //! @include example/set/difference.cpp
  246. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  247. constexpr auto difference = [](auto&& xs, auto&& ys) {
  248. return tag-dispatched;
  249. };
  250. #endif
  251. //! Returns the symmetric set-theoretic difference of two sets.
  252. //! @relates hana::set
  253. //!
  254. //! Given two sets `xs` and `ys`, `symmetric_difference(xs, ys)` is a new
  255. //! set containing all the elements of `xs` that are not contained in `ys`,
  256. //! and all the elements of `ys` that are not contained in `xs`. The
  257. //! symmetric difference of two sets satisfies the following:
  258. //! @code
  259. //! symmetric_difference(xs, ys) == union_(difference(xs, ys), difference(ys, xs))
  260. //! @endcode
  261. //!
  262. //!
  263. //! @param xs, ys
  264. //! Two sets to compute the symmetric difference of.
  265. //!
  266. //!
  267. //! Example
  268. //! -------
  269. //! @include example/set/symmetric_difference.cpp
  270. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  271. constexpr auto symmetric_difference = [](auto&& xs, auto&& ys) {
  272. return tag-dispatched;
  273. };
  274. #endif
  275. BOOST_HANA_NAMESPACE_END
  276. #endif // !BOOST_HANA_FWD_SET_HPP