set_hook.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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_SET_HOOK_HPP
  14. #define BOOST_INTRUSIVE_SET_HOOK_HPP
  15. #include <boost/intrusive/detail/config_begin.hpp>
  16. #include <boost/intrusive/intrusive_fwd.hpp>
  17. #include <boost/intrusive/detail/utilities.hpp>
  18. #include <boost/intrusive/detail/rbtree_node.hpp>
  19. #include <boost/intrusive/rbtree_algorithms.hpp>
  20. #include <boost/intrusive/options.hpp>
  21. #include <boost/intrusive/detail/generic_hook.hpp>
  22. namespace boost {
  23. namespace intrusive {
  24. /// @cond
  25. template<class VoidPointer, bool OptimizeSize = false>
  26. struct get_set_node_algo
  27. {
  28. typedef rbtree_algorithms<rbtree_node_traits<VoidPointer, OptimizeSize> > type;
  29. };
  30. /// @endcond
  31. //! Helper metafunction to define a \c set_base_hook that yields to the same
  32. //! type when the same options (either explicitly or implicitly) are used.
  33. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  34. template<class ...Options>
  35. #else
  36. template<class O1 = void, class O2 = void, class O3 = void, class O4 = void>
  37. #endif
  38. struct make_set_base_hook
  39. {
  40. /// @cond
  41. typedef typename pack_options
  42. < hook_defaults,
  43. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  44. O1, O2, O3, O4
  45. #else
  46. Options...
  47. #endif
  48. >::type packed_options;
  49. typedef generic_hook
  50. < get_set_node_algo<typename packed_options::void_pointer
  51. ,packed_options::optimize_size>
  52. , typename packed_options::tag
  53. , packed_options::link_mode
  54. , RbTreeBaseHookId
  55. > implementation_defined;
  56. /// @endcond
  57. typedef implementation_defined type;
  58. };
  59. //! Derive a class from set_base_hook in order to store objects in
  60. //! in a set/multiset. set_base_hook holds the data necessary to maintain
  61. //! the set/multiset and provides an appropriate value_traits class for set/multiset.
  62. //!
  63. //! The hook admits the following options: \c tag<>, \c void_pointer<>,
  64. //! \c link_mode<> and \c optimize_size<>.
  65. //!
  66. //! \c tag<> defines a tag to identify the node.
  67. //! The same tag value can be used in different classes, but if a class is
  68. //! derived from more than one \c list_base_hook, then each \c list_base_hook needs its
  69. //! unique tag.
  70. //!
  71. //! \c void_pointer<> is the pointer type that will be used internally in the hook
  72. //! and the the container configured to use this hook.
  73. //!
  74. //! \c link_mode<> will specify the linking mode of the hook (\c normal_link,
  75. //! \c auto_unlink or \c safe_link).
  76. //!
  77. //! \c optimize_size<> will tell the hook to optimize the hook for size instead
  78. //! of speed.
  79. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  80. template<class ...Options>
  81. #else
  82. template<class O1, class O2, class O3, class O4>
  83. #endif
  84. class set_base_hook
  85. : public make_set_base_hook<
  86. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  87. O1, O2, O3, O4
  88. #else
  89. Options...
  90. #endif
  91. >::type
  92. {
  93. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  94. public:
  95. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  96. //! initializes the node to an unlinked state.
  97. //!
  98. //! <b>Throws</b>: Nothing.
  99. set_base_hook();
  100. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  101. //! initializes the node to an unlinked state. The argument is ignored.
  102. //!
  103. //! <b>Throws</b>: Nothing.
  104. //!
  105. //! <b>Rationale</b>: Providing a copy-constructor
  106. //! makes classes using the hook STL-compliant without forcing the
  107. //! user to do some additional work. \c swap can be used to emulate
  108. //! move-semantics.
  109. set_base_hook(const set_base_hook& );
  110. //! <b>Effects</b>: Empty function. The argument is ignored.
  111. //!
  112. //! <b>Throws</b>: Nothing.
  113. //!
  114. //! <b>Rationale</b>: Providing an assignment operator
  115. //! makes classes using the hook STL-compliant without forcing the
  116. //! user to do some additional work. \c swap can be used to emulate
  117. //! move-semantics.
  118. set_base_hook& operator=(const set_base_hook& );
  119. //! <b>Effects</b>: If link_mode is \c normal_link, the destructor does
  120. //! nothing (ie. no code is generated). If link_mode is \c safe_link and the
  121. //! object is stored in a set an assertion is raised. If link_mode is
  122. //! \c auto_unlink and \c is_linked() is true, the node is unlinked.
  123. //!
  124. //! <b>Throws</b>: Nothing.
  125. ~set_base_hook();
  126. //! <b>Effects</b>: Swapping two nodes swaps the position of the elements
  127. //! related to those nodes in one or two containers. That is, if the node
  128. //! this is part of the element e1, the node x is part of the element e2
  129. //! and both elements are included in the containers s1 and s2, then after
  130. //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1
  131. //! at the position of e1. If one element is not in a container, then
  132. //! after the swap-operation the other element is not in a container.
  133. //! Iterators to e1 and e2 related to those nodes are invalidated.
  134. //!
  135. //! <b>Complexity</b>: Constant
  136. //!
  137. //! <b>Throws</b>: Nothing.
  138. void swap_nodes(set_base_hook &other);
  139. //! <b>Precondition</b>: link_mode must be \c safe_link or \c auto_unlink.
  140. //!
  141. //! <b>Returns</b>: true, if the node belongs to a container, false
  142. //! otherwise. This function can be used to test whether \c set::iterator_to
  143. //! will return a valid iterator.
  144. //!
  145. //! <b>Complexity</b>: Constant
  146. bool is_linked() const;
  147. //! <b>Effects</b>: Removes the node if it's inserted in a container.
  148. //! This function is only allowed if link_mode is \c auto_unlink.
  149. //!
  150. //! <b>Throws</b>: Nothing.
  151. void unlink();
  152. #endif
  153. };
  154. //! Helper metafunction to define a \c set_member_hook that yields to the same
  155. //! type when the same options (either explicitly or implicitly) are used.
  156. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  157. template<class ...Options>
  158. #else
  159. template<class O1 = void, class O2 = void, class O3 = void, class O4 = void>
  160. #endif
  161. struct make_set_member_hook
  162. {
  163. /// @cond
  164. typedef typename pack_options
  165. < hook_defaults,
  166. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  167. O1, O2, O3, O4
  168. #else
  169. Options...
  170. #endif
  171. >::type packed_options;
  172. typedef generic_hook
  173. < get_set_node_algo<typename packed_options::void_pointer
  174. ,packed_options::optimize_size>
  175. , member_tag
  176. , packed_options::link_mode
  177. , NoBaseHookId
  178. > implementation_defined;
  179. /// @endcond
  180. typedef implementation_defined type;
  181. };
  182. //! Put a public data member set_member_hook in order to store objects of this class in
  183. //! a set/multiset. set_member_hook holds the data necessary for maintaining the
  184. //! set/multiset and provides an appropriate value_traits class for set/multiset.
  185. //!
  186. //! The hook admits the following options: \c void_pointer<>,
  187. //! \c link_mode<> and \c optimize_size<>.
  188. //!
  189. //! \c void_pointer<> is the pointer type that will be used internally in the hook
  190. //! and the the container configured to use this hook.
  191. //!
  192. //! \c link_mode<> will specify the linking mode of the hook (\c normal_link,
  193. //! \c auto_unlink or \c safe_link).
  194. //!
  195. //! \c optimize_size<> will tell the hook to optimize the hook for size instead
  196. //! of speed.
  197. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  198. template<class ...Options>
  199. #else
  200. template<class O1, class O2, class O3, class O4>
  201. #endif
  202. class set_member_hook
  203. : public make_set_member_hook<
  204. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  205. O1, O2, O3, O4
  206. #else
  207. Options...
  208. #endif
  209. >::type
  210. {
  211. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  212. public:
  213. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  214. //! initializes the node to an unlinked state.
  215. //!
  216. //! <b>Throws</b>: Nothing.
  217. set_member_hook();
  218. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  219. //! initializes the node to an unlinked state. The argument is ignored.
  220. //!
  221. //! <b>Throws</b>: Nothing.
  222. //!
  223. //! <b>Rationale</b>: Providing a copy-constructor
  224. //! makes classes using the hook STL-compliant without forcing the
  225. //! user to do some additional work. \c swap can be used to emulate
  226. //! move-semantics.
  227. set_member_hook(const set_member_hook& );
  228. //! <b>Effects</b>: Empty function. The argument is ignored.
  229. //!
  230. //! <b>Throws</b>: Nothing.
  231. //!
  232. //! <b>Rationale</b>: Providing an assignment operator
  233. //! makes classes using the hook STL-compliant without forcing the
  234. //! user to do some additional work. \c swap can be used to emulate
  235. //! move-semantics.
  236. set_member_hook& operator=(const set_member_hook& );
  237. //! <b>Effects</b>: If link_mode is \c normal_link, the destructor does
  238. //! nothing (ie. no code is generated). If link_mode is \c safe_link and the
  239. //! object is stored in a set an assertion is raised. If link_mode is
  240. //! \c auto_unlink and \c is_linked() is true, the node is unlinked.
  241. //!
  242. //! <b>Throws</b>: Nothing.
  243. ~set_member_hook();
  244. //! <b>Effects</b>: Swapping two nodes swaps the position of the elements
  245. //! related to those nodes in one or two containers. That is, if the node
  246. //! this is part of the element e1, the node x is part of the element e2
  247. //! and both elements are included in the containers s1 and s2, then after
  248. //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1
  249. //! at the position of e1. If one element is not in a container, then
  250. //! after the swap-operation the other element is not in a container.
  251. //! Iterators to e1 and e2 related to those nodes are invalidated.
  252. //!
  253. //! <b>Complexity</b>: Constant
  254. //!
  255. //! <b>Throws</b>: Nothing.
  256. void swap_nodes(set_member_hook &other);
  257. //! <b>Precondition</b>: link_mode must be \c safe_link or \c auto_unlink.
  258. //!
  259. //! <b>Returns</b>: true, if the node belongs to a container, false
  260. //! otherwise. This function can be used to test whether \c set::iterator_to
  261. //! will return a valid iterator.
  262. //!
  263. //! <b>Complexity</b>: Constant
  264. bool is_linked() const;
  265. //! <b>Effects</b>: Removes the node if it's inserted in a container.
  266. //! This function is only allowed if link_mode is \c auto_unlink.
  267. //!
  268. //! <b>Throws</b>: Nothing.
  269. void unlink();
  270. #endif
  271. };
  272. } //namespace intrusive
  273. } //namespace boost
  274. #include <boost/intrusive/detail/config_end.hpp>
  275. #endif //BOOST_INTRUSIVE_SET_HOOK_HPP