-
Notifications
You must be signed in to change notification settings - Fork 15
/
gen_ByExpiry.go
983 lines (873 loc) · 22.3 KB
/
gen_ByExpiry.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
// Code generated by gotemplate. DO NOT EDIT.
package multi_index
import (
"fmt"
"github.com/eosspark/eos-go/common"
"github.com/eosspark/eos-go/libraries/container"
"github.com/eosspark/eos-go/libraries/multiindex"
)
// template type OrderedIndex(FinalIndex,FinalNode,SuperIndex,SuperNode,Value,Key,KeyFunc,Comparator,Multiply)
// OrderedIndex holds elements of the red-black tree
type ByExpiry struct {
super *TransactionIdWithExpiryIndexBase // index on the OrderedIndex, IndexBase is the last super index
final *TransactionIdWithExpiryIndex // index under the OrderedIndex, MultiIndex is the final index
Root *ByExpiryNode
size int
}
func (tree *ByExpiry) init(final *TransactionIdWithExpiryIndex) {
tree.final = final
tree.super = &TransactionIdWithExpiryIndexBase{}
tree.super.init(final)
}
func (tree *ByExpiry) clear() {
tree.Clear()
tree.super.clear()
}
/*generic class*/
/*generic class*/
// OrderedIndexNode is a single element within the tree
type ByExpiryNode struct {
Key common.TimePoint
super *TransactionIdWithExpiryIndexBaseNode
final *TransactionIdWithExpiryIndexNode
color colorByExpiry
Left *ByExpiryNode
Right *ByExpiryNode
Parent *ByExpiryNode
}
/*generic class*/
/*generic class*/
func (node *ByExpiryNode) value() *TransactionIdWithExpiry {
return node.super.value()
}
type colorByExpiry bool
const (
blackByExpiry, redByExpiry colorByExpiry = true, false
)
func (tree *ByExpiry) Insert(v TransactionIdWithExpiry) (IteratorByExpiry, bool) {
fn, res := tree.final.insert(v)
if res {
return tree.makeIterator(fn), true
}
return tree.End(), false
}
func (tree *ByExpiry) insert(v TransactionIdWithExpiry, fn *TransactionIdWithExpiryIndexNode) (*ByExpiryNode, bool) {
key := ByExpiryFunc(v)
node, res := tree.put(key)
if !res {
container.Logger.Warn("#ordered index insert failed")
return nil, false
}
sn, res := tree.super.insert(v, fn)
if res {
node.super = sn
node.final = fn
return node, true
}
tree.remove(node)
return nil, false
}
func (tree *ByExpiry) Erase(iter IteratorByExpiry) (itr IteratorByExpiry) {
itr = iter
itr.Next()
tree.final.erase(iter.node.final)
return
}
func (tree *ByExpiry) Erases(first, last IteratorByExpiry) {
for first != last {
first = tree.Erase(first)
}
}
func (tree *ByExpiry) erase(n *ByExpiryNode) {
tree.remove(n)
tree.super.erase(n.super)
n.super = nil
n.final = nil
}
func (tree *ByExpiry) erase_(iter multiindex.IteratorType) {
if itr, ok := iter.(IteratorByExpiry); ok {
tree.Erase(itr)
} else {
tree.super.erase_(iter)
}
}
func (tree *ByExpiry) Modify(iter IteratorByExpiry, mod func(*TransactionIdWithExpiry)) bool {
if _, b := tree.final.modify(mod, iter.node.final); b {
return true
}
return false
}
func (tree *ByExpiry) modify(n *ByExpiryNode) (*ByExpiryNode, bool) {
n.Key = ByExpiryFunc(*n.value())
if !tree.inPlace(n) {
tree.remove(n)
node, res := tree.put(n.Key)
if !res {
container.Logger.Warn("#ordered index modify failed")
tree.super.erase(n.super)
return nil, false
}
//n.Left = node.Left
//if n.Left != nil {
// n.Left.Parent = n
//}
//n.Right = node.Right
//if n.Right != nil {
// n.Right.Parent = n
//}
//n.Parent = node.Parent
//if n.Parent != nil {
// if n.Parent.Left == node {
// n.Parent.Left = n
// } else {
// n.Parent.Right = n
// }
//} else {
// tree.Root = n
//}
node.super = n.super
node.final = n.final
n = node
}
if sn, res := tree.super.modify(n.super); !res {
tree.remove(n)
return nil, false
} else {
n.super = sn
}
return n, true
}
func (tree *ByExpiry) modify_(iter multiindex.IteratorType, mod func(*TransactionIdWithExpiry)) bool {
if itr, ok := iter.(IteratorByExpiry); ok {
return tree.Modify(itr, mod)
} else {
return tree.super.modify_(iter, mod)
}
}
// Get searches the node in the tree by key and returns its value or nil if key is not found in tree.
// Second return parameter is true if key was found, otherwise false.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (tree *ByExpiry) Find(key common.TimePoint) IteratorByExpiry {
if true {
lower := tree.LowerBound(key)
if !lower.IsEnd() && ByExpireCompare(key, lower.Key()) == 0 {
return lower
}
return tree.End()
} else {
if node := tree.lookup(key); node != nil {
return IteratorByExpiry{tree, node, betweenByExpiry}
}
return tree.End()
}
}
// LowerBound returns an iterator pointing to the first element that is not less than the given key.
// Complexity: O(log N).
func (tree *ByExpiry) LowerBound(key common.TimePoint) IteratorByExpiry {
result := tree.End()
node := tree.Root
if node == nil {
return result
}
for {
if ByExpireCompare(key, node.Key) > 0 {
if node.Right != nil {
node = node.Right
} else {
return result
}
} else {
result.node = node
result.position = betweenByExpiry
if node.Left != nil {
node = node.Left
} else {
return result
}
}
}
}
// UpperBound returns an iterator pointing to the first element that is greater than the given key.
// Complexity: O(log N).
func (tree *ByExpiry) UpperBound(key common.TimePoint) IteratorByExpiry {
result := tree.End()
node := tree.Root
if node == nil {
return result
}
for {
if ByExpireCompare(key, node.Key) >= 0 {
if node.Right != nil {
node = node.Right
} else {
return result
}
} else {
result.node = node
result.position = betweenByExpiry
if node.Left != nil {
node = node.Left
} else {
return result
}
}
}
}
// Remove remove the node from the tree by key.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (tree *ByExpiry) Remove(key common.TimePoint) {
if true {
for lower := tree.LowerBound(key); lower.position != endByExpiry; {
if ByExpireCompare(lower.Key(), key) == 0 {
node := lower.node
lower.Next()
tree.remove(node)
} else {
break
}
}
} else {
node := tree.lookup(key)
tree.remove(node)
}
}
func (tree *ByExpiry) put(key common.TimePoint) (*ByExpiryNode, bool) {
var insertedNode *ByExpiryNode
if tree.Root == nil {
// Assert key is of comparator's type for initial tree
ByExpireCompare(key, key)
tree.Root = &ByExpiryNode{Key: key, color: redByExpiry}
insertedNode = tree.Root
} else {
node := tree.Root
loop := true
if true {
for loop {
compare := ByExpireCompare(key, node.Key)
switch {
case compare < 0:
if node.Left == nil {
node.Left = &ByExpiryNode{Key: key, color: redByExpiry}
insertedNode = node.Left
loop = false
} else {
node = node.Left
}
case compare >= 0:
if node.Right == nil {
node.Right = &ByExpiryNode{Key: key, color: redByExpiry}
insertedNode = node.Right
loop = false
} else {
node = node.Right
}
}
}
} else {
for loop {
compare := ByExpireCompare(key, node.Key)
switch {
case compare == 0:
node.Key = key
return node, false
case compare < 0:
if node.Left == nil {
node.Left = &ByExpiryNode{Key: key, color: redByExpiry}
insertedNode = node.Left
loop = false
} else {
node = node.Left
}
case compare > 0:
if node.Right == nil {
node.Right = &ByExpiryNode{Key: key, color: redByExpiry}
insertedNode = node.Right
loop = false
} else {
node = node.Right
}
}
}
}
insertedNode.Parent = node
}
tree.insertCase1(insertedNode)
tree.size++
return insertedNode, true
}
func (tree *ByExpiry) swapNode(node *ByExpiryNode, pred *ByExpiryNode) {
if node == pred {
return
}
tmp := ByExpiryNode{color: pred.color, Left: pred.Left, Right: pred.Right, Parent: pred.Parent}
pred.color = node.color
node.color = tmp.color
pred.Right = node.Right
if pred.Right != nil {
pred.Right.Parent = pred
}
node.Right = tmp.Right
if node.Right != nil {
node.Right.Parent = node
}
if pred.Parent == node {
pred.Left = node
node.Left = tmp.Left
if node.Left != nil {
node.Left.Parent = node
}
pred.Parent = node.Parent
if pred.Parent != nil {
if pred.Parent.Left == node {
pred.Parent.Left = pred
} else {
pred.Parent.Right = pred
}
} else {
tree.Root = pred
}
node.Parent = pred
} else {
pred.Left = node.Left
if pred.Left != nil {
pred.Left.Parent = pred
}
node.Left = tmp.Left
if node.Left != nil {
node.Left.Parent = node
}
pred.Parent = node.Parent
if pred.Parent != nil {
if pred.Parent.Left == node {
pred.Parent.Left = pred
} else {
pred.Parent.Right = pred
}
} else {
tree.Root = pred
}
node.Parent = tmp.Parent
if node.Parent != nil {
if node.Parent.Left == pred {
node.Parent.Left = node
} else {
node.Parent.Right = node
}
} else {
tree.Root = node
}
}
}
func (tree *ByExpiry) remove(node *ByExpiryNode) {
var child *ByExpiryNode
if node == nil {
return
}
if node.Left != nil && node.Right != nil {
pred := node.Left.maximumNode()
tree.swapNode(node, pred)
}
if node.Left == nil || node.Right == nil {
if node.Right == nil {
child = node.Left
} else {
child = node.Right
}
if node.color == blackByExpiry {
node.color = nodeColorByExpiry(child)
tree.deleteCase1(node)
}
tree.replaceNode(node, child)
if node.Parent == nil && child != nil {
child.color = blackByExpiry
}
}
tree.size--
}
func (tree *ByExpiry) lookup(key common.TimePoint) *ByExpiryNode {
node := tree.Root
for node != nil {
compare := ByExpireCompare(key, node.Key)
switch {
case compare == 0:
return node
case compare < 0:
node = node.Left
case compare > 0:
node = node.Right
}
}
return nil
}
// Empty returns true if tree does not contain any nodes
func (tree *ByExpiry) Empty() bool {
return tree.size == 0
}
// Size returns number of nodes in the tree.
func (tree *ByExpiry) Size() int {
return tree.size
}
// Keys returns all keys in-order
func (tree *ByExpiry) Keys() []common.TimePoint {
keys := make([]common.TimePoint, tree.size)
it := tree.Iterator()
for i := 0; it.Next(); i++ {
keys[i] = it.Key()
}
return keys
}
// Values returns all values in-order based on the key.
func (tree *ByExpiry) Values() []TransactionIdWithExpiry {
values := make([]TransactionIdWithExpiry, tree.size)
it := tree.Iterator()
for i := 0; it.Next(); i++ {
values[i] = it.Value()
}
return values
}
// Left returns the left-most (min) node or nil if tree is empty.
func (tree *ByExpiry) Left() *ByExpiryNode {
var parent *ByExpiryNode
current := tree.Root
for current != nil {
parent = current
current = current.Left
}
return parent
}
// Right returns the right-most (max) node or nil if tree is empty.
func (tree *ByExpiry) Right() *ByExpiryNode {
var parent *ByExpiryNode
current := tree.Root
for current != nil {
parent = current
current = current.Right
}
return parent
}
// Clear removes all nodes from the tree.
func (tree *ByExpiry) Clear() {
tree.Root = nil
tree.size = 0
}
// String returns a string representation of container
func (tree *ByExpiry) String() string {
str := "OrderedIndex\n"
if !tree.Empty() {
outputByExpiry(tree.Root, "", true, &str)
}
return str
}
func (node *ByExpiryNode) String() string {
if !node.color {
return fmt.Sprintf("(%v,%v)", node.Key, "red")
}
return fmt.Sprintf("(%v)", node.Key)
}
func outputByExpiry(node *ByExpiryNode, prefix string, isTail bool, str *string) {
if node.Right != nil {
newPrefix := prefix
if isTail {
newPrefix += "│ "
} else {
newPrefix += " "
}
outputByExpiry(node.Right, newPrefix, false, str)
}
*str += prefix
if isTail {
*str += "└── "
} else {
*str += "┌── "
}
*str += node.String() + "\n"
if node.Left != nil {
newPrefix := prefix
if isTail {
newPrefix += " "
} else {
newPrefix += "│ "
}
outputByExpiry(node.Left, newPrefix, true, str)
}
}
func (node *ByExpiryNode) grandparent() *ByExpiryNode {
if node != nil && node.Parent != nil {
return node.Parent.Parent
}
return nil
}
func (node *ByExpiryNode) uncle() *ByExpiryNode {
if node == nil || node.Parent == nil || node.Parent.Parent == nil {
return nil
}
return node.Parent.sibling()
}
func (node *ByExpiryNode) sibling() *ByExpiryNode {
if node == nil || node.Parent == nil {
return nil
}
if node == node.Parent.Left {
return node.Parent.Right
}
return node.Parent.Left
}
func (node *ByExpiryNode) isLeaf() bool {
if node == nil {
return true
}
if node.Right == nil && node.Left == nil {
return true
}
return false
}
func (tree *ByExpiry) rotateLeft(node *ByExpiryNode) {
right := node.Right
tree.replaceNode(node, right)
node.Right = right.Left
if right.Left != nil {
right.Left.Parent = node
}
right.Left = node
node.Parent = right
}
func (tree *ByExpiry) rotateRight(node *ByExpiryNode) {
left := node.Left
tree.replaceNode(node, left)
node.Left = left.Right
if left.Right != nil {
left.Right.Parent = node
}
left.Right = node
node.Parent = left
}
func (tree *ByExpiry) replaceNode(old *ByExpiryNode, new *ByExpiryNode) {
if old.Parent == nil {
tree.Root = new
} else {
if old == old.Parent.Left {
old.Parent.Left = new
} else {
old.Parent.Right = new
}
}
if new != nil {
new.Parent = old.Parent
}
}
func (tree *ByExpiry) insertCase1(node *ByExpiryNode) {
if node.Parent == nil {
node.color = blackByExpiry
} else {
tree.insertCase2(node)
}
}
func (tree *ByExpiry) insertCase2(node *ByExpiryNode) {
if nodeColorByExpiry(node.Parent) == blackByExpiry {
return
}
tree.insertCase3(node)
}
func (tree *ByExpiry) insertCase3(node *ByExpiryNode) {
uncle := node.uncle()
if nodeColorByExpiry(uncle) == redByExpiry {
node.Parent.color = blackByExpiry
uncle.color = blackByExpiry
node.grandparent().color = redByExpiry
tree.insertCase1(node.grandparent())
} else {
tree.insertCase4(node)
}
}
func (tree *ByExpiry) insertCase4(node *ByExpiryNode) {
grandparent := node.grandparent()
if node == node.Parent.Right && node.Parent == grandparent.Left {
tree.rotateLeft(node.Parent)
node = node.Left
} else if node == node.Parent.Left && node.Parent == grandparent.Right {
tree.rotateRight(node.Parent)
node = node.Right
}
tree.insertCase5(node)
}
func (tree *ByExpiry) insertCase5(node *ByExpiryNode) {
node.Parent.color = blackByExpiry
grandparent := node.grandparent()
grandparent.color = redByExpiry
if node == node.Parent.Left && node.Parent == grandparent.Left {
tree.rotateRight(grandparent)
} else if node == node.Parent.Right && node.Parent == grandparent.Right {
tree.rotateLeft(grandparent)
}
}
func (node *ByExpiryNode) maximumNode() *ByExpiryNode {
if node == nil {
return nil
}
for node.Right != nil {
node = node.Right
}
return node
}
func (tree *ByExpiry) deleteCase1(node *ByExpiryNode) {
if node.Parent == nil {
return
}
tree.deleteCase2(node)
}
func (tree *ByExpiry) deleteCase2(node *ByExpiryNode) {
sibling := node.sibling()
if nodeColorByExpiry(sibling) == redByExpiry {
node.Parent.color = redByExpiry
sibling.color = blackByExpiry
if node == node.Parent.Left {
tree.rotateLeft(node.Parent)
} else {
tree.rotateRight(node.Parent)
}
}
tree.deleteCase3(node)
}
func (tree *ByExpiry) deleteCase3(node *ByExpiryNode) {
sibling := node.sibling()
if nodeColorByExpiry(node.Parent) == blackByExpiry &&
nodeColorByExpiry(sibling) == blackByExpiry &&
nodeColorByExpiry(sibling.Left) == blackByExpiry &&
nodeColorByExpiry(sibling.Right) == blackByExpiry {
sibling.color = redByExpiry
tree.deleteCase1(node.Parent)
} else {
tree.deleteCase4(node)
}
}
func (tree *ByExpiry) deleteCase4(node *ByExpiryNode) {
sibling := node.sibling()
if nodeColorByExpiry(node.Parent) == redByExpiry &&
nodeColorByExpiry(sibling) == blackByExpiry &&
nodeColorByExpiry(sibling.Left) == blackByExpiry &&
nodeColorByExpiry(sibling.Right) == blackByExpiry {
sibling.color = redByExpiry
node.Parent.color = blackByExpiry
} else {
tree.deleteCase5(node)
}
}
func (tree *ByExpiry) deleteCase5(node *ByExpiryNode) {
sibling := node.sibling()
if node == node.Parent.Left &&
nodeColorByExpiry(sibling) == blackByExpiry &&
nodeColorByExpiry(sibling.Left) == redByExpiry &&
nodeColorByExpiry(sibling.Right) == blackByExpiry {
sibling.color = redByExpiry
sibling.Left.color = blackByExpiry
tree.rotateRight(sibling)
} else if node == node.Parent.Right &&
nodeColorByExpiry(sibling) == blackByExpiry &&
nodeColorByExpiry(sibling.Right) == redByExpiry &&
nodeColorByExpiry(sibling.Left) == blackByExpiry {
sibling.color = redByExpiry
sibling.Right.color = blackByExpiry
tree.rotateLeft(sibling)
}
tree.deleteCase6(node)
}
func (tree *ByExpiry) deleteCase6(node *ByExpiryNode) {
sibling := node.sibling()
sibling.color = nodeColorByExpiry(node.Parent)
node.Parent.color = blackByExpiry
if node == node.Parent.Left && nodeColorByExpiry(sibling.Right) == redByExpiry {
sibling.Right.color = blackByExpiry
tree.rotateLeft(node.Parent)
} else if nodeColorByExpiry(sibling.Left) == redByExpiry {
sibling.Left.color = blackByExpiry
tree.rotateRight(node.Parent)
}
}
func nodeColorByExpiry(node *ByExpiryNode) colorByExpiry {
if node == nil {
return blackByExpiry
}
return node.color
}
//////////////iterator////////////////
func (tree *ByExpiry) makeIterator(fn *TransactionIdWithExpiryIndexNode) IteratorByExpiry {
node := fn.GetSuperNode()
for {
if node == nil {
panic("Wrong index node type!")
} else if n, ok := node.(*ByExpiryNode); ok {
return IteratorByExpiry{tree: tree, node: n, position: betweenByExpiry}
} else {
node = node.(multiindex.NodeType).GetSuperNode()
}
}
}
// Iterator holding the iterator's state
type IteratorByExpiry struct {
tree *ByExpiry
node *ByExpiryNode
position positionByExpiry
}
type positionByExpiry byte
const (
beginByExpiry, betweenByExpiry, endByExpiry positionByExpiry = 0, 1, 2
)
// Iterator returns a stateful iterator whose elements are key/value pairs.
func (tree *ByExpiry) Iterator() IteratorByExpiry {
return IteratorByExpiry{tree: tree, node: nil, position: beginByExpiry}
}
func (tree *ByExpiry) Begin() IteratorByExpiry {
itr := IteratorByExpiry{tree: tree, node: nil, position: beginByExpiry}
itr.Next()
return itr
}
func (tree *ByExpiry) End() IteratorByExpiry {
return IteratorByExpiry{tree: tree, node: nil, position: endByExpiry}
}
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *IteratorByExpiry) Next() bool {
if iterator.position == endByExpiry {
goto end
}
if iterator.position == beginByExpiry {
left := iterator.tree.Left()
if left == nil {
goto end
}
iterator.node = left
goto between
}
if iterator.node.Right != nil {
iterator.node = iterator.node.Right
for iterator.node.Left != nil {
iterator.node = iterator.node.Left
}
goto between
}
if iterator.node.Parent != nil {
node := iterator.node
for iterator.node.Parent != nil {
iterator.node = iterator.node.Parent
if node == iterator.node.Left {
goto between
}
node = iterator.node
}
}
end:
iterator.node = nil
iterator.position = endByExpiry
return false
between:
iterator.position = betweenByExpiry
return true
}
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
// If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator.
func (iterator *IteratorByExpiry) Prev() bool {
if iterator.position == beginByExpiry {
goto begin
}
if iterator.position == endByExpiry {
right := iterator.tree.Right()
if right == nil {
goto begin
}
iterator.node = right
goto between
}
if iterator.node.Left != nil {
iterator.node = iterator.node.Left
for iterator.node.Right != nil {
iterator.node = iterator.node.Right
}
goto between
}
if iterator.node.Parent != nil {
node := iterator.node
for iterator.node.Parent != nil {
iterator.node = iterator.node.Parent
if node == iterator.node.Right {
goto between
}
node = iterator.node
//if iterator.tree.Comparator(node.Key, iterator.node.Key) >= 0 {
// goto between
//}
}
}
begin:
iterator.node = nil
iterator.position = beginByExpiry
return false
between:
iterator.position = betweenByExpiry
return true
}
func (iterator IteratorByExpiry) HasNext() bool {
return iterator.position != endByExpiry
}
func (iterator *IteratorByExpiry) HasPrev() bool {
return iterator.position != beginByExpiry
}
// Value returns the current element's value.
// Does not modify the state of the iterator.
func (iterator IteratorByExpiry) Value() TransactionIdWithExpiry {
return *iterator.node.value()
}
// Key returns the current element's key.
// Does not modify the state of the iterator.
func (iterator IteratorByExpiry) Key() common.TimePoint {
return iterator.node.Key
}
// Begin resets the iterator to its initial state (one-before-first)
// Call Next() to fetch the first element if any.
func (iterator *IteratorByExpiry) Begin() {
iterator.node = nil
iterator.position = beginByExpiry
}
func (iterator IteratorByExpiry) IsBegin() bool {
return iterator.position == beginByExpiry
}
// End moves the iterator past the last element (one-past-the-end).
// Call Prev() to fetch the last element if any.
func (iterator *IteratorByExpiry) End() {
iterator.node = nil
iterator.position = endByExpiry
}
func (iterator IteratorByExpiry) IsEnd() bool {
return iterator.position == endByExpiry
}
// Delete remove the node which pointed by the iterator
// Modifies the state of the iterator.
func (iterator *IteratorByExpiry) Delete() {
node := iterator.node
//iterator.Prev()
iterator.tree.remove(node)
}
func (tree *ByExpiry) inPlace(n *ByExpiryNode) bool {
prev := IteratorByExpiry{tree, n, betweenByExpiry}
next := IteratorByExpiry{tree, n, betweenByExpiry}
prev.Prev()
next.Next()
var (
prevResult int
nextResult int
)
if prev.IsBegin() {
prevResult = 1
} else {
prevResult = ByExpireCompare(n.Key, prev.Key())
}
if next.IsEnd() {
nextResult = -1
} else {
nextResult = ByExpireCompare(n.Key, next.Key())
}
return (true && prevResult >= 0 && nextResult <= 0) ||
(!true && prevResult > 0 && nextResult < 0)
}