set.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*!
  2. @file
  3. Forward declares `boost::hana::set`.
  4. @copyright Louis Dionne 2013-2016
  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`.
  29. //!
  30. //!
  31. //! Modeled concepts
  32. //! ----------------
  33. //! 1. `Comparable`\n
  34. //! Two sets are equal iff they contain the same elements, regardless of
  35. //! the order.
  36. //! @include example/set/comparable.cpp
  37. //!
  38. //! 2. Foldable\n
  39. //! Folding a set is equivalent to folding the sequence of its values.
  40. //! However, note that the values are not required to be in any specific
  41. //! order, so using the folds provided here with an operation that is not
  42. //! both commutative and associative will yield non-deterministic behavior.
  43. //! @include example/set/foldable.cpp
  44. //!
  45. //! 3. Searchable\n
  46. //! The elements in a set act as both its keys and its values. Since the
  47. //! elements of a set are unique, searching for an element will return
  48. //! either the only element which is equal to the searched value, or
  49. //! `nothing`. Also note that `operator[]` can be used instead of the
  50. //! `at_key` function.
  51. //! @include example/set/searchable.cpp
  52. //!
  53. //!
  54. //! Conversion from any `Foldable`
  55. //! ------------------------------
  56. //! Any `Foldable` structure can be converted into a `hana::set` with
  57. //! `to<set_tag>`. The elements of the structure must all be compile-time
  58. //! `Comparable`. If the structure contains duplicate elements, only
  59. //! the first occurence will appear in the resulting set. More
  60. //! specifically, conversion from a `Foldable` is equivalent to
  61. //! @code
  62. //! to<set_tag>(xs) == fold_left(xs, make_set(), insert)
  63. //! @endcode
  64. //!
  65. //! __Example__
  66. //! @include example/set/to.cpp
  67. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  68. template <typename implementation_defined>
  69. struct set {
  70. //! Copy-construct a set from another set. This constructor only
  71. //! exists when all the elements of the set are copy-constructible.
  72. constexpr set(set const& other) = default;
  73. //! Move-construct a set from another set. This constructor only
  74. //! exists when all the elements of the set are move-constructible.
  75. constexpr set(set&& other) = default;
  76. //! Equivalent to `hana::equal`
  77. template <typename X, typename Y>
  78. friend constexpr auto operator==(X&& x, Y&& y);
  79. //! Equivalent to `hana::not_equal`
  80. template <typename X, typename Y>
  81. friend constexpr auto operator!=(X&& x, Y&& y);
  82. //! Equivalent to `hana::at_key`
  83. template <typename Key>
  84. constexpr decltype(auto) operator[](Key&& key);
  85. };
  86. #else
  87. template <typename ...Xs>
  88. struct set;
  89. #endif
  90. //! Tag representing the `hana::set` container.
  91. //! @relates hana::set
  92. struct set_tag { };
  93. //! Function object for creating a `hana::set`.
  94. //! @relates hana::set
  95. //!
  96. //! Given zero or more values `xs...`, `make<set_tag>` returns a `set`
  97. //! containing those values. The values must all be compile-time
  98. //! `Comparable`, and no duplicate values may be provided. To create
  99. //! a `set` from a sequence with possible duplicates, use `to<set_tag>`
  100. //! instead.
  101. //!
  102. //!
  103. //! Example
  104. //! -------
  105. //! @include example/set/make.cpp
  106. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  107. template <>
  108. constexpr auto make<set_tag> = [](auto&& ...xs) {
  109. return set<implementation_defined>{forwarded(xs)...};
  110. };
  111. #endif
  112. //! Equivalent to `make<set_tag>`; provided for convenience.
  113. //! @relates hana::set
  114. //!
  115. //!
  116. //! Example
  117. //! -------
  118. //! @include example/set/make.cpp
  119. constexpr auto make_set = make<set_tag>;
  120. //! Insert an element in a `hana::set`.
  121. //! @relates hana::set
  122. //!
  123. //! If the set already contains an element that compares equal, then
  124. //! nothing is done and the set is returned as is.
  125. //!
  126. //!
  127. //! @param set
  128. //! The set in which to insert a value.
  129. //!
  130. //! @param element
  131. //! The value to insert. It must be compile-time `Comparable`.
  132. //!
  133. //!
  134. //! Example
  135. //! -------
  136. //! @include example/set/insert.cpp
  137. //!
  138. //!
  139. //! Benchmarks
  140. //! ----------
  141. //! <div class="benchmark-chart"
  142. //! style="min-width: 310px; height: 400px; margin: 0 auto"
  143. //! data-dataset="benchmark.insert.compile.json">
  144. //! </div>
  145. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  146. constexpr auto insert = [](auto&& set, auto&& element) {
  147. return tag-dispatched;
  148. };
  149. #endif
  150. //! Remove an element from a `hana::set`.
  151. //! @relates hana::set
  152. //!
  153. //! Returns a new set containing all the elements of the original,
  154. //! except the one comparing `equal` to the given element. If the set
  155. //! does not contain such an element, a new set equal to the original
  156. //! set is returned.
  157. //!
  158. //!
  159. //! @param set
  160. //! The set in which to remove a value.
  161. //!
  162. //! @param element
  163. //! The value to remove. It must be compile-time `Comparable`.
  164. //!
  165. //!
  166. //! Example
  167. //! -------
  168. //! @include example/set/erase_key.cpp
  169. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  170. constexpr auto erase_key = [](auto&& set, auto&& element) {
  171. return tag-dispatched;
  172. };
  173. #endif
  174. //! Equivalent to `to<set_tag>`; provided for convenience.
  175. //! @relates hana::set
  176. constexpr auto to_set = to<set_tag>;
  177. BOOST_HANA_NAMESPACE_END
  178. #endif // !BOOST_HANA_FWD_SET_HPP