-
Notifications
You must be signed in to change notification settings - Fork 401
/
tree.h
785 lines (725 loc) · 27.3 KB
/
tree.h
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
////////////////////////////////////////////////////////////////////////////////
//
// Visual Leak Detector - Red-black Tree Template
// Copyright (c) 2005-2006 Dan Moulding
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
// See COPYING.txt for the full terms of the GNU Lesser General Public License.
//
////////////////////////////////////////////////////////////////////////////////
#pragma once
#ifndef VLDBUILD
#error \
"This header should only be included by Visual Leak Detector when building it from source. \
Applications should never include this header."
#endif
#include "vldheap.h" // Provides internal new and delete operators.
#define TREE_DEFAULT_RESERVE 32 // By default, trees reserve enough space, in advance, for this many nodes.
////////////////////////////////////////////////////////////////////////////////
//
// The Tree Template Class
//
// This data structure is the internal data structure behind the lightweight
// STL-like container classes. This is a red-black tree which provides for
// fast insert, find, and erase operations.
//
// The binary tree nodes are overlaid on top of larger chunks of allocated
// memory (called chunks) which are arranged in a simple linked list. This
// allows the tree to grow (add nodes) dynamically without incurring a heap
// hit each time a new node is added.
//
// The Tree class provides member functions which make it easily adaptable to
// an STL-like interface so that it can be used as the backend for STL-like
// container classes.
//
template <typename T>
class Tree
{
public:
// This is a red-black tree.
enum color_e {
red,
black
};
// The node is the basic data structure which the tree is built from.
typedef struct node_s {
color_e color; // The node's color.
T key; // The node's value, by which nodes are sorted.
union {
struct node_s *left; // For nodes in the tree, the node's left child.
struct node_s *next; // For nodes in the free list, the next node on the free list.
};
struct node_s *parent; // The node's parent.
struct node_s *right; // The node's right child.
} node_t;
// Reserve capacity for the tree is allocated in large chunks with room for
// many nodes.
typedef struct chunk_s {
struct chunk_s *next; // Pointer to the next node in the chunk list.
node_t *nodes; // Pointer to an array (of variable size) where nodes are stored.
} chunk_t;
// Constructor
Tree ()
{
m_freelist = NULL;
InitializeCriticalSection(&m_lock);
m_nil.color = black;
m_nil.key = T();
m_nil.left = &m_nil;
m_nil.parent = &m_nil;
m_nil.right = &m_nil;
m_reserve = TREE_DEFAULT_RESERVE;
m_root = &m_nil;
m_store = NULL;
m_storetail = NULL;
}
// Copy constructor - The sole purpose of this constructor's existence is
// to ensure that trees are not being inadvertently copied.
Tree (const Tree& source)
{
assert(FALSE); // Do not make copies of trees!
}
// Destructor
~Tree ()
{
chunk_t *cur;
chunk_t *temp;
// Free all the chunks in the chunk list.
EnterCriticalSection(&m_lock);
cur = m_store;
while (cur != NULL) {
temp = cur;
cur = cur->next;
delete [] temp->nodes;
delete temp;
}
LeaveCriticalSection(&m_lock);
DeleteCriticalSection(&m_lock);
}
// operator = - Assignment operator. For efficiency, we want to avoid ever
// making copies of Trees (only pointer passing or reference passing
// should be performed). The sole purpose of this assignment operator is
// to ensure that no copying is being done inadvertently.
//
Tree<T>& operator = (const Tree<T> &other)
{
// Don't make copies of Trees!
assert(FALSE);
return *this;
}
// begin - Obtains a pointer to the first node (the node with the smallest
// key value) in the tree.
//
// Return Value:
//
// Returns a pointer to the first node in the tree.
//
typename Tree::node_t* begin () const
{
node_t *cur;
EnterCriticalSection(&m_lock);
if (m_root == &m_nil) {
LeaveCriticalSection(&m_lock);
return NULL;
}
cur = m_root;
while (cur->left != &m_nil) {
cur = cur->left;
}
LeaveCriticalSection(&m_lock);
return cur;
}
// erase - Erases the specified node from the tree. Note that this does
// not cause the key associated with the erased node to be freed. The
// caller is responsible for freeing any dynamically allocated memory
// associated with the key.
//
// - node (IN): Pointer to the node to erase from the tree.
//
// Return Value:
//
// None.
//
VOID erase (typename Tree::node_t *node)
{
node_t *child;
node_t *cur;
node_t *erasure;
node_t *sibling;
EnterCriticalSection(&m_lock);
if ((node->left == &m_nil) || (node->right == &m_nil)) {
// The node to be erased has less than two children. It can be directly
// removed from the tree.
erasure = node;
}
else {
// The node to be erased has two children. It can only be removed
// indirectly. The actual node will stay where it is, but it's contents
// will be replaced by it's in-order successor's contents. The successor
// node will then be erased. Find the successor.
erasure = node->right;
while (erasure->left != &m_nil) {
erasure = erasure->left;
}
}
// Select the child node which will replace the node to be erased.
if (erasure->left != &m_nil) {
child = erasure->left;
}
else {
child = erasure->right;
}
// Replace the node to be erased with the selected child.
child->parent = erasure->parent;
if (child->parent == &m_nil) {
// The root of the tree is being erased. The child becomes root.
m_root = child;
}
else {
if (erasure == erasure->parent->left) {
erasure->parent->left = child;
}
else {
erasure->parent->right = child;
}
}
if (erasure != node) {
// The node being erased from the tree is the successor of the actual
// node to be erased. Replace the contents of the node to be erased
// with the successor's contents.
node->key = erasure->key;
}
if (erasure->color == black) {
// The node being erased from the tree is black. Restructuring of the
// tree may be needed so that black-height is maintained.
cur = child;
while ((cur != m_root) && (cur->color == black)) {
if (cur == cur->parent->left) {
// Current node is a left child.
sibling = cur->parent->right;
if (sibling->color == red) {
// Sibling is red. Rotate sibling up and color it black.
sibling->color = black;
cur->parent->color = red;
_rotateleft(cur->parent);
sibling = cur->parent->right;
}
if ((sibling->left->color == black) && (sibling->right->color == black)) {
// Both of sibling's children are black. Color sibling red.
sibling->color = red;
cur = cur->parent;
}
else {
// At least one of sibling's children is red.
if (sibling->right->color == black) {
sibling->left->color = black;
sibling->color = red;
_rotateright(sibling);
sibling = cur->parent->right;
}
sibling->color = cur->parent->color;
cur->parent->color = black;
sibling->right->color = black;
_rotateleft(cur->parent);
cur = m_root;
}
}
else {
// Current node is a right child.
sibling = cur->parent->left;
if (sibling->color == red) {
// Sibling is red. Rotate sibling up and color it black.
sibling->color = black;
cur->parent->color = red;
_rotateright(cur->parent);
sibling = cur->parent->left;
}
if ((sibling->left->color == black) && (sibling->right->color == black)) {
// Both of sibling's children are black. Color sibling red.
sibling->color = red;
cur = cur->parent;
}
else {
// At least one of sibling's children is red.
if (sibling->left->color == black) {
sibling->right->color = black;
sibling->color = red;
_rotateleft(sibling);
sibling = cur->parent->left;
}
sibling->color = cur->parent->color;
cur->parent->color = black;
sibling->left->color = black;
_rotateright(cur->parent);
cur = m_root;
}
}
}
cur->color = black;
}
// Put the erased node onto the free list.
erasure->next = m_freelist;
m_freelist = erasure;
LeaveCriticalSection(&m_lock);
}
// erase - Erases the specified key from the tree. Note that this does
// not cause the key associated with the erased node to be freed. The
// caller is responsible for freeing any dynamically allocated memory
// associated with the key.
//
// - key (IN): The key to erase from the tree. This value is treated as
// the key for sorting within the tree. It must therefore be of a type
// which supports the "<" operator.
//
// Return Value:
//
// None.
//
VOID erase (const T &key)
{
node_t *node;
// Find the node to erase.
EnterCriticalSection(&m_lock);
node = m_root;
while (node != &m_nil) {
if (node->key < key) {
// Go right.
node = node->right;
}
else if (key < node->key) {
// Go left.
node = node->left;
}
else {
// Found it.
erase(node);
LeaveCriticalSection(&m_lock);
return;
}
}
LeaveCriticalSection(&m_lock);
// 'key' is not in the tree.
return;
}
// find - Obtains a pointer to the node corresponding to the specified key.
//
// - key (IN): The value to search for in the tree. This value is treated
// as the key for sorting within the tree. It must therefore be of a
// type which supports the "<" operator.
//
// Return Value:
//
// Returns a pointer to the node corresponding to the specified key. If
// the key is not in the tree, then "find" returns NULL.
//
typename Tree::node_t* find (const T &key) const
{
node_t *cur;
EnterCriticalSection(&m_lock);
cur = m_root;
while (cur != &m_nil) {
if (cur->key < key) {
// Go right.
cur = cur->right;
}
else if (key < cur->key) {
// Go left.
cur = cur->left;
}
else {
// Found it.
LeaveCriticalSection(&m_lock);
return cur;
}
}
LeaveCriticalSection(&m_lock);
// 'key' is not in the tree.
return NULL;
}
// insert - Inserts a new key into the tree.
//
// - key (IN): The key to insert into the tree. This value is treated as
// the key for sorting within the tree. It must therefore be of a type
// which supports the "<" operator.
//
// Return Value:
//
// Returns a pointer to the node corresponding to the newly inserted
// key. If an attempt is made to insert a key which is already in the
// tree, then NULL is returned and the new key is not inserted.
//
typename Tree::node_t* insert (const T &key)
{
node_t *cur;
node_t *node;
node_t *parent;
node_t *uncle;
EnterCriticalSection(&m_lock);
// Find the location where the new node should be inserted..
cur = m_root;
parent = &m_nil;
while (cur != &m_nil) {
parent = cur;
if (cur->key < key) {
// Go right.
cur = cur->right;
}
else if (key < cur->key) {
// Go left.
cur = cur->left;
}
else {
// Keys in the tree must be unique.
LeaveCriticalSection(&m_lock);
return NULL;
}
}
// Obtain a new node from the free list.
if (m_freelist == NULL) {
// Allocate additional storage.
reserve(m_reserve);
}
node = m_freelist;
m_freelist = m_freelist->next;
// Initialize the new node and insert it.
node->color = red;
node->key = key;
node->left = &m_nil;
node->parent = parent;
node->right = &m_nil;
if (parent == &m_nil) {
// The tree is empty. The new node becomes root.
m_root = node;
}
else {
if (parent->key < key) {
// New node is a right child.
parent->right = node;
}
else {
// New node is a left child.
parent->left = node;
}
}
// Rebalance and/or adjust the tree, if necessary.
cur = node;
while (cur->parent->color == red) {
// Double-red violation. Rebalancing/adjustment needed.
if (cur->parent == cur->parent->parent->left) {
// Parent is the left child. Uncle is the right child.
uncle = cur->parent->parent->right;
if (uncle->color == red) {
// Uncle is red. Recolor.
cur->parent->parent->color = red;
cur->parent->color = black;
uncle->color = black;
cur = cur->parent->parent;
}
else {
// Uncle is black. Restructure.
if (cur == cur->parent->right) {
cur = cur->parent;
_rotateleft(cur);
}
cur->parent->color = black;
cur->parent->parent->color = red;
_rotateright(cur->parent->parent);
}
}
else {
// Parent is the right child. Uncle is the left child.
uncle = cur->parent->parent->left;
if (uncle->color == red) {
// Uncle is red. Recolor.
cur->parent->parent->color = red;
cur->parent->color = black;
uncle->color = black;
cur = cur->parent->parent;
}
else {
// Uncle is black. Restructure.
if (cur == cur->parent->left) {
cur = cur->parent;
_rotateright(cur);
}
cur->parent->color = black;
cur->parent->parent->color = red;
_rotateleft(cur->parent->parent);
}
}
}
// The root node is always colored black.
m_root->color = black;
LeaveCriticalSection(&m_lock);
return node;
}
// next - Obtains a pointer to the in-order successor of the specified
// node.
//
// - node (IN): Pointer to the node whose in-order successor to retrieve.
//
// Return Value:
//
// Returns a pointer to the node's in-order successor. If the specified
// node corresponds to the largest value in the tree, then the specified
// node has no successor and "next" will return NULL.
//
typename Tree::node_t* next (typename Tree::node_t *node) const
{
node_t* cur;
if (node == NULL) {
return NULL;
}
EnterCriticalSection(&m_lock);
if (node->right != &m_nil) {
// 'node' has a right child. Successor is the far left node in
// the right subtree.
cur = node->right;
while (cur->left != &m_nil) {
cur = cur->left;
}
LeaveCriticalSection(&m_lock);
return cur;
}
else if (node->parent != &m_nil) {
// 'node' has no right child, but does have a parent.
if (node == node->parent->left) {
// 'node' is a left child; node's parent is successor.
LeaveCriticalSection(&m_lock);
return node->parent;
}
else {
// 'node' is a right child.
cur = node;
// Go up the tree until we find a parent to the right.
while (cur->parent != &m_nil) {
if (cur == cur->parent->right) {
cur = cur->parent;
continue;
}
else {
LeaveCriticalSection(&m_lock);
return cur->parent;
}
}
// There is no parent greater than 'node'. 'node' is the
// maximum node.
LeaveCriticalSection(&m_lock);
return NULL;
}
}
else {
// 'node' is root and root is the maximum node.
LeaveCriticalSection(&m_lock);
return NULL;
}
}
// prev - Obtains a pointer to the in-order predecessor of the specified
// node.
//
// - node (IN): Pointer to the node whose in-order predecessor to retrieve.
//
// Return Value:
//
// Returns a pointer to the node's in-order predecessor. If the specified
// node corresponds to the smallest value in the tree, then the specified
// node has no predecessor and "prev" will return NULL.
//
typename Tree::node_t* prev (typename Tree::node_t *node) const
{
node_t* cur;
if (node == NULL) {
return NULL;
}
EnterCriticalSection(&m_lock);
if (node->left != &m_nil) {
// 'node' has left child. Predecessor is the far right node in the
// left subtree.
cur = node->left;
while (cur->right != &m_nil) {
cur = cur->right;
}
LeaveCriticalSection(&m_lock);
return cur;
}
else if (node->parent != & m_nil) {
// 'node' has no left child, but does have a parent.
if (node == node->parent->right) {
// 'node' is a right child; node's parent is predecessor.
LeaveCriticalSection(&m_lock);
return node->parent;
}
else {
// 'node is a left child.
cur = node;
// Go up the tree until we find a parent to the left.
while (cur->parent != &m_nil) {
if (cur == cur->parent->left) {
cur = cur->parent;
continue;
}
else {
LeaveCriticalSection(&m_lock);
return cur->parent;
}
}
// There is no parent less than 'node'. 'node' is the minimum
// node.
LeaveCriticalSection(&m_lock);
return NULL;
}
}
else {
// 'node' is root and root is the minimum node.
LeaveCriticalSection(&m_lock);
return NULL;
}
}
// reserve - Reserves storage for a number of nodes in advance and/or sets
// the number of nodes for which the tree will automatically reserve
// storage when the tree needs to "grow" to accomodate new values being
// inserted into the tree. If this function is not called to set the
// reserve size to a specific value, then a pre-determined default value
// will be used. If this function is called when the tree currently has
// no reserve storage, then in addition to setting the tree's reserve
// value, it will also cause the tree to immediately reserve the
// specified amount of storage.
//
// - count (IN): The number of individual nodes' worth of storage to
// reserve.
//
// Return Value:
//
// Returns the previously defined reserve value.
//
UINT32 reserve (UINT32 count)
{
chunk_t *chunk;
UINT32 index;
UINT32 oldreserve = m_reserve;
if (count != m_reserve) {
if (count < 1) {
// Minimum reserve size is 1.
m_reserve = 1;
}
else {
m_reserve = count;
}
}
EnterCriticalSection(&m_lock);
if (m_freelist == NULL) {
// Allocate additional storage.
// Link a new chunk into the chunk list.
chunk = new Tree::chunk_t;
chunk->nodes = new Tree::node_s [m_reserve];
chunk->next = NULL;
if (m_store == NULL) {
m_store = chunk;
}
else {
m_storetail->next = chunk;
}
m_storetail = chunk;
// Link the individual nodes together to form a new free list.
for (index = 0; index < m_reserve - 1; index++) {
chunk->nodes[index].next = &chunk->nodes[index + 1];
}
chunk->nodes[index].next = NULL;
m_freelist = chunk->nodes;
}
LeaveCriticalSection(&m_lock);
return oldreserve;
}
private:
// _rotateleft: Rotates a pair of nodes counter-clockwise so that the parent
// node becomes the left child and the right child becomes the parent.
//
// - parent (IN): Pointer to the parent to rotate about.
//
// Return Value:
//
// None.
//
VOID _rotateleft (typename Tree::node_t *parent)
{
node_t *child = parent->right;
// Reassign the child's left subtree to the parent.
parent->right = child->left;
if (child->left != &m_nil) {
child->left->parent = parent;
}
// Reassign the child/parent relationship.
child->parent = parent->parent;
if (parent->parent == &m_nil) {
// The child becomes the new root node.
m_root = child;
}
else {
// Point the grandparent at the child.
if (parent == parent->parent->left) {
parent->parent->left = child;
}
else {
parent->parent->right = child;
}
}
child->left = parent;
parent->parent = child;
}
// _rotateright - Rotates a pair of nodes clockwise so that the parent node
// becomes the right child and the left child becomes the parent.
//
// - parent (IN): Pointer to the parent to rotate about.
//
// Return Value:
//
// None.
//
VOID _rotateright (typename Tree::node_t *parent)
{
node_t *child = parent->left;
// Reassign the child's right subtree to the parent.
parent->left = child->right;
if (child->right != &m_nil) {
child->right->parent = parent;
}
// Reassign the child/parent relationship.
child->parent = parent->parent;
if (parent->parent == &m_nil) {
// The child becomes the new root node.
m_root = child;
}
else {
// Point the grandparent at the child.
if (parent == parent->parent->left) {
parent->parent->left = child;
}
else {
parent->parent->right = child;
}
}
child->right = parent;
parent->parent = child;
}
// Private data members.
node_t *m_freelist; // Pointer to the list of free nodes (reserve storage).
mutable CRITICAL_SECTION m_lock; // Protects the tree's integrity against concurrent accesses.
node_t m_nil; // The tree's nil node. All leaf nodes point to this.
UINT32 m_reserve; // The size (in nodes) of the chunks of reserve storage.
node_t *m_root; // Pointer to the tree's root node.
chunk_t *m_store; // Pointer to the start of the chunk list.
chunk_t *m_storetail; // Pointer to the end of the chunk list.
};