unordered_set_of.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Boost.Bimap
  2. //
  3. // Copyright (c) 2006-2007 Matias Capeletto
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. /// \file unordered_set_of.hpp
  9. /// \brief Include support for unordered_set constrains for the bimap container
  10. #ifndef BOOST_BIMAP_UNORDERED_SET_OF_HPP
  11. #define BOOST_BIMAP_UNORDERED_SET_OF_HPP
  12. #if defined(_MSC_VER)
  13. #pragma once
  14. #endif
  15. #include <boost/config.hpp>
  16. #include <boost/bimap/detail/user_interface_config.hpp>
  17. #include <functional>
  18. #include <boost/functional/hash.hpp>
  19. #include <boost/mpl/bool.hpp>
  20. #include <boost/concept_check.hpp>
  21. #include <boost/bimap/detail/concept_tags.hpp>
  22. #include <boost/bimap/tags/support/value_type_of.hpp>
  23. #include <boost/bimap/detail/generate_index_binder.hpp>
  24. #include <boost/bimap/detail/generate_view_binder.hpp>
  25. #include <boost/bimap/detail/generate_relation_binder.hpp>
  26. #include <boost/multi_index/hashed_index.hpp>
  27. #include <boost/bimap/views/unordered_map_view.hpp>
  28. #include <boost/bimap/views/unordered_set_view.hpp>
  29. namespace boost {
  30. namespace bimaps {
  31. /// \brief Set Type Specification
  32. /**
  33. This struct is used to specify an unordered_set specification.
  34. It is not a container, it is just a metaprogramming facility to
  35. express the type of a set. Generally, this specification will
  36. be used in other place to create a container.
  37. It has the same syntax that an tr1::unordered_set instantiation,
  38. except that the allocator cannot be specified. The rationale behind
  39. this difference is that the allocator is not part of the
  40. unordered_set type specification, rather it is a container
  41. configuration parameter.
  42. The first parameter is the type of the objects in the set, the
  43. second one is a Hash Functor that takes objects of this type, and
  44. the third one is a Functor that compares them for equality.
  45. Bimap binding metafunctions can be used with this class in
  46. the following way:
  47. \code
  48. using namespace support;
  49. BOOST_STATIC_ASSERT( is_set_type_of< unordered_set_of<Type> >::value )
  50. BOOST_STATIC_ASSERT
  51. (
  52. is_same
  53. <
  54. unordered_set_of<Type,HashFunctor,EqualKey>::index_bind
  55. <
  56. KeyExtractor,
  57. Tag
  58. >::type,
  59. hashed_unique< tag<Tag>, KeyExtractor, HashFunctor, EqualKey >
  60. >::value
  61. )
  62. typedef bimap
  63. <
  64. unordered_set_of<Type>, RightKeyType
  65. > bimap_with_left_type_as_unordered_set;
  66. BOOST_STATIC_ASSERT
  67. (
  68. is_same
  69. <
  70. unordered_set_of<Type>::map_view_bind
  71. <
  72. member_at::left,
  73. bimap_with_left_type_as_unordered_set
  74. >::type,
  75. unordered_map_view
  76. <
  77. member_at::left,
  78. bimap_with_left_type_as_unordered_set
  79. >
  80. >::value
  81. )
  82. \endcode
  83. See also unordered_set_of_relation.
  84. **/
  85. template
  86. <
  87. class KeyType,
  88. class HashFunctor = hash< BOOST_DEDUCED_TYPENAME
  89. ::boost::bimaps::tags::support::value_type_of<KeyType>::type >,
  90. class EqualKey = std::equal_to< BOOST_DEDUCED_TYPENAME
  91. ::boost::bimaps::tags::support::value_type_of<KeyType>::type >
  92. >
  93. struct unordered_set_of : public ::boost::bimaps::detail::set_type_of_tag
  94. {
  95. /// User type, can be tagged
  96. typedef KeyType user_type;
  97. /// Type of the object that will be stored in the container
  98. typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::tags::support::
  99. value_type_of<user_type>::type value_type;
  100. /// Hash Functor that takes value_type objects
  101. typedef HashFunctor hasher;
  102. /// Functor that compare two value_type objects for equality
  103. typedef EqualKey key_equal;
  104. struct lazy_concept_checked
  105. {
  106. BOOST_CLASS_REQUIRE ( value_type,
  107. boost, AssignableConcept );
  108. BOOST_CLASS_REQUIRE3( hasher, std::size_t, value_type,
  109. boost, UnaryFunctionConcept );
  110. BOOST_CLASS_REQUIRE4( key_equal, bool, value_type, value_type,
  111. boost, BinaryFunctionConcept );
  112. typedef unordered_set_of type;
  113. };
  114. BOOST_BIMAP_GENERATE_INDEX_BINDER_2CP(
  115. // binds to
  116. multi_index::hashed_unique,
  117. // with
  118. hasher,
  119. key_equal
  120. )
  121. BOOST_BIMAP_GENERATE_MAP_VIEW_BINDER(
  122. // binds to
  123. views::unordered_map_view
  124. )
  125. BOOST_BIMAP_GENERATE_SET_VIEW_BINDER(
  126. // binds to
  127. views::unordered_set_view
  128. )
  129. typedef mpl::bool_<false> mutable_key;
  130. };
  131. /// \brief Set Of Relation Specification
  132. /**
  133. This struct is similar to unordered_set_of but it is bind logically to
  134. a relation. It is used in the bimap instantiation to specify the
  135. desired type of the main view. This struct implements internally
  136. a metafunction named bind_to that manages the quite complicated
  137. task of finding the right type of the set for the relation.
  138. \code
  139. template<class Relation>
  140. struct bind_to
  141. {
  142. typedef -unspecified- type;
  143. };
  144. \endcode
  145. See also unordered_set_of, is_set_type_of_relation.
  146. **/
  147. template
  148. <
  149. class HashFunctor = hash< _relation >,
  150. class EqualKey = std::equal_to< _relation >
  151. >
  152. struct unordered_set_of_relation : public ::boost::bimaps::detail::set_type_of_relation_tag
  153. {
  154. /// Hash Functor that takes value_type objects
  155. typedef HashFunctor hasher;
  156. /// Functor that compare two value_type objects for equality
  157. typedef EqualKey key_equal;
  158. BOOST_BIMAP_GENERATE_RELATION_BINDER_2CP(
  159. // binds to
  160. unordered_set_of,
  161. // with
  162. hasher,
  163. key_equal
  164. )
  165. typedef mpl::bool_<false> left_mutable_key;
  166. typedef mpl::bool_<false> right_mutable_key;
  167. };
  168. } // namespace bimaps
  169. } // namespace boost
  170. #endif // BOOST_BIMAP_UNORDERED_SET_OF_HPP