-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntervals.h
303 lines (275 loc) · 8.5 KB
/
Intervals.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
#pragma once
#include "XException.h" // RUNTIME_ERROR()
#include <concepts> // std::integral<>
#include <iterator> // std::input_iterator<>
#include <limits> // std::numeric_limits<>
#include <ostream> // std::basic_ostream<>
#include <utility> // std::cmp_less_equal()
#include <vector> // std::vector<>
namespace bux {
//
// Types
//
template<class T>
concept IntervalPt = std::integral<T> && !std::same_as<char,T>;
template<IntervalPt T>
class C_Intervals
{
public:
// Types
typedef std::pair<T,T> value_type;
typedef typename std::vector<value_type>::const_iterator const_iterator;
// Ctors
C_Intervals() {}
C_Intervals(const C_Intervals &other): m_Intervals(other.m_Intervals) {}
C_Intervals(C_Intervals &&other) { swap(other); }
C_Intervals(T id);
C_Intervals(value_type i);
C_Intervals(T start, T end): C_Intervals(value_type(start, end)) {}
template<std::input_iterator I>
C_Intervals(I start, I end) requires std::same_as<char, std::remove_cvref_t<decltype(*start)>>
{
while (start != end)
operator|=(T(*start++)); // The cast can be an undefined behavior but I don't care for now.
}
template<std::input_iterator I>
C_Intervals(I start, I end) requires
std::same_as<value_type, std::remove_cvref_t<decltype(*start)>> ||
(
!std::same_as<char,std::remove_cvref_t<decltype(*start)>> &&
std::integral<std::remove_cvref_t<decltype(*start)>> &&
std::cmp_less_equal(std::numeric_limits<T>::min(), std::numeric_limits<std::remove_cvref_t<decltype(*start)>>::min()) &&
std::cmp_less_equal(std::numeric_limits<std::remove_cvref_t<decltype(*start)>>::max(), std::numeric_limits<T>::max())
)
{
while (start != end)
operator|=(*start++);
}
// Nonvirtuals - Assignments
void operator=(const C_Intervals &other) { m_Intervals =other.m_Intervals; }
void operator=(C_Intervals &&other) { swap(other); }
void operator|=(const C_Intervals &other);
void operator&=(const C_Intervals &other);
void operator-=(const C_Intervals &other);
bool operator==(const C_Intervals &other) const { return m_Intervals == other.m_Intervals; }
// Nonvirtuals - Immutables
const_iterator begin() const { return m_Intervals.begin(); }
const_iterator end() const { return m_Intervals.end(); }
bool empty() const { return m_Intervals.empty(); }
size_t size() const { return m_Intervals.size(); }
// Nonvirtuals - Mutables
void complement();
void swap(C_Intervals &other) { m_Intervals.swap(other.m_Intervals); }
private:
// Data
std::vector<value_type> m_Intervals;
};
//
// Function Templates
//
template<IntervalPt T, class charT, class traits=std::char_traits<charT>>
std::basic_ostream<charT,traits> &
operator<<(std::basic_ostream<charT,traits> &out, const C_Intervals<T> &x)
{
bool first =true;
for (auto i: x)
{
out <<(first?"{[":", [") <<i.first <<',' <<i.second <<']';
first =false;
}
if (first)
out <<'{';
return out <<'}';
}
//
// Implement Class Templates
//
template<IntervalPt T>
C_Intervals<T>::C_Intervals(T id)
{
m_Intervals.emplace_back(id, id);
}
template<IntervalPt T>
C_Intervals<T>::C_Intervals(value_type i)
{
if (i.first > i.second)
RUNTIME_ERROR("({},{})", i.first, i.second);
m_Intervals.emplace_back(i);
}
template<IntervalPt T>
void C_Intervals<T>::operator|=(const C_Intervals<T> &other)
{
decltype(m_Intervals) dst;
const_iterator
ia =other.m_Intervals.begin(), ea =other.m_Intervals.end(),
ib =m_Intervals.begin(), eb =m_Intervals.end();
value_type temp;
bool tempInUse =false;
while (ia != ea && ib != eb)
{
if (ia->first > ib->first)
{
std::swap(ia, ib);
std::swap(ea, eb);
}
// ia->first <= ib->first for sure
if (ia->second >= ib->second)
// (*ib) devoured
++ib;
else if (ia->second < ib->first && ia->second +1 < ib->first)
// (*ia) and (*ib) disjointed
{
if (!tempInUse)
dst.emplace_back(*ia);
else
// Pending is over
{
if (temp.second +1 >= ia->first)
{
temp.second = ia->second;
dst.emplace_back(temp);
}
else
{
dst.emplace_back(temp);
dst.emplace_back(*ia);
}
tempInUse =false;
}
++ia;
}
else
// Pending interval
{
if (!tempInUse)
{
temp.first = ia->first;
tempInUse =true;
}
temp.second = ib->second;
++ia;
++ib;
}
}
if (ib != eb)
{
ia =ib;
ea =eb;
}
if (tempInUse)
{
while (ia != ea)
{
if (temp.second < ia->first && temp.second +1 < ia->first)
break;
temp.second = ia->second;
++ia;
}
dst.emplace_back(temp);
}
dst.insert(dst.end(), ia, ea);
dst.swap(m_Intervals);
}
template<IntervalPt T>
void C_Intervals<T>::operator&=(const C_Intervals<T> &other)
{
decltype(m_Intervals) dst;
const_iterator ia = other.m_Intervals.begin();
const_iterator ea =other.m_Intervals.end();
const_iterator ib =m_Intervals.begin();
const_iterator eb =m_Intervals.end();
while (ia != ea && ib != eb)
{
if (ia->first > ib->first)
{
std::swap(ia, ib);
std::swap(ea, eb);
}
// ia->first <= ib->first for sure
if (ia->second >= ib->second)
// (*ib) devoured
dst.emplace_back(*ib++);
else if (ia->second < ib->first)
// (*ia) and (*ib) disjointed
++ia;
else
// ia->first <= ib->first <= ia->second < ib->second
{
dst.emplace_back(ib->first, ia->second);
++ia;
}
}
dst.swap(m_Intervals);
}
template<IntervalPt T>
void C_Intervals<T>::operator-=(const C_Intervals<T> &other)
{
auto ib =other.m_Intervals.begin(),
eb =other.m_Intervals.end();
for (auto ia =m_Intervals.begin(); ia != m_Intervals.end() && ib != eb;)
{
if (ib->second < ia->first)
{
++ib;
continue;
}
// ib->first <= ib->second
// ia->first <= ib->second
// ia->first <= ia->second
if (ib->first <= ia->first)
{
if (ia->second <= ib->second)
// (*ia) devoured by (*ib)
ia = m_Intervals.erase(ia);
else
// LB of (*ia) is increased
{
ia->first = ib->second +1;
++ib;
}
}
else // ib->first > ia->first
{
if (ia->second <= ib->second)
{
if (ia->second >= ib->first)
ia->second = ib->first -1;
++ia;
}
else // ia->second > ib->second
{
ia = m_Intervals.insert(ia, value_type(ia->first, ib->first-1));
(++ia)->first = ib->second +1;
++ib;
}
}
} // for (auto ia =m_Intervals.begin(); ia != m_Intervals.end() && ib != eb;)
}
template<IntervalPt T>
void C_Intervals<T>::complement()
{
if (m_Intervals.empty())
{
m_Intervals.emplace_back(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
return;
}
decltype(m_Intervals) dst;
value_type t;
t.first = std::numeric_limits<T>::min();
for (const auto &i: m_Intervals)
{
if (i.first > std::numeric_limits<T>::min())
{
t.second = i.first -1;
dst.emplace_back(t);
}
t.first = i.second +1;
}
if (t.first < std::numeric_limits<T>::max())
{
t.second = std::numeric_limits<T>::max();
dst.emplace_back(t);
}
dst.swap(m_Intervals);
}
} // namespace bux