any_node_and_algorithms.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2014
  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_ANY_NODE_HPP
  13. #define BOOST_INTRUSIVE_ANY_NODE_HPP
  14. #ifndef BOOST_CONFIG_HPP
  15. # include <boost/config.hpp>
  16. #endif
  17. #if defined(BOOST_HAS_PRAGMA_ONCE)
  18. # pragma once
  19. #endif
  20. #include <boost/intrusive/detail/workaround.hpp>
  21. #include <boost/intrusive/pointer_rebind.hpp>
  22. #include <boost/intrusive/detail/mpl.hpp>
  23. #include <boost/intrusive/detail/algo_type.hpp>
  24. #include <cstddef>
  25. namespace boost {
  26. namespace intrusive {
  27. template<class VoidPointer>
  28. struct any_node
  29. {
  30. typedef any_node node;
  31. typedef typename pointer_rebind<VoidPointer, node>::type node_ptr;
  32. typedef typename pointer_rebind<VoidPointer, const node>::type const_node_ptr;
  33. node_ptr node_ptr_1;
  34. node_ptr node_ptr_2;
  35. node_ptr node_ptr_3;
  36. std::size_t size_t_1;
  37. };
  38. template<class VoidPointer>
  39. struct any_list_node_traits
  40. {
  41. typedef any_node<VoidPointer> node;
  42. typedef typename node::node_ptr node_ptr;
  43. typedef typename node::const_node_ptr const_node_ptr;
  44. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_next(const const_node_ptr & n)
  45. { return n->node_ptr_1; }
  46. BOOST_INTRUSIVE_FORCEINLINE static void set_next(node_ptr n, node_ptr next)
  47. { n->node_ptr_1 = next; }
  48. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_previous(const const_node_ptr & n)
  49. { return n->node_ptr_2; }
  50. BOOST_INTRUSIVE_FORCEINLINE static void set_previous(node_ptr n, node_ptr prev)
  51. { n->node_ptr_2 = prev; }
  52. };
  53. template<class VoidPointer>
  54. struct any_slist_node_traits
  55. {
  56. typedef any_node<VoidPointer> node;
  57. typedef typename node::node_ptr node_ptr;
  58. typedef typename node::const_node_ptr const_node_ptr;
  59. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_next(const const_node_ptr & n)
  60. { return n->node_ptr_1; }
  61. BOOST_INTRUSIVE_FORCEINLINE static void set_next(node_ptr n, node_ptr next)
  62. { n->node_ptr_1 = next; }
  63. };
  64. template<class VoidPointer>
  65. struct any_unordered_node_traits
  66. : public any_slist_node_traits<VoidPointer>
  67. {
  68. typedef any_slist_node_traits<VoidPointer> reduced_slist_node_traits;
  69. typedef typename reduced_slist_node_traits::node node;
  70. typedef typename reduced_slist_node_traits::node_ptr node_ptr;
  71. typedef typename reduced_slist_node_traits::const_node_ptr const_node_ptr;
  72. static const bool store_hash = true;
  73. static const bool optimize_multikey = true;
  74. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_next(const const_node_ptr & n)
  75. { return n->node_ptr_1; }
  76. BOOST_INTRUSIVE_FORCEINLINE static void set_next(node_ptr n, node_ptr next)
  77. { n->node_ptr_1 = next; }
  78. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_prev_in_group(const const_node_ptr & n)
  79. { return n->node_ptr_2; }
  80. BOOST_INTRUSIVE_FORCEINLINE static void set_prev_in_group(node_ptr n, node_ptr prev)
  81. { n->node_ptr_2 = prev; }
  82. BOOST_INTRUSIVE_FORCEINLINE static std::size_t get_hash(const const_node_ptr & n)
  83. { return n->size_t_1; }
  84. BOOST_INTRUSIVE_FORCEINLINE static void set_hash(const node_ptr & n, std::size_t h)
  85. { n->size_t_1 = h; }
  86. };
  87. template<class VoidPointer>
  88. struct any_rbtree_node_traits
  89. {
  90. typedef any_node<VoidPointer> node;
  91. typedef typename node::node_ptr node_ptr;
  92. typedef typename node::const_node_ptr const_node_ptr;
  93. typedef std::size_t color;
  94. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_parent(const const_node_ptr & n)
  95. { return n->node_ptr_1; }
  96. BOOST_INTRUSIVE_FORCEINLINE static void set_parent(node_ptr n, node_ptr p)
  97. { n->node_ptr_1 = p; }
  98. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_left(const const_node_ptr & n)
  99. { return n->node_ptr_2; }
  100. BOOST_INTRUSIVE_FORCEINLINE static void set_left(node_ptr n, node_ptr l)
  101. { n->node_ptr_2 = l; }
  102. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_right(const const_node_ptr & n)
  103. { return n->node_ptr_3; }
  104. BOOST_INTRUSIVE_FORCEINLINE static void set_right(node_ptr n, node_ptr r)
  105. { n->node_ptr_3 = r; }
  106. BOOST_INTRUSIVE_FORCEINLINE static color get_color(const const_node_ptr & n)
  107. { return n->size_t_1; }
  108. BOOST_INTRUSIVE_FORCEINLINE static void set_color(const node_ptr & n, color c)
  109. { n->size_t_1 = c; }
  110. BOOST_INTRUSIVE_FORCEINLINE static color black()
  111. { return 0u; }
  112. BOOST_INTRUSIVE_FORCEINLINE static color red()
  113. { return 1u; }
  114. };
  115. template<class VoidPointer>
  116. struct any_avltree_node_traits
  117. {
  118. typedef any_node<VoidPointer> node;
  119. typedef typename node::node_ptr node_ptr;
  120. typedef typename node::const_node_ptr const_node_ptr;
  121. typedef std::size_t balance;
  122. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_parent(const const_node_ptr & n)
  123. { return n->node_ptr_1; }
  124. BOOST_INTRUSIVE_FORCEINLINE static void set_parent(node_ptr n, node_ptr p)
  125. { n->node_ptr_1 = p; }
  126. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_left(const const_node_ptr & n)
  127. { return n->node_ptr_2; }
  128. BOOST_INTRUSIVE_FORCEINLINE static void set_left(node_ptr n, node_ptr l)
  129. { n->node_ptr_2 = l; }
  130. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_right(const const_node_ptr & n)
  131. { return n->node_ptr_3; }
  132. BOOST_INTRUSIVE_FORCEINLINE static void set_right(node_ptr n, node_ptr r)
  133. { n->node_ptr_3 = r; }
  134. BOOST_INTRUSIVE_FORCEINLINE static balance get_balance(const const_node_ptr & n)
  135. { return n->size_t_1; }
  136. BOOST_INTRUSIVE_FORCEINLINE static void set_balance(const node_ptr & n, balance b)
  137. { n->size_t_1 = b; }
  138. BOOST_INTRUSIVE_FORCEINLINE static balance negative()
  139. { return 0u; }
  140. BOOST_INTRUSIVE_FORCEINLINE static balance zero()
  141. { return 1u; }
  142. BOOST_INTRUSIVE_FORCEINLINE static balance positive()
  143. { return 2u; }
  144. };
  145. template<class VoidPointer>
  146. struct any_tree_node_traits
  147. {
  148. typedef any_node<VoidPointer> node;
  149. typedef typename node::node_ptr node_ptr;
  150. typedef typename node::const_node_ptr const_node_ptr;
  151. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_parent(const const_node_ptr & n)
  152. { return n->node_ptr_1; }
  153. BOOST_INTRUSIVE_FORCEINLINE static void set_parent(node_ptr n, node_ptr p)
  154. { n->node_ptr_1 = p; }
  155. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_left(const const_node_ptr & n)
  156. { return n->node_ptr_2; }
  157. BOOST_INTRUSIVE_FORCEINLINE static void set_left(node_ptr n, node_ptr l)
  158. { n->node_ptr_2 = l; }
  159. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_right(const const_node_ptr & n)
  160. { return n->node_ptr_3; }
  161. BOOST_INTRUSIVE_FORCEINLINE static void set_right(node_ptr n, node_ptr r)
  162. { n->node_ptr_3 = r; }
  163. };
  164. template<class VoidPointer>
  165. class any_node_traits
  166. {
  167. public:
  168. typedef any_node<VoidPointer> node;
  169. typedef typename node::node_ptr node_ptr;
  170. typedef typename node::const_node_ptr const_node_ptr;
  171. };
  172. template<class VoidPointer>
  173. class any_algorithms
  174. {
  175. template <class T>
  176. static void function_not_available_for_any_hooks(typename detail::enable_if<detail::is_same<T, bool> >::type)
  177. {}
  178. public:
  179. typedef any_node<VoidPointer> node;
  180. typedef typename node::node_ptr node_ptr;
  181. typedef typename node::const_node_ptr const_node_ptr;
  182. typedef any_node_traits<VoidPointer> node_traits;
  183. //! <b>Requires</b>: node must not be part of any tree.
  184. //!
  185. //! <b>Effects</b>: After the function unique(node) == true.
  186. //!
  187. //! <b>Complexity</b>: Constant.
  188. //!
  189. //! <b>Throws</b>: Nothing.
  190. //!
  191. //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
  192. BOOST_INTRUSIVE_FORCEINLINE static void init(const node_ptr & node)
  193. { node->node_ptr_1 = node_ptr(); };
  194. //! <b>Effects</b>: Returns true if node is in the same state as if called init(node)
  195. //!
  196. //! <b>Complexity</b>: Constant.
  197. //!
  198. //! <b>Throws</b>: Nothing.
  199. BOOST_INTRUSIVE_FORCEINLINE static bool inited(const const_node_ptr & node)
  200. { return !node->node_ptr_1; };
  201. BOOST_INTRUSIVE_FORCEINLINE static bool unique(const const_node_ptr & node)
  202. { return !node->node_ptr_1; }
  203. static void unlink(const node_ptr &)
  204. {
  205. //Auto-unlink hooks and unlink() are not available for any hooks
  206. any_algorithms<VoidPointer>::template function_not_available_for_any_hooks<node_ptr>();
  207. }
  208. static void swap_nodes(const node_ptr &, const node_ptr &)
  209. {
  210. //Any nodes have no swap_nodes capability because they don't know
  211. //what algorithm they must use to unlink the node from the container
  212. any_algorithms<VoidPointer>::template function_not_available_for_any_hooks<node_ptr>();
  213. }
  214. };
  215. ///@cond
  216. template<class NodeTraits>
  217. struct get_algo<AnyAlgorithm, NodeTraits>
  218. {
  219. typedef typename pointer_rebind<typename NodeTraits::node_ptr, void>::type void_pointer;
  220. typedef any_algorithms<void_pointer> type;
  221. };
  222. ///@endcond
  223. } //namespace intrusive
  224. } //namespace boost
  225. #endif //BOOST_INTRUSIVE_ANY_NODE_HPP