rbtree_node.hpp 6.1 KB

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