Debian Codegraph @

src/3rdparty/javascriptcore/JavaScriptCore/wtf/AVLTree.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 *
4 * Based on Abstract AVL Tree Template v1.5 by Walt Karas
5 * <http://geocities.com/wkaras/gen_cpp/avl_tree.html>.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef AVL_TREE_H_
33 #define AVL_TREE_H_cxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/AVLTree.h:33:9`!
34
35 #include "Assertions.h"cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/wtf/Assertions.h`/
36
37 namespace WTFcxx . . $ `<built-in>:467:9`! {
38
39 // Here is the reference class for BSet.
40 //
41 // class BSet
42 // {
43 // public:
44 //
45 // class ANY_bitref
46 // {
47 // public:
48 // operator bool ();
49 // void operator = (bool b);
50 // };
51 //
52 // // Does not have to initialize bits.
53 // BSet();
54 //
55 // // Must return a valid value for index when 0 <= index < maxDepth
56 // ANY_bitref operator [] (unsigned index);
57 //
58 // // Set all bits to 1.
59 // void set();
60 //
61 // // Set all bits to 0.
62 // void reset();
63 // };
64
65 template<unsigned maxDepth>
66 class AVLTreeDefaultBSetcxx . . $ QTWTF/AVLTreeDefaultBSet# {
67 public:
68 bool& operatorcxx . . $ QTWTF/AVLTreeDefaultBSet#`operator[]`(54c30d0c3feb698).[](unsigned i) { ASSERTcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Assertions.h:184:9`!(i < maxDepth); return m_datacxx . . $ QTWTF/AVLTreeDefaultBSet#m_data.[i]; }
69 void setcxx . . $ QTWTF/AVLTreeDefaultBSet#set(49f6e7a06ebc5aa8).() { for (unsigned i = 0; i < maxDepth; ++i) m_datacxx . . $ QTWTF/AVLTreeDefaultBSet#m_data.[i] = true; }
70 void resetcxx . . $ QTWTF/AVLTreeDefaultBSet#reset(49f6e7a06ebc5aa8).() { for (unsigned i = 0; i < maxDepth; ++i) m_datacxx . . $ QTWTF/AVLTreeDefaultBSet#m_data.[i] = false; }
71
72 private:
73 bool m_datacxx . . $ QTWTF/AVLTreeDefaultBSet#m_data.[maxDepth];
74 };
75
76 // How to determine maxDepth:
77 // d Minimum number of nodes
78 // 2 2
79 // 3 4
80 // 4 7
81 // 5 12
82 // 6 20
83 // 7 33
84 // 8 54
85 // 9 88
86 // 10 143
87 // 11 232
88 // 12 376
89 // 13 609
90 // 14 986
91 // 15 1,596
92 // 16 2,583
93 // 17 4,180
94 // 18 6,764
95 // 19 10,945
96 // 20 17,710
97 // 21 28,656
98 // 22 46,367
99 // 23 75,024
100 // 24 121,392
101 // 25 196,417
102 // 26 317,810
103 // 27 514,228
104 // 28 832,039
105 // 29 1,346,268
106 // 30 2,178,308
107 // 31 3,524,577
108 // 32 5,702,886
109 // 33 9,227,464
110 // 34 14,930,351
111 // 35 24,157,816
112 // 36 39,088,168
113 // 37 63,245,985
114 // 38 102,334,154
115 // 39 165,580,140
116 // 40 267,914,295
117 // 41 433,494,436
118 // 42 701,408,732
119 // 43 1,134,903,169
120 // 44 1,836,311,902
121 // 45 2,971,215,072
122 //
123 // E.g., if, in a particular instantiation, the maximum number of nodes in a tree instance is 1,000,000, the maximum depth should be 28.
124 // You pick 28 because MN(28) is 832,039, which is less than or equal to 1,000,000, and MN(29) is 1,346,268, which is strictly greater than 1,000,000.
125
126 template <class Abstractor, unsigned maxDepth = 32, class BSet = AVLTreeDefaultBSetcxx . . $ QTWTF/AVLTreeDefaultBSet#<maxDepth> >
127 class AVLTreecxx . . $ QTWTF/AVLTree# {
128 public:
129
130 typedef typename Abstractor::key keycxx . . $ QTWTF/AVLTree#key#;
131 typedef typename Abstractor::handle handlecxx . . $ QTWTF/AVLTree#handle#;
132 typedef typename Abstractor::size sizecxx . . $ QTWTF/AVLTree#size#;
133
134 enum SearchTypecxx . . $ QTWTF/AVLTree#SearchType# {
135 EQUALcxx . . $ QTWTF/AVLTree#EQUAL. = 1,
136 LESScxx . . $ QTWTF/AVLTree#LESS. = 2,
137 GREATERcxx . . $ QTWTF/AVLTree#GREATER. = 4,
138 LESS_EQUALcxx . . $ QTWTF/AVLTree#LESS_EQUAL. = EQUALcxx . . $ QTWTF/AVLTree#EQUAL. |cxx . . $ `operator|`(68626a7e09ca3b35). LESScxx . . $ QTWTF/AVLTree#LESS.,
139 GREATER_EQUALcxx . . $ QTWTF/AVLTree#GREATER_EQUAL. = EQUALcxx . . $ QTWTF/AVLTree#EQUAL. |cxx . . $ `operator|`(68626a7e09ca3b35). GREATERcxx . . $ QTWTF/AVLTree#GREATER.
140 };
141
142
143 Abstractor& abstractorcxx . . $ QTWTF/AVLTree#abstractor(19b2b6f17af048b).() { return abscxx . . $ QTWTF/AVLTree#abs.; }
144
145 inline handlecxx . . $ QTWTF/AVLTree#handle# insertcxx . . $ QTWTF/AVLTree#insert(4b6052a634f9db5d).(handlecxx . . $ QTWTF/AVLTree#handle# h);
146
147 inline handlecxx . . $ QTWTF/AVLTree#handle# searchcxx . . $ QTWTF/AVLTree#search(715a8e81fbeb4ebe).(keycxx . . $ QTWTF/AVLTree#key# k, SearchTypecxx . . $ QTWTF/AVLTree#SearchType# st = EQUALcxx . . $ QTWTF/AVLTree#EQUAL.);
148 inline handlecxx . . $ QTWTF/AVLTree#handle# search_leastcxx . . $ QTWTF/AVLTree#search_least(c7de0be71f349065).();
149 inline handlecxx . . $ QTWTF/AVLTree#handle# search_greatestcxx . . $ QTWTF/AVLTree#search_greatest(c7de0be71f349065).();
150
151 inline handlecxx . . $ QTWTF/AVLTree#handle# removecxx . . $ QTWTF/AVLTree#remove(26fdd1dab066c64).(keycxx . . $ QTWTF/AVLTree#key# k);
152
153 inline handlecxx . . $ QTWTF/AVLTree#handle# substcxx . . $ QTWTF/AVLTree#subst(4b6052a634f9db5d).(handlecxx . . $ QTWTF/AVLTree#handle# new_node);
154
155 void purgecxx . . $ QTWTF/AVLTree#purge(49f6e7a06ebc5aa8).() { abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).(); }
156
157 bool is_emptycxx . . $ QTWTF/AVLTree#is_empty(c9857a73d68e59ba).() { return abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).(); }
158
159 AVLTreecxx . . $ QTWTF/AVLTree#`AVLTree<Abstractor, maxDepth, BSet>`(49f6e7a06ebc5aa8).() { abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).(); }
160
161 class Iteratorcxx . . $ QTWTF/AVLTree#Iterator# {
162 public:
163
164 // Initialize depth to invalid value, to indicate iterator is
165 // invalid. (Depth is zero-base.)
166 Iteratorcxx . . $ QTWTF/AVLTree#Iterator#Iterator(49f6e7a06ebc5aa8).() { depthcxx . . $ QTWTF/AVLTree#Iterator#depth. = ~0U; }
167
168 void start_itercxx . . $ QTWTF/AVLTree#Iterator#start_iter(5dc828d2926099ec).(AVLTree &tree, keycxx . . $ QTWTF/AVLTree#key# k, SearchTypecxx . . $ QTWTF/AVLTree#SearchType# st = EQUALcxx . . $ QTWTF/AVLTree#EQUAL.)
169 {
170 // Mask of high bit in an int.
171 const int MASK_HIGH_BIT = (int) ~ ((~ (unsigned) 0) >> 1);
172
173 // Save the tree that we're going to iterate through in a
174 // member variable.
175 tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_. = &tree;
176
177 int cmp, target_cmp;
178 handlecxx . . $ QTWTF/AVLTree#handle# h = tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_.->abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root.;
179 unsigned d = 0;
180
181 depthcxx . . $ QTWTF/AVLTree#Iterator#depth. = ~0U;
182
183 if (h ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#Iterator#null(c7de0be71f349065).())
184 // Tree is empty.
185 return;
186
187 if (st & LESScxx . . $ QTWTF/AVLTree#LESS.)
188 // Key can be greater than key of starting node.
189 target_cmp = 1;
190 else if (st & GREATERcxx . . $ QTWTF/AVLTree#GREATER.)
191 // Key can be less than key of starting node.
192 target_cmp = -1;
193 else
194 // Key must be same as key of starting node.
195 target_cmp = 0;
196
197 for (;;) {
198 cmp = cmp_k_ncxx . . $ QTWTF/AVLTree#Iterator#cmp_k_n(be68e6082678efb8).(k, h);
199 if (cmp == 0) {
200 if (st & EQUALcxx . . $ QTWTF/AVLTree#EQUAL.) {
201 // Equal node was sought and found as starting node.
202 depthcxx . . $ QTWTF/AVLTree#Iterator#depth. = d;
203 break;
204 }
205 cmp = -target_cmp;
206 } else if (target_cmp != 0) {
207 if (!((cmp ^ target_cmp) & MASK_HIGH_BIT)) {
208 // cmp and target_cmp are both negative or both positive.
209 depthcxx . . $ QTWTF/AVLTree#Iterator#depth. = d;
210 }
211 }
212 h = cmp < 0 ? get_ltcxx . . $ QTWTF/AVLTree#Iterator#get_lt(4b6052a634f9db5d).(h) : get_gtcxx . . $ QTWTF/AVLTree#Iterator#get_gt(4b6052a634f9db5d).(h);
213 if (h ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#Iterator#null(c7de0be71f349065).())
214 break;
215 branchcxx . . $ QTWTF/AVLTree#Iterator#branch.[d] = cmp > 0;
216 path_hcxx . . $ QTWTF/AVLTree#Iterator#path_h.[d++] = h;
217 }
218 }
219
220 void start_iter_leastcxx . . $ QTWTF/AVLTree#Iterator#start_iter_least(b57a849fac8d162b).(AVLTree &tree)
221 {
222 tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_. = &tree;
223
224 handlecxx . . $ QTWTF/AVLTree#handle# h = tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_.->abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root.;
225
226 depthcxx . . $ QTWTF/AVLTree#Iterator#depth. = ~0U;
227
228 branchcxx . . $ QTWTF/AVLTree#Iterator#branch..reset();
229
230 while (h !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#Iterator#null(c7de0be71f349065).()) {
231 if (depthcxx . . $ QTWTF/AVLTree#Iterator#depth. != ~0U)
232 path_hcxx . . $ QTWTF/AVLTree#Iterator#path_h.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.] = h;
233 depthcxx . . $ QTWTF/AVLTree#Iterator#depth.++;
234 h = get_ltcxx . . $ QTWTF/AVLTree#Iterator#get_lt(4b6052a634f9db5d).(h);
235 }
236 }
237
238 void start_iter_greatestcxx . . $ QTWTF/AVLTree#Iterator#start_iter_greatest(b57a849fac8d162b).(AVLTree &tree)
239 {
240 tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_. = &tree;
241
242 handlecxx . . $ QTWTF/AVLTree#handle# h = tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_.->abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root.;
243
244 depthcxx . . $ QTWTF/AVLTree#Iterator#depth. = ~0U;
245
246 branchcxx . . $ QTWTF/AVLTree#Iterator#branch..set();
247
248 while (h !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#Iterator#null(c7de0be71f349065).()) {
249 if (depthcxx . . $ QTWTF/AVLTree#Iterator#depth. != ~0U)
250 path_hcxx . . $ QTWTF/AVLTree#Iterator#path_h.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.] = h;
251 depthcxx . . $ QTWTF/AVLTree#Iterator#depth.++;
252 h = get_gtcxx . . $ QTWTF/AVLTree#Iterator#get_gt(4b6052a634f9db5d).(h);
253 }
254 }
255
256 handlecxx . . $ QTWTF/AVLTree#handle# operatorcxx . . $ QTWTF/AVLTree#Iterator#`operator*`(c7de0be71f349065).*()
257 {
258 if (depthcxx . . $ QTWTF/AVLTree#Iterator#depth. == ~0U)
259 return nullcxx . . $ QTWTF/AVLTree#Iterator#null(c7de0be71f349065).();
260
261 return depthcxx . . $ QTWTF/AVLTree#Iterator#depth. == 0 ? tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_.->abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. : path_hcxx . . $ QTWTF/AVLTree#Iterator#path_h.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth. - 1];
262 }
263
264 void operatorcxx . . $ QTWTF/AVLTree#Iterator#operator++(49f6e7a06ebc5aa8).++()
265 {
266 if (depthcxx . . $ QTWTF/AVLTree#Iterator#depth. != ~0U) {
267 handlecxx . . $ QTWTF/AVLTree#handle# h = get_gtcxx . . $ QTWTF/AVLTree#Iterator#get_gt(4b6052a634f9db5d).(**this);
268 if (h ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#Iterator#null(c7de0be71f349065).()) {
269 do {
270 if (depthcxx . . $ QTWTF/AVLTree#Iterator#depth. == 0) {
271 depthcxx . . $ QTWTF/AVLTree#Iterator#depth. = ~0U;
272 break;
273 }
274 depthcxx . . $ QTWTF/AVLTree#Iterator#depth.--;
275 } while (branchcxx . . $ QTWTF/AVLTree#Iterator#branch.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.]);
276 } else {
277 branchcxx . . $ QTWTF/AVLTree#Iterator#branch.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.] = true;
278 path_hcxx . . $ QTWTF/AVLTree#Iterator#path_h.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.++] = h;
279 for (;;) {
280 h = get_ltcxx . . $ QTWTF/AVLTree#Iterator#get_lt(4b6052a634f9db5d).(h);
281 if (h ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#Iterator#null(c7de0be71f349065).())
282 break;
283 branchcxx . . $ QTWTF/AVLTree#Iterator#branch.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.] = false;
284 path_hcxx . . $ QTWTF/AVLTree#Iterator#path_h.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.++] = h;
285 }
286 }
287 }
288 }
289
290 void operatorcxx . . $ QTWTF/AVLTree#Iterator#operator--(49f6e7a06ebc5aa8).--()
291 {
292 if (depthcxx . . $ QTWTF/AVLTree#Iterator#depth. != ~0U) {
293 handlecxx . . $ QTWTF/AVLTree#handle# h = get_ltcxx . . $ QTWTF/AVLTree#Iterator#get_lt(4b6052a634f9db5d).(**this);
294 if (h ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#Iterator#null(c7de0be71f349065).())
295 do {
296 if (depthcxx . . $ QTWTF/AVLTree#Iterator#depth. == 0) {
297 depthcxx . . $ QTWTF/AVLTree#Iterator#depth. = ~0U;
298 break;
299 }
300 depthcxx . . $ QTWTF/AVLTree#Iterator#depth.--;
301 } while (!branchcxx . . $ QTWTF/AVLTree#Iterator#branch.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.]);
302 else {
303 branchcxx . . $ QTWTF/AVLTree#Iterator#branch.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.] = false;
304 path_hcxx . . $ QTWTF/AVLTree#Iterator#path_h.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.++] = h;
305 for (;;) {
306 h = get_gtcxx . . $ QTWTF/AVLTree#Iterator#get_gt(4b6052a634f9db5d).(h);
307 if (h ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#Iterator#null(c7de0be71f349065).())
308 break;
309 branchcxx . . $ QTWTF/AVLTree#Iterator#branch.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.] = true;
310 path_hcxx . . $ QTWTF/AVLTree#Iterator#path_h.[depthcxx . . $ QTWTF/AVLTree#Iterator#depth.++] = h;
311 }
312 }
313 }
314 }
315
316 void operatorcxx . . $ QTWTF/AVLTree#Iterator#operator++(d4f767463ce0a6b3).++(int) { ++(*this); }
317 void operatorcxx . . $ QTWTF/AVLTree#Iterator#operator--(d4f767463ce0a6b3).--(int) { --(*this); }
318
319 protected:
320
321 // Tree being iterated over.
322 AVLTree *tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_.;
323
324 // Records a path into the tree. If branch[n] is true, indicates
325 // take greater branch from the nth node in the path, otherwise
326 // take the less branch. branch[0] gives branch from root, and
327 // so on.
328 BSet branchcxx . . $ QTWTF/AVLTree#Iterator#branch.;
329
330 // Zero-based depth of path into tree.
331 unsigned depthcxx . . $ QTWTF/AVLTree#Iterator#depth.;
332
333 // Handles of nodes in path from root to current node (returned by *).
334 handlecxx . . $ QTWTF/AVLTree#handle# path_hcxx . . $ QTWTF/AVLTree#Iterator#path_h.[maxDepth - 1];
335
336 int cmp_k_ncxx . . $ QTWTF/AVLTree#Iterator#cmp_k_n(be68e6082678efb8).(keycxx . . $ QTWTF/AVLTree#key# k, handlecxx . . $ QTWTF/AVLTree#handle# h) { return tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_.->abscxx . . $ QTWTF/AVLTree#abs..compare_key_node(k, h); }
337 int cmp_n_ncxx . . $ QTWTF/AVLTree#Iterator#cmp_n_n(1864087b0395d431).(handlecxx . . $ QTWTF/AVLTree#handle# h1, handlecxx . . $ QTWTF/AVLTree#handle# h2) { return tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_.->abscxx . . $ QTWTF/AVLTree#abs..compare_node_node(h1, h2); }
338 handlecxx . . $ QTWTF/AVLTree#handle# get_ltcxx . . $ QTWTF/AVLTree#Iterator#get_lt(4b6052a634f9db5d).(handlecxx . . $ QTWTF/AVLTree#handle# h) { return tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_.->abscxx . . $ QTWTF/AVLTree#abs..get_less(h); }
339 handlecxx . . $ QTWTF/AVLTree#handle# get_gtcxx . . $ QTWTF/AVLTree#Iterator#get_gt(4b6052a634f9db5d).(handlecxx . . $ QTWTF/AVLTree#handle# h) { return tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_.->abscxx . . $ QTWTF/AVLTree#abs..get_greater(h); }
340 handlecxx . . $ QTWTF/AVLTree#handle# nullcxx . . $ QTWTF/AVLTree#Iterator#null(c7de0be71f349065).() { return tree_cxx . . $ QTWTF/AVLTree#Iterator#tree_.->abscxx . . $ QTWTF/AVLTree#abs..null(); }
341 };
342
343 template<typename fwd_iter>
344 bool buildcxx . . $ QTWTF/AVLTree#build(b0167a7bdc9daaa0).(fwd_iter p, sizecxx . . $ QTWTF/AVLTree#size# num_nodes)
345 {
346 if (num_nodes ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). 0) {
347 abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
348 return true;
349 }
350
351 // Gives path to subtree being built. If branch[N] is false, branch
352 // less from the node at depth N, if true branch greater.
353 BSet branch;
354
355 // If rem[N] is true, then for the current subtree at depth N, it's
356 // greater subtree has one more node than it's less subtree.
357 BSet rem;
358
359 // Depth of root node of current subtree.
360 unsigned depth = 0;
361
362 // Number of nodes in current subtree.
363 sizecxx . . $ QTWTF/AVLTree#size# num_sub = num_nodes;
364
365 // The algorithm relies on a stack of nodes whose less subtree has
366 // been built, but whose right subtree has not yet been built. The
367 // stack is implemented as linked list. The nodes are linked
368 // together by having the "greater" handle of a node set to the
369 // next node in the list. "less_parent" is the handle of the first
370 // node in the list.
371 handlecxx . . $ QTWTF/AVLTree#handle# less_parent = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
372
373 // h is root of current subtree, child is one of its children.
374 handlecxx . . $ QTWTF/AVLTree#handle# h, child;
375
376 for (;;) {
377 while (num_sub >cxx . . $ `operator>`(8f8a8e6a6f1fc74b). 2) {
378 // Subtract one for root of subtree.
379 num_sub--;
380 rem[depth] = !!(num_sub & 1);
381 branch[depth++] = false;
382 num_sub >>= 1;
383 }
384
385 if (num_sub ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). 2) {
386 // Build a subtree with two nodes, slanting to greater.
387 // I arbitrarily chose to always have the extra node in the
388 // greater subtree when there is an odd number of nodes to
389 // split between the two subtrees.
390
391 h = *p;
392 p++;
393 child = *p;
394 p++;
395 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(child, nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).());
396 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(child, nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).());
397 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(child, 0);
398 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(h, child);
399 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(h, nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).());
400 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(h, 1);
401 } else { // num_sub == 1
402 // Build a subtree with one node.
403
404 h = *p;
405 p++;
406 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(h, nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).());
407 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(h, nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).());
408 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(h, 0);
409 }
410
411 while (depth) {
412 depth--;
413 if (!branch[depth])
414 // We've completed a less subtree.
415 break;
416
417 // We've completed a greater subtree, so attach it to
418 // its parent (that is less than it). We pop the parent
419 // off the stack of less parents.
420 child = h;
421 h = less_parent;
422 less_parent = get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(h);
423 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(h, child);
424 // num_sub = 2 * (num_sub - rem[depth]) + rem[depth] + 1
425 num_sub <<= 1;
426 num_sub +=cxx . . $ `operator+=`(79c69f12ebd2f310). 1 - rem[depth];
427 if (num_sub & (num_sub - 1))
428 // num_sub is not a power of 2
429 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(h, 0);
430 else
431 // num_sub is a power of 2
432 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(h, 1);
433 }
434
435 if (num_sub ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). num_nodes)
436 // We've completed the full tree.
437 break;
438
439 // The subtree we've completed is the less subtree of the
440 // next node in the sequence.
441
442 child = h;
443 h = *p;
444 p++;
445 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(h, child);
446
447 // Put h into stack of less parents.
448 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(h, less_parent);
449 less_parent = h;
450
451 // Proceed to creating greater than subtree of h.
452 branch[depth] = true;
453 num_sub +=cxx . . $ `operator+=`(79c69f12ebd2f310). rem[depth++];
454
455 } // end for (;;)
456
457 abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. = h;
458
459 return true;
460 }
461
462 protected:
463
464 friend class Iteratorcxx . . $ QTWTF/AVLTree#Iterator#;
465
466 // Create a class whose sole purpose is to take advantage of
467 // the "empty member" optimization.
468 struct abs_plus_rootcxx . . $ QTWTF/AVLTree#abs_plus_root# : public Abstractor {
469 // The handle of the root element in the AVL tree.
470 handlecxx . . $ QTWTF/AVLTree#handle# rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root.;
471 };
472
473 abs_plus_rootcxx . . $ QTWTF/AVLTree#abs_plus_root# abscxx . . $ QTWTF/AVLTree#abs.;
474
475
476 handlecxx . . $ QTWTF/AVLTree#handle# get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(handlecxx . . $ QTWTF/AVLTree#handle# h) { return abscxx . . $ QTWTF/AVLTree#abs..get_less(h); }
477 void set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(handlecxx . . $ QTWTF/AVLTree#handle# h, handlecxx . . $ QTWTF/AVLTree#handle# lh) { abscxx . . $ QTWTF/AVLTree#abs..set_less(h, lh); }
478
479 handlecxx . . $ QTWTF/AVLTree#handle# get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(handlecxx . . $ QTWTF/AVLTree#handle# h) { return abscxx . . $ QTWTF/AVLTree#abs..get_greater(h); }
480 void set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(handlecxx . . $ QTWTF/AVLTree#handle# h, handlecxx . . $ QTWTF/AVLTree#handle# gh) { abscxx . . $ QTWTF/AVLTree#abs..set_greater(h, gh); }
481
482 int get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(handlecxx . . $ QTWTF/AVLTree#handle# h) { return abscxx . . $ QTWTF/AVLTree#abs..get_balance_factor(h); }
483 void set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(handlecxx . . $ QTWTF/AVLTree#handle# h, int bf) { abscxx . . $ QTWTF/AVLTree#abs..set_balance_factor(h, bf); }
484
485 int cmp_k_ncxx . . $ QTWTF/AVLTree#cmp_k_n(be68e6082678efb8).(keycxx . . $ QTWTF/AVLTree#key# k, handlecxx . . $ QTWTF/AVLTree#handle# h) { return abscxx . . $ QTWTF/AVLTree#abs..compare_key_node(k, h); }
486 int cmp_n_ncxx . . $ QTWTF/AVLTree#cmp_n_n(1864087b0395d431).(handlecxx . . $ QTWTF/AVLTree#handle# h1, handlecxx . . $ QTWTF/AVLTree#handle# h2) { return abscxx . . $ QTWTF/AVLTree#abs..compare_node_node(h1, h2); }
487
488 handlecxx . . $ QTWTF/AVLTree#handle# nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).() { return abscxx . . $ QTWTF/AVLTree#abs..null(); }
489
490 private:
491
492 // Balances subtree, returns handle of root node of subtree
493 // after balancing.
494 handlecxx . . $ QTWTF/AVLTree#handle# balancecxx . . $ QTWTF/AVLTree#balance(4b6052a634f9db5d).(handlecxx . . $ QTWTF/AVLTree#handle# bal_h)
495 {
496 handlecxx . . $ QTWTF/AVLTree#handle# deep_h;
497
498 // Either the "greater than" or the "less than" subtree of
499 // this node has to be 2 levels deeper (or else it wouldn't
500 // need balancing).
501
502 if (get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(bal_h) >cxx . . $ `operator>`(8f8a8e6a6f1fc74b). 0) {
503 // "Greater than" subtree is deeper.
504
505 deep_h = get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(bal_h);
506
507 if (get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(deep_h) <cxx . . $ `operator<`(8f8a8e6a6f1fc74b). 0) {
508 handlecxx . . $ QTWTF/AVLTree#handle# old_h = bal_h;
509 bal_h = get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(deep_h);
510
511 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(old_h, get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(bal_h));
512 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(deep_h, get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(bal_h));
513 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(bal_h, old_h);
514 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(bal_h, deep_h);
515
516 int bf = get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(bal_h);
517 if (bf != 0) {
518 if (bf > 0) {
519 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(old_h, -1);
520 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(deep_h, 0);
521 } else {
522 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(deep_h, 1);
523 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(old_h, 0);
524 }
525 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(bal_h, 0);
526 } else {
527 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(old_h, 0);
528 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(deep_h, 0);
529 }
530 } else {
531 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(bal_h, get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(deep_h));
532 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(deep_h, bal_h);
533 if (get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(deep_h) ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). 0) {
534 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(deep_h, -1);
535 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(bal_h, 1);
536 } else {
537 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(deep_h, 0);
538 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(bal_h, 0);
539 }
540 bal_h = deep_h;
541 }
542 } else {
543 // "Less than" subtree is deeper.
544
545 deep_h = get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(bal_h);
546
547 if (get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(deep_h) >cxx . . $ `operator>`(8f8a8e6a6f1fc74b). 0) {
548 handlecxx . . $ QTWTF/AVLTree#handle# old_h = bal_h;
549 bal_h = get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(deep_h);
550 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(old_h, get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(bal_h));
551 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(deep_h, get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(bal_h));
552 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(bal_h, old_h);
553 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(bal_h, deep_h);
554
555 int bf = get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(bal_h);
556 if (bf != 0) {
557 if (bf < 0) {
558 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(old_h, 1);
559 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(deep_h, 0);
560 } else {
561 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(deep_h, -1);
562 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(old_h, 0);
563 }
564 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(bal_h, 0);
565 } else {
566 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(old_h, 0);
567 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(deep_h, 0);
568 }
569 } else {
570 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(bal_h, get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(deep_h));
571 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(deep_h, bal_h);
572 if (get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(deep_h) ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). 0) {
573 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(deep_h, 1);
574 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(bal_h, -1);
575 } else {
576 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(deep_h, 0);
577 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(bal_h, 0);
578 }
579 bal_h = deep_h;
580 }
581 }
582
583 return bal_h;
584 }
585
586 };
587
588 template <class Abstractor, unsigned maxDepth, class BSet>
589 inline typename AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::handlecxx . . $ QTWTF/AVLTree#handle#
590 AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::insertcxx . . $ QTWTF/AVLTree#insert(4b6052a634f9db5d).(handlecxx . . $ QTWTF/AVLTree#handle# h)
591 {
592 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(h, nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).());
593 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(h, nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).());
594 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(h, 0);
595
596 if (abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).())
597 abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. = h;
598 else {
599 // Last unbalanced node encountered in search for insertion point.
600 handlecxx . . $ QTWTF/AVLTree#handle# unbal = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
601 // Parent of last unbalanced node.
602 handlecxx . . $ QTWTF/AVLTree#handle# parent_unbal = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
603 // Balance factor of last unbalanced node.
604 int unbal_bf;
605
606 // Zero-based depth in tree.
607 unsigned depth = 0, unbal_depth = 0;
608
609 // Records a path into the tree. If branch[n] is true, indicates
610 // take greater branch from the nth node in the path, otherwise
611 // take the less branch. branch[0] gives branch from root, and
612 // so on.
613 BSet branch;
614
615 handlecxx . . $ QTWTF/AVLTree#handle# hh = abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root.;
616 handlecxx . . $ QTWTF/AVLTree#handle# parent = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
617 int cmp;
618
619 do {
620 if (get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(hh) !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). 0) {
621 unbal = hh;
622 parent_unbal = parent;
623 unbal_depth = depth;
624 }
625 cmp = cmp_n_ncxx . . $ QTWTF/AVLTree#cmp_n_n(1864087b0395d431).(h, hh);
626 if (cmp == 0)
627 // Duplicate key.
628 return hh;
629 parent = hh;
630 hh = cmp < 0 ? get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(hh) : get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(hh);
631 branch[depth++] = cmp > 0;
632 } while (hh !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).());
633
634 // Add node to insert as leaf of tree.
635 if (cmp < 0)
636 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(parent, h);
637 else
638 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(parent, h);
639
640 depth = unbal_depth;
641
642 if (unbal ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).())
643 hh = abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root.;
644 else {
645 cmp = branch[depth++] ? 1 : -1;
646 unbal_bf = get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(unbal);
647 if (cmp < 0)
648 unbal_bf--;
649 else // cmp > 0
650 unbal_bf++;
651 hh = cmp < 0 ? get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(unbal) : get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(unbal);
652 if ((unbal_bf != -2) && (unbal_bf != 2)) {
653 // No rebalancing of tree is necessary.
654 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(unbal, unbal_bf);
655 unbal = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
656 }
657 }
658
659 if (hh !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).())
660 while (h !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). hh) {
661 cmp = branch[depth++] ? 1 : -1;
662 if (cmp < 0) {
663 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(hh, -1);
664 hh = get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(hh);
665 } else { // cmp > 0
666 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(hh, 1);
667 hh = get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(hh);
668 }
669 }
670
671 if (unbal !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).()) {
672 unbal = balancecxx . . $ QTWTF/AVLTree#balance(4b6052a634f9db5d).(unbal);
673 if (parent_unbal ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).())
674 abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. = unbal;
675 else {
676 depth = unbal_depth - 1;
677 cmp = branch[depth] ? 1 : -1;
678 if (cmp < 0)
679 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(parent_unbal, unbal);
680 else // cmp > 0
681 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(parent_unbal, unbal);
682 }
683 }
684 }
685
686 return h;
687 }
688
689 template <class Abstractor, unsigned maxDepth, class BSet>
690 inline typename AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::handlecxx . . $ QTWTF/AVLTree#handle#
691 AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::searchcxx . . $ QTWTF/AVLTree#search(715a8e81fbeb4ebe).(keycxx . . $ QTWTF/AVLTree#key# k, typename AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::SearchTypecxx . . $ QTWTF/AVLTree#SearchType# st)
692 {
693 const int MASK_HIGH_BIT = (int) ~ ((~ (unsigned) 0) >> 1);
694
695 int cmp, target_cmp;
696 handlecxx . . $ QTWTF/AVLTree#handle# match_h = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
697 handlecxx . . $ QTWTF/AVLTree#handle# h = abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root.;
698
699 if (st & LESScxx . . $ QTWTF/AVLTree#LESS.)
700 target_cmp = 1;
701 else if (st & GREATERcxx . . $ QTWTF/AVLTree#GREATER.)
702 target_cmp = -1;
703 else
704 target_cmp = 0;
705
706 while (h !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).()) {
707 cmp = cmp_k_ncxx . . $ QTWTF/AVLTree#cmp_k_n(be68e6082678efb8).(k, h);
708 if (cmp == 0) {
709 if (st & EQUALcxx . . $ QTWTF/AVLTree#EQUAL.) {
710 match_h = h;
711 break;
712 }
713 cmp = -target_cmp;
714 } else if (target_cmp != 0)
715 if (!((cmp ^ target_cmp) & MASK_HIGH_BIT))
716 // cmp and target_cmp are both positive or both negative.
717 match_h = h;
718 h = cmp < 0 ? get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(h) : get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(h);
719 }
720
721 return match_h;
722 }
723
724 template <class Abstractor, unsigned maxDepth, class BSet>
725 inline typename AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::handlecxx . . $ QTWTF/AVLTree#handle#
726 AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::search_leastcxx . . $ QTWTF/AVLTree#search_least(c7de0be71f349065).()
727 {
728 handlecxx . . $ QTWTF/AVLTree#handle# h = abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root., parent = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
729
730 while (h !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).()) {
731 parent = h;
732 h = get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(h);
733 }
734
735 return parent;
736 }
737
738 template <class Abstractor, unsigned maxDepth, class BSet>
739 inline typename AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::handlecxx . . $ QTWTF/AVLTree#handle#
740 AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::search_greatestcxx . . $ QTWTF/AVLTree#search_greatest(c7de0be71f349065).()
741 {
742 handlecxx . . $ QTWTF/AVLTree#handle# h = abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root., parent = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
743
744 while (h !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).()) {
745 parent = h;
746 h = get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(h);
747 }
748
749 return parent;
750 }
751
752 template <class Abstractor, unsigned maxDepth, class BSet>
753 inline typename AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::handlecxx . . $ QTWTF/AVLTree#handle#
754 AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::removecxx . . $ QTWTF/AVLTree#remove(26fdd1dab066c64).(keycxx . . $ QTWTF/AVLTree#key# k)
755 {
756 // Zero-based depth in tree.
757 unsigned depth = 0, rm_depth;
758
759 // Records a path into the tree. If branch[n] is true, indicates
760 // take greater branch from the nth node in the path, otherwise
761 // take the less branch. branch[0] gives branch from root, and
762 // so on.
763 BSet branch;
764
765 handlecxx . . $ QTWTF/AVLTree#handle# h = abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root.;
766 handlecxx . . $ QTWTF/AVLTree#handle# parent = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).(), child;
767 int cmp, cmp_shortened_sub_with_path = 0;
768
769 for (;;) {
770 if (h ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).())
771 // No node in tree with given key.
772 return nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
773 cmp = cmp_k_ncxx . . $ QTWTF/AVLTree#cmp_k_n(be68e6082678efb8).(k, h);
774 if (cmp == 0)
775 // Found node to remove.
776 break;
777 parent = h;
778 h = cmp < 0 ? get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(h) : get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(h);
779 branch[depth++] = cmp > 0;
780 cmp_shortened_sub_with_path = cmp;
781 }
782 handlecxx . . $ QTWTF/AVLTree#handle# rm = h;
783 handlecxx . . $ QTWTF/AVLTree#handle# parent_rm = parent;
784 rm_depth = depth;
785
786 // If the node to remove is not a leaf node, we need to get a
787 // leaf node, or a node with a single leaf as its child, to put
788 // in the place of the node to remove. We will get the greatest
789 // node in the less subtree (of the node to remove), or the least
790 // node in the greater subtree. We take the leaf node from the
791 // deeper subtree, if there is one.
792
793 if (get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(h) <cxx . . $ `operator<`(8f8a8e6a6f1fc74b). 0) {
794 child = get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(h);
795 branch[depth] = false;
796 cmp = -1;
797 } else {
798 child = get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(h);
799 branch[depth] = true;
800 cmp = 1;
801 }
802 depth++;
803
804 if (child !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).()) {
805 cmp = -cmp;
806 do {
807 parent = h;
808 h = child;
809 if (cmp < 0) {
810 child = get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(h);
811 branch[depth] = false;
812 } else {
813 child = get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(h);
814 branch[depth] = true;
815 }
816 depth++;
817 } while (child !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).());
818
819 if (parent ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). rm)
820 // Only went through do loop once. Deleted node will be replaced
821 // in the tree structure by one of its immediate children.
822 cmp_shortened_sub_with_path = -cmp;
823 else
824 cmp_shortened_sub_with_path = cmp;
825
826 // Get the handle of the opposite child, which may not be null.
827 child = cmp > 0 ? get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(h) : get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(h);
828 }
829
830 if (parent ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).())
831 // There were only 1 or 2 nodes in this tree.
832 abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. = child;
833 else if (cmp_shortened_sub_with_path < 0)
834 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(parent, child);
835 else
836 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(parent, child);
837
838 // "path" is the parent of the subtree being eliminated or reduced
839 // from a depth of 2 to 1. If "path" is the node to be removed, we
840 // set path to the node we're about to poke into the position of the
841 // node to be removed.
842 handlecxx . . $ QTWTF/AVLTree#handle# path = parent ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). rm ? h : parent;
843
844 if (h !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). rm) {
845 // Poke in the replacement for the node to be removed.
846 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(h, get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(rm));
847 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(h, get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(rm));
848 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(h, get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(rm));
849 if (parent_rm ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).())
850 abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. = h;
851 else {
852 depth = rm_depth - 1;
853 if (branch[depth])
854 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(parent_rm, h);
855 else
856 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(parent_rm, h);
857 }
858 }
859
860 if (path !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).()) {
861 // Create a temporary linked list from the parent of the path node
862 // to the root node.
863 h = abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root.;
864 parent = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
865 depth = 0;
866 while (h !=cxx . . $ QTWTF/`operator!=`(53f2ef751fb2c4b). path) {
867 if (branch[depth++]) {
868 child = get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(h);
869 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(h, parent);
870 } else {
871 child = get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(h);
872 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(h, parent);
873 }
874 parent = h;
875 h = child;
876 }
877
878 // Climb from the path node to the root node using the linked
879 // list, restoring the tree structure and rebalancing as necessary.
880 bool reduced_depth = true;
881 int bf;
882 cmp = cmp_shortened_sub_with_path;
883 for (;;) {
884 if (reduced_depth) {
885 bf = get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(h);
886 if (cmp < 0)
887 bf++;
888 else // cmp > 0
889 bf--;
890 if ((bf == -2) || (bf == 2)) {
891 h = balancecxx . . $ QTWTF/AVLTree#balance(4b6052a634f9db5d).(h);
892 bf = get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(h);
893 } else
894 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(h, bf);
895 reduced_depth = (bf == 0);
896 }
897 if (parent ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).())
898 break;
899 child = h;
900 h = parent;
901 cmp = branch[--depth] ? 1 : -1;
902 if (cmp < 0) {
903 parent = get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(h);
904 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(h, child);
905 } else {
906 parent = get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(h);
907 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(h, child);
908 }
909 }
910 abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. = h;
911 }
912
913 return rm;
914 }
915
916 template <class Abstractor, unsigned maxDepth, class BSet>
917 inline typename AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::handlecxx . . $ QTWTF/AVLTree#handle#
918 AVLTreecxx . . $ QTWTF/AVLTree#<Abstractor, maxDepth, BSet>::substcxx . . $ QTWTF/AVLTree#subst(4b6052a634f9db5d).(handlecxx . . $ QTWTF/AVLTree#handle# new_node)
919 {
920 handlecxx . . $ QTWTF/AVLTree#handle# h = abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root.;
921 handlecxx . . $ QTWTF/AVLTree#handle# parent = nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
922 int cmp, last_cmp;
923
924 /* Search for node already in tree with same key. */
925 for (;;) {
926 if (h ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).())
927 /* No node in tree with same key as new node. */
928 return nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).();
929 cmp = cmp_n_ncxx . . $ QTWTF/AVLTree#cmp_n_n(1864087b0395d431).(new_node, h);
930 if (cmp == 0)
931 /* Found the node to substitute new one for. */
932 break;
933 last_cmp = cmp;
934 parent = h;
935 h = cmp < 0 ? get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(h) : get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(h);
936 }
937
938 /* Copy tree housekeeping fields from node in tree to new node. */
939 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(new_node, get_ltcxx . . $ QTWTF/AVLTree#get_lt(4b6052a634f9db5d).(h));
940 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(new_node, get_gtcxx . . $ QTWTF/AVLTree#get_gt(4b6052a634f9db5d).(h));
941 set_bfcxx . . $ QTWTF/AVLTree#set_bf(78392a97a45a32d1).(new_node, get_bfcxx . . $ QTWTF/AVLTree#get_bf(9c6258a5dba002d0).(h));
942
943 if (parent ==cxx . . $ QTWTF/`operator==`(53f2ef751fb2c4b). nullcxx . . $ QTWTF/AVLTree#null(c7de0be71f349065).())
944 /* New node is also new root. */
945 abscxx . . $ QTWTF/AVLTree#abs..rootcxx . . $ QTWTF/AVLTree#abs_plus_root#root. = new_node;
946 else {
947 /* Make parent point to new node. */
948 if (last_cmp < 0)
949 set_ltcxx . . $ QTWTF/AVLTree#set_lt(52663f169aaad1de).(parent, new_node);
950 else
951 set_gtcxx . . $ QTWTF/AVLTree#set_gt(52663f169aaad1de).(parent, new_node);
952 }
953
954 return h;
955 }
956
957 }
958
959 #endif