/
IBSTree.h
975 lines (831 loc) · 27.2 KB
/
IBSTree.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
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
/*
* See the dyninst/COPYRIGHT file for copyright information.
*
* We provide the Paradyn Tools (below described as "Paradyn")
* on an AS IS basis, and do not warrant its validity or performance.
* We reserve the right to update, modify, or discontinue this
* software at any time. We shall have no obligation to supply such
* updates or modifications or any other form of support to you.
*
* By your use of Paradyn, you understand and agree that we (or any
* other person or entity with proprietary rights in Paradyn) are
* under no obligation to provide either maintenance services,
* update services, notices of latent defects, or correction of
* defects for Paradyn.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// $Id$
#ifndef _IBSTree_h_
#define _IBSTree_h_
/*******************************************************/
/* header files */
/*******************************************************/
#include <assert.h>
#include "dyntypes.h"
#include "concurrent.h"
#include <stddef.h>
#include <vector>
#include <set>
#include <limits>
#include <iostream>
/** Template class for Interval Binary Search Tree. The implementation is
* based on a red-black tree (derived from our codeRange implementation)
* to control the tree height and thus insertion and search cost.
*
* Unlike our codeRangeTree, this data structure can represent overlapping
* intervals. It is useful for executing stabbing queries and more
* generally for finding invervals that overlap a particular interval.
* A stabbing query requires O(log(N) + L) time in the worst case, where
* L is the number of overlapping intervals, and insertion requires
* O(log^2(N)) time (compare to O(log(N)) for a standard RB-tree).
*
* This class requires a worst case storage O(N log(N))
*
* For more information:
*
* @TECHREPORT{Hanson90theibs-tree:,
* author = {Eric N. Hanson and Moez Chaabouni},
* title = {The IBS-tree: A data structure for finding all intervals that overlap a point},
* institution = {},
* year = {1990}
* }
**/
/** EXTREMELY IMPORTANT XXX: Assuming that all intervals have lower bound
predicate <= and upper bound predicate > ; that is, intervals are
[a,b) **/
// windows.h defines min(), max() macros that interfere with numeric_limits
#undef min
#undef max
namespace Dyninst {
namespace IBS {
typedef enum { TREE_RED, TREE_BLACK } color_t;
}
template <typename T = int, typename U = void*>
class SimpleInterval
{
public:
typedef T type;
SimpleInterval(T low, T high, U id) {
low_ = low;
high_ = high;
id_ = id;
}
SimpleInterval() {}
virtual ~SimpleInterval() {}
virtual T low() const { return low_; }
virtual T high() const { return high_; }
virtual U id() const { return id_; }
protected:
T low_{};
T high_{};
U id_{}; // some arbitrary unique identifier
};
template<class ITYPE>
class IBSTree;
template<class ITYPE = SimpleInterval<> >
class IBSNode {
friend class IBSTree<ITYPE>;
typedef typename ITYPE::type interval_type;
public:
IBSNode() :
val_(0),
color(IBS::TREE_BLACK),
left(NULL),
right(NULL),
parent(NULL) { }
/** constructor for non-nil elements **/
IBSNode(interval_type value, IBSNode *n) :
val_(value),
color(IBS::TREE_RED),
left(n),
right(n),
parent(NULL) { }
~IBSNode() { }
interval_type value() const { return val_; }
interval_type operator*() const { return value; }
friend std::ostream& operator<<(std::ostream& stream, const IBSNode<ITYPE>& node)
{
if(node.left) stream << *(node.left);
stream << node.val_ << std::endl;
if(node.right) stream << *(node.right);
return stream;
}
private:
/* The endpoint of an interval range */
interval_type val_;
/* Intervals indexed by this node */
std::set<ITYPE *> less;
std::set<ITYPE *> greater;
std::set<ITYPE *> equal;
IBS::color_t color;
IBSNode<ITYPE> *left;
IBSNode<ITYPE> *right;
IBSNode<ITYPE> *parent;
};
template<class ITYPE = SimpleInterval<> >
std::ostream &operator<<(std::ostream &os, std::set<ITYPE *> &s) {
for (auto i = s.begin(); i != s.end(); i++) {
std::cerr << "[0x" << std::hex << (*i)->low()
<< ", 0x" << (*i)->high() << std::dec << ") ";
}
return os;
}
template<class ITYPE = SimpleInterval<> >
class IBSTree {
public:
typedef typename ITYPE::type interval_type;
typedef IBSNode<ITYPE>* iterator;
typedef const IBSNode<ITYPE>* const_iterator;
typedef ITYPE value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t difference_type;
typedef size_t size_type;
IBSNode<ITYPE> *nil;
private:
/** size of tree **/
boost::atomic<int> treeSize;
/** pointer to the tree root **/
IBSNode<ITYPE> *root;
/** reader-writer lock to coordinate concurrent operations **/
mutable dyn_rwlock rwlock;
/** RB-tree left rotation with modification to enforce IBS invariants **/
void leftRotate(IBSNode<ITYPE> *);
/** RB-tree right rotation with modification to enforce IBS invariants **/
void rightRotate(IBSNode<ITYPE> *);
void removeInterval(IBSNode<ITYPE> *R, ITYPE *range);
/** Insertion operations: insert the left or right endpoint of
an interval into the tree (may or may not add a node **/
IBSNode<ITYPE>* addLeft(ITYPE *I, IBSNode<ITYPE> *R);
IBSNode<ITYPE>* addRight(ITYPE *I, IBSNode<ITYPE> *R);
/** Find the lowest valued ancestor of node R that has R in its
left subtree -- used in addLeft to determine whether all of
the values in R's right subtree are covered by an interval **/
interval_type rightUp(IBSNode<ITYPE> *R);
/** Symmetric to rightUp **/
interval_type leftUp(IBSNode<ITYPE> *R);
/** Tree-balancing algorithm on insertion **/
void insertFixup(IBSNode<ITYPE> *x);
/** Finds the precessor of the node; this node will have its value
copied to the target node of a deletion and will itself be deleted **/
//IBSNode* treePredecessor(IBSNode *);
/** Find a node with the provided value (interval endpoint) **/
//IBSNode* findNode(int) const;
/** Delete all nodes in the subtree rooted at the parameter **/
void destroy(IBSNode<ITYPE> *);
void findIntervals(interval_type X, IBSNode<ITYPE> *R, std::set<ITYPE *> &S) const;
void findIntervals(ITYPE *I, IBSNode<ITYPE> *R, std::set<ITYPE *> &S) const;
void PrintPreorder(IBSNode<ITYPE> *n, int indent);
std::ostream& doIndent(int n)
{
std::cerr.width(n);
std::cerr << "";
return std::cerr;
}
int height(IBSNode<ITYPE> *n);
int CountMarks(IBSNode<ITYPE> *R) const;
public:
friend std::ostream& operator<<(std::ostream& stream, const IBSTree<ITYPE>& tree)
{
return stream << *(tree.root);
}
public:
/** public for debugging purposes **/
//StatContainer stats_;
IBSTree() :
nil(new IBSNode<ITYPE>),
treeSize(0),
root(nil)
{
//stats_.add("insert",TimerStat);
//stats_.add("remove",TimerStat);
}
~IBSTree() {
destroy(root);
delete nil;
}
size_type size() const {
return treeSize.load();
}
const_iterator begin() const {
dyn_rwlock::shared_lock l(rwlock);
iterator b = root;
while(b->left) b = b->left;
return b;
}
const_iterator end() const {
dyn_rwlock::shared_lock l(rwlock);
iterator e = root;
while(e->right) e = e->right;
return e;
}
int CountMarks() const;
bool empty() const {
return (root == nil);
}
void insert(ITYPE *);
void remove(ITYPE *);
/** Find all intervals that overlap the provided point. Returns
the number of intervals found **/
int find(interval_type, std::set<ITYPE *> &) const;
int find(ITYPE *I, std::set<ITYPE *> &) const;
/** Finds the very next interval(s) with left endpoint
= supremum(X) **/
void successor(interval_type X, std::set<ITYPE *> &) const;
/** Use only when no two intervals share the same lower bound **/
ITYPE * successor(interval_type X) const;
/** Delete all entries in the tree **/
void clear();
void PrintPreorder() {
dyn_rwlock::shared_lock l(rwlock);
PrintPreorder(root, 0);
}
};
template<class ITYPE>
void IBSTree<ITYPE>::rightRotate(IBSNode<ITYPE> *pivot)
{
if(!pivot || (pivot == nil))
return;
IBSNode<ITYPE> *y = pivot->left;
if(y == nil)
return;
pivot->left = y->right;
if(y->right != nil)
y->right->parent = pivot;
y->parent = pivot->parent;
if(!pivot->parent) {
root = y;
}
else if(pivot == pivot->parent->left)
pivot->parent->left = y;
else
pivot->parent->right = y;
y->right = pivot;
pivot->parent = y;
/* Maintain the IBS annotation invariants */
// 1. Copy all marks from the < slot of the pivot (old subtree root)
// to the < and = slots of y (new subtree root). Maintains containment.
y->less.insert(pivot->less.begin(), pivot->less.end());
y->equal.insert(pivot->less.begin(), pivot->less.end());
// 2. For each mark in y's > slot, if it was not in pivot's >,
// *move* it to pivot's <. Maintains containment and maximality,
// because it ensures that nodes under y->right covered by
// the mark before rotation are now (which are now under pivot->left)
// are still covered by the mark (if y > can't cover them still)
// 3. Simultaneously, if the mark in in y's > slot AND pivot's > slot
// (before rotation), remove the mark from pivot's > and = slots
// (preserving maximality).
typename std::set< ITYPE * >::iterator it = y->greater.begin();
while( it != y->greater.end() )
{
ITYPE *tmp = *it;
typename std::set< ITYPE *>::iterator pit = pivot->greater.find( tmp );
if(pit == pivot->greater.end()) {
// Case 2
pivot->less.insert( tmp );
// remove from y's >. This invalidates the iterator, so
// update first
typename std::set< ITYPE * >::iterator del = it;
++it;
y->greater.erase( del );
} else {
// Case 3
// remove from pivot's >
pivot->greater.erase( pit );
pit = pivot->equal.find( tmp );
if(pit != pivot->equal.end())
pivot->equal.erase( pit );
++it;
}
}
}
template<class ITYPE>
void IBSTree<ITYPE>::leftRotate(IBSNode<ITYPE> *pivot)
{
if(!pivot || (pivot == nil))
return;
IBSNode<ITYPE> *y = pivot->right;
if(y == nil)
return;
pivot->right = y->left;
if(y->left != nil)
y->left->parent = pivot;
y->parent = pivot->parent;
if(!pivot->parent) {
root = y;
}
else if(pivot == pivot->parent->left)
pivot->parent->left = y;
else
pivot->parent->right = y;
y->left = pivot;
pivot->parent = y;
/* Fix up the IBS annotations. These are exactly opposeite of the
rules for right rotation */
y->greater.insert(pivot->greater.begin(),pivot->greater.end());
y->equal.insert(pivot->greater.begin(),pivot->greater.end());
typename std::set< ITYPE * >::iterator it = y->less.begin();
while( it != y->less.end() )
{
ITYPE *tmp = *it;
typename std::set< ITYPE *>::iterator pit = pivot->less.find( tmp );
if(pit == pivot->less.end()) {
// Case 2
pivot->greater.insert( tmp );
// remove from y's <. This invalidates the iterator, so
// update first
typename std::set< ITYPE * >::iterator del = it;
++it;
y->less.erase( del );
} else {
// Case 3
// remove from pivot's < and =
pivot->less.erase( pit );
pit = pivot->equal.find( tmp );
if(pit != pivot->equal.end())
pivot->equal.erase( pit );
++it;
}
}
}
template<class ITYPE>
IBSNode<ITYPE>*
IBSTree<ITYPE>::addLeft(ITYPE *I, IBSNode<ITYPE> *R)
{
IBSNode<ITYPE> *parent = NULL;
// these calls can't be inlined as they're to virtuals
interval_type ilow = I->low();
interval_type ihigh = I->high();
while(1) {
bool created = false;
if(R == nil) {
// create a new node
IBSNode<ITYPE> *tmp = new IBSNode<ITYPE>( ilow, nil );
treeSize.fetch_add(1);
created = true;
if(parent == NULL) // must be the root
root = tmp;
else {
tmp->parent = parent;
if(tmp->value() < parent->value()) {
parent->left = tmp;
} else if(tmp->value() > parent->value()) {
parent->right = tmp;
} else {
assert(0); // can't get here
}
}
R = tmp;
}
interval_type rval = R->value();
if(rval == ilow) {
if( rightUp(R) <= ihigh ) {
R->greater.insert( I );
}
R->equal.insert( I ); // assumes closed lower bound
if(created)
return R;
else
return NULL;
}
else if(rval < ilow) {
parent = R;
R = R->right;
continue;
}
else if(rval > ilow) {
if(rval < ihigh) {
R->equal.insert( I );
}
if( rightUp(R) <= ihigh ) {
R->greater.insert( I );
}
parent = R;
R = R->left;
continue;
} else {
assert(0); // can't get here, but gcc whines
return NULL;
}
}
assert(0);
return NULL; // make GCC happy
}
template<class ITYPE>
IBSNode<ITYPE> *
IBSTree<ITYPE>::addRight(ITYPE *I, IBSNode<ITYPE> *R)
{
IBSNode<ITYPE> *parent = NULL;
// these calls can't be inlined as they're to virtuals
interval_type ilow = I->low();
interval_type ihigh = I->high();
while(1)
{
bool created = false;
if(R == nil) {
IBSNode<ITYPE> *tmp = new IBSNode<ITYPE>(ihigh,nil);
treeSize.fetch_add(1);
created = true;
if(parent == NULL) // must be the root
root = tmp;
else {
tmp->parent = parent;
if(tmp->value() < parent->value())
parent->left = tmp;
else if(tmp->value() > parent->value())
parent->right = tmp;
else
assert(0); // can't get here
}
R = tmp;
}
interval_type rval = R->value();
if(rval == ihigh) {
// Case 1. Everything in R's left subtree will be
// within the interval (that is, the nearest ancestor
// node that has R in its right subtree [leftUp(R)]
// has a value equal to or exceeding the low bound of I
//
// NB the upper bound of the interval is open, so don't
// add to r.equals here. Compare addLeft
if(leftUp(R) >= ilow)
R->less.insert(I);
if(created)
return R;
else
return NULL;
}
else if(rval < ihigh) {
if(rval > ilow) {
// R is in the interval
R->equal.insert( I );
}
if( leftUp(R) >= ilow ) {
// Everything to the left of R is within the interval
R->less.insert( I );
}
parent = R;
R = R->right;
continue;
}
else if(rval > ihigh) {
// R not in the interval
parent = R;
R = R->left;
continue;
}
else {
assert(0);
return NULL;
}
}
assert(0);
return NULL; // make GCC happy
}
/* Traverse upward in the tree, looking for the nearest ancestor
that has R in its left subtree and return that value. Since this
routine is used to compute an upper bound on an interval, failure
to find a node should return +infinity */
template<class ITYPE>
typename ITYPE::type
IBSTree<ITYPE>::rightUp(IBSNode<ITYPE> *R)
{
while(NULL != R->parent) {
if(R->parent->left == R)
return R->parent->value();
R = R->parent;
}
return std::numeric_limits<interval_type>::max();
}
/* Same as rightUp, only looking for the nearest ancestor node that
has R in its RIGHT subtree, returning NEGATIVE infinity upon failure */
template<class ITYPE>
typename ITYPE::type
IBSTree<ITYPE>::leftUp(IBSNode<ITYPE> *R)
{
while(NULL != R->parent) {
if(R->parent->right == R)
return R->parent->value();
R = R->parent;
}
// XXX is this right? for unsigned values, min() is a possible value
return std::numeric_limits<interval_type>::min();
}
/* Restore RB-tree invariants after node insertion */
template<class ITYPE>
void IBSTree<ITYPE>::insertFixup(IBSNode<ITYPE> *x)
{
x->color = IBS::TREE_RED;
while((x != root) && (x->parent->color == IBS::TREE_RED)) {
if(x->parent == x->parent->parent->left) {
IBSNode<ITYPE>* y = x->parent->parent->right;
if(y->color == IBS::TREE_RED) {
x->parent->color = IBS::TREE_BLACK;
y->color = IBS::TREE_BLACK;
x->parent->parent->color = IBS::TREE_RED;
x = x->parent->parent;
}
else {
if(x == x->parent->right) {
x = x->parent;
leftRotate(x);
}
x->parent->color = IBS::TREE_BLACK;
x->parent->parent->color = IBS::TREE_RED;
rightRotate(x->parent->parent);
}
}
else {
IBSNode<ITYPE> *y = x->parent->parent->left;
if(y->color == IBS::TREE_RED) {
x->parent->color = IBS::TREE_BLACK;
y->color = IBS::TREE_BLACK;
x->parent->parent->color = IBS::TREE_RED;
x = x->parent->parent;
}
else {
if(x == x->parent->left) {
x = x->parent;
rightRotate(x);
}
x->parent->color = IBS::TREE_BLACK;
x->parent->parent->color = IBS::TREE_RED;
leftRotate(x->parent->parent);
}
}
}
root->color = IBS::TREE_BLACK;
}
template<class ITYPE>
void IBSTree<ITYPE>::destroy(IBSNode<ITYPE> *n)
{
if(!n || (n == nil))
return;
if(n->left != nil)
destroy(n->left);
if(n->right != nil)
destroy(n->right);
delete n;
}
/* void deleteFixup(IBSNode<ITYPE> *)
{
// XXX not implemented
assert(0);
}*/
template<class ITYPE>
void IBSTree<ITYPE>::findIntervals(interval_type X, IBSNode<ITYPE> *R, std::set<ITYPE *> &S) const
{
while(R != nil) {
if(X == R->value()) {
S.insert(R->equal.begin(),R->equal.end());
return;
}
else if(X < R->value()) {
S.insert(R->less.begin(),R->less.end());
R = R->left;
} else {
S.insert(R->greater.begin(),R->greater.end());
R = R->right;
}
}
}
/* Find all intervals that intersect an interval:
If low is < a node, take the < set (any interval in < contains low)
If low or high are > a node, take the > set
If low <= a node and high > a node, take the = set
NB Because this traversal may go both directions in the tree,
it remains a recursive operation and is less efficient
than a pointwise stabbing query.
*/
template<class ITYPE>
void IBSTree<ITYPE>::findIntervals(ITYPE * I, IBSNode<ITYPE> *R, std::set<ITYPE *> &S) const
{
if(R == nil) return;
interval_type low = I->low();
interval_type high = I->high();
if(low < R->value()) {
S.insert(R->less.begin(),R->less.end());
findIntervals(I,R->left,S);
}
if(low > R->value() || high > R->value()) {
S.insert(R->greater.begin(),R->greater.end());
findIntervals(I,R->right,S);
}
if(low <= R->value() && high > R->value()) {
S.insert(R->equal.begin(),R->equal.end());
}
else if(low == R->value() && high == R->value()) {
// XXX explicitly allow zero-length intervals
// to `match' the starting value
S.insert(R->equal.begin(),R->equal.end());
}
}
template<class ITYPE>
void IBSTree<ITYPE>::removeInterval(IBSNode<ITYPE> *R, ITYPE *range)
{
if(R == nil) return;
interval_type low = range->low();
interval_type high = range->high();
// FIXME this doesn't use maximality and containment to minimize the
// number of set::erase calls
if(low < R->value()) {
R->less.erase(range);
removeInterval(R->left,range);
}
if(low > R->value() || high > R->value()) {
R->greater.erase(range);
removeInterval(R->right,range);
}
if(low <= R->value() && high > R->value()) {
R->equal.erase(range);
}
else if(low == R->value() && high == R->value()) {
// XXX explicitly allow zero-length intervals
// to `match' the starting value
R->equal.erase(range);
}
}
template<class ITYPE>
int IBSTree<ITYPE>::CountMarks(IBSNode<ITYPE> *R) const
{
if(R == nil) return 0;
return (R->less.size() + R->greater.size() + R->equal.size()) +
CountMarks(R->left) + CountMarks(R->right);
}
/***************** Public methods *****************/
template<class ITYPE>
void IBSTree<ITYPE>::insert(ITYPE *range)
{
//stats_.startTimer("insert");
dyn_rwlock::unique_lock l(rwlock);
// Insert the endpoints of the range, rebalancing if new
// nodes were created
IBSNode<ITYPE> *x = addLeft(range,root);
if(x) {
insertFixup(x);
}
x = addRight(range,root);
if(x) {
insertFixup(x);
}
//stats_.stopTimer("insert");
}
template<class ITYPE>
void IBSTree<ITYPE>::remove(ITYPE * range)
{
//stats_.startTimer("remove");
// 1. Remove all interval markers corresponding to range from the tree,
// using the reverse of the insertion procedure.
// 2. If no other intervals use the endpoints of this interval
// (find this how?), remove their endpoints (complex) and fix
// up the tree.
// XXX FIXME XXX
// Currently being very lazy and inefficient: only removing interval
// markers (not end nodes even if they end up unused), and also removing
// elements from each of the <, >, and = sets of each node (following
// the tests of the insertion procedures would avoid many of these
// O(log n) lookups
dyn_rwlock::unique_lock l(rwlock);
removeInterval(root,range);
//stats_.stopTimer("remove");
}
template<class ITYPE>
int IBSTree<ITYPE>::find(interval_type X, std::set<ITYPE *> &out) const
{
unsigned size = out.size();
{
dyn_rwlock::shared_lock l(rwlock);
findIntervals(X,root,out);
}
return out.size() - size;
}
template<class ITYPE>
int IBSTree<ITYPE>::find(ITYPE * I, std::set<ITYPE *> &out) const
{
unsigned size = out.size();
{
dyn_rwlock::shared_lock l(rwlock);
findIntervals(I,root,out);
}
return out.size() - size;
}
template<class ITYPE>
void IBSTree<ITYPE>::successor(interval_type X, std::set<ITYPE *> &out) const
{
IBSNode<ITYPE> *n = root;
IBSNode<ITYPE> *last = nil;
std::vector< IBSNode<ITYPE>* > stack;
dyn_rwlock::shared_lock l(rwlock);
/* last will hold the node immediately greater than X */
while(1) {
if(n == nil) {
if(last != nil) {
typename std::set<ITYPE *>::iterator sit = last->equal.begin();
for( ; sit != last->equal.end(); ++sit) {
if((*sit)->low() == last->value()) out.insert(*sit);
}
if(!out.empty())
break;
else {
// have missed out. pop back up to the last node where
// we went left and then advance down its right path
n = last->right;
if(!stack.empty()) {
last = stack.back();
stack.pop_back();
} else {
last = nil;
}
continue;
}
} else
break;
}
if(X >= n->value()) {
n = n->right;
} else {
if(last != nil)
stack.push_back(last);
last = n;
n = n->left;
}
}
}
template<class ITYPE>
ITYPE * IBSTree<ITYPE>::successor(interval_type X) const
{
std::set<ITYPE *> out;
{
dyn_rwlock::shared_lock l(rwlock);
successor(X,out);
}
assert( out.size() <= 1 );
if(!out.empty())
return *out.begin();
else
return NULL;
}
template<class ITYPE>
void IBSTree<ITYPE>::clear() {
if(root == nil) return;
dyn_rwlock::unique_lock l(rwlock);
destroy(root);
root = nil;
treeSize.store(0);
}
template<class ITYPE>
int IBSTree<ITYPE>::height(IBSNode<ITYPE> *n)
{
if(!n)
return 0;
int leftHeight, rightHeight;
{
dyn_rwlock::shared_lock l(rwlock);
leftHeight = 1 + height(n->left);
rightHeight = 1 + height(n->right);
}
if(leftHeight > rightHeight)
return leftHeight;
else
return rightHeight;
}
template<class ITYPE>
void IBSTree<ITYPE>::PrintPreorder(IBSNode<ITYPE> *n, int indent)
{
if(n == nil) return;
// print self
doIndent(indent) << "node: 0x" << std::hex << n->value() << std::dec << " (" << n->value() << ")" << std::endl;
if (!n->less.empty())
doIndent(indent) << " <: " << n->less << std::endl;
if (!n->equal.empty())
doIndent(indent) << " =: " << n->equal << std::endl;
if (!n->greater.empty())
doIndent(indent) << " >: " << n->greater << std::endl;
// print children
PrintPreorder(n->left, indent + 1);
PrintPreorder(n->right, indent + 1);
if(n == root) {
std::cerr << "tree height: " << height(root) << std::endl;
}
}
template<class ITYPE>
int IBSTree<ITYPE>::CountMarks() const
{
dyn_rwlock::shared_lock l(rwlock);
return CountMarks(root);
}
}/* Dyninst */
#endif