avl_set_hook.hpp 11 KB

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