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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
#include "misc/MurmurHash.h"
#include "Lexer.h"
#include "Exceptions.h"
#include "Vocabulary.h"
#include "misc/IntervalSet.h"
using namespace antlr4;
using namespace antlr4::misc;
IntervalSet const IntervalSet::COMPLETE_CHAR_SET =
IntervalSet::of(Lexer::MIN_CHAR_VALUE, Lexer::MAX_CHAR_VALUE);
IntervalSet const IntervalSet::EMPTY_SET;
IntervalSet::IntervalSet() : _intervals() {
}
IntervalSet::IntervalSet(const IntervalSet &set) : IntervalSet() {
_intervals = set._intervals;
}
IntervalSet::IntervalSet(IntervalSet&& set) : IntervalSet(std::move(set._intervals)) {
}
IntervalSet::IntervalSet(std::vector<Interval>&& intervals) : _intervals(std::move(intervals)) {
}
IntervalSet& IntervalSet::operator=(const IntervalSet& other) {
_intervals = other._intervals;
return *this;
}
IntervalSet& IntervalSet::operator=(IntervalSet&& other) {
_intervals = move(other._intervals);
return *this;
}
IntervalSet IntervalSet::of(ssize_t a) {
return IntervalSet({ Interval(a, a) });
}
IntervalSet IntervalSet::of(ssize_t a, ssize_t b) {
return IntervalSet({ Interval(a, b) });
}
void IntervalSet::clear() {
_intervals.clear();
}
void IntervalSet::add(ssize_t el) {
add(el, el);
}
void IntervalSet::add(ssize_t a, ssize_t b) {
add(Interval(a, b));
}
void IntervalSet::add(const Interval &addition) {
if (addition.b < addition.a) {
return;
}
// find position in list
for (auto iterator = _intervals.begin(); iterator != _intervals.end(); ++iterator) {
Interval r = *iterator;
if (addition == r) {
return;
}
if (addition.adjacent(r) || !addition.disjoint(r)) {
// next to each other, make a single larger interval
Interval bigger = addition.Union(r);
*iterator = bigger;
// make sure we didn't just create an interval that
// should be merged with next interval in list
while (iterator + 1 != _intervals.end()) {
Interval next = *++iterator;
if (!bigger.adjacent(next) && bigger.disjoint(next)) {
break;
}
// if we bump up against or overlap next, merge
iterator = _intervals.erase(iterator);// remove this one
--iterator; // move backwards to what we just set
*iterator = bigger.Union(next); // set to 3 merged ones
// ml: no need to advance iterator, we do that in the next round anyway. ++iterator; // first call to next after previous duplicates the result
}
return;
}
if (addition.startsBeforeDisjoint(r)) {
// insert before r
//--iterator;
_intervals.insert(iterator, addition);
return;
}
// if disjoint and after r, a future iteration will handle it
}
// ok, must be after last interval (and disjoint from last interval)
// just add it
_intervals.push_back(addition);
}
IntervalSet IntervalSet::Or(const std::vector<IntervalSet> &sets) {
IntervalSet result;
for (const auto &s : sets) {
result.addAll(s);
}
return result;
}
IntervalSet& IntervalSet::addAll(const IntervalSet &set) {
// walk set and add each interval
for (auto const& interval : set._intervals) {
add(interval);
}
return *this;
}
IntervalSet IntervalSet::complement(ssize_t minElement, ssize_t maxElement) const {
return complement(IntervalSet::of(minElement, maxElement));
}
IntervalSet IntervalSet::complement(const IntervalSet &vocabulary) const {
return vocabulary.subtract(*this);
}
IntervalSet IntervalSet::subtract(const IntervalSet &other) const {
return subtract(*this, other);
}
IntervalSet IntervalSet::subtract(const IntervalSet &left, const IntervalSet &right) {
if (left.isEmpty()) {
return IntervalSet();
}
if (right.isEmpty()) {
// right set has no elements; just return the copy of the current set
return left;
}
IntervalSet result(left);
size_t resultI = 0;
size_t rightI = 0;
while (resultI < result._intervals.size() && rightI < right._intervals.size()) {
Interval &resultInterval = result._intervals[resultI];
const Interval &rightInterval = right._intervals[rightI];
// operation: (resultInterval - rightInterval) and update indexes
if (rightInterval.b < resultInterval.a) {
rightI++;
continue;
}
if (rightInterval.a > resultInterval.b) {
resultI++;
continue;
}
Interval beforeCurrent;
Interval afterCurrent;
if (rightInterval.a > resultInterval.a) {
beforeCurrent = Interval(resultInterval.a, rightInterval.a - 1);
}
if (rightInterval.b < resultInterval.b) {
afterCurrent = Interval(rightInterval.b + 1, resultInterval.b);
}
if (beforeCurrent.a > -1) { // -1 is the default value
if (afterCurrent.a > -1) {
// split the current interval into two
result._intervals[resultI] = beforeCurrent;
result._intervals.insert(result._intervals.begin() + resultI + 1, afterCurrent);
resultI++;
rightI++;
} else {
// replace the current interval
result._intervals[resultI] = beforeCurrent;
resultI++;
}
} else {
if (afterCurrent.a > -1) {
// replace the current interval
result._intervals[resultI] = afterCurrent;
rightI++;
} else {
// remove the current interval (thus no need to increment resultI)
result._intervals.erase(result._intervals.begin() + resultI);
}
}
}
// If rightI reached right.intervals.size(), no more intervals to subtract from result.
// If resultI reached result.intervals.size(), we would be subtracting from an empty set.
// Either way, we are done.
return result;
}
IntervalSet IntervalSet::Or(const IntervalSet &a) const {
IntervalSet result;
result.addAll(*this);
result.addAll(a);
return result;
}
IntervalSet IntervalSet::And(const IntervalSet &other) const {
IntervalSet intersection;
size_t i = 0;
size_t j = 0;
// iterate down both interval lists looking for nondisjoint intervals
while (i < _intervals.size() && j < other._intervals.size()) {
Interval mine = _intervals[i];
Interval theirs = other._intervals[j];
if (mine.startsBeforeDisjoint(theirs)) {
// move this iterator looking for interval that might overlap
i++;
} else if (theirs.startsBeforeDisjoint(mine)) {
// move other iterator looking for interval that might overlap
j++;
} else if (mine.properlyContains(theirs)) {
// overlap, add intersection, get next theirs
intersection.add(mine.intersection(theirs));
j++;
} else if (theirs.properlyContains(mine)) {
// overlap, add intersection, get next mine
intersection.add(mine.intersection(theirs));
i++;
} else if (!mine.disjoint(theirs)) {
// overlap, add intersection
intersection.add(mine.intersection(theirs));
// Move the iterator of lower range [a..b], but not
// the upper range as it may contain elements that will collide
// with the next iterator. So, if mine=[0..115] and
// theirs=[115..200], then intersection is 115 and move mine
// but not theirs as theirs may collide with the next range
// in thisIter.
// move both iterators to next ranges
if (mine.startsAfterNonDisjoint(theirs)) {
j++;
} else if (theirs.startsAfterNonDisjoint(mine)) {
i++;
}
}
}
return intersection;
}
bool IntervalSet::contains(ssize_t el) const {
if (_intervals.empty() || el < _intervals.front().a || el > _intervals.back().b) {
return false;
}
return std::binary_search(_intervals.begin(), _intervals.end(), Interval(el, el), [](const Interval &lhs, const Interval &rhs) {
return lhs.b < rhs.a;
});
}
bool IntervalSet::isEmpty() const {
return _intervals.empty();
}
ssize_t IntervalSet::getSingleElement() const {
if (_intervals.size() == 1) {
if (_intervals[0].a == _intervals[0].b) {
return _intervals[0].a;
}
}
return Token::INVALID_TYPE; // XXX: this value is 0, but 0 is a valid interval range, how can that work?
}
ssize_t IntervalSet::getMaxElement() const {
if (_intervals.empty()) {
return Token::INVALID_TYPE;
}
return _intervals.back().b;
}
ssize_t IntervalSet::getMinElement() const {
if (_intervals.empty()) {
return Token::INVALID_TYPE;
}
return _intervals.front().a;
}
std::vector<Interval> const& IntervalSet::getIntervals() const {
return _intervals;
}
size_t IntervalSet::hashCode() const {
size_t hash = MurmurHash::initialize();
for (const auto &interval : _intervals) {
hash = MurmurHash::update(hash, interval.a);
hash = MurmurHash::update(hash, interval.b);
}
return MurmurHash::finish(hash, _intervals.size() * 2);
}
bool IntervalSet::operator == (const IntervalSet &other) const {
if (_intervals.empty() && other._intervals.empty())
return true;
if (_intervals.size() != other._intervals.size())
return false;
return std::equal(_intervals.begin(), _intervals.end(), other._intervals.begin());
}
std::string IntervalSet::toString() const {
return toString(false);
}
std::string IntervalSet::toString(bool elemAreChar) const {
if (_intervals.empty()) {
return "{}";
}
std::stringstream ss;
size_t effectiveSize = size();
if (effectiveSize > 1) {
ss << "{";
}
bool firstEntry = true;
for (const auto &interval : _intervals) {
if (!firstEntry)
ss << ", ";
firstEntry = false;
ssize_t a = interval.a;
ssize_t b = interval.b;
if (a == b) {
if (a == -1) {
ss << "<EOF>";
} else if (elemAreChar) {
ss << "'" << static_cast<char>(a) << "'";
} else {
ss << a;
}
} else {
if (elemAreChar) {
ss << "'" << static_cast<char>(a) << "'..'" << static_cast<char>(b) << "'";
} else {
ss << a << ".." << b;
}
}
}
if (effectiveSize > 1) {
ss << "}";
}
return ss.str();
}
std::string IntervalSet::toString(const dfa::Vocabulary &vocabulary) const {
if (_intervals.empty()) {
return "{}";
}
std::stringstream ss;
size_t effectiveSize = size();
if (effectiveSize > 1) {
ss << "{";
}
bool firstEntry = true;
for (const auto &interval : _intervals) {
if (!firstEntry)
ss << ", ";
firstEntry = false;
ssize_t a = interval.a;
ssize_t b = interval.b;
if (a == b) {
ss << elementName(vocabulary, a);
} else {
for (ssize_t i = a; i <= b; i++) {
if (i > a) {
ss << ", ";
}
ss << elementName(vocabulary, i);
}
}
}
if (effectiveSize > 1) {
ss << "}";
}
return ss.str();
}
std::string IntervalSet::elementName(const dfa::Vocabulary &vocabulary, ssize_t a) const {
if (a == -1) {
return "<EOF>";
} else if (a == -2) {
return "<EPSILON>";
} else {
return vocabulary.getDisplayName(a);
}
}
size_t IntervalSet::size() const {
size_t result = 0;
for (const auto &interval : _intervals) {
result += size_t(interval.b - interval.a + 1);
}
return result;
}
std::vector<ssize_t> IntervalSet::toList() const {
std::vector<ssize_t> result;
for (const auto &interval : _intervals) {
ssize_t a = interval.a;
ssize_t b = interval.b;
for (ssize_t v = a; v <= b; v++) {
result.push_back(v);
}
}
return result;
}
std::set<ssize_t> IntervalSet::toSet() const {
std::set<ssize_t> result;
for (const auto &interval : _intervals) {
ssize_t a = interval.a;
ssize_t b = interval.b;
for (ssize_t v = a; v <= b; v++) {
result.insert(v);
}
}
return result;
}
ssize_t IntervalSet::get(size_t i) const {
size_t index = 0;
for (const auto &interval : _intervals) {
ssize_t a = interval.a;
ssize_t b = interval.b;
for (ssize_t v = a; v <= b; v++) {
if (index == i) {
return v;
}
index++;
}
}
return -1;
}
void IntervalSet::remove(ssize_t el) {
for (size_t i = 0; i < _intervals.size(); ++i) {
Interval &interval = _intervals[i];
ssize_t a = interval.a;
ssize_t b = interval.b;
if (el < a) {
break; // list is sorted and el is before this interval; not here
}
// if whole interval x..x, rm
if (el == a && el == b) {
_intervals.erase(_intervals.begin() + (long)i);
break;
}
// if on left edge x..b, adjust left
if (el == a) {
interval.a++;
break;
}
// if on right edge a..x, adjust right
if (el == b) {
interval.b--;
break;
}
// if in middle a..x..b, split interval
if (el > a && el < b) { // found in this interval
ssize_t oldb = interval.b;
interval.b = el - 1; // [a..x-1]
add(el + 1, oldb); // add [x+1..b]
break; // ml: not in the Java code but I believe we also should stop searching here, as we found x.
}
}
}
|