avl_set_hook.hpp 11 KB

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