bs_set_hook.hpp 10 KB

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