rbtree_node.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Olaf Krzikalla 2004-2006.
  4. // (C) Copyright Ion Gaztanaga 2006-2013.
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/intrusive for documentation.
  11. //
  12. /////////////////////////////////////////////////////////////////////////////
  13. #ifndef BOOST_INTRUSIVE_RBTREE_NODE_HPP
  14. #define BOOST_INTRUSIVE_RBTREE_NODE_HPP
  15. #ifndef BOOST_CONFIG_HPP
  16. # include <boost/config.hpp>
  17. #endif
  18. #if defined(BOOST_HAS_PRAGMA_ONCE)
  19. # pragma once
  20. #endif
  21. #include <boost/intrusive/detail/config_begin.hpp>
  22. #include <boost/intrusive/detail/workaround.hpp>
  23. #include <boost/intrusive/pointer_rebind.hpp>
  24. #include <boost/intrusive/rbtree_algorithms.hpp>
  25. #include <boost/intrusive/pointer_plus_bits.hpp>
  26. #include <boost/intrusive/detail/mpl.hpp>
  27. #include <boost/intrusive/detail/tree_node.hpp>
  28. namespace boost {
  29. namespace intrusive {
  30. /////////////////////////////////////////////////////////////////////////////
  31. // //
  32. // Generic node_traits for any pointer type //
  33. // //
  34. /////////////////////////////////////////////////////////////////////////////
  35. //This is the compact representation: 3 pointers
  36. template<class VoidPointer>
  37. struct compact_rbtree_node
  38. {
  39. typedef compact_rbtree_node<VoidPointer> node;
  40. typedef typename pointer_rebind<VoidPointer, node >::type node_ptr;
  41. typedef typename pointer_rebind<VoidPointer, const node >::type const_node_ptr;
  42. enum color { red_t, black_t };
  43. node_ptr parent_, left_, right_;
  44. };
  45. //This is the normal representation: 3 pointers + enum
  46. template<class VoidPointer>
  47. struct rbtree_node
  48. {
  49. typedef rbtree_node<VoidPointer> node;
  50. typedef typename pointer_rebind<VoidPointer, node >::type node_ptr;
  51. typedef typename pointer_rebind<VoidPointer, const node >::type const_node_ptr;
  52. enum color { red_t, black_t };
  53. node_ptr parent_, left_, right_;
  54. color color_;
  55. };
  56. //This is the default node traits implementation
  57. //using a node with 3 generic pointers plus an enum
  58. template<class VoidPointer>
  59. struct default_rbtree_node_traits_impl
  60. {
  61. typedef rbtree_node<VoidPointer> node;
  62. typedef typename node::node_ptr node_ptr;
  63. typedef typename node::const_node_ptr const_node_ptr;
  64. typedef typename node::color color;
  65. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_parent(const const_node_ptr & n)
  66. { return n->parent_; }
  67. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_parent(const node_ptr & n)
  68. { return n->parent_; }
  69. BOOST_INTRUSIVE_FORCEINLINE static void set_parent(node_ptr n, node_ptr p)
  70. { n->parent_ = p; }
  71. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_left(const const_node_ptr & n)
  72. { return n->left_; }
  73. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_left(const node_ptr & n)
  74. { return n->left_; }
  75. BOOST_INTRUSIVE_FORCEINLINE static void set_left(node_ptr n, node_ptr l)
  76. { n->left_ = l; }
  77. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_right(const const_node_ptr & n)
  78. { return n->right_; }
  79. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_right(const node_ptr & n)
  80. { return n->right_; }
  81. BOOST_INTRUSIVE_FORCEINLINE static void set_right(node_ptr n, node_ptr r)
  82. { n->right_ = r; }
  83. BOOST_INTRUSIVE_FORCEINLINE static color get_color(const const_node_ptr & n)
  84. { return n->color_; }
  85. BOOST_INTRUSIVE_FORCEINLINE static color get_color(const node_ptr & n)
  86. { return n->color_; }
  87. BOOST_INTRUSIVE_FORCEINLINE static void set_color(const node_ptr & n, color c)
  88. { n->color_ = c; }
  89. BOOST_INTRUSIVE_FORCEINLINE static color black()
  90. { return node::black_t; }
  91. BOOST_INTRUSIVE_FORCEINLINE static color red()
  92. { return node::red_t; }
  93. };
  94. //This is the compact node traits implementation
  95. //using a node with 3 generic pointers
  96. template<class VoidPointer>
  97. struct compact_rbtree_node_traits_impl
  98. {
  99. typedef compact_rbtree_node<VoidPointer> node;
  100. typedef typename node::node_ptr node_ptr;
  101. typedef typename node::const_node_ptr const_node_ptr;
  102. typedef pointer_plus_bits<node_ptr, 1> ptr_bit;
  103. typedef typename node::color color;
  104. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_parent(const const_node_ptr & n)
  105. { return ptr_bit::get_pointer(n->parent_); }
  106. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_parent(const node_ptr & n)
  107. { return ptr_bit::get_pointer(n->parent_); }
  108. BOOST_INTRUSIVE_FORCEINLINE static void set_parent(node_ptr n, node_ptr p)
  109. { ptr_bit::set_pointer(n->parent_, p); }
  110. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_left(const const_node_ptr & n)
  111. { return n->left_; }
  112. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_left(const node_ptr & n)
  113. { return n->left_; }
  114. BOOST_INTRUSIVE_FORCEINLINE static void set_left(node_ptr n, node_ptr l)
  115. { n->left_ = l; }
  116. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_right(const const_node_ptr & n)
  117. { return n->right_; }
  118. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_right(const node_ptr & n)
  119. { return n->right_; }
  120. BOOST_INTRUSIVE_FORCEINLINE static void set_right(node_ptr n, node_ptr r)
  121. { n->right_ = r; }
  122. BOOST_INTRUSIVE_FORCEINLINE static color get_color(const const_node_ptr & n)
  123. { return (color)ptr_bit::get_bits(n->parent_); }
  124. BOOST_INTRUSIVE_FORCEINLINE static color get_color(const node_ptr & n)
  125. { return (color)ptr_bit::get_bits(n->parent_); }
  126. BOOST_INTRUSIVE_FORCEINLINE static void set_color(const node_ptr & n, color c)
  127. { ptr_bit::set_bits(n->parent_, c != 0); }
  128. BOOST_INTRUSIVE_FORCEINLINE static color black()
  129. { return node::black_t; }
  130. BOOST_INTRUSIVE_FORCEINLINE static color red()
  131. { return node::red_t; }
  132. };
  133. //Dispatches the implementation based on the boolean
  134. template<class VoidPointer, bool Compact>
  135. struct rbtree_node_traits_dispatch
  136. : public default_rbtree_node_traits_impl<VoidPointer>
  137. {};
  138. template<class VoidPointer>
  139. struct rbtree_node_traits_dispatch<VoidPointer, true>
  140. : public compact_rbtree_node_traits_impl<VoidPointer>
  141. {};
  142. //Inherit from rbtree_node_traits_dispatch depending on the embedding capabilities
  143. template<class VoidPointer, bool OptimizeSize = false>
  144. struct rbtree_node_traits
  145. : public rbtree_node_traits_dispatch
  146. < VoidPointer
  147. , OptimizeSize &&
  148. (max_pointer_plus_bits
  149. < VoidPointer
  150. , detail::alignment_of<compact_rbtree_node<VoidPointer> >::value
  151. >::value >= 1)
  152. >
  153. {};
  154. } //namespace intrusive
  155. } //namespace boost
  156. #include <boost/intrusive/detail/config_end.hpp>
  157. #endif //BOOST_INTRUSIVE_RBTREE_NODE_HPP