set_hook.hpp 11 KB

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