unordered_set_hook.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Olaf Krzikalla 2004-2006.
  4. // (C) Copyright Ion Gaztanaga 2006-2013
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/intrusive for documentation.
  11. //
  12. /////////////////////////////////////////////////////////////////////////////
  13. #ifndef BOOST_INTRUSIVE_UNORDERED_SET_HOOK_HPP
  14. #define BOOST_INTRUSIVE_UNORDERED_SET_HOOK_HPP
  15. #include <boost/intrusive/detail/config_begin.hpp>
  16. #include <boost/intrusive/intrusive_fwd.hpp>
  17. #include <boost/pointer_cast.hpp>
  18. #include <boost/intrusive/detail/utilities.hpp>
  19. #include <boost/intrusive/pointer_traits.hpp>
  20. #include <boost/intrusive/slist_hook.hpp>
  21. #include <boost/intrusive/options.hpp>
  22. #include <boost/intrusive/pointer_traits.hpp>
  23. #include <boost/intrusive/detail/generic_hook.hpp>
  24. namespace boost {
  25. namespace intrusive {
  26. /// @cond
  27. template<class VoidPointer, bool StoreHash, bool OptimizeMultiKey>
  28. struct unordered_node
  29. : public slist_node<VoidPointer>
  30. {
  31. typedef typename pointer_traits
  32. <VoidPointer>::template rebind_pointer
  33. < unordered_node<VoidPointer, StoreHash, OptimizeMultiKey> >::type
  34. node_ptr;
  35. node_ptr prev_in_group_;
  36. std::size_t hash_;
  37. };
  38. template<class VoidPointer>
  39. struct unordered_node<VoidPointer, false, true>
  40. : public slist_node<VoidPointer>
  41. {
  42. typedef typename pointer_traits
  43. <VoidPointer>::template rebind_pointer
  44. < unordered_node<VoidPointer, false, true> >::type
  45. node_ptr;
  46. node_ptr prev_in_group_;
  47. };
  48. template<class VoidPointer>
  49. struct unordered_node<VoidPointer, true, false>
  50. : public slist_node<VoidPointer>
  51. {
  52. typedef typename pointer_traits
  53. <VoidPointer>::template rebind_pointer
  54. < unordered_node<VoidPointer, true, false> >::type
  55. node_ptr;
  56. std::size_t hash_;
  57. };
  58. template<class VoidPointer, bool StoreHash, bool OptimizeMultiKey>
  59. struct unordered_node_traits
  60. : public slist_node_traits<VoidPointer>
  61. {
  62. typedef slist_node_traits<VoidPointer> reduced_slist_node_traits;
  63. typedef unordered_node<VoidPointer, StoreHash, OptimizeMultiKey> node;
  64. typedef typename pointer_traits
  65. <VoidPointer>::template rebind_pointer
  66. < node >::type node_ptr;
  67. typedef typename pointer_traits
  68. <VoidPointer>::template rebind_pointer
  69. < const node >::type const_node_ptr;
  70. static const bool store_hash = StoreHash;
  71. static const bool optimize_multikey = OptimizeMultiKey;
  72. static node_ptr get_next(const const_node_ptr & n)
  73. {
  74. return pointer_traits<node_ptr>::pointer_to(static_cast<node&>(*n->next_));
  75. }
  76. static void set_next(const node_ptr & n, const node_ptr & next)
  77. { n->next_ = next; }
  78. static node_ptr get_prev_in_group(const const_node_ptr & n)
  79. { return n->prev_in_group_; }
  80. static void set_prev_in_group(const node_ptr & n, const node_ptr & prev)
  81. { n->prev_in_group_ = prev; }
  82. static std::size_t get_hash(const const_node_ptr & n)
  83. { return n->hash_; }
  84. static void set_hash(const node_ptr & n, std::size_t h)
  85. { n->hash_ = h; }
  86. };
  87. template<class NodeTraits>
  88. struct unordered_group_adapter
  89. {
  90. typedef typename NodeTraits::node node;
  91. typedef typename NodeTraits::node_ptr node_ptr;
  92. typedef typename NodeTraits::const_node_ptr const_node_ptr;
  93. static node_ptr get_next(const const_node_ptr & n)
  94. { return NodeTraits::get_prev_in_group(n); }
  95. static void set_next(const node_ptr & n, const node_ptr & next)
  96. { NodeTraits::set_prev_in_group(n, next); }
  97. };
  98. template<class NodeTraits>
  99. struct unordered_algorithms
  100. : public circular_slist_algorithms<NodeTraits>
  101. {
  102. typedef circular_slist_algorithms<NodeTraits> base_type;
  103. typedef unordered_group_adapter<NodeTraits> group_traits;
  104. typedef circular_slist_algorithms<group_traits> group_algorithms;
  105. typedef NodeTraits node_traits;
  106. typedef typename NodeTraits::node node;
  107. typedef typename NodeTraits::node_ptr node_ptr;
  108. typedef typename NodeTraits::const_node_ptr const_node_ptr;
  109. static void init(typename base_type::node_ptr n)
  110. {
  111. base_type::init(n);
  112. group_algorithms::init(n);
  113. }
  114. static void init_header(typename base_type::node_ptr n)
  115. {
  116. base_type::init_header(n);
  117. group_algorithms::init_header(n);
  118. }
  119. static void unlink(typename base_type::node_ptr n)
  120. {
  121. base_type::unlink(n);
  122. group_algorithms::unlink(n);
  123. }
  124. };
  125. template<class VoidPointer, bool StoreHash, bool OptimizeMultiKey>
  126. struct get_uset_node_algo
  127. {
  128. typedef typename detail::if_c
  129. < (StoreHash || OptimizeMultiKey)
  130. , unordered_node_traits<VoidPointer, StoreHash, OptimizeMultiKey>
  131. , slist_node_traits<VoidPointer>
  132. >::type node_traits_type;
  133. typedef typename detail::if_c
  134. < OptimizeMultiKey
  135. , unordered_algorithms<node_traits_type>
  136. , circular_slist_algorithms<node_traits_type>
  137. >::type type;
  138. };
  139. /// @endcond
  140. //! Helper metafunction to define a \c unordered_set_base_hook that yields to the same
  141. //! type when the same options (either explicitly or implicitly) are used.
  142. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  143. template<class ...Options>
  144. #else
  145. template<class O1 = void, class O2 = void, class O3 = void, class O4 = void>
  146. #endif
  147. struct make_unordered_set_base_hook
  148. {
  149. /// @cond
  150. typedef typename pack_options
  151. < hook_defaults,
  152. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  153. O1, O2, O3, O4
  154. #else
  155. Options...
  156. #endif
  157. >::type packed_options;
  158. typedef generic_hook
  159. < get_uset_node_algo<typename packed_options::void_pointer
  160. , packed_options::store_hash
  161. , packed_options::optimize_multikey
  162. >
  163. , typename packed_options::tag
  164. , packed_options::link_mode
  165. , HashBaseHookId
  166. > implementation_defined;
  167. /// @endcond
  168. typedef implementation_defined type;
  169. };
  170. //! Derive a class from unordered_set_base_hook in order to store objects in
  171. //! in an unordered_set/unordered_multi_set. unordered_set_base_hook holds the data necessary to maintain
  172. //! the unordered_set/unordered_multi_set and provides an appropriate value_traits class for unordered_set/unordered_multi_set.
  173. //!
  174. //! The hook admits the following options: \c tag<>, \c void_pointer<>,
  175. //! \c link_mode<>, \c store_hash<> and \c optimize_multikey<>.
  176. //!
  177. //! \c tag<> defines a tag to identify the node.
  178. //! The same tag value can be used in different classes, but if a class is
  179. //! derived from more than one \c list_base_hook, then each \c list_base_hook needs its
  180. //! unique tag.
  181. //!
  182. //! \c void_pointer<> is the pointer type that will be used internally in the hook
  183. //! and the the container configured to use this hook.
  184. //!
  185. //! \c link_mode<> will specify the linking mode of the hook (\c normal_link,
  186. //! \c auto_unlink or \c safe_link).
  187. //!
  188. //! \c store_hash<> will tell the hook to store the hash of the value
  189. //! to speed up rehashings.
  190. //!
  191. //! \c optimize_multikey<> will tell the hook to store a link to form a group
  192. //! with other value with the same value to speed up searches and insertions
  193. //! in unordered_multisets with a great number of with equivalent keys.
  194. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  195. template<class ...Options>
  196. #else
  197. template<class O1, class O2, class O3, class O4>
  198. #endif
  199. class unordered_set_base_hook
  200. : public make_unordered_set_base_hook<
  201. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  202. O1, O2, O3, O4
  203. #else
  204. Options...
  205. #endif
  206. >::type
  207. {
  208. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  209. public:
  210. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  211. //! initializes the node to an unlinked state.
  212. //!
  213. //! <b>Throws</b>: Nothing.
  214. unordered_set_base_hook();
  215. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  216. //! initializes the node to an unlinked state. The argument is ignored.
  217. //!
  218. //! <b>Throws</b>: Nothing.
  219. //!
  220. //! <b>Rationale</b>: Providing a copy-constructor
  221. //! makes classes using the hook STL-compliant without forcing the
  222. //! user to do some additional work. \c swap can be used to emulate
  223. //! move-semantics.
  224. unordered_set_base_hook(const unordered_set_base_hook& );
  225. //! <b>Effects</b>: Empty function. The argument is ignored.
  226. //!
  227. //! <b>Throws</b>: Nothing.
  228. //!
  229. //! <b>Rationale</b>: Providing an assignment operator
  230. //! makes classes using the hook STL-compliant without forcing the
  231. //! user to do some additional work. \c swap can be used to emulate
  232. //! move-semantics.
  233. unordered_set_base_hook& operator=(const unordered_set_base_hook& );
  234. //! <b>Effects</b>: If link_mode is \c normal_link, the destructor does
  235. //! nothing (ie. no code is generated). If link_mode is \c safe_link and the
  236. //! object is stored in an unordered_set an assertion is raised. If link_mode is
  237. //! \c auto_unlink and \c is_linked() is true, the node is unlinked.
  238. //!
  239. //! <b>Throws</b>: Nothing.
  240. ~unordered_set_base_hook();
  241. //! <b>Effects</b>: Swapping two nodes swaps the position of the elements
  242. //! related to those nodes in one or two containers. That is, if the node
  243. //! this is part of the element e1, the node x is part of the element e2
  244. //! and both elements are included in the containers s1 and s2, then after
  245. //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1
  246. //! at the position of e1. If one element is not in a container, then
  247. //! after the swap-operation the other element is not in a container.
  248. //! Iterators to e1 and e2 related to those nodes are invalidated.
  249. //!
  250. //! <b>Complexity</b>: Constant
  251. //!
  252. //! <b>Throws</b>: Nothing.
  253. void swap_nodes(unordered_set_base_hook &other);
  254. //! <b>Precondition</b>: link_mode must be \c safe_link or \c auto_unlink.
  255. //!
  256. //! <b>Returns</b>: true, if the node belongs to a container, false
  257. //! otherwise. This function can be used to test whether \c unordered_set::iterator_to
  258. //! will return a valid iterator.
  259. //!
  260. //! <b>Complexity</b>: Constant
  261. bool is_linked() const;
  262. //! <b>Effects</b>: Removes the node if it's inserted in a container.
  263. //! This function is only allowed if link_mode is \c auto_unlink.
  264. //!
  265. //! <b>Throws</b>: Nothing.
  266. void unlink();
  267. #endif
  268. };
  269. //! Helper metafunction to define a \c unordered_set_member_hook that yields to the same
  270. //! type when the same options (either explicitly or implicitly) are used.
  271. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  272. template<class ...Options>
  273. #else
  274. template<class O1 = void, class O2 = void, class O3 = void, class O4 = void>
  275. #endif
  276. struct make_unordered_set_member_hook
  277. {
  278. /// @cond
  279. typedef typename pack_options
  280. < hook_defaults,
  281. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  282. O1, O2, O3, O4
  283. #else
  284. Options...
  285. #endif
  286. >::type packed_options;
  287. typedef generic_hook
  288. < get_uset_node_algo< typename packed_options::void_pointer
  289. , packed_options::store_hash
  290. , packed_options::optimize_multikey
  291. >
  292. , member_tag
  293. , packed_options::link_mode
  294. , NoBaseHookId
  295. > implementation_defined;
  296. /// @endcond
  297. typedef implementation_defined type;
  298. };
  299. //! Put a public data member unordered_set_member_hook in order to store objects of this class in
  300. //! an unordered_set/unordered_multi_set. unordered_set_member_hook holds the data necessary for maintaining the
  301. //! unordered_set/unordered_multi_set and provides an appropriate value_traits class for unordered_set/unordered_multi_set.
  302. //!
  303. //! The hook admits the following options: \c void_pointer<>,
  304. //! \c link_mode<> and \c store_hash<>.
  305. //!
  306. //! \c void_pointer<> is the pointer type that will be used internally in the hook
  307. //! and the the container configured to use this hook.
  308. //!
  309. //! \c link_mode<> will specify the linking mode of the hook (\c normal_link,
  310. //! \c auto_unlink or \c safe_link).
  311. //!
  312. //! \c store_hash<> will tell the hook to store the hash of the value
  313. //! to speed up rehashings.
  314. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  315. template<class ...Options>
  316. #else
  317. template<class O1, class O2, class O3, class O4>
  318. #endif
  319. class unordered_set_member_hook
  320. : public make_unordered_set_member_hook<
  321. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  322. O1, O2, O3, O4
  323. #else
  324. Options...
  325. #endif
  326. >::type
  327. {
  328. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  329. public:
  330. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  331. //! initializes the node to an unlinked state.
  332. //!
  333. //! <b>Throws</b>: Nothing.
  334. unordered_set_member_hook();
  335. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  336. //! initializes the node to an unlinked state. The argument is ignored.
  337. //!
  338. //! <b>Throws</b>: Nothing.
  339. //!
  340. //! <b>Rationale</b>: Providing a copy-constructor
  341. //! makes classes using the hook STL-compliant without forcing the
  342. //! user to do some additional work. \c swap can be used to emulate
  343. //! move-semantics.
  344. unordered_set_member_hook(const unordered_set_member_hook& );
  345. //! <b>Effects</b>: Empty function. The argument is ignored.
  346. //!
  347. //! <b>Throws</b>: Nothing.
  348. //!
  349. //! <b>Rationale</b>: Providing an assignment operator
  350. //! makes classes using the hook STL-compliant without forcing the
  351. //! user to do some additional work. \c swap can be used to emulate
  352. //! move-semantics.
  353. unordered_set_member_hook& operator=(const unordered_set_member_hook& );
  354. //! <b>Effects</b>: If link_mode is \c normal_link, the destructor does
  355. //! nothing (ie. no code is generated). If link_mode is \c safe_link and the
  356. //! object is stored in an unordered_set an assertion is raised. If link_mode is
  357. //! \c auto_unlink and \c is_linked() is true, the node is unlinked.
  358. //!
  359. //! <b>Throws</b>: Nothing.
  360. ~unordered_set_member_hook();
  361. //! <b>Effects</b>: Swapping two nodes swaps the position of the elements
  362. //! related to those nodes in one or two containers. That is, if the node
  363. //! this is part of the element e1, the node x is part of the element e2
  364. //! and both elements are included in the containers s1 and s2, then after
  365. //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1
  366. //! at the position of e1. If one element is not in a container, then
  367. //! after the swap-operation the other element is not in a container.
  368. //! Iterators to e1 and e2 related to those nodes are invalidated.
  369. //!
  370. //! <b>Complexity</b>: Constant
  371. //!
  372. //! <b>Throws</b>: Nothing.
  373. void swap_nodes(unordered_set_member_hook &other);
  374. //! <b>Precondition</b>: link_mode must be \c safe_link or \c auto_unlink.
  375. //!
  376. //! <b>Returns</b>: true, if the node belongs to a container, false
  377. //! otherwise. This function can be used to test whether \c unordered_set::iterator_to
  378. //! will return a valid iterator.
  379. //!
  380. //! <b>Complexity</b>: Constant
  381. bool is_linked() const;
  382. //! <b>Effects</b>: Removes the node if it's inserted in a container.
  383. //! This function is only allowed if link_mode is \c auto_unlink.
  384. //!
  385. //! <b>Throws</b>: Nothing.
  386. void unlink();
  387. #endif
  388. };
  389. } //namespace intrusive
  390. } //namespace boost
  391. #include <boost/intrusive/detail/config_end.hpp>
  392. #endif //BOOST_INTRUSIVE_UNORDERED_SET_HOOK_HPP