Debian Codegraph @

include/2geom/intervaltree/interval_tree.h

raw
1 #ifndef E_INTERVAL_TREE
2 #define E_INTERVAL_TREE
3
4 // From Emin Martinian, licenced LGPL and MPL with permission
5
6 #include <vector>
7 #include <math.h>
8 #include <limits>
9 #include <iostream>
10
11 using std::vector;
12
13 // The interval_tree.h and interval_tree.cc files contain code for
14 // interval trees implemented using red-black-trees as described in
15 // the book _Introduction_To_Algorithms_ by Cormen, Leisserson,
16 // and Rivest.
17
18 // CONVENTIONS:
19 // Function names: Each word in a function name begins with
20 // a capital letter. An example funcntion name is
21 // CreateRedTree(a,b,c). Furthermore, each function name
22 // should begin with a capital letter to easily distinguish
23 // them from variables.
24 //
25 // Variable names: Each word in a variable name begins with
26 // a capital letter EXCEPT the first letter of the variable
27 // name. For example, int newLongInt. Global variables have
28 // names beginning with "g". An example of a global
29 // variable name is gNewtonsConstant.
30
31
32 #ifndef MAX_INT
33 #define MAX_INT INT_MAX // some architechturs define INT_MAX not MAX_INT
34 #endif
35
36 // The Interval class is an Abstract Base Class. This means that no
37 // instance of the Interval class can exist. Only classes which
38 // inherit from the Interval class can exist. Furthermore any class
39 // which inherits from the Interval class must define the member
40 // functions GetLowPoint and GetHighPoint.
41 //
42 // The GetLowPoint should return the lowest point of the interval and
43 // the GetHighPoint should return the highest point of the interval.
44
45 class Interval {
46 public:
47 Interval();
48 virtual ~Interval();
49 virtual int GetLowPoint() const = 0;
50 virtual int GetHighPoint() const = 0;
51 virtual void Print() const;
52 };
53
54 class IntervalTreeNode {
55 friend class IntervalTree;
56 public:
57 void Print(IntervalTreeNode*,
58 IntervalTreeNode*) const;
59 IntervalTreeNode();
60 IntervalTreeNode(Interval *);
61 ~IntervalTreeNode();
62 protected:
63 Interval * storedInterval;
64 int key;
65 int high;
66 int maxHigh;
67 bool red; /* if red=0 then the node is black */
68 IntervalTreeNode * left;
69 IntervalTreeNode * right;
70 IntervalTreeNode * parent;
71 };
72
73 struct it_recursion_node {
74 public:
75 /* this structure stores the information needed when we take the */
76 /* right branch in searching for intervals but possibly come back */
77 /* and check the left branch as well. */
78
79 IntervalTreeNode * start_node;
80 unsigned int parentIndex;
81 bool tryRightBranch;
82 } ;
83
84
85 class IntervalTree {
86 public:
87 IntervalTree();
88 ~IntervalTree();
89 void Print() const;
90 Interval * DeleteNode(IntervalTreeNode *);
91 IntervalTreeNode * Insert(Interval *);
92 IntervalTreeNode * GetPredecessorOf(IntervalTreeNode *) const;
93 IntervalTreeNode * GetSuccessorOf(IntervalTreeNode *) const;
94 vector<void *> Enumerate(int low, int high) ;
95 void CheckAssumptions() const;
96 protected:
97 /* A sentinel is used for root and for nil. These sentinels are */
98 /* created when ITTreeCreate is caled. root->left should always */
99 /* point to the node which is the root of the tree. nil points to a */
100 /* node which should always be black but has arbitrary children and */
101 /* parent and no key or info. The point of using these sentinels is so */
102 /* that the root and nil nodes do not require special cases in the code */
103 IntervalTreeNode * root;
104 IntervalTreeNode * nil;
105 void LeftRotate(IntervalTreeNode *);
106 void RightRotate(IntervalTreeNode *);
107 void TreeInsertHelp(IntervalTreeNode *);
108 void TreePrintHelper(IntervalTreeNode *) const;
109 void FixUpMaxHigh(IntervalTreeNode *);
110 void DeleteFixUp(IntervalTreeNode *);
111 void CheckMaxHighFields(IntervalTreeNode *) const;
112 int CheckMaxHighFieldsHelper(IntervalTreeNode * y,
113 const int currentHigh,
114 int match) const;
115 private:
116 unsigned int recursionNodeStackSize;
117 it_recursion_node * recursionNodeStack;
118 unsigned int currentParent;
119 unsigned int recursionNodeStackTop;
120 };
121
122
123 #endif
124
125
126