1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QRBTREE_P_H
#define QRBTREE_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtCore/qglobal.h>
QT_BEGIN_NAMESPACE
template <class T>
struct QRBTree
{
struct Node
{
inline Node() : parent(0), left(0), right(0), red(true) { }
inline ~Node() {if (left) delete left; if (right) delete right;}
T data;
Node *parent;
Node *left;
Node *right;
bool red;
};
inline QRBTree() : root(0), freeList(0) { }
inline ~QRBTree();
inline void clear();
void attachBefore(Node *parent, Node *child);
void attachAfter(Node *parent, Node *child);
inline Node *front(Node *node) const;
inline Node *back(Node *node) const;
Node *next(Node *node) const;
Node *previous(Node *node) const;
inline void deleteNode(Node *&node);
inline Node *newNode();
// Return 1 if 'left' comes after 'right', 0 if equal, and -1 otherwise.
// 'left' and 'right' cannot be null.
int order(Node *left, Node *right);
inline bool validate() const;
private:
void rotateLeft(Node *node);
void rotateRight(Node *node);
void update(Node *node);
inline void attachLeft(Node *parent, Node *child);
inline void attachRight(Node *parent, Node *child);
int blackDepth(Node *top) const;
bool checkRedBlackProperty(Node *top) const;
void swapNodes(Node *n1, Node *n2);
void detach(Node *node);
// 'node' must be black. rebalance will reduce the depth of black nodes by one in the sibling tree.
void rebalance(Node *node);
public:
Node *root;
private:
Node *freeList;
};
template <class T>
inline QRBTree<T>::~QRBTree()
{
clear();
while (freeList) {
// Avoid recursively calling the destructor, as this list may become large.
Node *next = freeList->right;
freeList->right = 0;
delete freeList;
freeList = next;
}
}
template <class T>
inline void QRBTree<T>::clear()
{
if (root)
delete root;
root = 0;
}
template <class T>
void QRBTree<T>::rotateLeft(Node *node)
{
// | | //
// N B //
// / \ / \ //
// A B ---> N D //
// / \ / \ //
// C D A C //
Node *&ref = (node->parent ? (node == node->parent->left ? node->parent->left : node->parent->right) : root);
ref = node->right;
node->right->parent = node->parent;
// : //
// N //
// / :| //
// A B //
// / \ //
// C D //
node->right = ref->left;
if (ref->left)
ref->left->parent = node;
// : | //
// N B //
// / \ : \ //
// A C D //
ref->left = node;
node->parent = ref;
// | //
// B //
// / \ //
// N D //
// / \ //
// A C //
}
template <class T>
void QRBTree<T>::rotateRight(Node *node)
{
// | | //
// N A //
// / \ / \ //
// A B ---> C N //
// / \ / \ //
// C D D B //
Node *&ref = (node->parent ? (node == node->parent->left ? node->parent->left : node->parent->right) : root);
ref = node->left;
node->left->parent = node->parent;
node->left = ref->right;
if (ref->right)
ref->right->parent = node;
ref->right = node;
node->parent = ref;
}
template <class T>
void QRBTree<T>::update(Node *node) // call this after inserting a node
{
for (;;) {
Node *parent = node->parent;
// if the node is the root, color it black
if (!parent) {
node->red = false;
return;
}
// if the parent is black, the node can be left red
if (!parent->red)
return;
// at this point, the parent is red and cannot be the root
Node *grandpa = parent->parent;
Q_ASSERT(grandpa);
Node *uncle = (parent == grandpa->left ? grandpa->right : grandpa->left);
if (uncle && uncle->red) {
// grandpa's black, parent and uncle are red.
// let parent and uncle be black, grandpa red and recursively update grandpa.
Q_ASSERT(!grandpa->red);
parent->red = false;
uncle->red = false;
grandpa->red = true;
node = grandpa;
continue;
}
// at this point, uncle is black
if (node == parent->right && parent == grandpa->left)
rotateLeft(node = parent);
else if (node == parent->left && parent == grandpa->right)
rotateRight(node = parent);
parent = node->parent;
if (parent == grandpa->left) {
rotateRight(grandpa);
parent->red = false;
grandpa->red = true;
} else {
rotateLeft(grandpa);
parent->red = false;
grandpa->red = true;
}
return;
}
}
template <class T>
inline void QRBTree<T>::attachLeft(Node *parent, Node *child)
{
Q_ASSERT(!parent->left);
parent->left = child;
child->parent = parent;
update(child);
}
template <class T>
inline void QRBTree<T>::attachRight(Node *parent, Node *child)
{
Q_ASSERT(!parent->right);
parent->right = child;
child->parent = parent;
update(child);
}
template <class T>
void QRBTree<T>::attachBefore(Node *parent, Node *child)
{
if (!root)
update(root = child);
else if (!parent)
attachRight(back(root), child);
else if (parent->left)
attachRight(back(parent->left), child);
else
attachLeft(parent, child);
}
template <class T>
void QRBTree<T>::attachAfter(Node *parent, Node *child)
{
if (!root)
update(root = child);
else if (!parent)
attachLeft(front(root), child);
else if (parent->right)
attachLeft(front(parent->right), child);
else
attachRight(parent, child);
}
template <class T>
void QRBTree<T>::swapNodes(Node *n1, Node *n2)
{
// Since iterators must not be invalidated, it is not sufficient to only swap the data.
if (n1->parent == n2) {
n1->parent = n2->parent;
n2->parent = n1;
} else if (n2->parent == n1) {
n2->parent = n1->parent;
n1->parent = n2;
} else {
qSwap(n1->parent, n2->parent);
}
qSwap(n1->left, n2->left);
qSwap(n1->right, n2->right);
qSwap(n1->red, n2->red);
if (n1->parent) {
if (n1->parent->left == n2)
n1->parent->left = n1;
else
n1->parent->right = n1;
} else {
root = n1;
}
if (n2->parent) {
if (n2->parent->left == n1)
n2->parent->left = n2;
else
n2->parent->right = n2;
} else {
root = n2;
}
if (n1->left)
n1->left->parent = n1;
if (n1->right)
n1->right->parent = n1;
if (n2->left)
n2->left->parent = n2;
if (n2->right)
n2->right->parent = n2;
}
template <class T>
void QRBTree<T>::detach(Node *node) // call this before removing a node.
{
if (node->right)
swapNodes(node, front(node->right));
Node *child = (node->left ? node->left : node->right);
if (!node->red) {
if (child && child->red)
child->red = false;
else
rebalance(node);
}
Node *&ref = (node->parent ? (node == node->parent->left ? node->parent->left : node->parent->right) : root);
ref = child;
if (child)
child->parent = node->parent;
node->left = node->right = node->parent = 0;
}
// 'node' must be black. rebalance will reduce the depth of black nodes by one in the sibling tree.
template <class T>
void QRBTree<T>::rebalance(Node *node)
{
Q_ASSERT(!node->red);
for (;;) {
if (!node->parent)
return;
// at this point, node is not a parent, it is black, thus it must have a sibling.
Node *sibling = (node == node->parent->left ? node->parent->right : node->parent->left);
Q_ASSERT(sibling);
if (sibling->red) {
sibling->red = false;
node->parent->red = true;
if (node == node->parent->left)
rotateLeft(node->parent);
else
rotateRight(node->parent);
sibling = (node == node->parent->left ? node->parent->right : node->parent->left);
Q_ASSERT(sibling);
}
// at this point, the sibling is black.
Q_ASSERT(!sibling->red);
if ((!sibling->left || !sibling->left->red) && (!sibling->right || !sibling->right->red)) {
bool parentWasRed = node->parent->red;
sibling->red = true;
node->parent->red = false;
if (parentWasRed)
return;
node = node->parent;
continue;
}
// at this point, at least one of the sibling's children is red.
if (node == node->parent->left) {
if (!sibling->right || !sibling->right->red) {
Q_ASSERT(sibling->left);
sibling->red = true;
sibling->left->red = false;
rotateRight(sibling);
sibling = sibling->parent;
Q_ASSERT(sibling);
}
sibling->red = node->parent->red;
node->parent->red = false;
Q_ASSERT(sibling->right->red);
sibling->right->red = false;
rotateLeft(node->parent);
} else {
if (!sibling->left || !sibling->left->red) {
Q_ASSERT(sibling->right);
sibling->red = true;
sibling->right->red = false;
rotateLeft(sibling);
sibling = sibling->parent;
Q_ASSERT(sibling);
}
sibling->red = node->parent->red;
node->parent->red = false;
Q_ASSERT(sibling->left->red);
sibling->left->red = false;
rotateRight(node->parent);
}
return;
}
}
template <class T>
inline typename QRBTree<T>::Node *QRBTree<T>::front(Node *node) const
{
while (node->left)
node = node->left;
return node;
}
template <class T>
inline typename QRBTree<T>::Node *QRBTree<T>::back(Node *node) const
{
while (node->right)
node = node->right;
return node;
}
template <class T>
typename QRBTree<T>::Node *QRBTree<T>::next(Node *node) const
{
if (node->right)
return front(node->right);
while (node->parent && node == node->parent->right)
node = node->parent;
return node->parent;
}
template <class T>
typename QRBTree<T>::Node *QRBTree<T>::previous(Node *node) const
{
if (node->left)
return back(node->left);
while (node->parent && node == node->parent->left)
node = node->parent;
return node->parent;
}
template <class T>
int QRBTree<T>::blackDepth(Node *top) const
{
if (!top)
return 0;
int leftDepth = blackDepth(top->left);
int rightDepth = blackDepth(top->right);
if (leftDepth != rightDepth)
return -1;
if (!top->red)
++leftDepth;
return leftDepth;
}
template <class T>
bool QRBTree<T>::checkRedBlackProperty(Node *top) const
{
if (!top)
return true;
if (top->left && !checkRedBlackProperty(top->left))
return false;
if (top->right && !checkRedBlackProperty(top->right))
return false;
return !(top->red && ((top->left && top->left->red) || (top->right && top->right->red)));
}
template <class T>
inline bool QRBTree<T>::validate() const
{
return checkRedBlackProperty(root) && blackDepth(root) != -1;
}
template <class T>
inline void QRBTree<T>::deleteNode(Node *&node)
{
Q_ASSERT(node);
detach(node);
node->right = freeList;
freeList = node;
node = 0;
}
template <class T>
inline typename QRBTree<T>::Node *QRBTree<T>::newNode()
{
if (freeList) {
Node *node = freeList;
freeList = freeList->right;
node->parent = node->left = node->right = 0;
node->red = true;
return node;
}
return new Node;
}
// Return 1 if 'left' comes after 'right', 0 if equal, and -1 otherwise.
// 'left' and 'right' cannot be null.
template <class T>
int QRBTree<T>::order(Node *left, Node *right)
{
Q_ASSERT(left && right);
if (left == right)
return 0;
QVector<Node *> leftAncestors;
QVector<Node *> rightAncestors;
while (left) {
leftAncestors.push_back(left);
left = left->parent;
}
while (right) {
rightAncestors.push_back(right);
right = right->parent;
}
Q_ASSERT(leftAncestors.back() == root && rightAncestors.back() == root);
while (!leftAncestors.empty() && !rightAncestors.empty() && leftAncestors.back() == rightAncestors.back()) {
leftAncestors.pop_back();
rightAncestors.pop_back();
}
if (!leftAncestors.empty())
return (leftAncestors.back() == leftAncestors.back()->parent->left ? -1 : 1);
if (!rightAncestors.empty())
return (rightAncestors.back() == rightAncestors.back()->parent->right ? -1 : 1);
// The code should never reach this point.
Q_ASSERT(!leftAncestors.empty() || !rightAncestors.empty());
return 0;
}
QT_END_NAMESPACE
#endif
|