-
Notifications
You must be signed in to change notification settings - Fork 1
/
intervalset.h
174 lines (145 loc) · 3.65 KB
/
intervalset.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
#ifndef JWUTIL_INTERVALSET_H
#define JWUTIL_INTERVALSET_H
#include <set>
#include <assert.h>
namespace jw_util
{
template <typename Type>
class IntervalSet
{
public:
struct Interval
{
Type offset;
mutable Type limit;
bool operator<(const Interval &interval) const
{
return offset < interval.offset;
}
bool operator<(Type val) const
{
return offset < val;
}
};
typedef std::set<Interval> Set;
void insert(Type offset, Type limit)
{
Interval interval;
interval.offset = offset;
interval.limit = limit;
insert(interval);
}
void insert(Interval interval)
{
typename Set::iterator next = set.upper_bound(interval);
typename Set::iterator prev = next;
if (prev != set.begin() && (--prev)->limit >= interval.offset)
{
if (interval.limit > prev->limit)
{
prev->limit = interval.limit;
}
}
else
{
prev = set.insert(next, interval);
}
while (next != set.end() && prev->limit >= next->offset)
{
#ifndef NDEBUG
typename Set::iterator next2 = prev;
next2++;
assert(next == next2);
#endif
if (next->limit > prev->limit)
{
prev->limit = next->limit;
}
next = set.erase(next);
}
}
void remove(Type offset, Type limit)
{
typename Set::iterator next = set.upper_bound(offset);
typename Set::iterator prev = next;
if (prev != set.begin() && (--prev)->limit > offset) {
prev->limit = offset;
}
while (next != set.end() && next->offset < limit) {
if (next->limit > limit) {
next->offset = limit;
break;
} else {
next = set.erase(next);
}
}
}
bool intersects(Type offset, Type limit)
{
typename Set::iterator i = set.upper_bound(offset);
if (i != set.end() && i->offset < limit) {
return true;
}
if (i != set.begin() && (--i)->limit > offset) {
return true;
}
return false;
}
void merge(Type max_gap)
{
typename Set::iterator prev = set.begin();
if (prev == set.end()) {return;}
typename Set::iterator next = prev;
next++;
while (next != set.end())
{
#ifndef NDEBUG
typename Set::iterator next2 = prev;
next2++;
assert(next == next2);
#endif
if (prev->limit + max_gap >= next->offset)
{
prev->limit = next->limit;
next = set.erase(next);
}
else
{
prev++;
next++;
}
}
}
void assert_increasing()
{
Type prev;
typename Set::iterator i = set.cbegin();
while (i != set.cend())
{
if (i != set.cbegin())
{
assert(prev < i->offset);
}
assert(i->offset < i->limit);
prev = i->limit;
i++;
}
}
void clear()
{
set.clear();
}
Set &get_set() {return set;}
const Set &get_set() const {return set;}
typename Set::size_type size() const {return set.size();}
bool empty() const {return set.empty();}
protected:
Set set;
#ifdef INTERVALSET_EXPAND_LAST_CHECK
// TODO: Implement this
bool valid_last = false;
Set::iterator last;
#endif
};
}
#endif // JWUTIL_INTERVALSET_H