avl_set_hook.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013
  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. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTRUSIVE_AVL_SET_HOOK_HPP
  13. #define BOOST_INTRUSIVE_AVL_SET_HOOK_HPP
  14. #include <boost/intrusive/detail/config_begin.hpp>
  15. #include <boost/intrusive/intrusive_fwd.hpp>
  16. #include <boost/intrusive/detail/utilities.hpp>
  17. #include <boost/intrusive/detail/avltree_node.hpp>
  18. #include <boost/intrusive/avltree_algorithms.hpp>
  19. #include <boost/intrusive/options.hpp>
  20. #include <boost/intrusive/detail/generic_hook.hpp>
  21. namespace boost {
  22. namespace intrusive {
  23. /// @cond
  24. template<class VoidPointer, bool OptimizeSize = false>
  25. struct get_avl_set_node_algo
  26. {
  27. typedef avltree_algorithms<avltree_node_traits<VoidPointer, OptimizeSize> > type;
  28. };
  29. /// @endcond
  30. //! Helper metafunction to define a \c avl_set_base_hook that yields to the same
  31. //! type when the same options (either explicitly or implicitly) are used.
  32. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  33. template<class ...Options>
  34. #else
  35. template<class O1 = void, class O2 = void, class O3 = void, class O4 = void>
  36. #endif
  37. struct make_avl_set_base_hook
  38. {
  39. /// @cond
  40. typedef typename pack_options
  41. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  42. <hook_defaults, O1, O2, O3, O4>
  43. #else
  44. <hook_defaults, Options...>
  45. #endif
  46. ::type packed_options;
  47. typedef generic_hook
  48. < get_avl_set_node_algo<typename packed_options::void_pointer
  49. ,packed_options::optimize_size>
  50. , typename packed_options::tag
  51. , packed_options::link_mode
  52. , AvlTreeBaseHookId
  53. > implementation_defined;
  54. /// @endcond
  55. typedef implementation_defined type;
  56. };
  57. //! Derive a class from avl_set_base_hook in order to store objects in
  58. //! in an avl_set/avl_multiset. avl_set_base_hook holds the data necessary to maintain
  59. //! the avl_set/avl_multiset and provides an appropriate value_traits class for avl_set/avl_multiset.
  60. //!
  61. //! The hook admits the following options: \c tag<>, \c void_pointer<>,
  62. //! \c link_mode<> and \c optimize_size<>.
  63. //!
  64. //! \c tag<> defines a tag to identify the node.
  65. //! The same tag value can be used in different classes, but if a class is
  66. //! derived from more than one \c list_base_hook, then each \c list_base_hook needs its
  67. //! unique tag.
  68. //!
  69. //! \c void_pointer<> is the pointer type that will be used internally in the hook
  70. //! and the the container configured to use this hook.
  71. //!
  72. //! \c link_mode<> will specify the linking mode of the hook (\c normal_link,
  73. //! \c auto_unlink or \c safe_link).
  74. //!
  75. //! \c optimize_size<> will tell the hook to optimize the hook for size instead
  76. //! of speed.
  77. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  78. template<class ...Options>
  79. #else
  80. template<class O1, class O2, class O3, class O4>
  81. #endif
  82. class avl_set_base_hook
  83. : public make_avl_set_base_hook
  84. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  85. <O1, O2, O3, O4>
  86. #else
  87. <Options...>
  88. #endif
  89. ::type
  90. {
  91. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  92. public:
  93. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  94. //! initializes the node to an unlinked state.
  95. //!
  96. //! <b>Throws</b>: Nothing.
  97. avl_set_base_hook();
  98. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  99. //! initializes the node to an unlinked state. The argument is ignored.
  100. //!
  101. //! <b>Throws</b>: Nothing.
  102. //!
  103. //! <b>Rationale</b>: Providing a copy-constructor
  104. //! makes classes using the hook STL-compliant without forcing the
  105. //! user to do some additional work. \c swap can be used to emulate
  106. //! move-semantics.
  107. avl_set_base_hook(const avl_set_base_hook& );
  108. //! <b>Effects</b>: Empty function. The argument is ignored.
  109. //!
  110. //! <b>Throws</b>: Nothing.
  111. //!
  112. //! <b>Rationale</b>: Providing an assignment operator
  113. //! makes classes using the hook STL-compliant without forcing the
  114. //! user to do some additional work. \c swap can be used to emulate
  115. //! move-semantics.
  116. avl_set_base_hook& operator=(const avl_set_base_hook& );
  117. //! <b>Effects</b>: If link_mode is \c normal_link, the destructor does
  118. //! nothing (ie. no code is generated). If link_mode is \c safe_link and the
  119. //! object is stored in a set an assertion is raised. If link_mode is
  120. //! \c auto_unlink and \c is_linked() is true, the node is unlinked.
  121. //!
  122. //! <b>Throws</b>: Nothing.
  123. ~avl_set_base_hook();
  124. //! <b>Effects</b>: Swapping two nodes swaps the position of the elements
  125. //! related to those nodes in one or two containers. That is, if the node
  126. //! this is part of the element e1, the node x is part of the element e2
  127. //! and both elements are included in the containers s1 and s2, then after
  128. //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1
  129. //! at the position of e1. If one element is not in a container, then
  130. //! after the swap-operation the other element is not in a container.
  131. //! Iterators to e1 and e2 related to those nodes are invalidated.
  132. //!
  133. //! <b>Complexity</b>: Constant
  134. //!
  135. //! <b>Throws</b>: Nothing.
  136. void swap_nodes(avl_set_base_hook &other);
  137. //! <b>Precondition</b>: link_mode must be \c safe_link or \c auto_unlink.
  138. //!
  139. //! <b>Returns</b>: true, if the node belongs to a container, false
  140. //! otherwise. This function can be used to test whether \c set::iterator_to
  141. //! will return a valid iterator.
  142. //!
  143. //! <b>Complexity</b>: Constant
  144. bool is_linked() const;
  145. //! <b>Effects</b>: Removes the node if it's inserted in a container.
  146. //! This function is only allowed if link_mode is \c auto_unlink.
  147. //!
  148. //! <b>Throws</b>: Nothing.
  149. void unlink();
  150. #endif
  151. };
  152. //! Helper metafunction to define a \c avl_set_member_hook that yields to the same
  153. //! type when the same options (either explicitly or implicitly) are used.
  154. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  155. template<class ...Options>
  156. #else
  157. template<class O1 = void, class O2 = void, class O3 = void, class O4 = void>
  158. #endif
  159. struct make_avl_set_member_hook
  160. {
  161. /// @cond
  162. typedef typename pack_options
  163. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  164. <hook_defaults, O1, O2, O3, O4>
  165. #else
  166. <hook_defaults, Options...>
  167. #endif
  168. ::type packed_options;
  169. typedef generic_hook
  170. < get_avl_set_node_algo<typename packed_options::void_pointer
  171. ,packed_options::optimize_size>
  172. , member_tag
  173. , packed_options::link_mode
  174. , NoBaseHookId
  175. > implementation_defined;
  176. /// @endcond
  177. typedef implementation_defined type;
  178. };
  179. //! Put a public data member avl_set_member_hook in order to store objects of this class in
  180. //! an avl_set/avl_multiset. avl_set_member_hook holds the data necessary for maintaining the
  181. //! avl_set/avl_multiset and provides an appropriate value_traits class for avl_set/avl_multiset.
  182. //!
  183. //! The hook admits the following options: \c void_pointer<>,
  184. //! \c link_mode<> and \c optimize_size<>.
  185. //!
  186. //! \c void_pointer<> is the pointer type that will be used internally in the hook
  187. //! and the the container configured to use this hook.
  188. //!
  189. //! \c link_mode<> will specify the linking mode of the hook (\c normal_link,
  190. //! \c auto_unlink or \c safe_link).
  191. //!
  192. //! \c optimize_size<> will tell the hook to optimize the hook for size instead
  193. //! of speed.
  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 avl_set_member_hook
  200. : public make_avl_set_member_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. avl_set_member_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. avl_set_member_hook(const avl_set_member_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. avl_set_member_hook& operator=(const avl_set_member_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 a 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. ~avl_set_member_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(avl_set_member_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 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. } //namespace intrusive
  270. } //namespace boost
  271. #include <boost/intrusive/detail/config_end.hpp>
  272. #endif //BOOST_INTRUSIVE_AVL_SET_HOOK_HPP