1 /* Copyright (C) 2009-2023 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Transactional Memory Library (libitm).
6 Libitm is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 // Implements an AA tree (http://en.wikipedia.org/wiki/AA_tree) with an
26 // integer key, and data attached to the node via flexible array member.
30 namespace GTM HIDDEN
{
32 // The code for rebalancing the tree is greatly simplified by never
33 // having to check for null pointers. Instead, leaf node links point
34 // to this node, NIL, which points to itself.
35 const aa_node_base
aa_node_base::s_nil(0);
38 // Remove left horizontal links. Swap the pointers of horizontal left links.
43 aa_node_base
*l
= this->link(L
);
44 if (this->m_level
!= 0 && l
->m_level
== this->m_level
)
46 this->set_link(L
, l
->link(R
));
54 // Remove consecutive horizontal links. Take the middle node,
55 // elevate it, and return it.
58 aa_node_base::split ()
60 aa_node_base
*r
= this->link(R
);
61 if (this->m_level
!= 0 && r
->link(R
)->m_level
== this->m_level
)
63 this->set_link(R
, r
->link(L
));
71 // Decrease the level of THIS to be one more than the level of its children.
74 aa_node_base::decrease_level ()
76 aa_node_base
*l
= this->link(L
);
77 aa_node_base
*r
= this->link(R
);
78 level_type llev
= l
->m_level
;
79 level_type rlev
= r
->m_level
;
80 level_type should_be
= (llev
< rlev
? llev
: rlev
) + 1;
82 if (should_be
< this->m_level
)
84 this->m_level
= should_be
;
86 r
->m_level
= should_be
;
90 // Find and return the node in the tree with key K.
92 template<typename KEY
>
93 typename aa_tree_key
<KEY
>::node_ptr
94 aa_tree_key
<KEY
>::find(KEY k
) const
102 t
= t
->link(k
> t
->key
);
104 while (!t
->is_nil());
108 // Insert N into T and rebalance. Return the new balanced tree.
110 template<typename KEY
>
111 typename aa_tree_key
<KEY
>::node_ptr
112 aa_tree_key
<KEY
>::insert_1 (node_ptr t
, node_ptr n
)
114 bool dir
= n
->key
> t
->key
;
115 node_ptr c
= t
->link(dir
);
117 // Insert the node, recursively.
124 // Rebalance the tree, as needed.
131 template<typename KEY
>
133 aa_tree_key
<KEY
>::insert(node_ptr n
)
138 m_tree
= insert_1 (m_tree
, n
);
141 // Delete K from T and rebalance. Return the new balanced tree.
143 template<typename KEY
>
144 typename aa_tree_key
<KEY
>::node_ptr
145 aa_tree_key
<KEY
>::erase_1 (node_ptr t
, KEY k
, node_ptr
*pfree
)
150 // If this is the node we're looking for, delete it. Else recurse.
153 node_ptr l
, sub
, end
;
155 l
= t
->link(node::L
);
156 r
= t
->link(node::R
);
161 // If this is a leaf node, simply remove the node. Otherwise,
162 // we have to find either a predecessor or a successor node to
168 sub
= r
, dir
= node::L
;
171 sub
= l
, dir
= node::R
;
173 // Find the successor or predecessor.
174 for (end
= sub
; !end
->link(dir
)->is_nil(); end
= end
->link(dir
))
177 // Remove it (but don't free) from the subtree.
178 sub
= erase_1 (sub
, end
->key
, 0);
180 // Replace T with the successor we just extracted.
181 end
->set_link(!dir
, sub
);
187 t
->set_link(dir
, erase_1 (t
->link(dir
), k
, pfree
));
190 // Rebalance the tree.
193 r
= t
->link(node::R
)->skew();
194 t
->set_link(node::R
, r
);
195 r
->set_link(node::R
, r
->link(node::R
)->skew());
197 t
->set_link(node::R
, t
->link(node::R
)->split());
202 template<typename KEY
>
203 typename aa_tree_key
<KEY
>::node_ptr
204 aa_tree_key
<KEY
>::erase (KEY k
)
210 node_ptr do_free
= 0;
211 t
= erase_1 (t
, k
, &do_free
);
218 // Instantiate key classes.
220 template class aa_tree_key
<uintptr_t>;