Debian Codegraph @

src/2geom/intervaltree/interval_tree.cc

raw symbols SCIP
1 #include "interval_tree.h"
2 #include <stdio.h>
3 #include <math.h>
4 #include <assert.h>
5
6 // From Emin Martinian, licenced LGPL and MPL with permission
7
8
9 using namespace std;
10
11 // If the symbol CHECK_INTERVAL_TREE_ASSUMPTIONS is defined then the
12 // code does a lot of extra checking to make sure certain assumptions
13 // are satisfied. This only needs to be done if you suspect bugs are
14 // present or if you make significant changes and want to make sure
15 // your changes didn't mess anything up.
16 //#define CHECK_INTERVAL_TREE_ASSUMPTIONS 1
17
18
19 const int MIN_INT=-MAX_INT;
20
21 IntervalTreeNode::IntervalTreeNode(){}
22
23 IntervalTreeNode::IntervalTreeNode(Interval * newInterval)
24 : storedInterval (newInterval) ,
25 key(newInterval->GetLowPoint()),
26 high(newInterval->GetHighPoint()) ,
27 maxHigh(high) {
28 }
29 IntervalTreeNode::~IntervalTreeNode(){}
30 Interval::Interval(){}
31 Interval::~Interval(){}
32 void Interval::Print() const {
33 cout << "No Print Method defined for instance of Interval" << endl;
34 }
35
36 IntervalTree::IntervalTree()
37 {
38 nil = new IntervalTreeNode;
39 nil->left = nil->right = nil->parent = nil;
40 nil->red = 0;
41 nil->key = nil->high = nil->maxHigh = MIN_INT;
42 nil->storedInterval = NULL;
43
44 root = new IntervalTreeNode;
45 root->parent = root->left = root->right = nil;
46 root->key = root->high = root->maxHigh = MAX_INT;
47 root->red=0;
48 root->storedInterval = NULL;
49
50 /* the following are used for the Enumerate function */
51 recursionNodeStackSize = 8; // the tree depth is approximately lg(n), this is a 256 element tree. The code will adapt to deeper trees, but this saves considerable space for small trees.
52 recursionNodeStack = new it_recursion_node[recursionNodeStackSize];
53 recursionNodeStackTop = 1;
54 recursionNodeStack[0].start_node = NULL;
55
56 }
57
58 /***********************************************************************/
59 /* FUNCTION: LeftRotate */
60 /**/
61 /* INPUTS: the node to rotate on */
62 /**/
63 /* OUTPUT: None */
64 /**/
65 /* Modifies Input: this, x */
66 /**/
67 /* EFFECTS: Rotates as described in _Introduction_To_Algorithms by */
68 /* Cormen, Leiserson, Rivest (Chapter 14). Basically this */
69 /* makes the parent of x be to the left of x, x the parent of */
70 /* its parent before the rotation and fixes other pointers */
71 /* accordingly. Also updates the maxHigh fields of x and y */
72 /* after rotation. */
73 /***********************************************************************/
74
75 void IntervalTree::LeftRotate(IntervalTreeNode* x) {
76 IntervalTreeNode* y;
77
78 /* I originally wrote this function to use the sentinel for */
79 /* nil to avoid checking for nil. However this introduces a */
80 /* very subtle bug because sometimes this function modifies */
81 /* the parent pointer of nil. This can be a problem if a */
82 /* function which calls LeftRotate also uses the nil sentinel */
83 /* and expects the nil sentinel's parent pointer to be unchanged */
84 /* after calling this function. For example, when DeleteFixUP */
85 /* calls LeftRotate it expects the parent pointer of nil to be */
86 /* unchanged. */
87
88 y=x->right;
89 x->right=y->left;
90
91 if (y->left != nil) y->left->parent=x; /* used to use sentinel here */
92 /* and do an unconditional assignment instead of testing for nil */
93
94 y->parent=x->parent;
95
96 /* instead of checking if x->parent is the root as in the book, we */
97 /* count on the root sentinel to implicitly take care of this case */
98 if( x == x->parent->left) {
99 x->parent->left=y;
100 } else {
101 x->parent->right=y;
102 }
103 y->left=x;
104 x->parent=y;
105
106 x->maxHigh=std::max(x->left->maxHigh,std::max(x->right->maxHigh,x->high));
107 y->maxHigh=std::max(x->maxHigh,std::max(y->right->maxHigh,y->high));
108 #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
109 CheckAssumptions();
110 #elif defined(DEBUG_ASSERT)
111 assert(!nil->red,"nil not red in ITLeftRotate");
112 assert((nil->maxHigh=MIN_INT),
113 "nil->maxHigh != MIN_INT in ITLeftRotate");
114 #endif
115 }
116
117
118 /***********************************************************************/
119 /* FUNCTION: RighttRotate */
120 /**/
121 /* INPUTS: node to rotate on */
122 /**/
123 /* OUTPUT: None */
124 /**/
125 /* Modifies Input?: this, y */
126 /**/
127 /* EFFECTS: Rotates as described in _Introduction_To_Algorithms by */
128 /* Cormen, Leiserson, Rivest (Chapter 14). Basically this */
129 /* makes the parent of x be to the left of x, x the parent of */
130 /* its parent before the rotation and fixes other pointers */
131 /* accordingly. Also updates the maxHigh fields of x and y */
132 /* after rotation. */
133 /***********************************************************************/
134
135
136 void IntervalTree::RightRotate(IntervalTreeNode* y) {
137 IntervalTreeNode* x;
138
139 /* I originally wrote this function to use the sentinel for */
140 /* nil to avoid checking for nil. However this introduces a */
141 /* very subtle bug because sometimes this function modifies */
142 /* the parent pointer of nil. This can be a problem if a */
143 /* function which calls LeftRotate also uses the nil sentinel */
144 /* and expects the nil sentinel's parent pointer to be unchanged */
145 /* after calling this function. For example, when DeleteFixUP */
146 /* calls LeftRotate it expects the parent pointer of nil to be */
147 /* unchanged. */
148
149 x=y->left;
150 y->left=x->right;
151
152 if (nil != x->right) x->right->parent=y; /*used to use sentinel here */
153 /* and do an unconditional assignment instead of testing for nil */
154
155 /* instead of checking if x->parent is the root as in the book, we */
156 /* count on the root sentinel to implicitly take care of this case */
157 x->parent=y->parent;
158 if( y == y->parent->left) {
159 y->parent->left=x;
160 } else {
161 y->parent->right=x;
162 }
163 x->right=y;
164 y->parent=x;
165
166 y->maxHigh=std::max(y->left->maxHigh,std::max(y->right->maxHigh,y->high));
167 x->maxHigh=std::max(x->left->maxHigh,std::max(y->maxHigh,x->high));
168 #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
169 CheckAssumptions();
170 #elif defined(DEBUG_ASSERT)
171 assert(!nil->red,"nil not red in ITRightRotate");
172 assert((nil->maxHigh=MIN_INT),
173 "nil->maxHigh != MIN_INT in ITRightRotate");
174 #endif
175 }
176
177 /***********************************************************************/
178 /* FUNCTION: TreeInsertHelp */
179 /**/
180 /* INPUTS: z is the node to insert */
181 /**/
182 /* OUTPUT: none */
183 /**/
184 /* Modifies Input: this, z */
185 /**/
186 /* EFFECTS: Inserts z into the tree as if it were a regular binary tree */
187 /* using the algorithm described in _Introduction_To_Algorithms_ */
188 /* by Cormen et al. This function is only intended to be called */
189 /* by the InsertTree function and not by the user */
190 /***********************************************************************/
191
192 void IntervalTree::TreeInsertHelp(IntervalTreeNode* z) {
193 /* This function should only be called by InsertITTree (see above) */
194 IntervalTreeNode* x;
195 IntervalTreeNode* y;
196
197 z->left=z->right=nil;
198 y=root;
199 x=root->left;
200 while( x != nil) {
201 y=x;
202 if ( x->key > z->key) {
203 x=x->left;
204 } else { /* x->key <= z->key */
205 x=x->right;
206 }
207 }
208 z->parent=y;
209 if ( (y == root) ||
210 (y->key > z->key) ) {
211 y->left=z;
212 } else {
213 y->right=z;
214 }
215
216
217 #if defined(DEBUG_ASSERT)
218 assert(!nil->red,"nil not red in ITTreeInsertHelp");
219 assert((nil->maxHigh=MIN_INT),
220 "nil->maxHigh != MIN_INT in ITTreeInsertHelp");
221 #endif
222 }
223
224
225 /***********************************************************************/
226 /* FUNCTION: FixUpMaxHigh */
227 /**/
228 /* INPUTS: x is the node to start from*/
229 /**/
230 /* OUTPUT: none */
231 /**/
232 /* Modifies Input: this */
233 /**/
234 /* EFFECTS: Travels up to the root fixing the maxHigh fields after */
235 /* an insertion or deletion */
236 /***********************************************************************/
237
238 void IntervalTree::FixUpMaxHigh(IntervalTreeNode * x) {
239 while(x != root) {
240 x->maxHigh=std::max(x->high,std::max(x->left->maxHigh,x->right->maxHigh));
241 x=x->parent;
242 }
243 #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
244 CheckAssumptions();
245 #endif
246 }
247
248 /* Before calling InsertNode the node x should have its key set */
249
250 /***********************************************************************/
251 /* FUNCTION: InsertNode */
252 /**/
253 /* INPUTS: newInterval is the interval to insert*/
254 /**/
255 /* OUTPUT: This function returns a pointer to the newly inserted node */
256 /* which is guaranteed to be valid until this node is deleted. */
257 /* What this means is if another data structure stores this */
258 /* pointer then the tree does not need to be searched when this */
259 /* is to be deleted. */
260 /**/
261 /* Modifies Input: tree */
262 /**/
263 /* EFFECTS: Creates a node node which contains the appropriate key and */
264 /* info pointers and inserts it into the tree. */
265 /***********************************************************************/
266
267 IntervalTreeNode * IntervalTree::Insert(Interval * newInterval)
268 {
269 IntervalTreeNode * y;
270 IntervalTreeNode * x;
271 IntervalTreeNode * newNode;
272
273 x = new IntervalTreeNode(newInterval);
274 TreeInsertHelp(x);
275 FixUpMaxHigh(x->parent);
276 newNode = x;
277 x->red=1;
278 while(x->parent->red) { /* use sentinel instead of checking for root */
279 if (x->parent == x->parent->parent->left) {
280 y=x->parent->parent->right;
281 if (y->red) {
282 x->parent->red=0;
283 y->red=0;
284 x->parent->parent->red=1;
285 x=x->parent->parent;
286 } else {
287 if (x == x->parent->right) {
288 x=x->parent;
289 LeftRotate(x);
290 }
291 x->parent->red=0;
292 x->parent->parent->red=1;
293 RightRotate(x->parent->parent);
294 }
295 } else { /* case for x->parent == x->parent->parent->right */
296 /* this part is just like the section above with */
297 /* left and right interchanged */
298 y=x->parent->parent->left;
299 if (y->red) {
300 x->parent->red=0;
301 y->red=0;
302 x->parent->parent->red=1;
303 x=x->parent->parent;
304 } else {
305 if (x == x->parent->left) {
306 x=x->parent;
307 RightRotate(x);
308 }
309 x->parent->red=0;
310 x->parent->parent->red=1;
311 LeftRotate(x->parent->parent);
312 }
313 }
314 }
315 root->left->red=0;
316 return(newNode);
317
318 #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
319 CheckAssumptions();
320 #elif defined(DEBUG_ASSERT)
321 assert(!nil->red,"nil not red in ITTreeInsert");
322 assert(!root->red,"root not red in ITTreeInsert");
323 assert((nil->maxHigh=MIN_INT),
324 "nil->maxHigh != MIN_INT in ITTreeInsert");
325 #endif
326 }
327
328 /***********************************************************************/
329 /* FUNCTION: GetSuccessorOf */
330 /**/
331 /* INPUTS: x is the node we want the successor of */
332 /**/
333 /* OUTPUT: This function returns the successor of x or NULL if no */
334 /* successor exists. */
335 /**/
336 /* Modifies Input: none */
337 /**/
338 /* Note: uses the algorithm in _Introduction_To_Algorithms_ */
339 /***********************************************************************/
340
341 IntervalTreeNode * IntervalTree::GetSuccessorOf(IntervalTreeNode * x) const
342 {
343 IntervalTreeNode* y;
344
345 if (nil != (y = x->right)) { /* assignment to y is intentional */
346 while(y->left != nil) { /* returns the minimum of the right subtree of x */
347 y=y->left;
348 }
349 return(y);
350 } else {
351 y=x->parent;
352 while(x == y->right) { /* sentinel used instead of checking for nil */
353 x=y;
354 y=y->parent;
355 }
356 if (y == root) return(nil);
357 return(y);
358 }
359 }
360
361 /***********************************************************************/
362 /* FUNCTION: GetPredecessorOf */
363 /**/
364 /* INPUTS: x is the node to get predecessor of */
365 /**/
366 /* OUTPUT: This function returns the predecessor of x or NULL if no */
367 /* predecessor exists. */
368 /**/
369 /* Modifies Input: none */
370 /**/
371 /* Note: uses the algorithm in _Introduction_To_Algorithms_ */
372 /***********************************************************************/
373
374 IntervalTreeNode * IntervalTree::GetPredecessorOf(IntervalTreeNode * x) const {
375 IntervalTreeNode* y;
376
377 if (nil != (y = x->left)) { /* assignment to y is intentional */
378 while(y->right != nil) { /* returns the maximum of the left subtree of x */
379 y=y->right;
380 }
381 return(y);
382 } else {
383 y=x->parent;
384 while(x == y->left) {
385 if (y == root) return(nil);
386 x=y;
387 y=y->parent;
388 }
389 return(y);
390 }
391 }
392
393 /***********************************************************************/
394 /* FUNCTION: Print */
395 /**/
396 /* INPUTS: none */
397 /**/
398 /* OUTPUT: none */
399 /**/
400 /* EFFECTS: This function recursively prints the nodes of the tree */
401 /* inorder. */
402 /**/
403 /* Modifies Input: none */
404 /**/
405 /* Note: This function should only be called from ITTreePrint */
406 /***********************************************************************/
407
408 void IntervalTreeNode::Print(IntervalTreeNode * nil,
409 IntervalTreeNode * root) const {
410 storedInterval->Print();
411 printf(", k=%i, h=%i, mH=%i",key,high,maxHigh);
412 printf(" l->key=");
413 if( left == nil) printf("NULL"); else printf("%i",left->key);
414 printf(" r->key=");
415 if( right == nil) printf("NULL"); else printf("%i",right->key);
416 printf(" p->key=");
417 if( parent == root) printf("NULL"); else printf("%i",parent->key);
418 printf(" red=%i\n",red);
419 }
420
421 void IntervalTree::TreePrintHelper( IntervalTreeNode* x) const {
422
423 if (x != nil) {
424 TreePrintHelper(x->left);
425 x->Print(nil,root);
426 TreePrintHelper(x->right);
427 }
428 }
429
430 IntervalTree::~IntervalTree() {
431 IntervalTreeNode * x = root->left;
432 vector<IntervalTreeNode *> stuffToFree;
433
434 if (x != nil) {
435 if (x->left != nil) {
436 stuffToFree.push_back(x->left);
437 }
438 if (x->right != nil) {
439 stuffToFree.push_back(x->right);
440 }
441 // delete x->storedInterval;
442 delete x;
443 while(! stuffToFree.empty() ) {
444 x = stuffToFree.back();
445 stuffToFree.pop_back();
446 if (x->left != nil) {
447 stuffToFree.push_back(x->left);
448 }
449 if (x->right != nil) {
450 stuffToFree.push_back(x->right);
451 }
452 // delete x->storedInterval;
453 delete x;
454 }
455 }
456 delete nil;
457 delete root;
458 delete[] recursionNodeStack;
459 }
460
461
462 /***********************************************************************/
463 /* FUNCTION: Print */
464 /**/
465 /* INPUTS: none */
466 /**/
467 /* OUTPUT: none */
468 /**/
469 /* EFFECT: This function recursively prints the nodes of the tree */
470 /* inorder. */
471 /**/
472 /* Modifies Input: none */
473 /**/
474 /***********************************************************************/
475
476 void IntervalTree::Print() const {
477 TreePrintHelper(root->left);
478 }
479
480 /***********************************************************************/
481 /* FUNCTION: DeleteFixUp */
482 /**/
483 /* INPUTS: x is the child of the spliced */
484 /* out node in DeleteNode. */
485 /**/
486 /* OUTPUT: none */
487 /**/
488 /* EFFECT: Performs rotations and changes colors to restore red-black */
489 /* properties after a node is deleted */
490 /**/
491 /* Modifies Input: this, x */
492 /**/
493 /* The algorithm from this function is from _Introduction_To_Algorithms_ */
494 /***********************************************************************/
495
496 void IntervalTree::DeleteFixUp(IntervalTreeNode* x) {
497 IntervalTreeNode * w;
498 IntervalTreeNode * rootLeft = root->left;
499
500 while( (!x->red) && (rootLeft != x)) {
501 if (x == x->parent->left) {
502 w=x->parent->right;
503 if (w->red) {
504 w->red=0;
505 x->parent->red=1;
506 LeftRotate(x->parent);
507 w=x->parent->right;
508 }
509 if ( (!w->right->red) && (!w->left->red) ) {
510 w->red=1;
511 x=x->parent;
512 } else {
513 if (!w->right->red) {
514 w->left->red=0;
515 w->red=1;
516 RightRotate(w);
517 w=x->parent->right;
518 }
519 w->red=x->parent->red;
520 x->parent->red=0;
521 w->right->red=0;
522 LeftRotate(x->parent);
523 x=rootLeft; /* this is to exit while loop */
524 }
525 } else { /* the code below is has left and right switched from above */
526 w=x->parent->left;
527 if (w->red) {
528 w->red=0;
529 x->parent->red=1;
530 RightRotate(x->parent);
531 w=x->parent->left;
532 }
533 if ( (!w->right->red) && (!w->left->red) ) {
534 w->red=1;
535 x=x->parent;
536 } else {
537 if (!w->left->red) {
538 w->right->red=0;
539 w->red=1;
540 LeftRotate(w);
541 w=x->parent->left;
542 }
543 w->red=x->parent->red;
544 x->parent->red=0;
545 w->left->red=0;
546 RightRotate(x->parent);
547 x=rootLeft; /* this is to exit while loop */
548 }
549 }
550 }
551 x->red=0;
552
553 #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
554 CheckAssumptions();
555 #elif defined(DEBUG_ASSERT)
556 assert(!nil->red,"nil not black in ITDeleteFixUp");
557 assert((nil->maxHigh=MIN_INT),
558 "nil->maxHigh != MIN_INT in ITDeleteFixUp");
559 #endif
560 }
561
562
563 /***********************************************************************/
564 /* FUNCTION: DeleteNode */
565 /**/
566 /* INPUTS: tree is the tree to delete node z from */
567 /**/
568 /* OUTPUT: returns the Interval stored at deleted node */
569 /**/
570 /* EFFECT: Deletes z from tree and but don't call destructor */
571 /* Then calls FixUpMaxHigh to fix maxHigh fields then calls */
572 /* ITDeleteFixUp to restore red-black properties */
573 /**/
574 /* Modifies Input: z */
575 /**/
576 /* The algorithm from this function is from _Introduction_To_Algorithms_ */
577 /***********************************************************************/
578
579 Interval * IntervalTree::DeleteNode(IntervalTreeNode * z){
580 IntervalTreeNode* y;
581 IntervalTreeNode* x;
582 Interval * returnValue = z->storedInterval;
583
584 y= ((z->left == nil) || (z->right == nil)) ? z : GetSuccessorOf(z);
585 x= (y->left == nil) ? y->right : y->left;
586 if (root == (x->parent = y->parent)) { /* assignment of y->p to x->p is intentional */
587 root->left=x;
588 } else {
589 if (y == y->parent->left) {
590 y->parent->left=x;
591 } else {
592 y->parent->right=x;
593 }
594 }
595 if (y != z) { /* y should not be nil in this case */
596
597 #ifdef DEBUG_ASSERT
598 assert( (y!=nil),"y is nil in DeleteNode \n");
599 #endif
600 /* y is the node to splice out and x is its child */
601
602 y->maxHigh = MIN_INT;
603 y->left=z->left;
604 y->right=z->right;
605 y->parent=z->parent;
606 z->left->parent=z->right->parent=y;
607 if (z == z->parent->left) {
608 z->parent->left=y;
609 } else {
610 z->parent->right=y;
611 }
612 FixUpMaxHigh(x->parent);
613 if (!(y->red)) {
614 y->red = z->red;
615 DeleteFixUp(x);
616 } else
617 y->red = z->red;
618 delete z;
619 #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
620 CheckAssumptions();
621 #elif defined(DEBUG_ASSERT)
622 assert(!nil->red,"nil not black in ITDelete");
623 assert((nil->maxHigh=MIN_INT),"nil->maxHigh != MIN_INT in ITDelete");
624 #endif
625 } else {
626 FixUpMaxHigh(x->parent);
627 if (!(y->red)) DeleteFixUp(x);
628 delete y;
629 #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
630 CheckAssumptions();
631 #elif defined(DEBUG_ASSERT)
632 assert(!nil->red,"nil not black in ITDelete");
633 assert((nil->maxHigh=MIN_INT),"nil->maxHigh != MIN_INT in ITDelete");
634 #endif
635 }
636 return returnValue;
637 }
638
639
640 /***********************************************************************/
641 /* FUNCTION: Overlap */
642 /**/
643 /* INPUTS: [a1,a2] and [b1,b2] are the low and high endpoints of two */
644 /* closed intervals. */
645 /**/
646 /* OUTPUT: stack containing pointers to the nodes between [low,high] */
647 /**/
648 /* Modifies Input: none */
649 /**/
650 /* EFFECT: returns 1 if the intervals overlap, and 0 otherwise */
651 /***********************************************************************/
652
653 int Overlap(int a1, int a2, int b1, int b2) {
654 if (a1 <= b1) {
655 return( (b1 <= a2) );
656 } else {
657 return( (a1 <= b2) );
658 }
659 }
660
661
662 /***********************************************************************/
663 /* FUNCTION: Enumerate */
664 /**/
665 /* INPUTS: tree is the tree to look for intervals overlapping the */
666 /* closed interval [low,high] */
667 /**/
668 /* OUTPUT: stack containing pointers to the nodes overlapping */
669 /* [low,high] */
670 /**/
671 /* Modifies Input: none */
672 /**/
673 /* EFFECT: Returns a stack containing pointers to nodes containing */
674 /* intervals which overlap [low,high] in O(max(N,k*log(N))) */
675 /* where N is the number of intervals in the tree and k is */
676 /* the number of overlapping intervals */
677 /**/
678 /* Note: This basic idea for this function comes from the */
679 /* _Introduction_To_Algorithms_ book by Cormen et al, but */
680 /* modifications were made to return all overlapping intervals */
681 /* instead of just the first overlapping interval as in the */
682 /* book. The natural way to do this would require recursive */
683 /* calls of a basic search function. I translated the */
684 /* recursive version into an interative version with a stack */
685 /* as described below. */
686 /***********************************************************************/
687
688
689
690 /* The basic idea for the function below is to take the IntervalSearch */
691 /* function from the book and modify to find all overlapping intervals */
692 /* instead of just one. This means that any time we take the left */
693 /* branch down the tree we must also check the right branch if and only if */
694 /* we find an overlapping interval in that left branch. Note this is a */
695 /* recursive condition because if we go left at the root then go left */
696 /* again at the first left child and find an overlap in the left subtree */
697 /* of the left child of root we must recursively check the right subtree */
698 /* of the left child of root as well as the right child of root. */
699
700 vector<void *> IntervalTree::Enumerate(int low, int high) {
701 vector<void *> enumResultStack;
702 IntervalTreeNode* x=root->left;
703 int stuffToDo = (x != nil);
704
705 // Possible speed up: add min field to prune right searches //
706
707 #ifdef DEBUG_ASSERT
708 assert((recursionNodeStackTop == 1),
709 "recursionStack not empty when entering IntervalTree::Enumerate");
710 #endif
711 currentParent = 0;
712
713 while(stuffToDo) {
714 if (Overlap(low,high,x->key,x->high) ) {
715 enumResultStack.push_back(x->storedInterval);
716 recursionNodeStack[currentParent].tryRightBranch=true;
717 }
718 if(x->left->maxHigh >= low) { // implies x != nil
719 if ( recursionNodeStackTop == recursionNodeStackSize ) {
720 recursionNodeStackSize *= 2;
721 recursionNodeStack = (it_recursion_node *)
722 realloc(recursionNodeStack,
723 recursionNodeStackSize * sizeof(it_recursion_node));
724 if (recursionNodeStack == NULL)
725 assert("realloc failed in IntervalTree::Enumerate\n");
726 }
727 recursionNodeStack[recursionNodeStackTop].start_node = x;
728 recursionNodeStack[recursionNodeStackTop].tryRightBranch = 0;
729 recursionNodeStack[recursionNodeStackTop].parentIndex = currentParent;
730 currentParent = recursionNodeStackTop++;
731 x = x->left;
732 } else {
733 x = x->right;
734 }
735 stuffToDo = (x != nil);
736 while( (!stuffToDo) && (recursionNodeStackTop > 1) ) {
737 if(recursionNodeStack[--recursionNodeStackTop].tryRightBranch) {
738 x=recursionNodeStack[recursionNodeStackTop].start_node->right;
739 currentParent=recursionNodeStack[recursionNodeStackTop].parentIndex;
740 recursionNodeStack[currentParent].tryRightBranch=true;
741 stuffToDo = ( x != nil);
742 }
743 }
744 }
745 #ifdef DEBUG_ASSERT
746 assert((recursionNodeStackTop == 1),
747 "recursionStack not empty when exiting IntervalTree::Enumerate");
748 #endif
749 return enumResultStack;
750 }
751
752
753
754 int IntervalTree::CheckMaxHighFieldsHelper(IntervalTreeNode * y,
755 const int currentHigh,
756 int match) const
757 {
758 if (y != nil) {
759 match = CheckMaxHighFieldsHelper(y->left,currentHigh,match) ?
760 1 : match;
761 assert(y->high <= currentHigh);
762 if (y->high == currentHigh)
763 match = 1;
764 match = CheckMaxHighFieldsHelper(y->right,currentHigh,match) ?
765 1 : match;
766 }
767 return match;
768 }
769
770
771
772 /* Make sure the maxHigh fields for everything makes sense. *
773 * If something is wrong, print a warning and exit */
774 void IntervalTree::CheckMaxHighFields(IntervalTreeNode * x) const {
775 if (x != nil) {
776 CheckMaxHighFields(x->left);
777 if(!(CheckMaxHighFieldsHelper(x,x->maxHigh,0) > 0)) {
778 assert("error found in CheckMaxHighFields.\n");
779 }
780 CheckMaxHighFields(x->right);
781 }
782 }
783
784 void IntervalTree::CheckAssumptions() const {
785 assert(nil->key == MIN_INT);
786 assert(nil->high == MIN_INT);
787 assert(nil->maxHigh == MIN_INT);
788 assert(root->key == MAX_INT);
789 assert(root->high == MAX_INT);
790 assert(root->maxHigh == MAX_INT);
791 assert(nil->storedInterval == NULL);
792 assert(root->storedInterval == NULL);
793 assert(nil->red == 0);
794 assert(root->red == 0);
795 CheckMaxHighFields(root->left);
796 }
797
798
799