/
rbtree.go
1066 lines (981 loc) · 25.8 KB
/
rbtree.go
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package rbtree
import (
"fmt"
"math"
"os"
"sync"
"github.com/gogo/protobuf/sortkeys"
"gopkg.in/src-d/go-git.v4/utils/binary"
)
//
// Public definitions
//
// Item is the object stored in each tree node.
type Item struct {
Key uint32
Value uint32
}
// Allocator is the allocator for nodes in a RBTree.
type Allocator struct {
HibernationThreshold int
storage []node
gaps map[uint32]bool
hibernatedData [7][]byte
hibernatedStorageLen int
hibernatedGapsLen int
}
// NewAllocator creates a new allocator for RBTree's nodes.
func NewAllocator() *Allocator {
return &Allocator{
storage: []node{},
gaps: map[uint32]bool{},
}
}
// Size returns the currently allocated size.
func (allocator Allocator) Size() int {
return len(allocator.storage)
}
// Used returns the number of nodes contained in the allocator.
func (allocator Allocator) Used() int {
if allocator.storage == nil {
panic("hibernated allocators cannot be used")
}
return len(allocator.storage) - len(allocator.gaps)
}
// Clone copies an existing RBTree allocator.
func (allocator Allocator) Clone() *Allocator {
if allocator.storage == nil {
panic("cannot clone a hibernated allocator")
}
newAllocator := &Allocator{
HibernationThreshold: allocator.HibernationThreshold,
storage: make([]node, len(allocator.storage), cap(allocator.storage)),
gaps: map[uint32]bool{},
}
copy(newAllocator.storage, allocator.storage)
for key, val := range allocator.gaps {
newAllocator.gaps[key] = val
}
return newAllocator
}
// Hibernate compresses the allocated memory.
func (allocator *Allocator) Hibernate() {
if allocator.hibernatedStorageLen > 0 {
panic("cannot hibernate an already hibernated Allocator")
}
if len(allocator.storage) < allocator.HibernationThreshold {
return
}
allocator.hibernatedStorageLen = len(allocator.storage)
if allocator.hibernatedStorageLen == 0 {
return
}
buffers := [6][]uint32{}
for i := 0; i < len(buffers); i++ {
buffers[i] = make([]uint32, len(allocator.storage))
}
// we deinterleave to achieve a better compression ratio
for i, n := range allocator.storage {
buffers[0][i] = n.item.Key
buffers[1][i] = n.item.Value
buffers[2][i] = n.left
buffers[3][i] = n.parent
buffers[4][i] = n.right
if n.color {
buffers[5][i] = 1
}
}
allocator.storage = nil
wg := &sync.WaitGroup{}
wg.Add(len(buffers) + 1)
for i, buffer := range buffers {
go func(i int, buffer []uint32) {
allocator.hibernatedData[i] = CompressUInt32Slice(buffer)
buffers[i] = nil
wg.Done()
}(i, buffer)
}
// compress gaps
go func() {
if len(allocator.gaps) > 0 {
allocator.hibernatedGapsLen = len(allocator.gaps)
gapsBuffer := make([]uint32, len(allocator.gaps))
i := 0
for key := range allocator.gaps {
gapsBuffer[i] = key
i++
}
sortkeys.Uint32s(gapsBuffer)
allocator.hibernatedData[len(buffers)] = CompressUInt32Slice(gapsBuffer)
}
allocator.gaps = nil
wg.Done()
}()
wg.Wait()
}
// Boot performs the opposite of Hibernate() - decompresses and restores the allocated memory.
func (allocator *Allocator) Boot() {
if allocator.hibernatedStorageLen == 0 {
// not hibernated
return
}
if allocator.hibernatedData[0] == nil {
panic("cannot boot a serialized Allocator")
}
allocator.gaps = map[uint32]bool{}
buffers := [6][]uint32{}
wg := &sync.WaitGroup{}
wg.Add(len(buffers) + 1)
for i := 0; i < len(buffers); i++ {
go func(i int) {
buffers[i] = make([]uint32, allocator.hibernatedStorageLen)
DecompressUInt32Slice(allocator.hibernatedData[i], buffers[i])
allocator.hibernatedData[i] = nil
wg.Done()
}(i)
}
go func() {
if allocator.hibernatedGapsLen > 0 {
gapData := allocator.hibernatedData[len(buffers)]
buffer := make([]uint32, allocator.hibernatedGapsLen)
DecompressUInt32Slice(gapData, buffer)
for _, key := range buffer {
allocator.gaps[key] = true
}
allocator.hibernatedData[len(buffers)] = nil
allocator.hibernatedGapsLen = 0
}
wg.Done()
}()
wg.Wait()
allocator.storage = make([]node, allocator.hibernatedStorageLen, (allocator.hibernatedStorageLen*3)/2)
for i := range allocator.storage {
n := &allocator.storage[i]
n.item.Key = buffers[0][i]
n.item.Value = buffers[1][i]
n.left = buffers[2][i]
n.parent = buffers[3][i]
n.right = buffers[4][i]
n.color = buffers[5][i] > 0
}
allocator.hibernatedStorageLen = 0
}
// Serialize writes the hibernated allocator on disk.
func (allocator *Allocator) Serialize(path string) error {
if allocator.storage != nil {
panic("serialization requires the hibernated state")
}
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
err = binary.WriteVariableWidthInt(file, int64(allocator.hibernatedStorageLen))
if err != nil {
return err
}
err = binary.WriteVariableWidthInt(file, int64(allocator.hibernatedGapsLen))
if err != nil {
return err
}
for i, hse := range allocator.hibernatedData {
err = binary.WriteVariableWidthInt(file, int64(len(hse)))
if err != nil {
return err
}
_, err = file.Write(hse)
if err != nil {
return err
}
allocator.hibernatedData[i] = nil
}
return nil
}
// Deserialize reads a hibernated allocator from disk.
func (allocator *Allocator) Deserialize(path string) error {
if allocator.storage != nil {
panic("deserialization requires the hibernated state")
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
x, err := binary.ReadVariableWidthInt(file)
if err != nil {
return err
}
allocator.hibernatedStorageLen = int(x)
x, err = binary.ReadVariableWidthInt(file)
if err != nil {
return err
}
allocator.hibernatedGapsLen = int(x)
for i := range allocator.hibernatedData {
x, err = binary.ReadVariableWidthInt(file)
if err != nil {
return err
}
allocator.hibernatedData[i] = make([]byte, int(x))
n, err := file.Read(allocator.hibernatedData[i])
if err != nil {
return err
}
if n != int(x) {
return fmt.Errorf("incomplete read %d: %d instead of %d", i, n, x)
}
}
return nil
}
func (allocator *Allocator) malloc() uint32 {
if allocator.storage == nil {
panic("hibernated allocators cannot be used")
}
if len(allocator.gaps) > 0 {
var key uint32
for key = range allocator.gaps {
break
}
delete(allocator.gaps, key)
return key
}
n := len(allocator.storage)
if n == 0 {
// zero is reserved
allocator.storage = append(allocator.storage, node{})
n = 1
}
if n == negativeLimitNode-1 {
// math.MaxUint32 is reserved
panic("the size of my RBTree allocator has reached the maximum value for uint32, sorry")
}
doAssert(n < negativeLimitNode)
allocator.storage = append(allocator.storage, node{})
return uint32(n)
}
func (allocator *Allocator) free(n uint32) {
if allocator.storage == nil {
panic("hibernated allocators cannot be used")
}
if n == 0 {
panic("node #0 is special and cannot be deallocated")
}
_, exists := allocator.gaps[n]
doAssert(!exists)
allocator.storage[n] = node{}
allocator.gaps[n] = true
}
// RBTree is a red-black tree with an API similar to C++ STL's.
//
// The implementation is inspired (read: stolen) from:
// http://en.literateprograms.org/Red-black_tree_(C)#chunk use:private function prototypes.
//
// The code was optimized for the simple integer types of Key and Value.
// The code was further optimized for using allocators.
// Credits: Yaz Saito.
type RBTree struct {
// Root of the tree
root uint32
// The minimum and maximum nodes under the tree.
minNode, maxNode uint32
// Number of nodes under root, including the root
count int32
// Nodes allocator
allocator *Allocator
}
// NewRBTree creates a new red-black binary tree.
func NewRBTree(allocator *Allocator) *RBTree {
return &RBTree{allocator: allocator}
}
func (tree RBTree) storage() []node {
return tree.allocator.storage
}
// Allocator returns the bound nodes allocator.
func (tree RBTree) Allocator() *Allocator {
return tree.allocator
}
// Len returns the number of elements in the tree.
func (tree RBTree) Len() int {
return int(tree.count)
}
// CloneShallow performs a shallow copy of the tree - the nodes are assumed to already exist in the allocator.
func (tree RBTree) CloneShallow(allocator *Allocator) *RBTree {
clone := tree
clone.allocator = allocator
return &clone
}
// CloneDeep performs a deep copy of the tree - the nodes are created from scratch.
func (tree RBTree) CloneDeep(allocator *Allocator) *RBTree {
clone := &RBTree{
count: tree.count,
allocator: allocator,
}
nodeMap := map[uint32]uint32{}
originStorage := tree.storage()
for iter := tree.Min(); !iter.Limit(); iter = iter.Next() {
newNode := allocator.malloc()
cloneNode := &allocator.storage[newNode]
cloneNode.item = *iter.Item()
cloneNode.color = originStorage[iter.node].color
nodeMap[iter.node] = newNode
}
cloneStorage := allocator.storage
for iter := tree.Min(); !iter.Limit(); iter = iter.Next() {
cloneNode := &cloneStorage[nodeMap[iter.node]]
originNode := originStorage[iter.node]
cloneNode.left = nodeMap[originNode.left]
cloneNode.right = nodeMap[originNode.right]
cloneNode.parent = nodeMap[originNode.parent]
}
clone.root = nodeMap[tree.root]
clone.minNode = nodeMap[tree.minNode]
clone.maxNode = nodeMap[tree.maxNode]
return clone
}
// Erase removes all the nodes from the tree.
func (tree *RBTree) Erase() {
nodes := make([]uint32, 0, tree.count)
for iter := tree.Min(); !iter.Limit(); iter = iter.Next() {
nodes = append(nodes, iter.node)
}
for _, node := range nodes {
tree.allocator.free(node)
}
tree.root = 0
tree.minNode = 0
tree.maxNode = 0
tree.count = 0
}
// Get is a convenience function for finding an element equal to Key. Returns
// nil if not found.
func (tree RBTree) Get(key uint32) *uint32 {
n, exact := tree.findGE(key)
if exact {
return &tree.storage()[n].item.Value
}
return nil
}
// Min creates an iterator that points to the minimum item in the tree.
// If the tree is empty, returns Limit()
func (tree *RBTree) Min() Iterator {
return Iterator{tree, tree.minNode}
}
// Max creates an iterator that points at the maximum item in the tree.
//
// If the tree is empty, returns NegativeLimit().
func (tree *RBTree) Max() Iterator {
if tree.maxNode == 0 {
return Iterator{tree, negativeLimitNode}
}
return Iterator{tree, tree.maxNode}
}
// Limit creates an iterator that points beyond the maximum item in the tree.
func (tree *RBTree) Limit() Iterator {
return Iterator{tree, 0}
}
// NegativeLimit creates an iterator that points before the minimum item in the tree.
func (tree *RBTree) NegativeLimit() Iterator {
return Iterator{tree, negativeLimitNode}
}
// FindGE finds the smallest element N such that N >= Key, and returns the
// iterator pointing to the element. If no such element is found,
// returns tree.Limit().
func (tree *RBTree) FindGE(key uint32) Iterator {
n, _ := tree.findGE(key)
return Iterator{tree, n}
}
// FindLE finds the largest element N such that N <= Key, and returns the
// iterator pointing to the element. If no such element is found,
// returns iter.NegativeLimit().
func (tree *RBTree) FindLE(key uint32) Iterator {
n, exact := tree.findGE(key)
if exact {
return Iterator{tree, n}
}
if n != 0 {
return Iterator{tree, doPrev(n, tree.storage())}
}
if tree.maxNode == 0 {
return Iterator{tree, negativeLimitNode}
}
return Iterator{tree, tree.maxNode}
}
// Insert an item. If the item is already in the tree, do nothing and
// return false. Else return true.
func (tree *RBTree) Insert(item Item) (bool, Iterator) {
// TODO: delay creating n until it is found to be inserted
n := tree.doInsert(item)
if n == 0 {
return false, Iterator{}
}
alloc := tree.storage()
insN := n
alloc[n].color = red
for true {
// Case 1: N is at the root
if alloc[n].parent == 0 {
alloc[n].color = black
break
}
// Case 2: The parent is black, so the tree already
// satisfies the RB properties
if alloc[alloc[n].parent].color == black {
break
}
// Case 3: parent and uncle are both red.
// Then paint both black and make grandparent red.
grandparent := alloc[alloc[n].parent].parent
var uncle uint32
if isLeftChild(alloc[n].parent, alloc) {
uncle = alloc[grandparent].right
} else {
uncle = alloc[grandparent].left
}
if uncle != 0 && alloc[uncle].color == red {
alloc[alloc[n].parent].color = black
alloc[uncle].color = black
alloc[grandparent].color = red
n = grandparent
continue
}
// Case 4: parent is red, uncle is black (1)
if isRightChild(n, alloc) && isLeftChild(alloc[n].parent, alloc) {
tree.rotateLeft(alloc[n].parent)
n = alloc[n].left
continue
}
if isLeftChild(n, alloc) && isRightChild(alloc[n].parent, alloc) {
tree.rotateRight(alloc[n].parent)
n = alloc[n].right
continue
}
// Case 5: parent is read, uncle is black (2)
alloc[alloc[n].parent].color = black
alloc[grandparent].color = red
if isLeftChild(n, alloc) {
tree.rotateRight(grandparent)
} else {
tree.rotateLeft(grandparent)
}
break
}
return true, Iterator{tree, insN}
}
// DeleteWithKey deletes an item with the given Key. Returns true iff the item was
// found.
func (tree *RBTree) DeleteWithKey(key uint32) bool {
n, exact := tree.findGE(key)
if exact {
tree.doDelete(n)
return true
}
return false
}
// DeleteWithIterator deletes the current item.
//
// REQUIRES: !iter.Limit() && !iter.NegativeLimit()
func (tree *RBTree) DeleteWithIterator(iter Iterator) {
doAssert(!iter.Limit() && !iter.NegativeLimit())
tree.doDelete(iter.node)
}
// Iterator allows scanning tree elements in sort order.
//
// Iterator invalidation rule is the same as C++ std::map<>'s. That
// is, if you delete the element that an iterator points to, the
// iterator becomes invalid. For other operation types, the iterator
// remains valid.
type Iterator struct {
tree *RBTree
node uint32
}
// Equal checks for the underlying nodes equality.
func (iter Iterator) Equal(other Iterator) bool {
return iter.node == other.node
}
// Limit checks if the iterator points beyond the max element in the tree.
func (iter Iterator) Limit() bool {
return iter.node == 0
}
// Min checks if the iterator points to the minimum element in the tree.
func (iter Iterator) Min() bool {
return iter.node == iter.tree.minNode
}
// Max checks if the iterator points to the maximum element in the tree.
func (iter Iterator) Max() bool {
return iter.node == iter.tree.maxNode
}
// NegativeLimit checks if the iterator points before the minimum element in the tree.
func (iter Iterator) NegativeLimit() bool {
return iter.node == negativeLimitNode
}
// Item returns the current element. Allows mutating the node
// (key to be changed with care!).
//
// The result is nil if iter.Limit() || iter.NegativeLimit().
func (iter Iterator) Item() *Item {
if iter.Limit() || iter.NegativeLimit() {
return nil
}
return &iter.tree.storage()[iter.node].item
}
// Next creates a new iterator that points to the successor of the current element.
//
// REQUIRES: !iter.Limit()
func (iter Iterator) Next() Iterator {
doAssert(!iter.Limit())
if iter.NegativeLimit() {
return Iterator{iter.tree, iter.tree.minNode}
}
return Iterator{iter.tree, doNext(iter.node, iter.tree.storage())}
}
// Prev creates a new iterator that points to the predecessor of the current
// node.
//
// REQUIRES: !iter.NegativeLimit()
func (iter Iterator) Prev() Iterator {
doAssert(!iter.NegativeLimit())
if !iter.Limit() {
return Iterator{iter.tree, doPrev(iter.node, iter.tree.storage())}
}
if iter.tree.maxNode == 0 {
return Iterator{iter.tree, negativeLimitNode}
}
return Iterator{iter.tree, iter.tree.maxNode}
}
func doAssert(b bool) {
if !b {
panic("rbtree internal assertion failed")
}
}
const (
red = false
black = true
negativeLimitNode = math.MaxUint32
)
type node struct {
item Item
parent, left, right uint32
color bool // black or red
}
//
// Internal node attribute accessors
//
func getColor(n uint32, allocator []node) bool {
if n == 0 {
return black
}
return allocator[n].color
}
func isLeftChild(n uint32, allocator []node) bool {
return n == allocator[allocator[n].parent].left
}
func isRightChild(n uint32, allocator []node) bool {
return n == allocator[allocator[n].parent].right
}
func sibling(n uint32, allocator []node) uint32 {
doAssert(allocator[n].parent != 0)
if isLeftChild(n, allocator) {
return allocator[allocator[n].parent].right
}
return allocator[allocator[n].parent].left
}
// Return the minimum node that's larger than N. Return nil if no such
// node is found.
func doNext(n uint32, allocator []node) uint32 {
if allocator[n].right != 0 {
m := allocator[n].right
for allocator[m].left != 0 {
m = allocator[m].left
}
return m
}
for n != 0 {
p := allocator[n].parent
if p == 0 {
return 0
}
if isLeftChild(n, allocator) {
return p
}
n = p
}
return 0
}
// Return the maximum node that's smaller than N. Return nil if no
// such node is found.
func doPrev(n uint32, allocator []node) uint32 {
if allocator[n].left != 0 {
return maxPredecessor(n, allocator)
}
for n != 0 {
p := allocator[n].parent
if p == 0 {
break
}
if isRightChild(n, allocator) {
return p
}
n = p
}
return negativeLimitNode
}
// Return the predecessor of "n".
func maxPredecessor(n uint32, allocator []node) uint32 {
doAssert(allocator[n].left != 0)
m := allocator[n].left
for allocator[m].right != 0 {
m = allocator[m].right
}
return m
}
//
// Tree methods
//
//
// Private methods
//
func (tree *RBTree) recomputeMinNode() {
alloc := tree.storage()
tree.minNode = tree.root
if tree.minNode != 0 {
for alloc[tree.minNode].left != 0 {
tree.minNode = alloc[tree.minNode].left
}
}
}
func (tree *RBTree) recomputeMaxNode() {
alloc := tree.storage()
tree.maxNode = tree.root
if tree.maxNode != 0 {
for alloc[tree.maxNode].right != 0 {
tree.maxNode = alloc[tree.maxNode].right
}
}
}
func (tree *RBTree) maybeSetMinNode(n uint32) {
alloc := tree.storage()
if tree.minNode == 0 {
tree.minNode = n
tree.maxNode = n
} else if alloc[n].item.Key < alloc[tree.minNode].item.Key {
tree.minNode = n
}
}
func (tree *RBTree) maybeSetMaxNode(n uint32) {
alloc := tree.storage()
if tree.maxNode == 0 {
tree.minNode = n
tree.maxNode = n
} else if alloc[n].item.Key > alloc[tree.maxNode].item.Key {
tree.maxNode = n
}
}
// Try inserting "item" into the tree. Return nil if the item is
// already in the tree. Otherwise return a new (leaf) node.
func (tree *RBTree) doInsert(item Item) uint32 {
if tree.root == 0 {
n := tree.allocator.malloc()
tree.storage()[n].item = item
tree.root = n
tree.minNode = n
tree.maxNode = n
tree.count++
return n
}
parent := tree.root
storage := tree.storage()
for true {
parentNode := storage[parent]
comp := int(item.Key) - int(parentNode.item.Key)
if comp == 0 {
return 0
} else if comp < 0 {
if parentNode.left == 0 {
n := tree.allocator.malloc()
storage = tree.storage()
newNode := &storage[n]
newNode.item = item
newNode.parent = parent
storage[parent].left = n
tree.count++
tree.maybeSetMinNode(n)
return n
}
parent = parentNode.left
} else {
if parentNode.right == 0 {
n := tree.allocator.malloc()
storage = tree.storage()
newNode := &storage[n]
newNode.item = item
newNode.parent = parent
storage[parent].right = n
tree.count++
tree.maybeSetMaxNode(n)
return n
}
parent = parentNode.right
}
}
panic("should not reach here")
}
// Find a node whose item >= Key. The 2nd return Value is true iff the
// node.item==Key. Returns (nil, false) if all nodes in the tree are <
// Key.
func (tree RBTree) findGE(key uint32) (uint32, bool) {
alloc := tree.storage()
n := tree.root
for true {
if n == 0 {
return 0, false
}
comp := int(key) - int(alloc[n].item.Key)
if comp == 0 {
return n, true
} else if comp < 0 {
if alloc[n].left != 0 {
n = alloc[n].left
} else {
return n, false
}
} else {
if alloc[n].right != 0 {
n = alloc[n].right
} else {
succ := doNext(n, alloc)
if succ == 0 {
return 0, false
}
return succ, key == alloc[succ].item.Key
}
}
}
panic("should not reach here")
}
// Delete N from the tree.
func (tree *RBTree) doDelete(n uint32) {
alloc := tree.storage()
if alloc[n].left != 0 && alloc[n].right != 0 {
pred := maxPredecessor(n, alloc)
tree.swapNodes(n, pred)
}
doAssert(alloc[n].left == 0 || alloc[n].right == 0)
child := alloc[n].right
if child == 0 {
child = alloc[n].left
}
if alloc[n].color == black {
alloc[n].color = getColor(child, alloc)
tree.deleteCase1(n)
}
tree.replaceNode(n, child)
if alloc[n].parent == 0 && child != 0 {
alloc[child].color = black
}
tree.allocator.free(n)
tree.count--
if tree.count == 0 {
tree.minNode = 0
tree.maxNode = 0
} else {
if tree.minNode == n {
tree.recomputeMinNode()
}
if tree.maxNode == n {
tree.recomputeMaxNode()
}
}
}
// Move n to the pred's place, and vice versa
//
func (tree *RBTree) swapNodes(n, pred uint32) {
doAssert(pred != n)
alloc := tree.storage()
isLeft := isLeftChild(pred, alloc)
tmp := alloc[pred]
tree.replaceNode(n, pred)
alloc[pred].color = alloc[n].color
if tmp.parent == n {
// swap the positions of n and pred
if isLeft {
alloc[pred].left = n
alloc[pred].right = alloc[n].right
if alloc[pred].right != 0 {
alloc[alloc[pred].right].parent = pred
}
} else {
alloc[pred].left = alloc[n].left
if alloc[pred].left != 0 {
alloc[alloc[pred].left].parent = pred
}
alloc[pred].right = n
}
alloc[n].item = tmp.item
alloc[n].parent = pred
alloc[n].left = tmp.left
if alloc[n].left != 0 {
alloc[alloc[n].left].parent = n
}
alloc[n].right = tmp.right
if alloc[n].right != 0 {
alloc[alloc[n].right].parent = n
}
} else {
alloc[pred].left = alloc[n].left
if alloc[pred].left != 0 {
alloc[alloc[pred].left].parent = pred
}
alloc[pred].right = alloc[n].right
if alloc[pred].right != 0 {
alloc[alloc[pred].right].parent = pred
}
if isLeft {
alloc[tmp.parent].left = n
} else {
alloc[tmp.parent].right = n
}
alloc[n].item = tmp.item
alloc[n].parent = tmp.parent
alloc[n].left = tmp.left
if alloc[n].left != 0 {
alloc[alloc[n].left].parent = n
}
alloc[n].right = tmp.right
if alloc[n].right != 0 {
alloc[alloc[n].right].parent = n
}
}
alloc[n].color = tmp.color
}
func (tree *RBTree) deleteCase1(n uint32) {
alloc := tree.storage()
for true {
if alloc[n].parent != 0 {
if getColor(sibling(n, alloc), alloc) == red {
alloc[alloc[n].parent].color = red
alloc[sibling(n, alloc)].color = black
if n == alloc[alloc[n].parent].left {
tree.rotateLeft(alloc[n].parent)
} else {
tree.rotateRight(alloc[n].parent)
}
}
if getColor(alloc[n].parent, alloc) == black &&
getColor(sibling(n, alloc), alloc) == black &&
getColor(alloc[sibling(n, alloc)].left, alloc) == black &&
getColor(alloc[sibling(n, alloc)].right, alloc) == black {
alloc[sibling(n, alloc)].color = red
n = alloc[n].parent
continue
} else {
// case 4
if getColor(alloc[n].parent, alloc) == red &&
getColor(sibling(n, alloc), alloc) == black &&
getColor(alloc[sibling(n, alloc)].left, alloc) == black &&
getColor(alloc[sibling(n, alloc)].right, alloc) == black {
alloc[sibling(n, alloc)].color = red
alloc[alloc[n].parent].color = black
} else {
tree.deleteCase5(n)
}
}
}
break
}
}
func (tree *RBTree) deleteCase5(n uint32) {
alloc := tree.storage()
if n == alloc[alloc[n].parent].left &&
getColor(sibling(n, alloc), alloc) == black &&
getColor(alloc[sibling(n, alloc)].left, alloc) == red &&
getColor(alloc[sibling(n, alloc)].right, alloc) == black {
alloc[sibling(n, alloc)].color = red
alloc[alloc[sibling(n, alloc)].left].color = black
tree.rotateRight(sibling(n, alloc))
} else if n == alloc[alloc[n].parent].right &&
getColor(sibling(n, alloc), alloc) == black &&
getColor(alloc[sibling(n, alloc)].right, alloc) == red &&
getColor(alloc[sibling(n, alloc)].left, alloc) == black {
alloc[sibling(n, alloc)].color = red
alloc[alloc[sibling(n, alloc)].right].color = black
tree.rotateLeft(sibling(n, alloc))
}
// case 6
alloc[sibling(n, alloc)].color = getColor(alloc[n].parent, alloc)
alloc[alloc[n].parent].color = black
if n == alloc[alloc[n].parent].left {
doAssert(getColor(alloc[sibling(n, alloc)].right, alloc) == red)
alloc[alloc[sibling(n, alloc)].right].color = black
tree.rotateLeft(alloc[n].parent)
} else {
doAssert(getColor(alloc[sibling(n, alloc)].left, alloc) == red)
alloc[alloc[sibling(n, alloc)].left].color = black
tree.rotateRight(alloc[n].parent)
}
}
func (tree *RBTree) replaceNode(oldn, newn uint32) {
alloc := tree.storage()
if alloc[oldn].parent == 0 {
tree.root = newn