-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntervalTreeTest.cpp
84 lines (77 loc) · 2.41 KB
/
IntervalTreeTest.cpp
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
#include <iostream>
#include <stdlib.h>
#include "IntervalTree.h"
using namespace std;
int intersect(vector<Interval*> intervals, Interval *x){
int result = 0;
for (int i = 0; i < intervals.size(); i++)
if (intervals[i]->doesOverlap(x)) result += 1;
return result;
}
int main(int argc, char *argv[]){
IntervalTree *t = new IntervalTree();
vector<Interval*> intervals;
vector<int> nodeIds;
srand(time(NULL));
int numT = atoi(argv[1]);
int T = 100;
for (int i = 0; i < numT; i++){
int start = rand() % T;
int end = rand() % T;
if (start > end){
int temp = start;
start = end;
end = temp;
}
if (rand () % 10 == 0) end = -1;
Interval *interval = new Interval(start, end);
intervals.push_back(new Interval(start,end));
int u = rand() % 100;
nodeIds.push_back(u);
// cout << i << ": " << "Created Interval ( " << start << ", " << end << ", " << u << ")" << endl << flush;
t->add(u, interval);
// cout << "************TREE START*****************" << endl;
// t->printTree();
// cout << "***********TREE END********************" << endl;
}
for (int i = 0; i < numT; i++){
Interval *x = intervals[i];
vector<IntervalNode*> foundIntervals;
t->search(x, foundIntervals);
vector<Interval*> actualIntervals;
vector<int> actualNodeIds;
for (int j = 0; j < numT; j++)
if (intervals[j]->doesOverlap(x)){
actualIntervals.push_back(intervals[j]);
actualNodeIds.push_back(nodeIds[j]);
}
for (int j = 0; j < actualIntervals.size(); j++){
Interval *a = actualIntervals[j];
bool flag = false;
for (int k = 0; k < foundIntervals.size(); k++){
Interval *f = foundIntervals[k]->interval;
if ((f->getStartTime() == a->getStartTime()) && (f->getEndTime() == a->getEndTime())){
flag = true;
break;
}
}
if (flag == false){
cout << "Did not find interval (" << a->getStartTime() << ", " << a->getEndTime() << "," << actualNodeIds[j] << ")";
cout << " for (" << x->getStartTime() << ", " << x->getEndTime() << ")" << endl;
}
}
int a = foundIntervals.size();
int b = intersect(intervals,x);
int c = actualIntervals.size();
if ((a != b) || (b != c) || (a!=c))
cout << a << ", " << b << ", " << c << endl;
}
cout << "main: CP5 " << endl << flush;
for (int i = 0; i< numT; i++){
//Interval *x = intervals.back();
//intervals.pop_back();
Interval *x = intervals[i];
IntervalNode *n = t->findInterval(t->root,nodeIds[i],x);
t->removeNode(n);
}
}