forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
interval_map.h
358 lines (298 loc) · 9.93 KB
/
interval_map.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
/* -*- Mode: C++ -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ``interval_map''
*
* The Initial Developer of the Original Code is Netscape
* Communications Corp. Portions created by the Initial Developer are
* Copyright (C) 2001 the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chris Waterson <waterson@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef interval_map_h__
#define interval_map_h__
/*
A utility class that maps an interval to an object, allowing clients
to look up the object by a point within the interval.
*/
// TODO:
// - removing intervals
// - container iterators
#include <fstream>
#include <assert.h>
template<class coord, class T>
class interval_map {
protected:
class const_iterator;
friend class const_iterator;
struct node {
T m_data;
coord m_min;
coord m_max;
node *m_before; // intervals before this one
node *m_within; // intervals within this one
node *m_after; // intervals after this one
int m_bal;
};
public:
/**
* A unidirectional const iterator that is used to enumerate the
* intervals that overlap a specific point.
*/
class const_iterator {
protected:
const node *m_node;
const coord m_point;
friend class interval_map;
const_iterator(const node *n, const coord &point)
: m_node(n), m_point(point) {}
void advance();
public:
const_iterator() : m_node(0), m_point(0) {}
const_iterator(const const_iterator &iter)
: m_node(iter.m_node), m_point(iter.m_point) {}
const_iterator &
operator=(const const_iterator &iter) {
m_node = iter.m_node;
m_point = iter.m_point; }
const T &
operator*() const { return m_node->m_data; }
const T *
operator->() const { return &m_node->m_data; }
const_iterator &
operator++() { advance(); return *this; }
const_iterator
operator++(int) {
const_iterator temp(*this);
advance();
return temp; }
bool
operator==(const const_iterator &iter) const {
return m_node == iter.m_node; }
bool
operator!=(const const_iterator &iter) const {
return !iter.operator==(*this); }
};
interval_map() : m_root(0) {}
~interval_map() { delete m_root; }
/**
* Insert aData for the interval [aMin, aMax]
*/
void put(coord min, coord max, const T &data) {
put_into(&m_root, min, max, data);
#ifdef DEBUG
verify(m_root, 0);
#endif
}
/**
* Return an iterator that will enumerate the data for all intervals
* intersecting |aPoint|.
*/
const_iterator get(coord point) const;
/**
* Return an iterator that marks the end-point of iteration.
*/
const_iterator end() const {
return const_iterator(0, 0); }
protected:
void put_into(node **link, coord min, coord max, const T &data, bool *subsumed = 0);
void left_rotate(node **link, node *node);
void right_rotate(node **link, node *node);
#ifdef DEBUG
void verify(node *node, int depth);
#endif
node *m_root;
};
template<class coord, class T>
void
interval_map<coord, T>::put_into(node **root, coord min, coord max, const T &data, bool *subsumed)
{
assert(min < max);
node *interval = *root;
if (interval) {
bool before = min < interval->m_min;
bool after = max > interval->m_max;
if (!before || !after) {
// The interval we're adding does not completely subsume
// the |interval|. So we've got one of these situations:
//
// |======| |======| |======|
// |------| |--| |------|
//
// where |==| is the existing interval, and |--| is the
// new interval we're inserting. If there's left or right
// slop, then we ``split'' the new interval in half:
//
// |======| |======|
// |--|---| |---|--|
//
// and insert it both in the ``within'' and ``before'' (or
// ``after'') subtrees.
//
if (before) {
if (max > interval->m_min) {
put_into(&interval->m_within, interval->m_min, max, data);
max = interval->m_min;
}
bool was_subsumed = true;
put_into(&interval->m_before, min, max, data, &was_subsumed);
if (! was_subsumed) {
if (interval->m_bal < 0) {
if (interval->m_before->m_bal > 0)
left_rotate(&interval->m_before, interval->m_before);
right_rotate(root, interval);
}
else
--interval->m_bal;
if (subsumed)
*subsumed = (interval->m_bal == 0);
}
return;
}
if (after) {
if (min < interval->m_max) {
put_into(&interval->m_within, min, interval->m_max, data);
min = interval->m_max;
}
bool was_subsumed = true;
put_into(&interval->m_after, min, max, data, &was_subsumed);
if (! was_subsumed) {
if (interval->m_bal > 0) {
if (interval->m_after->m_bal < 0)
right_rotate(&interval->m_after, interval->m_after);
left_rotate(root, interval);
}
else
++interval->m_bal;
if (subsumed)
*subsumed = (interval->m_bal == 0);
}
return;
}
put_into(&interval->m_within, min, max, data);
return;
}
// If we get here, the interval we're adding completely
// subsumes |interval|. We'll go ahead and insert a new
// interval immediately above |interval|, with |interval| as
// the new interval's |m_within|.
}
if (subsumed)
*subsumed = false;
node *n = new node();
n->m_data = data;
n->m_before = n->m_after = 0;
n->m_min = min;
n->m_max = max;
n->m_within = interval;
n->m_bal = 0;
*root = n;
}
/*
* (*link) (*link)
* | == left rotate ==> |
* (x) (y)
* / \ / \
* a (y) <== right rotate == (x) c
* / \ / \
* b c a b
*/
template<class coord, class T>
void
interval_map<coord, T>::left_rotate(node **link, node *x)
{
node *y = x->m_after;
x->m_after = y->m_before;
*link = y;
y->m_before = x;
--x->m_bal;
--y->m_bal;
}
template<class coord, class T>
void
interval_map<coord, T>::right_rotate(node **link, node *y)
{
node *x = y->m_before;
y->m_before = x->m_after;
*link = x;
x->m_after = y;
++y->m_bal;
++x->m_bal;
}
template<class coord, class T>
interval_map<coord, T>::const_iterator
interval_map<coord, T>::get(coord point) const
{
node *interval = m_root;
while (interval) {
if (point < interval->m_min)
interval = interval->m_before;
else if (point > interval->m_max)
interval = interval->m_after;
else
break;
}
return const_iterator(interval, point);
}
template<class coord, class T>
void
interval_map<coord, T>::const_iterator::advance()
{
assert(m_node);
m_node = m_node->m_within;
while (m_node) {
if (m_point < m_node->m_min)
m_node = m_node->m_before;
else if (m_point > m_node->m_max)
m_node = m_node->m_after;
else
break;
}
}
#ifdef DEBUG
template<class coord, class T>
void
interval_map<coord, T>::verify(node<coord, T> *node, int depth)
{
if (node->m_after)
verify(node->m_after, depth + 1);
for (int i = 0; i < depth; ++i)
cout << " ";
hex(cout);
cout << node << "(";
dec(cout);
cout << node->m_bal << ")";
hex(cout);
cout << "[" << node->m_min << "," << node->m_max << "]";
cout << "@" << node->m_data;
cout << endl;
if (node->m_before)
verify(node->m_before, depth + 1);
}
#endif // DEBUG
#endif // interval_map_h__