1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2 * Use of this file is governed by the BSD 3-clause license that
3 * can be found in the LICENSE.txt file in the project root.
6 #include "misc/MurmurHash.h"
8 #include "Exceptions.h"
9 #include "Vocabulary.h"
11 #include "misc/IntervalSet.h"
13 using namespace antlr4;
14 using namespace antlr4::misc;
16 IntervalSet const IntervalSet::COMPLETE_CHAR_SET =
17 IntervalSet::of(Lexer::MIN_CHAR_VALUE, Lexer::MAX_CHAR_VALUE);
19 IntervalSet const IntervalSet::EMPTY_SET;
21 IntervalSet::IntervalSet() : _intervals() {
24 IntervalSet::IntervalSet(const IntervalSet &set) : IntervalSet() {
25 _intervals = set._intervals;
28 IntervalSet::IntervalSet(IntervalSet&& set) : IntervalSet(std::move(set._intervals)) {
31 IntervalSet::IntervalSet(std::vector<Interval>&& intervals) : _intervals(std::move(intervals)) {
34 IntervalSet& IntervalSet::operator=(const IntervalSet& other) {
35 _intervals = other._intervals;
39 IntervalSet& IntervalSet::operator=(IntervalSet&& other) {
40 _intervals = move(other._intervals);
44 IntervalSet IntervalSet::of(ssize_t a) {
45 return IntervalSet({ Interval(a, a) });
48 IntervalSet IntervalSet::of(ssize_t a, ssize_t b) {
49 return IntervalSet({ Interval(a, b) });
52 void IntervalSet::clear() {
56 void IntervalSet::add(ssize_t el) {
60 void IntervalSet::add(ssize_t a, ssize_t b) {
64 void IntervalSet::add(const Interval &addition) {
65 if (addition.b < addition.a) {
69 // find position in list
70 for (auto iterator = _intervals.begin(); iterator != _intervals.end(); ++iterator) {
71 Interval r = *iterator;
76 if (addition.adjacent(r) || !addition.disjoint(r)) {
77 // next to each other, make a single larger interval
78 Interval bigger = addition.Union(r);
81 // make sure we didn't just create an interval that
82 // should be merged with next interval in list
83 while (iterator + 1 != _intervals.end()) {
84 Interval next = *++iterator;
85 if (!bigger.adjacent(next) && bigger.disjoint(next)) {
89 // if we bump up against or overlap next, merge
90 iterator = _intervals.erase(iterator);// remove this one
91 --iterator; // move backwards to what we just set
92 *iterator = bigger.Union(next); // set to 3 merged ones
93 // ml: no need to advance iterator, we do that in the next round anyway. ++iterator; // first call to next after previous duplicates the result
98 if (addition.startsBeforeDisjoint(r)) {
101 _intervals.insert(iterator, addition);
105 // if disjoint and after r, a future iteration will handle it
108 // ok, must be after last interval (and disjoint from last interval)
110 _intervals.push_back(addition);
113 IntervalSet IntervalSet::Or(const std::vector<IntervalSet> &sets) {
115 for (const auto &s : sets) {
121 IntervalSet& IntervalSet::addAll(const IntervalSet &set) {
122 // walk set and add each interval
123 for (auto const& interval : set._intervals) {
129 IntervalSet IntervalSet::complement(ssize_t minElement, ssize_t maxElement) const {
130 return complement(IntervalSet::of(minElement, maxElement));
133 IntervalSet IntervalSet::complement(const IntervalSet &vocabulary) const {
134 return vocabulary.subtract(*this);
137 IntervalSet IntervalSet::subtract(const IntervalSet &other) const {
138 return subtract(*this, other);
141 IntervalSet IntervalSet::subtract(const IntervalSet &left, const IntervalSet &right) {
142 if (left.isEmpty()) {
143 return IntervalSet();
146 if (right.isEmpty()) {
147 // right set has no elements; just return the copy of the current set
151 IntervalSet result(left);
154 while (resultI < result._intervals.size() && rightI < right._intervals.size()) {
155 Interval &resultInterval = result._intervals[resultI];
156 const Interval &rightInterval = right._intervals[rightI];
158 // operation: (resultInterval - rightInterval) and update indexes
160 if (rightInterval.b < resultInterval.a) {
165 if (rightInterval.a > resultInterval.b) {
170 Interval beforeCurrent;
171 Interval afterCurrent;
172 if (rightInterval.a > resultInterval.a) {
173 beforeCurrent = Interval(resultInterval.a, rightInterval.a - 1);
176 if (rightInterval.b < resultInterval.b) {
177 afterCurrent = Interval(rightInterval.b + 1, resultInterval.b);
180 if (beforeCurrent.a > -1) { // -1 is the default value
181 if (afterCurrent.a > -1) {
182 // split the current interval into two
183 result._intervals[resultI] = beforeCurrent;
184 result._intervals.insert(result._intervals.begin() + resultI + 1, afterCurrent);
188 // replace the current interval
189 result._intervals[resultI] = beforeCurrent;
193 if (afterCurrent.a > -1) {
194 // replace the current interval
195 result._intervals[resultI] = afterCurrent;
198 // remove the current interval (thus no need to increment resultI)
199 result._intervals.erase(result._intervals.begin() + resultI);
204 // If rightI reached right.intervals.size(), no more intervals to subtract from result.
205 // If resultI reached result.intervals.size(), we would be subtracting from an empty set.
206 // Either way, we are done.
210 IntervalSet IntervalSet::Or(const IntervalSet &a) const {
212 result.addAll(*this);
217 IntervalSet IntervalSet::And(const IntervalSet &other) const {
218 IntervalSet intersection;
222 // iterate down both interval lists looking for nondisjoint intervals
223 while (i < _intervals.size() && j < other._intervals.size()) {
224 Interval mine = _intervals[i];
225 Interval theirs = other._intervals[j];
227 if (mine.startsBeforeDisjoint(theirs)) {
228 // move this iterator looking for interval that might overlap
230 } else if (theirs.startsBeforeDisjoint(mine)) {
231 // move other iterator looking for interval that might overlap
233 } else if (mine.properlyContains(theirs)) {
234 // overlap, add intersection, get next theirs
235 intersection.add(mine.intersection(theirs));
237 } else if (theirs.properlyContains(mine)) {
238 // overlap, add intersection, get next mine
239 intersection.add(mine.intersection(theirs));
241 } else if (!mine.disjoint(theirs)) {
242 // overlap, add intersection
243 intersection.add(mine.intersection(theirs));
245 // Move the iterator of lower range [a..b], but not
246 // the upper range as it may contain elements that will collide
247 // with the next iterator. So, if mine=[0..115] and
248 // theirs=[115..200], then intersection is 115 and move mine
249 // but not theirs as theirs may collide with the next range
251 // move both iterators to next ranges
252 if (mine.startsAfterNonDisjoint(theirs)) {
254 } else if (theirs.startsAfterNonDisjoint(mine)) {
263 bool IntervalSet::contains(size_t el) const {
264 return contains(symbolToNumeric(el));
267 bool IntervalSet::contains(ssize_t el) const {
268 if (_intervals.empty())
271 if (el < _intervals[0].a) // list is sorted and el is before first interval; not here
274 for (const auto &interval : _intervals) {
275 if (el >= interval.a && el <= interval.b) {
276 return true; // found in this interval
282 bool IntervalSet::isEmpty() const {
283 return _intervals.empty();
286 ssize_t IntervalSet::getSingleElement() const {
287 if (_intervals.size() == 1) {
288 if (_intervals[0].a == _intervals[0].b) {
289 return _intervals[0].a;
293 return Token::INVALID_TYPE; // XXX: this value is 0, but 0 is a valid interval range, how can that work?
296 ssize_t IntervalSet::getMaxElement() const {
297 if (_intervals.empty()) {
298 return Token::INVALID_TYPE;
301 return _intervals.back().b;
304 ssize_t IntervalSet::getMinElement() const {
305 if (_intervals.empty()) {
306 return Token::INVALID_TYPE;
309 return _intervals[0].a;
312 std::vector<Interval> const& IntervalSet::getIntervals() const {
316 size_t IntervalSet::hashCode() const {
317 size_t hash = MurmurHash::initialize();
318 for (const auto &interval : _intervals) {
319 hash = MurmurHash::update(hash, interval.a);
320 hash = MurmurHash::update(hash, interval.b);
323 return MurmurHash::finish(hash, _intervals.size() * 2);
326 bool IntervalSet::operator == (const IntervalSet &other) const {
327 if (_intervals.empty() && other._intervals.empty())
330 if (_intervals.size() != other._intervals.size())
333 return std::equal(_intervals.begin(), _intervals.end(), other._intervals.begin());
336 std::string IntervalSet::toString() const {
337 return toString(false);
340 std::string IntervalSet::toString(bool elemAreChar) const {
341 if (_intervals.empty()) {
345 std::stringstream ss;
346 size_t effectiveSize = size();
347 if (effectiveSize > 1) {
351 bool firstEntry = true;
352 for (const auto &interval : _intervals) {
357 ssize_t a = interval.a;
358 ssize_t b = interval.b;
362 } else if (elemAreChar) {
363 ss << "'" << static_cast<char>(a) << "'";
369 ss << "'" << static_cast<char>(a) << "'..'" << static_cast<char>(b) << "'";
371 ss << a << ".." << b;
375 if (effectiveSize > 1) {
382 std::string IntervalSet::toString(const std::vector<std::string> &tokenNames) const {
383 return toString(dfa::Vocabulary::fromTokenNames(tokenNames));
386 std::string IntervalSet::toString(const dfa::Vocabulary &vocabulary) const {
387 if (_intervals.empty()) {
391 std::stringstream ss;
392 size_t effectiveSize = size();
393 if (effectiveSize > 1) {
397 bool firstEntry = true;
398 for (const auto &interval : _intervals) {
403 ssize_t a = interval.a;
404 ssize_t b = interval.b;
406 ss << elementName(vocabulary, a);
408 for (ssize_t i = a; i <= b; i++) {
412 ss << elementName(vocabulary, i);
416 if (effectiveSize > 1) {
423 std::string IntervalSet::elementName(const std::vector<std::string> &tokenNames, ssize_t a) const {
424 return elementName(dfa::Vocabulary::fromTokenNames(tokenNames), a);
427 std::string IntervalSet::elementName(const dfa::Vocabulary &vocabulary, ssize_t a) const {
430 } else if (a == -2) {
433 return vocabulary.getDisplayName(a);
437 size_t IntervalSet::size() const {
439 for (const auto &interval : _intervals) {
440 result += size_t(interval.b - interval.a + 1);
445 std::vector<ssize_t> IntervalSet::toList() const {
446 std::vector<ssize_t> result;
447 for (const auto &interval : _intervals) {
448 ssize_t a = interval.a;
449 ssize_t b = interval.b;
450 for (ssize_t v = a; v <= b; v++) {
457 std::set<ssize_t> IntervalSet::toSet() const {
458 std::set<ssize_t> result;
459 for (const auto &interval : _intervals) {
460 ssize_t a = interval.a;
461 ssize_t b = interval.b;
462 for (ssize_t v = a; v <= b; v++) {
469 ssize_t IntervalSet::get(size_t i) const {
471 for (const auto &interval : _intervals) {
472 ssize_t a = interval.a;
473 ssize_t b = interval.b;
474 for (ssize_t v = a; v <= b; v++) {
484 void IntervalSet::remove(size_t el) {
485 remove(symbolToNumeric(el));
488 void IntervalSet::remove(ssize_t el) {
489 for (size_t i = 0; i < _intervals.size(); ++i) {
490 Interval &interval = _intervals[i];
491 ssize_t a = interval.a;
492 ssize_t b = interval.b;
494 break; // list is sorted and el is before this interval; not here
497 // if whole interval x..x, rm
498 if (el == a && el == b) {
499 _intervals.erase(_intervals.begin() + (long)i);
502 // if on left edge x..b, adjust left
507 // if on right edge a..x, adjust right
512 // if in middle a..x..b, split interval
513 if (el > a && el < b) { // found in this interval
514 ssize_t oldb = interval.b;
515 interval.b = el - 1; // [a..x-1]
516 add(el + 1, oldb); // add [x+1..b]
518 break; // ml: not in the Java code but I believe we also should stop searching here, as we found x.