avltree_node.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_AVLTREE_NODE_HPP
  13. #define BOOST_INTRUSIVE_AVLTREE_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/config_begin.hpp>
  21. #include <boost/intrusive/detail/workaround.hpp>
  22. #include <boost/intrusive/pointer_rebind.hpp>
  23. #include <boost/intrusive/avltree_algorithms.hpp>
  24. #include <boost/intrusive/pointer_plus_bits.hpp>
  25. #include <boost/intrusive/detail/mpl.hpp>
  26. namespace boost {
  27. namespace intrusive {
  28. /////////////////////////////////////////////////////////////////////////////
  29. // //
  30. // Generic node_traits for any pointer type //
  31. // //
  32. /////////////////////////////////////////////////////////////////////////////
  33. //This is the compact representation: 3 pointers
  34. template<class VoidPointer>
  35. struct compact_avltree_node
  36. {
  37. typedef typename pointer_rebind<VoidPointer, compact_avltree_node<VoidPointer> >::type node_ptr;
  38. typedef typename pointer_rebind<VoidPointer, const compact_avltree_node<VoidPointer> >::type const_node_ptr;
  39. enum balance { negative_t, zero_t, positive_t };
  40. node_ptr parent_, left_, right_;
  41. };
  42. //This is the normal representation: 3 pointers + enum
  43. template<class VoidPointer>
  44. struct avltree_node
  45. {
  46. typedef typename pointer_rebind<VoidPointer, avltree_node<VoidPointer> >::type node_ptr;
  47. typedef typename pointer_rebind<VoidPointer, const avltree_node<VoidPointer> >::type const_node_ptr;
  48. enum balance { negative_t, zero_t, positive_t };
  49. node_ptr parent_, left_, right_;
  50. balance balance_;
  51. };
  52. //This is the default node traits implementation
  53. //using a node with 3 generic pointers plus an enum
  54. template<class VoidPointer>
  55. struct default_avltree_node_traits_impl
  56. {
  57. typedef avltree_node<VoidPointer> node;
  58. typedef typename node::node_ptr node_ptr;
  59. typedef typename node::const_node_ptr const_node_ptr;
  60. typedef typename node::balance balance;
  61. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_parent(const const_node_ptr & n)
  62. { return n->parent_; }
  63. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_parent(const node_ptr & n)
  64. { return n->parent_; }
  65. BOOST_INTRUSIVE_FORCEINLINE static void set_parent(node_ptr n, node_ptr p)
  66. { n->parent_ = p; }
  67. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_left(const const_node_ptr & n)
  68. { return n->left_; }
  69. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_left(const node_ptr & n)
  70. { return n->left_; }
  71. BOOST_INTRUSIVE_FORCEINLINE static void set_left(node_ptr n, node_ptr l)
  72. { n->left_ = l; }
  73. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_right(const const_node_ptr & n)
  74. { return n->right_; }
  75. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_right(const node_ptr & n)
  76. { return n->right_; }
  77. BOOST_INTRUSIVE_FORCEINLINE static void set_right(node_ptr n, node_ptr r)
  78. { n->right_ = r; }
  79. BOOST_INTRUSIVE_FORCEINLINE static balance get_balance(const const_node_ptr & n)
  80. { return n->balance_; }
  81. BOOST_INTRUSIVE_FORCEINLINE static balance get_balance(const node_ptr & n)
  82. { return n->balance_; }
  83. BOOST_INTRUSIVE_FORCEINLINE static void set_balance(const node_ptr & n, balance b)
  84. { n->balance_ = b; }
  85. BOOST_INTRUSIVE_FORCEINLINE static balance negative()
  86. { return node::negative_t; }
  87. BOOST_INTRUSIVE_FORCEINLINE static balance zero()
  88. { return node::zero_t; }
  89. BOOST_INTRUSIVE_FORCEINLINE static balance positive()
  90. { return node::positive_t; }
  91. };
  92. //This is the compact node traits implementation
  93. //using a node with 3 generic pointers
  94. template<class VoidPointer>
  95. struct compact_avltree_node_traits_impl
  96. {
  97. typedef compact_avltree_node<VoidPointer> node;
  98. typedef typename node::node_ptr node_ptr;
  99. typedef typename node::const_node_ptr const_node_ptr;
  100. typedef typename node::balance balance;
  101. typedef pointer_plus_bits<node_ptr, 2> ptr_bit;
  102. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_parent(const const_node_ptr & n)
  103. { return ptr_bit::get_pointer(n->parent_); }
  104. BOOST_INTRUSIVE_FORCEINLINE static void set_parent(node_ptr n, node_ptr p)
  105. { ptr_bit::set_pointer(n->parent_, p); }
  106. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_left(const const_node_ptr & n)
  107. { return n->left_; }
  108. BOOST_INTRUSIVE_FORCEINLINE static void set_left(node_ptr n, node_ptr l)
  109. { n->left_ = l; }
  110. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_right(const const_node_ptr & n)
  111. { return n->right_; }
  112. BOOST_INTRUSIVE_FORCEINLINE static void set_right(node_ptr n, node_ptr r)
  113. { n->right_ = r; }
  114. BOOST_INTRUSIVE_FORCEINLINE static balance get_balance(const const_node_ptr & n)
  115. { return (balance)ptr_bit::get_bits(n->parent_); }
  116. BOOST_INTRUSIVE_FORCEINLINE static void set_balance(const node_ptr & n, balance b)
  117. { ptr_bit::set_bits(n->parent_, (std::size_t)b); }
  118. BOOST_INTRUSIVE_FORCEINLINE static balance negative()
  119. { return node::negative_t; }
  120. BOOST_INTRUSIVE_FORCEINLINE static balance zero()
  121. { return node::zero_t; }
  122. BOOST_INTRUSIVE_FORCEINLINE static balance positive()
  123. { return node::positive_t; }
  124. };
  125. //Dispatches the implementation based on the boolean
  126. template<class VoidPointer, bool Compact>
  127. struct avltree_node_traits_dispatch
  128. : public default_avltree_node_traits_impl<VoidPointer>
  129. {};
  130. template<class VoidPointer>
  131. struct avltree_node_traits_dispatch<VoidPointer, true>
  132. : public compact_avltree_node_traits_impl<VoidPointer>
  133. {};
  134. //Inherit from rbtree_node_traits_dispatch depending on the embedding capabilities
  135. template<class VoidPointer, bool OptimizeSize = false>
  136. struct avltree_node_traits
  137. : public avltree_node_traits_dispatch
  138. < VoidPointer
  139. , OptimizeSize &&
  140. max_pointer_plus_bits
  141. < VoidPointer
  142. , detail::alignment_of<compact_avltree_node<VoidPointer> >::value
  143. >::value >= 2u
  144. >
  145. {};
  146. } //namespace intrusive
  147. } //namespace boost
  148. #include <boost/intrusive/detail/config_end.hpp>
  149. #endif //BOOST_INTRUSIVE_AVLTREE_NODE_HPP