-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathFiBA.hpp
2057 lines (1851 loc) · 64.7 KB
/
FiBA.hpp
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
#ifndef __FIBA_H__
#define __FIBA_H__
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <ostream>
#include <sstream>
#include <tuple>
#include <utility>
#include <vector>
#include "utils.h"
#include "BulkAdapter.hpp"
#ifdef _MIMALLOC
#include "mimalloc-new-delete.h"
#endif
namespace btree {
using namespace std;
#ifdef COLLECT_STATS
#define IF_COLLECT_STATS(x) x
#else
#define IF_COLLECT_STATS(x) ;
#endif
#define FAKE_NODE ((Node *)0x1L)
IF_COLLECT_STATS(static long statsCombineCount = 0);
IF_COLLECT_STATS(static long statsTotalRepairAggCount = 0);
IF_COLLECT_STATS(static long statsTotalSpineRepairCount = 0);
IF_COLLECT_STATS(static long statsTotalNumNodesRepaired = 0);
IF_COLLECT_STATS(static long statsSumRootDegree = 0);
enum Kind { classic, knuckle, finger };
template<typename _timeT, int minArity, Kind kind, typename binOpFunc,
bool early_stopping=false, bool use_freelist=true>
class Aggregate {
public:
typedef typename binOpFunc::In inT;
typedef typename binOpFunc::Partial aggT;
typedef typename binOpFunc::Out outT;
typedef _timeT timeT;
private:
static int const maxArity = 2 * minArity;
class Node {
typedef Node* NodeP;
NodeP _parent;
aggT _agg;
int _arity;
bool _leftSpine, _rightSpine;
timeT _times[maxArity];
aggT _values[maxArity];
NodeP* _children;
aggT recalcAgg(binOpFunc const &op) const {
if (kind==finger) {
if (isRoot() || (_leftSpine && _rightSpine))
return recalcAggInner(op);
if (_leftSpine)
return recalcAggLeft(op);
if (_rightSpine)
return recalcAggRight(op);
}
assert(hasAggUp());
return recalcAggUp(op);
}
aggT recalcAggInner(binOpFunc const &op) const {
aggT result = binOpFunc::identity;
if (isLeaf()) {
for (int i=0, n=_arity-1; i<n; i++) {
result = op.combine(result, getValue(i));
IF_COLLECT_STATS(++statsCombineCount);
}
} else {
if (_arity > 1) {
result = op.combine(result, getValue(0));
IF_COLLECT_STATS(++statsCombineCount);
}
for (int i=1, n=_arity-1; i<n; i++) {
result = op.combine(result, getChild(i)->getAgg());
IF_COLLECT_STATS(++statsCombineCount);
result = op.combine(result, getValue(i));
IF_COLLECT_STATS(++statsCombineCount);
}
}
return result;
}
aggT recalcAggLeft(binOpFunc const &op) const {
aggT result = recalcAggInner(op);
if (!isLeaf()){
result = op.combine(result, getChild(_arity-1)->getAgg());
IF_COLLECT_STATS(++statsCombineCount);
}
if (!isRoot() && !_parent->isRoot()) {
result = op.combine(result, _parent->getAgg());
IF_COLLECT_STATS(++statsCombineCount);
}
return result;
}
aggT recalcAggRight(binOpFunc const &op) const {
aggT result = recalcAggInner(op);
if (!isLeaf()) {
result = op.combine(getChild(0)->getAgg(), result);
IF_COLLECT_STATS(++statsCombineCount);
}
if (!isRoot() && !_parent->isRoot()) {
result = op.combine(_parent->getAgg(), result);
IF_COLLECT_STATS(++statsCombineCount);
}
return result;
}
aggT recalcAggUp(binOpFunc const &op) const {
if (isLeaf())
return recalcAggInner(op);
if (_arity == 1)
return getChild(0)->getAgg();
aggT leftAgg = getChild(0)->getAgg();
aggT innerAgg = recalcAggInner(op);
aggT rightAgg = getChild(_arity - 1)->getAgg();
IF_COLLECT_STATS(++statsCombineCount);
IF_COLLECT_STATS(++statsCombineCount);
return op.combine(leftAgg, op.combine(innerAgg, rightAgg));
}
public:
bool recalcLeftSpine() const {
if (isRoot())
return false;
if (!(_parent->isRoot() || _parent->leftSpine()))
return false;
return this == _parent->getChild(0);
}
bool recalcRightSpine() const {
if (isRoot())
return false;
if (!(_parent->isRoot() || _parent->rightSpine()))
return false;
return this == _parent->getChild(_parent->arity()-1);
}
private:
void setChild(int i, NodeP node) {
assert(0 <= i && i < _arity);
_children[i] = node;
}
public:
void init(bool isLeaf) {
_parent = NULL;
_agg = binOpFunc::identity;
_arity = 1;
_leftSpine = false;
_rightSpine = false;
if (isLeaf) {
if (_children != NULL) {
delete[] _children;
_children = NULL;
}
} else {
if (_children == NULL)
_children = new NodeP[maxArity + 1];
_children[0] = NULL; // just to be safe, probably not needed
}
}
Node(bool isLeaf) {
_children = NULL;
init(isLeaf);
}
~Node() { delete[] _children; }
int arity() const { return _arity; }
void becomeRoot(binOpFunc const &op) {
// assert(!isRoot() && _parent->isRoot() && _parent->arity()==1);
_parent = NULL;
_leftSpine = false;
_rightSpine = false;
if (kind==finger)
localRepairAgg(op);
}
int childIndex() const {
assert(!isRoot());
for (int i=0, n=_parent->arity(); i<n; i++)
if (this == _parent->getChild(i))
return i;
assert(false);
return -1;
}
aggT getAgg() const { return _agg; }
NodeP getChild(int i) const {
assert(!isLeaf() && 0 <= i && i < _arity);
return _children[i];
}
timeT getTime(int i) const {
assert(0 <= i && i < _arity - 1);
return _times[i];
}
aggT getValue(int i) const {
assert(0 <= i && i < _arity - 1);
return _values[i];
}
bool hasAggUp() const {
if (kind==finger)
return !(isRoot() || _leftSpine || _rightSpine);
else
return true;
}
int height() const {
int result = 0;
Node const* descendent = this;
while (!descendent->isLeaf()) {
descendent = descendent->getChild(0);
result++;
}
return result;
}
bool isDescendent(Node* node) const {
Node const* ancestor = this;
while (ancestor != NULL) {
if (ancestor == node)
return true;
ancestor = ancestor->parent();
}
return false;
}
bool isLeaf() const { return NULL == _children; }
bool isRoot() const { return NULL == _parent; }
bool localCheckInvariant(binOpFunc const &op, bool strong,
const char* f, int l) const {
bool ok = true;
ok &= (0 <= _arity);
if(!ok){cerr<<f<<":"<<l<<": FAILED"<<endl<<*this; throw 0;}
if (strong) {
ok &= (isRoot() || minArity <= _arity) && (_arity <= maxArity);
if(!ok){cerr<<f<<":"<<l<<": FAILED"<<endl<<*this; throw 0;}
} else {
ok &= (isRoot() || minArity - 1 <= _arity) && (_arity <= maxArity + 1);
if(!ok){cerr<<f<<":"<<l<<": FAILED"<<endl<<*this; throw 0;}
}
if (isLeaf()) {
for (int i=0, n=_arity-2; i<n; i++) {
ok &= (getTime(i) < getTime(i+1));
if(!ok){cerr<<f<<":"<<l<<": FAILED"<<endl<<*this; throw 0;}
}
} else {
for (int i=0, n=_arity-1; i<n; i++) {
Node* leftNode = getChild(i);
if (leftNode->arity() >= 2) {
timeT leftTime = leftNode->getTime(leftNode->arity() - 2);
ok &= (leftTime < getTime(i));
if(!ok){cerr<<f<<":"<<l<<": FAILED"<<endl<<*this; throw 0;}
}
Node* rightNode = getChild(i + 1);
if (rightNode->arity() >= 2) {
timeT rightTime = rightNode->getTime(0);
ok &= getTime(i) < rightTime;
if(!ok){cerr<<f<<":"<<l<<": FAILED"<<endl<<*this; throw 0;}
}
}
for (int i=0, n=_arity; i<n; i++) {
ok &= (getChild(i)->parent() == this);
if(!ok){cerr<<f<<":"<<l<<": FAILED"<<endl<<*this; throw 0;}
}
}
ok &= (_leftSpine == recalcLeftSpine());
if(!ok){cerr<<f<<":"<<l<<": FAILED"<<endl<<*this; throw 0;}
ok &= (_rightSpine == recalcRightSpine());
if(!ok){cerr<<f<<":"<<l<<": FAILED"<<endl<<*this; throw 0;}
if (strong) {
ok &= (_agg == recalcAgg(op));
if(!ok){cerr<<f<<":"<<l<<": FAILED"<<endl<<*this; throw 0;}
}
return ok;
}
void localEvict(binOpFunc const &op, int index) {
assert(!isLeaf() && 1 < _arity && index < _arity - 1);
for (int i=index, n=_arity-2; i<n; i++) {
setEntry(i, getTime(i + 1), getValue(i + 1));
setChild(i + 1, getChild(i + 2));
}
if (index == _arity-2 && (isRoot() || _rightSpine))
getChild(_arity-2)->_rightSpine = true;
_arity--;
localRepairAggIfUp(op);
}
bool localEvictUpTo(Aggregate* tree, binOpFunc const &op, timeT time) {
int index;
bool found = localSearch(time, index);
int toPop = index + (found ? 1 : 0);
if (toPop > 0) {
if (!isLeaf())
for (int i=0; i<toPop; i++)
tree->deleteNode(getChild(i), true);
popFront(op, toPop);
}
return toPop > 0;
}
void localEvictEntry(binOpFunc const &op, timeT time) {
int index;
bool found = localSearch(time, index);
assert(found);
for (int i=index, n=_arity-2; i<n; i++)
setEntry(i, getTime(i + 1), getValue(i + 1));
_arity--;
localRepairAggIfUp(op);
}
void localInsert(binOpFunc const &op, timeT time, aggT value, NodeP node, bool repairs=true) {
assert(!isLeaf() && _arity <= maxArity);
int i = _arity - 1;
if (i==0 || time>getTime(i-1)) {
pushBack(op, time, value, node);
} else {
_arity++;
while (i>0 && time<getTime(i-1)) {
setEntry(i, getTime(i-1), getValue(i-1));
setChild(i+1, getChild(i));
i--;
}
setEntry(i, time, value);
setChild(i + 1, node);
node->_parent = this;
if (repairs) localRepairAggIfUp(op);
}
}
bool localInsertEntry(binOpFunc const &op, timeT time, aggT value,bool repairs=true) {
int index;
bool found = localSearch(time, index);
assert((found || isLeaf()) && _arity <= maxArity);
if (found) {
setEntry(index, time, op.combine(getValue(index), value));
if (repairs) localRepairAggIfUp(op);
} else {
if (_arity == 0 || index == _arity - 1) {
pushBackEntry(op, time, value);
} else {
_arity++;
for (int i=_arity-2; i>index; i--)
setEntry(i, getTime(i - 1), getValue(i - 1));
setEntry(index, time, value);
if (repairs) localRepairAggIfUp(op);
}
}
return !found;
}
void localRepairAggIfUp(binOpFunc const &op) {
if (hasAggUp())
localRepairAgg(op);
}
bool localRepairAgg(binOpFunc const &op) {
IF_COLLECT_STATS(statsTotalNumNodesRepaired++);
aggT const newAgg = recalcAgg(op);
if (_agg != newAgg) { //careful: '!=' and '==' asymmetric for geomean
_agg = newAgg;
return true;
}
return false;
}
bool localSearch(timeT const& time, int& index) {
int n;
for (index=0, n=_arity-1; index<n; index++) {
if (time < getTime(index))
return false;
else if (time == getTime(index))
return true;
}
assert(index == _arity - 1);
return false;
}
NodeP parent() const { return _parent; }
void setParent(Node* p) { _parent = p; }
void popBack(binOpFunc const &op, int howMany) {
assert(howMany <= _arity);
_arity -= howMany;
if (!isLeaf() && (isRoot() || _rightSpine) && _arity>0)
getChild(_arity - 1)->_rightSpine = true;
localRepairAggIfUp(op);
}
void popFront(binOpFunc const &op, int howMany) {
assert(howMany < _arity);
for (int i=0, n=_arity-1-howMany; i<n; i++)
setEntry(i, getTime(i + howMany), getValue(i + howMany));
if (!isLeaf())
for (int i=0, n=_arity-howMany; i<n; i++)
setChild(i, getChild(i + howMany));
_arity -= howMany;
if (!isLeaf() && (isRoot() || _leftSpine))
getChild(0)->_leftSpine = true;
localRepairAggIfUp(op);
}
void doWalkRec(std::function<void(const Node*, const timeT, const aggT)> callback) {
if (!isLeaf())
getChild(0)->doWalkRec(callback);
for (int i=0, n=_arity-1; i<n; i++) {
callback(this, getTime(i), getValue(i));
if (!isLeaf())
getChild(i+1)->doWalkRec(callback);
}
}
ostream& print(ostream& os, int const indent) const {
for (int c=0; c<indent; c++) os << " ";
if (isLeaf()) {
os << "(";
for (int i=0, n=_arity-1; i<n; i++) {
if (i > 0)
os << ' ';
os << getTime(i) << "/" << getValue(i);
}
} else {
os << "(" << endl;
for (int i=0, n=_arity-1; i<n; i++) {
getChild(i)->print(os, indent+1);
for (int c=0; c<indent; c++) os << " ";
os << " " << getTime(i) << "/" << getValue(i) << endl;
}
getChild(_arity-1)->print(os, indent+1);
for (int c=0; c<indent; c++) os << " ";
}
os << ")/ " << _agg;
if (isRoot()) os << " root";
if (isLeaf()) os << " leaf";
if (kind==finger && _leftSpine) os << " left-spine";
if (kind==finger && _rightSpine) os << " right-spine";
os << endl;
return os;
}
ostream& repr(ostream& os) const {
os << "<";
for (int i=0;i<this->arity()-1;++i) {
if (i>0) os << ", ";
os << this->getTime(i);
}
os << ">";
os << "(";
if (isRoot()) os << " root";
if (isLeaf()) os << " leaf";
if (kind==finger && _leftSpine) os << " left-spine";
if (kind==finger && _rightSpine) os << " right-spine";
os << ")";
return os;
}
ostream& printPython(ostream& os, int const indent) const {
for (int c=0; c<indent; c++) os << " ";
os << "{ 'times': [";
for (int i=0, n=_arity-1; i<n; i++) {
if (i > 0) os << ", ";
os << getTime(i);
}
os << "]";
if (!isLeaf()) {
os << "," << endl;
for (int c=0; c<indent; c++) os << " ";
os << " 'children': [" << endl;
for (int i=0, n=_arity-1; i<n; i++) {
getChild(i)->printPython(os, indent+1);
os << "," << endl;
}
getChild(_arity-1)->printPython(os, indent+1);
os << "]";
}
os << "}";
if (indent == 0) os << endl;
return os;
}
void pushBack(binOpFunc const &op, timeT time, aggT value, NodeP node) {
assert(!isLeaf() && 0 < _arity && _arity <= maxArity);
_arity++;
setEntry(_arity-2, time, value);
setChild(_arity-1, node);
if (isRoot() || _rightSpine) {
getChild(_arity-2)->_rightSpine = false;
node->_rightSpine = true;
}
if (hasAggUp()) {
_agg = op.combine(_agg, value);
IF_COLLECT_STATS(++statsCombineCount);
_agg = op.combine(_agg, node->getAgg());
IF_COLLECT_STATS(++statsCombineCount);
}
node->_parent = this;
}
void pushBackEntry(binOpFunc const &op, timeT time, aggT value) {
assert(isLeaf() && _arity <= maxArity);
_arity++;
setEntry(_arity - 2, time, value);
if (hasAggUp()) {
_agg = op.combine(_agg, value);
IF_COLLECT_STATS(++statsCombineCount);
}
}
void pushFront(binOpFunc const& op, NodeP node, timeT time, aggT value) {
assert(!isLeaf() && 0 < _arity && _arity <= maxArity);
_arity++;
for (int i=_arity-2; i>=1; i--)
setEntry(i, getTime(i - 1), getValue(i - 1));
for (int i=_arity-1; i>=1; i--)
setChild(i, getChild(i - 1));
setEntry(0, time, value);
setChild(0, node);
if (isRoot() || _leftSpine) {
getChild(1)->_leftSpine = false;
node->_leftSpine = true;
}
if (hasAggUp()) {
_agg = op.combine(value, _agg);
IF_COLLECT_STATS(++statsCombineCount);
_agg = op.combine(node->getAgg(), _agg);
IF_COLLECT_STATS(++statsCombineCount);
}
node->_parent = this;
}
void pushFrontEntry(binOpFunc const &op, timeT time, aggT value) {
assert(isLeaf() && _arity <= maxArity);
_arity++;
for (int i=_arity-2; i>=1; i--)
setEntry(i, getTime(i-1), getValue(i-1));
setEntry(0, time, value);
if (hasAggUp()) {
_agg = op.combine(value, _agg);
IF_COLLECT_STATS(++statsCombineCount);
}
}
bool rightSpine() const { return _rightSpine; }
void setRightSpine(bool value=true) { _rightSpine = value; }
void clear() { _arity = 1; _agg = binOpFunc::identity; }
void setEntry(int i, timeT time, aggT value) {
assert(0 <= i && i < _arity - 1);
_times[i] = time;
_values[i] = value;
}
bool leftSpine() const { return _leftSpine; }
void setLeftSpine(bool value=true) { _leftSpine = value; }
void setOnlyChild(NodeP child) {
assert(!isLeaf() && _arity==1);
_arity = 1;
setChild(0, child);
child->_parent = this;
child->_leftSpine = isRoot() || _leftSpine;
child->_rightSpine = isRoot() || _rightSpine;
if (hasAggUp())
_agg = child->getAgg();
}
friend inline std::ostream& operator<<(std::ostream& os, Node const& x) {
return x.print(os, 0);
}
friend inline std::ostream& operator<<(std::ostream& os, Node const* x) {
if (x == NULL)
return os << "NULL";
else if (x == FAKE_NODE)
return os << "FAKE_NODE";
else
return x->repr(os);
}
};
template<typename T, bool freelistFlag>
class FreeListMgr { /* template */};
template<typename T>
class FreeListMgr<T, true> {
public:
vector<T> _freeList;
FreeListMgr() : _freeList() { /* no op */ }
~FreeListMgr() {
while (!_freeList.empty()) {
Node* node = newNode(true);
deleteNode(node, false);
}
}
void deleteNode(Node* node, bool recursive) {
if (recursive && !node->isLeaf())
for (int i=0, n=node->arity(); i<n; i++)
_freeList.push_back(node->getChild(i));
delete node;
}
Node* newNode(bool isLeaf) {
if (_freeList.empty())
return new Node(isLeaf);
Node* node = _freeList.back();
_freeList.pop_back();
if (!node->isLeaf())
for (int i=0, n=node->arity(); i<n; i++)
_freeList.push_back(node->getChild(i));
node->init(isLeaf);
return node;
}
};
template<typename T>
class FreeListMgr<T, false> { // With the free list turned off
public:
void deleteNode(Node* node, bool recursive) {
/* todo: check me */
if (!recursive) {
delete node;
return ;
}
std::deque<Node*> forCleanUp;
forCleanUp.push_back(node);
while (!forCleanUp.empty()) {
node = forCleanUp.front(); forCleanUp.pop_front();
if (!node->isLeaf()) {
for (int i=0, n=node->arity(); i<n; i++)
forCleanUp.push_back(node->getChild(i));
}
delete node;
}
}
Node* newNode(bool isLeaf) { return new Node(isLeaf); }
};
binOpFunc _binOp;
FreeListMgr<Node*, use_freelist> _freeListMgr;
Node *_root;
Node *_leftFinger, *_rightFinger;
size_t _size;
void deleteNode(Node* node, bool recursive) {
_freeListMgr.deleteNode(node, recursive);
}
Node* newNode(bool isLeaf) {
return _freeListMgr.newNode(isLeaf);
}
void heightDecrease() {
if (false) cout << "-- height-decrease" << endl;
assert(_root->arity() == 1);
Node* oldRoot = _root;
_root = oldRoot->getChild(0);
_root->becomeRoot(_binOp);
deleteNode(oldRoot, false);
assert(checkInvariant(__FILE__, __LINE__, _root));
}
void heightIncrease(bool check=true) {
if (false) cout << "-- height-increase" << endl;
Node* oldRoot = _root;
_root = newNode(false);
_root->setOnlyChild(oldRoot);
if (check) assert(checkInvariant(__FILE__, __LINE__, oldRoot));
}
Node* leastCommonAncestor(Node* node1, Node* node2) const {
int height1 = node1->height(), height2 = node2->height();
while (height1 < height2) {
node1 = node1->parent();
height1++;
}
while (height1 > height2) {
node2 = node2->parent();
height2++;
}
assert(height1 == height2);
while (node1 != node2) {
node1 = node1->parent();
node2 = node2->parent();
}
assert(node1 == node2 && node1 != NULL);
return node1;
}
Node* merge(Node* parent, int nodeIdx, int siblingIdx) {
if (false) cout << "-- merge" << endl;
int betweenIndex = (nodeIdx < siblingIdx) ? nodeIdx : siblingIdx;
timeT betweenTime = parent->getTime(betweenIndex);
aggT betweenValue = parent->getValue(betweenIndex);
Node* left = parent->getChild(betweenIndex);
Node* right = parent->getChild(betweenIndex + 1);
if (left->isLeaf()) {
left->pushBackEntry(_binOp, betweenTime, betweenValue);
for (int i=0, n=right->arity()-1; i<n; i++)
left->pushBackEntry(_binOp, right->getTime(i), right->getValue(i));
} else {
left->pushBack(_binOp, betweenTime, betweenValue, right->getChild(0));
for (int i=0, n=right->arity()-1; i<n; i++)
left->pushBack(_binOp, right->getTime(i), right->getValue(i),
right->getChild(i + 1));
}
parent->localEvict(_binOp, betweenIndex);
if (kind!=classic && _rightFinger == right)
_rightFinger = left;
deleteNode(right, false);
// assert(checkInvariant(__FILE__, __LINE__, left));
return left;
}
void mergeNotSibling(Node* node, Node* neighbor, Node* ancestor) {
int a = -1;
for (int i=0, n=ancestor->arity()-1; i<n; i++)
if (ancestor->getTime(i) < neighbor->getTime(0))
a = i;
if (node->isLeaf()) {
neighbor->pushFrontEntry(_binOp, ancestor->getTime(a),
ancestor->getValue(a));
for (int i=node->arity()-2; i>=0; i--)
neighbor->pushFrontEntry(_binOp, node->getTime(i), node->getValue(i));
} else {
neighbor->pushFront(_binOp, node->getChild(node->arity()-1),
ancestor->getTime(a), ancestor->getValue(a));
for (int i=node->arity()-2; i>=0; i--)
neighbor->pushFront(_binOp, node->getChild(i),
node->getTime(i), node->getValue(i));
}
assert(node->parent() != ancestor);
assert(node->childIndex() == node->parent()->arity()-1);
node->parent()->popBack(_binOp, 1); //avoid double-delete
deleteNode(node, false);
for (int i=0; i<=a; i++)
deleteNode(ancestor->getChild(i), true);
ancestor->popFront(_binOp, a + 1);
}
void move(Node* parent, int recipientIdx, int giverIdx) {
if (false) cout << "-- move" << endl;
Node* recipient = parent->getChild(recipientIdx);
Node* giver = parent->getChild(giverIdx);
int betweenIdx = (recipientIdx < giverIdx) ? recipientIdx : giverIdx;
timeT betweenTime = parent->getTime(betweenIdx);
aggT betweenValue = parent->getValue(betweenIdx);
if (recipientIdx < giverIdx) {
timeT stolenTime = giver->getTime(0);
aggT stolenValue = giver->getValue(0);
parent->setEntry(betweenIdx, stolenTime, stolenValue);
if (recipient->isLeaf()) {
recipient->pushBackEntry(_binOp, betweenTime, betweenValue);
} else {
Node* stolenNode = giver->getChild(0);
recipient->pushBack(_binOp, betweenTime, betweenValue, stolenNode);
}
giver->popFront(_binOp, 1);
} else {
timeT stolenTime = giver->getTime(giver->arity() - 2);
aggT stolenValue = giver->getValue(giver->arity() - 2);
parent->setEntry(betweenIdx, stolenTime, stolenValue);
if (recipient->isLeaf()) {
recipient->pushFrontEntry(_binOp, betweenTime, betweenValue);
} else {
Node* stolenNode = giver->getChild(giver->arity() - 1);
recipient->pushFront(_binOp, stolenNode, betweenTime, betweenValue);
}
giver->popBack(_binOp, 1);
}
parent->localRepairAggIfUp(_binOp);
assert(checkInvariant(__FILE__, __LINE__, recipient, giver));
}
void moveBatch(Node* node, Node* neighbor, Node* ancestor, int batchSize) {
assert(0 < batchSize && batchSize < neighbor->arity());
int a = -1;
for (int i=0, n=ancestor->arity()-1; i<n; i++)
if (ancestor->getTime(i) < neighbor->getTime(0))
a = i;
if (node->isLeaf()) {
node->pushBackEntry(_binOp, ancestor->getTime(a), ancestor->getValue(a));
for (int i=0, n=batchSize-1; i<n; i++)
node->pushBackEntry(_binOp, neighbor->getTime(i),
neighbor->getValue(i));
} else {
node->pushBack(_binOp, ancestor->getTime(a), ancestor->getValue(a),
neighbor->getChild(0));
for (int i=0, n=batchSize-1; i<n; i++)
node->pushBack(_binOp, neighbor->getTime(i), neighbor->getValue(i),
neighbor->getChild(i + 1));
}
ancestor->setEntry(a, neighbor->getTime(batchSize - 1),
neighbor->getValue(batchSize - 1));
neighbor->popFront(_binOp, batchSize);
}
Node* pickEvictionSibling(Node* node, int& nodeIdx, int& siblingIdx) const {
Node* parent = node->parent();
assert(NULL != parent && 1 < parent->arity());
nodeIdx = node->childIndex();
if (nodeIdx == parent->arity() - 1)
siblingIdx = nodeIdx - 1;
else
siblingIdx = nodeIdx + 1;
return parent->getChild(siblingIdx);
}
aggT rangeQueryRec(Node const& node, timeT tFrom, timeT tTo) const {
timeT const TMIN = std::numeric_limits<timeT>::min();
timeT const TMAX = std::numeric_limits<timeT>::max();
if (tFrom == TMIN && tTo == TMAX && node.hasAggUp())
return node.getAgg();
aggT res = binOpFunc::identity;
if (!node.isLeaf()) {
timeT const tNext = node.getTime(0);
if (tFrom < tNext) {
res = _binOp.combine(res, rangeQueryRec(*node.getChild(0),
tFrom,
tNext<=tTo ? TMAX : tTo));
IF_COLLECT_STATS(++statsCombineCount);
}
}
for (int i=0, n=node.arity()-1; i<n; i++) {
timeT tCurr = node.getTime(i);
if (tFrom <= tCurr && tCurr <= tTo) {
res = _binOp.combine(res, node.getValue(i));
IF_COLLECT_STATS(++statsCombineCount);
}
if (!node.isLeaf() && i + 1 < n) {
timeT tNext = node.getTime(i + 1);
if (tCurr < tTo && tFrom < tNext) {
res = _binOp.combine(res, rangeQueryRec(*node.getChild(i + 1),
tFrom<=tCurr ? TMIN : tFrom,
tNext<=tTo ? TMAX : tTo));
IF_COLLECT_STATS(++statsCombineCount);
}
}
}
assert(node.isLeaf() || node.arity() > 1);
if (!node.isLeaf()) {
timeT tCurr = node.getTime(node.arity() - 2);
if (tCurr < tTo) {
res = _binOp.combine(res, rangeQueryRec(*node.getChild(node.arity()-1),
tFrom<=tCurr ? TMIN : tFrom,
tTo));
IF_COLLECT_STATS(++statsCombineCount);
}
}
return res;
}
Node* rebalanceAfterEvict(Node* node, bool* hitLeft, bool* hitRight, Node* toRepair=NULL) {
*hitLeft = node->leftSpine();
*hitRight = node->rightSpine();
if (node == toRepair)
node->localRepairAggIfUp(_binOp);
while (!node->isRoot() && node->arity() < minArity) {
Node* parent = node->parent();
int nodeIndex, siblingIndex;
Node* sibling = pickEvictionSibling(node, nodeIndex, siblingIndex);
*hitRight |= sibling->rightSpine();
*hitLeft |= sibling->leftSpine();
if (sibling->arity() <= minArity) {
node = merge(parent, nodeIndex, siblingIndex);
if (parent->isRoot() && parent->arity() == 1)
heightDecrease();
else
node = parent;
} else {
move(parent, nodeIndex, siblingIndex);
node = parent;
}
if (node == toRepair)
node->localRepairAggIfUp(_binOp);
*hitLeft |= node->leftSpine();
*hitRight |= node->rightSpine();
}
return node;
}
Node* rebalanceAfterInsert(Node* node, bool* hitLeft, bool* hitRight) {
*hitLeft = node->leftSpine();
*hitRight = node->rightSpine();
while (node->arity() > maxArity) {
if (node->isRoot()) {
heightIncrease();
*hitLeft = true;
*hitRight = true;
}
split(node);
node = node->parent();
*hitLeft |= node->leftSpine();
*hitRight |= node->rightSpine();
}
return node;
}
void repairAggs(Node* top, bool hitLeft, bool hitRight) {
// STATS: total number of calls to repairAggs
IF_COLLECT_STATS(statsTotalRepairAggCount++);
IF_COLLECT_STATS(statsSumRootDegree += _root->arity());
if (kind==finger) {
if (!top->hasAggUp()) {
top->localRepairAgg(_binOp);
} else {
while (top->hasAggUp()) {
top = top->parent();
if (!top->localRepairAgg(_binOp) && early_stopping) return;
}
}
IF_COLLECT_STATS(bool spineRepairs = false;)
if (top->leftSpine() || (hitLeft && top->isRoot())) {
IF_COLLECT_STATS(spineRepairs = true;)
Node* left = top;
while (!left->isLeaf()) {
left = left->getChild(0);
left->localRepairAgg(_binOp);
}
}
if (top->rightSpine() || (hitRight && top->isRoot())) {
IF_COLLECT_STATS(spineRepairs = true;)
Node* right = top;
while (!right->isLeaf()) {
right = right->getChild(right->arity() - 1);
right->localRepairAgg(_binOp);
}
}
IF_COLLECT_STATS(statsTotalSpineRepairCount += (int) spineRepairs;)
} else {
top = top->parent();
while (NULL != top) {
if (!top->localRepairAgg(_binOp) && early_stopping) return;
top = top->parent();
}
}
}
void repairLeftSpineInfo(Node* node, bool recurse) {
if (!node->isRoot())
node->setLeftSpine();
if (recurse) {
while (!node->isLeaf()) {
node = node->getChild(0);
node->setLeftSpine();
}
assert(node->isLeaf());
_leftFinger = node;
}
}
struct BoundaryLevel {
Node* node;
Node* neighbor;
Node* ancestor;
BoundaryLevel(Node* node, Node* neighbor, Node* ancestor)
: node(node), neighbor(neighbor), ancestor(ancestor)
{ }
};
using BoundaryT = vector<BoundaryLevel>;
void searchBoundary(timeT time, BoundaryT& result) const {
Node* node = _root;
if (kind!=classic && !_root->isLeaf()) {
if (time < _root->getTime(0)) {
node = _leftFinger;
while (!node->isRoot() && node->getTime(node->arity() - 2) < time)
node = node->parent();