aatree.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (C) 2009-2022 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. #ifndef LIBITM_AATREE_H
  22. #define LIBITM_AATREE_H 1
  23. namespace GTM HIDDEN {
  24. template<typename KEY> class aa_tree_key;
  25. class aa_node_base
  26. {
  27. public:
  28. static const bool L = false;
  29. static const bool R = true;
  30. private:
  31. typedef unsigned int level_type;
  32. aa_node_base *m_link[2];
  33. level_type m_level;
  34. static const aa_node_base s_nil;
  35. public:
  36. aa_node_base(level_type l = 1)
  37. : m_link { const_cast<aa_node_base *>(&s_nil),
  38. const_cast<aa_node_base *>(&s_nil) },
  39. m_level(l)
  40. { }
  41. bool is_nil() const { return this == &s_nil; }
  42. aa_node_base * link(bool d) { return m_link[d]; }
  43. void set_link(bool d, aa_node_base *val) { m_link[d] = val; }
  44. aa_node_base *skew();
  45. aa_node_base *split();
  46. void decrease_level();
  47. static void *operator new (size_t s) { return xmalloc (s); }
  48. static void operator delete (void *p) { free (p); }
  49. };
  50. template<typename KEY>
  51. struct aa_node_key : public aa_node_base
  52. {
  53. typedef aa_node_base base;
  54. KEY key;
  55. explicit aa_node_key(KEY k) : key(k) { }
  56. aa_node_key * link(bool d)
  57. {
  58. return static_cast<aa_node_key *>(base::link(d));
  59. }
  60. aa_node_key *skew() { return static_cast<aa_node_key *>(base::skew()); }
  61. aa_node_key *split() { return static_cast<aa_node_key *>(base::split()); }
  62. };
  63. template<typename KEY, typename DATA>
  64. struct aa_node : public aa_node_key<KEY>
  65. {
  66. typedef aa_node_key<KEY> base;
  67. DATA data;
  68. explicit aa_node(KEY k) : base(k) { }
  69. aa_node * link(bool d)
  70. {
  71. return static_cast<aa_node *>(base::link(d));
  72. }
  73. };
  74. template<typename KEY>
  75. class aa_tree_key
  76. {
  77. public:
  78. typedef aa_node_key<KEY> node;
  79. typedef node *node_ptr;
  80. protected:
  81. node_ptr m_tree;
  82. protected:
  83. aa_tree_key() : m_tree(0) { }
  84. node_ptr find(KEY k) const;
  85. static node_ptr insert_1 (node_ptr t, node_ptr n);
  86. void insert(node_ptr n);
  87. static node_ptr erase_1 (node_ptr t, KEY k, node_ptr *pfree);
  88. node_ptr erase(KEY k);
  89. };
  90. extern template class aa_tree_key<uintptr_t>;
  91. template<typename KEY, typename DATA>
  92. class aa_tree : public aa_tree_key<KEY>
  93. {
  94. public:
  95. typedef aa_tree_key<KEY> base;
  96. typedef aa_node<KEY, DATA> node;
  97. typedef node *node_ptr;
  98. typedef void (*trav_callback)(KEY, DATA *, void *);
  99. private:
  100. static void clear_1 (node_ptr);
  101. static void traverse_1 (node_ptr, trav_callback, void *);
  102. public:
  103. aa_tree() = default;
  104. ~aa_tree() { clear(); }
  105. static void *operator new (size_t s, aa_tree<KEY, DATA>* p) { return p; }
  106. DATA *find(KEY k) const
  107. {
  108. node_ptr n = static_cast<node_ptr>(base::find (k));
  109. return n ? &n->data : 0;
  110. }
  111. DATA *insert(KEY k)
  112. {
  113. node_ptr n = new node(k);
  114. base::insert(n);
  115. return &n->data;
  116. }
  117. void erase(KEY k)
  118. {
  119. node_ptr n = static_cast<node_ptr>(base::erase (k));
  120. delete n;
  121. }
  122. node_ptr remove(KEY k, DATA** data)
  123. {
  124. node_ptr n = static_cast<node_ptr>(base::erase (k));
  125. *data = (n ? &n->data : 0);
  126. return n;
  127. }
  128. void clear()
  129. {
  130. node_ptr n = static_cast<node_ptr>(this->m_tree);
  131. if (n)
  132. {
  133. this->m_tree = 0;
  134. clear_1 (n);
  135. }
  136. }
  137. void traverse (trav_callback cb, void *cb_data)
  138. {
  139. node_ptr t = static_cast<node_ptr>(this->m_tree);
  140. if (t != 0)
  141. traverse_1 (t, cb, cb_data);
  142. }
  143. };
  144. template<typename KEY, typename DATA>
  145. void
  146. aa_tree<KEY, DATA>::clear_1 (node_ptr t)
  147. {
  148. if (t->is_nil())
  149. return;
  150. clear_1 (t->link(node::L));
  151. clear_1 (t->link(node::R));
  152. delete t;
  153. }
  154. template<typename KEY, typename DATA>
  155. void
  156. aa_tree<KEY, DATA>::traverse_1 (node_ptr t, trav_callback cb, void *cb_data)
  157. {
  158. if (t->is_nil())
  159. return;
  160. cb (t->key, &t->data, cb_data);
  161. traverse_1 (t->link(node::L), cb, cb_data);
  162. traverse_1 (t->link(node::R), cb, cb_data);
  163. }
  164. } // namespace GTM
  165. #endif // LIBITM_AATREE_H