Debian Codegraph @

SeqLib/IntervalTree.h

raw symbols SCIP
1 /*Copyright (c) 2011 Erik Garrison
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE.
20 */
21
22 /* Modifed by Jeremiah Wala to switch unique_ptr to traditional
23 pointer (requiring free on destruction) */
24
25 #ifndef SEQLIB_INTERVAL_TREE_H__
26 #define SEQLIB_INTERVAL_TREE_H__
27
28 #include <vector>
29 #include <algorithm>
30 #include <iostream>
31
32 namespace SeqLib {
33
34 template <class T, typename K = std::size_t>
35 class TInterval {
36 public:
37 K start;
38 K stop;
39 T value;
40 TInterval(K s, K e, const T& v)
41 : start(s)
42 , stop(e)
43 , value(v)
44 { }
45 };
46
47 template <class T, typename K>
48 K intervalStart(const TInterval<T,K>& i) {
49 return i.start;
50 }
51
52 template <class T, typename K>
53 K intervalStop(const TInterval<T,K>& i) {
54 return i.stop;
55 }
56
57 template <class T, typename K>
58 std::ostream& operator<<(std::ostream& out, TInterval<T,K>& i) {
59 out << "Interval(" << i.start << ", " << i.stop << "): " << i.value;
60 return out;
61 }
62
63 template <class T, typename K = std::size_t>
64 class IntervalStartSorter {
65 public:
66 bool operator() (const TInterval<T,K>& a, const TInterval<T,K>& b) {
67 return a.start < b.start;
68 }
69 };
70
71 template <class T, typename K = std::size_t>
72 class TIntervalTree {
73
74 public:
75 typedef TInterval<T,K> interval;
76 typedef std::vector<interval> intervalVector;
77 typedef TIntervalTree<T,K> intervalTree;
78
79 intervalVector intervals;
80 intervalTree * left;
81 intervalTree * right;
82 K center;
83
84 // jwala added destructor
85 ~TIntervalTree<T,K>() {
86 if (left)
87 delete left;
88 if (right)
89 delete right;
90 }
91
92 TIntervalTree<T,K>(void)
93 : left(NULL)
94 , right(NULL)
95 , center(0)
96 { }
97
98 private:
99 intervalTree* copyTree(const intervalTree& orig){
100 return (new intervalTree(orig));
101 }
102 public:
103
104 TIntervalTree<T,K>(const intervalTree& other)
105 : intervals(other.intervals),
106 left(other.left ? copyTree(*other.left) : NULL),
107 right(other.right ? copyTree(*other.right) : NULL),
108 center(other.center)
109 {
110 }
111
112 public:
113
114 TIntervalTree<T,K>& operator=(const intervalTree& other) {
115 center = other.center;
116 intervals = other.intervals;
117 left = other.left ? copyTree(*other.left) : NULL;
118 right = other.right ? copyTree(*other.right) : NULL;
119 return *this;
120 }
121
122 // Note: changes the order of ivals
123 TIntervalTree<T,K>(
124 intervalVector& ivals,
125 std::size_t depth = 16,
126 std::size_t minbucket = 64,
127 K leftextent = 0,
128 K rightextent = 0,
129 std::size_t maxbucket = 512
130 )
131 : left(NULL)
132 , right(NULL)
133 {
134
135 --depth;
136 IntervalStartSorter<T,K> intervalStartSorter;
137 if (depth == 0 || (ivals.size() < minbucket && ivals.size() < maxbucket)) {
138 std::sort(ivals.begin(), ivals.end(), intervalStartSorter);
139 intervals = ivals;
140 } else {
141 if (leftextent == 0 && rightextent == 0) {
142 // sort intervals by start
143 std::sort(ivals.begin(), ivals.end(), intervalStartSorter);
144 }
145
146 K leftp = 0;
147 K rightp = 0;
148 K centerp = 0;
149
150 if (leftextent || rightextent) {
151 leftp = leftextent;
152 rightp = rightextent;
153 } else {
154 leftp = ivals.front().start;
155 std::vector<K> stops;
156 stops.resize(ivals.size());
157 transform(ivals.begin(), ivals.end(), stops.begin(), intervalStop<T,K>);
158 rightp = *max_element(stops.begin(), stops.end());
159 }
160
161 //centerp = ( leftp + rightp ) / 2;
162 centerp = ivals.at(ivals.size() / 2).start;
163 center = centerp;
164
165 intervalVector lefts;
166 intervalVector rights;
167
168 for (typename intervalVector::const_iterator i = ivals.begin(); i != ivals.end(); ++i) {
169 const interval& interval = *i;
170 if (interval.stop < center) {
171 lefts.push_back(interval);
172 } else if (interval.start > center) {
173 rights.push_back(interval);
174 } else {
175 intervals.push_back(interval);
176 }
177 }
178
179 if (!lefts.empty()) {
180 left = new intervalTree(lefts, depth, minbucket, leftp, centerp);
181 }
182 if (!rights.empty()) {
183 right = new intervalTree(rights, depth, minbucket, centerp, rightp);
184 }
185 }
186 }
187
188 intervalVector findOverlapping(K start, K stop) const {
189 intervalVector ov;
190 this->findOverlapping(start, stop, ov);
191 return ov;
192 }
193
194 void findOverlapping(K start, K stop, intervalVector& overlapping) const {
195 if (!intervals.empty() && ! (stop < intervals.front().start)) {
196 for (typename intervalVector::const_iterator i = intervals.begin(); i != intervals.end(); ++i) {
197 const interval& interval = *i;
198 if (interval.stop >= start && interval.start <= stop) {
199 overlapping.push_back(interval);
200 }
201 }
202 }
203
204 if (left && start <= center) {
205 left->findOverlapping(start, stop, overlapping);
206 }
207
208 if (right && stop >= center) {
209 right->findOverlapping(start, stop, overlapping);
210 }
211
212 }
213
214 intervalVector findContained(K start, K stop) const {
215 intervalVector contained;
216 this->findContained(start, stop, contained);
217 return contained;
218 }
219
220 void findContained(K start, K stop, intervalVector& contained) const {
221 if (!intervals.empty() && ! (stop < intervals.front().start)) {
222 for (typename intervalVector::const_iterator i = intervals.begin(); i != intervals.end(); ++i) {
223 const interval& interval = *i;
224 if (interval.start >= start && interval.stop <= stop) {
225 contained.push_back(interval);
226 }
227 }
228 }
229
230 if (left && start <= center) {
231 left->findContained(start, stop, contained);
232 }
233
234 if (right && stop >= center) {
235 right->findContained(start, stop, contained);
236 }
237
238 }
239
240 //~TIntervalTree(void) = default;
241
242 };
243
244 }
245 #endif