bstree_algorithms_base.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2014-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_BSTREE_ALGORITHMS_BASE_HPP
  13. #define BOOST_INTRUSIVE_BSTREE_ALGORITHMS_BASE_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/uncast.hpp>
  21. namespace boost {
  22. namespace intrusive {
  23. template<class NodeTraits>
  24. class bstree_algorithms_base
  25. {
  26. public:
  27. typedef typename NodeTraits::node node;
  28. typedef NodeTraits node_traits;
  29. typedef typename NodeTraits::node_ptr node_ptr;
  30. typedef typename NodeTraits::const_node_ptr const_node_ptr;
  31. //! <b>Requires</b>: 'n' is a node from the tree except the header.
  32. //!
  33. //! <b>Effects</b>: Returns the next node of the tree.
  34. //!
  35. //! <b>Complexity</b>: Average constant time.
  36. //!
  37. //! <b>Throws</b>: Nothing.
  38. static node_ptr next_node(node_ptr n) BOOST_NOEXCEPT
  39. {
  40. node_ptr const n_right(NodeTraits::get_right(n));
  41. if(n_right){
  42. return minimum(n_right);
  43. }
  44. else {
  45. node_ptr p(NodeTraits::get_parent(n));
  46. while(n == NodeTraits::get_right(p)){
  47. n = p;
  48. p = NodeTraits::get_parent(p);
  49. }
  50. return NodeTraits::get_right(n) != p ? p : n;
  51. }
  52. }
  53. //! <b>Requires</b>: 'n' is a node from the tree except the leftmost node.
  54. //!
  55. //! <b>Effects</b>: Returns the previous node of the tree.
  56. //!
  57. //! <b>Complexity</b>: Average constant time.
  58. //!
  59. //! <b>Throws</b>: Nothing.
  60. static node_ptr prev_node(node_ptr n) BOOST_NOEXCEPT
  61. {
  62. if(is_header(n)){
  63. return NodeTraits::get_right(n);
  64. }
  65. else if(NodeTraits::get_left(n)){
  66. return maximum(NodeTraits::get_left(n));
  67. }
  68. else {
  69. node_ptr p(n);
  70. node_ptr x = NodeTraits::get_parent(p);
  71. while(p == NodeTraits::get_left(x)){
  72. p = x;
  73. x = NodeTraits::get_parent(x);
  74. }
  75. return x;
  76. }
  77. }
  78. //! <b>Requires</b>: 'n' is a node of a tree but not the header.
  79. //!
  80. //! <b>Effects</b>: Returns the minimum node of the subtree starting at p.
  81. //!
  82. //! <b>Complexity</b>: Logarithmic to the size of the subtree.
  83. //!
  84. //! <b>Throws</b>: Nothing.
  85. static node_ptr minimum(node_ptr n)
  86. {
  87. for(node_ptr p_left = NodeTraits::get_left(n)
  88. ;p_left
  89. ;p_left = NodeTraits::get_left(n)){
  90. n = p_left;
  91. }
  92. return n;
  93. }
  94. //! <b>Requires</b>: 'n' is a node of a tree but not the header.
  95. //!
  96. //! <b>Effects</b>: Returns the maximum node of the subtree starting at p.
  97. //!
  98. //! <b>Complexity</b>: Logarithmic to the size of the subtree.
  99. //!
  100. //! <b>Throws</b>: Nothing.
  101. static node_ptr maximum(node_ptr n)
  102. {
  103. for(node_ptr p_right = NodeTraits::get_right(n)
  104. ;p_right
  105. ;p_right = NodeTraits::get_right(n)){
  106. n = p_right;
  107. }
  108. return n;
  109. }
  110. //! <b>Requires</b>: p is a node of a tree.
  111. //!
  112. //! <b>Effects</b>: Returns true if p is the header of the tree.
  113. //!
  114. //! <b>Complexity</b>: Constant.
  115. //!
  116. //! <b>Throws</b>: Nothing.
  117. static bool is_header(const_node_ptr p) BOOST_NOEXCEPT
  118. {
  119. node_ptr p_left (NodeTraits::get_left(p));
  120. node_ptr p_right(NodeTraits::get_right(p));
  121. if(!NodeTraits::get_parent(p) || //Header condition when empty tree
  122. (p_left && p_right && //Header always has leftmost and rightmost
  123. (p_left == p_right || //Header condition when only node
  124. (NodeTraits::get_parent(p_left) != p ||
  125. NodeTraits::get_parent(p_right) != p ))
  126. //When tree size > 1 headers can't be leftmost's
  127. //and rightmost's parent
  128. )){
  129. return true;
  130. }
  131. return false;
  132. }
  133. //! <b>Requires</b>: 'n' is a node of the tree or a header node.
  134. //!
  135. //! <b>Effects</b>: Returns the header of the tree.
  136. //!
  137. //! <b>Complexity</b>: Logarithmic.
  138. //!
  139. //! <b>Throws</b>: Nothing.
  140. static node_ptr get_header(const_node_ptr n)
  141. {
  142. node_ptr nn(detail::uncast(n));
  143. node_ptr p(NodeTraits::get_parent(n));
  144. //If p is null, then nn is the header of an empty tree
  145. if(p){
  146. //Non-empty tree, check if nn is neither root nor header
  147. node_ptr pp(NodeTraits::get_parent(p));
  148. //If granparent is not equal to nn, then nn is neither root nor header,
  149. //the try the fast path
  150. if(nn != pp){
  151. do{
  152. nn = p;
  153. p = pp;
  154. pp = NodeTraits::get_parent(pp);
  155. }while(nn != pp);
  156. nn = p;
  157. }
  158. //Check if nn is root or header when size() > 0
  159. else if(!bstree_algorithms_base::is_header(nn)){
  160. nn = p;
  161. }
  162. }
  163. return nn;
  164. }
  165. };
  166. } //namespace intrusive
  167. } //namespace boost
  168. #endif //BOOST_INTRUSIVE_BSTREE_ALGORITHMS_BASE_HPP