aatree.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Copyright (C) 2009-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. // Implements an AA tree (http://en.wikipedia.org/wiki/AA_tree) with an
  20. // integer key, and data attached to the node via flexible array member.
  21. #include "libitm_i.h"
  22. namespace GTM HIDDEN {
  23. // The code for rebalancing the tree is greatly simplified by never
  24. // having to check for null pointers. Instead, leaf node links point
  25. // to this node, NIL, which points to itself.
  26. const aa_node_base aa_node_base::s_nil(0);
  27. // Remove left horizontal links. Swap the pointers of horizontal left links.
  28. aa_node_base *
  29. aa_node_base::skew ()
  30. {
  31. aa_node_base *l = this->link(L);
  32. if (this->m_level != 0 && l->m_level == this->m_level)
  33. {
  34. this->set_link(L, l->link(R));
  35. l->set_link(R, this);
  36. return l;
  37. }
  38. return this;
  39. }
  40. // Remove consecutive horizontal links. Take the middle node,
  41. // elevate it, and return it.
  42. aa_node_base *
  43. aa_node_base::split ()
  44. {
  45. aa_node_base *r = this->link(R);
  46. if (this->m_level != 0 && r->link(R)->m_level == this->m_level)
  47. {
  48. this->set_link(R, r->link(L));
  49. r->set_link(L, this);
  50. r->m_level += 1;
  51. return r;
  52. }
  53. return this;
  54. }
  55. // Decrease the level of THIS to be one more than the level of its children.
  56. void
  57. aa_node_base::decrease_level ()
  58. {
  59. aa_node_base *l = this->link(L);
  60. aa_node_base *r = this->link(R);
  61. level_type llev = l->m_level;
  62. level_type rlev = r->m_level;
  63. level_type should_be = (llev < rlev ? llev : rlev) + 1;
  64. if (should_be < this->m_level)
  65. {
  66. this->m_level = should_be;
  67. if (should_be < rlev)
  68. r->m_level = should_be;
  69. }
  70. }
  71. // Find and return the node in the tree with key K.
  72. template<typename KEY>
  73. typename aa_tree_key<KEY>::node_ptr
  74. aa_tree_key<KEY>::find(KEY k) const
  75. {
  76. node_ptr t = m_tree;
  77. if (t != 0)
  78. do
  79. {
  80. if (t->key == k)
  81. return t;
  82. t = t->link(k > t->key);
  83. }
  84. while (!t->is_nil());
  85. return 0;
  86. }
  87. // Insert N into T and rebalance. Return the new balanced tree.
  88. template<typename KEY>
  89. typename aa_tree_key<KEY>::node_ptr
  90. aa_tree_key<KEY>::insert_1 (node_ptr t, node_ptr n)
  91. {
  92. bool dir = n->key > t->key;
  93. node_ptr c = t->link(dir);
  94. // Insert the node, recursively.
  95. if (c->is_nil())
  96. c = n;
  97. else
  98. c = insert_1 (c, n);
  99. t->set_link(dir, c);
  100. // Rebalance the tree, as needed.
  101. t = t->skew();
  102. t = t->split();
  103. return t;
  104. }
  105. template<typename KEY>
  106. void
  107. aa_tree_key<KEY>::insert(node_ptr n)
  108. {
  109. if (m_tree == 0)
  110. m_tree = n;
  111. else
  112. m_tree = insert_1 (m_tree, n);
  113. }
  114. // Delete K from T and rebalance. Return the new balanced tree.
  115. template<typename KEY>
  116. typename aa_tree_key<KEY>::node_ptr
  117. aa_tree_key<KEY>::erase_1 (node_ptr t, KEY k, node_ptr *pfree)
  118. {
  119. node_ptr r;
  120. bool dir;
  121. // If this is the node we're looking for, delete it. Else recurse.
  122. if (k == t->key)
  123. {
  124. node_ptr l, sub, end;
  125. l = t->link(node::L);
  126. r = t->link(node::R);
  127. if (pfree)
  128. *pfree = t;
  129. // If this is a leaf node, simply remove the node. Otherwise,
  130. // we have to find either a predecessor or a successor node to
  131. // replace this one.
  132. if (l->is_nil())
  133. {
  134. if (r->is_nil())
  135. return r;
  136. sub = r, dir = node::L;
  137. }
  138. else
  139. sub = l, dir = node::R;
  140. // Find the successor or predecessor.
  141. for (end = sub; !end->link(dir)->is_nil(); end = end->link(dir))
  142. continue;
  143. // Remove it (but don't free) from the subtree.
  144. sub = erase_1 (sub, end->key, 0);
  145. // Replace T with the successor we just extracted.
  146. end->set_link(!dir, sub);
  147. t = end;
  148. }
  149. else
  150. {
  151. dir = k > t->key;
  152. t->set_link(dir, erase_1 (t->link(dir), k, pfree));
  153. }
  154. // Rebalance the tree.
  155. t->decrease_level();
  156. t = t->skew();
  157. r = t->link(node::R)->skew();
  158. t->set_link(node::R, r);
  159. r->set_link(node::R, r->link(node::R)->skew());
  160. t = t->split ();
  161. t->set_link(node::R, t->link(node::R)->split());
  162. return t;
  163. }
  164. template<typename KEY>
  165. typename aa_tree_key<KEY>::node_ptr
  166. aa_tree_key<KEY>::erase (KEY k)
  167. {
  168. node_ptr t = m_tree;
  169. if (t == 0)
  170. return 0;
  171. node_ptr do_free = 0;
  172. t = erase_1 (t, k, &do_free);
  173. if (t->is_nil())
  174. t = 0;
  175. m_tree = t;
  176. return do_free;
  177. }
  178. // Instantiate key classes.
  179. template class aa_tree_key<uintptr_t>;
  180. } // namespace GTM