diff options
author | Andrius Merkys <andrius.merkys@gmail.com> | 2018-11-23 04:52:08 -0500 |
---|---|---|
committer | Andrius Merkys <andrius.merkys@gmail.com> | 2018-11-23 04:52:08 -0500 |
commit | 7b19e9be5be41c69c451b63c526bee059881f9b1 (patch) | |
tree | 699bf0523df6868d15843981ea9914ac096ee270 /runtime/Java/src/org/antlr/v4/runtime/misc/IntervalSet.java | |
parent | 1d0464db4ec5e5c20b2ae62bb3c4eceaa6840bde (diff) |
New upstream version 4.7.1
Diffstat (limited to 'runtime/Java/src/org/antlr/v4/runtime/misc/IntervalSet.java')
-rw-r--r-- | runtime/Java/src/org/antlr/v4/runtime/misc/IntervalSet.java | 66 |
1 files changed, 24 insertions, 42 deletions
diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/IntervalSet.java b/runtime/Java/src/org/antlr/v4/runtime/misc/IntervalSet.java index 27fb574..3dcb0f3 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/IntervalSet.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/IntervalSet.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2016 The ANTLR Project. All rights reserved. + * 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. */ @@ -384,30 +384,24 @@ public class IntervalSet implements IntSet { @Override public boolean contains(int el) { int n = intervals.size(); - for (int i = 0; i < n; i++) { - Interval I = intervals.get(i); + int l = 0; + int r = n - 1; + // Binary search for the element in the (sorted, + // disjoint) array of intervals. + while (l <= r) { + int m = (l + r) / 2; + Interval I = intervals.get(m); int a = I.a; int b = I.b; - if ( el<a ) { - break; // list is sorted and el is before this interval; not here - } - if ( el>=a && el<=b ) { - return true; // found in this interval + if ( b<el ) { + l = m + 1; + } else if ( a>el ) { + r = m - 1; + } else { // el >= a && el <= b + return true; } } return false; -/* - for (ListIterator iter = intervals.listIterator(); iter.hasNext();) { - Interval I = (Interval) iter.next(); - if ( el<I.a ) { - break; // list is sorted and el is before this interval; not here - } - if ( el>=I.a && el<=I.b ) { - return true; // found in this interval - } - } - return false; - */ } /** {@inheritDoc} */ @@ -416,41 +410,29 @@ public class IntervalSet implements IntSet { return intervals==null || intervals.isEmpty(); } - /** {@inheritDoc} */ - @Override - public int getSingleElement() { - if ( intervals!=null && intervals.size()==1 ) { - Interval I = intervals.get(0); - if ( I.a == I.b ) { - return I.a; - } - } - return Token.INVALID_TYPE; - } - /** - * Returns the maximum value contained in the set. + * Returns the maximum value contained in the set if not isNil(). * - * @return the maximum value contained in the set. If the set is empty, this - * method returns {@link Token#INVALID_TYPE}. + * @return the maximum value contained in the set. + * @throws RuntimeException if set is empty */ public int getMaxElement() { if ( isNil() ) { - return Token.INVALID_TYPE; + throw new RuntimeException("set is empty"); } Interval last = intervals.get(intervals.size()-1); return last.b; } /** - * Returns the minimum value contained in the set. + * Returns the minimum value contained in the set if not isNil(). * - * @return the minimum value contained in the set. If the set is empty, this - * method returns {@link Token#INVALID_TYPE}. + * @return the minimum value contained in the set. + * @throws RuntimeException if set is empty */ public int getMinElement() { if ( isNil() ) { - return Token.INVALID_TYPE; + throw new RuntimeException("set is empty"); } return intervals.get(0).a; @@ -505,11 +487,11 @@ public class IntervalSet implements IntSet { int b = I.b; if ( a==b ) { if ( a==Token.EOF ) buf.append("<EOF>"); - else if ( elemAreChar ) buf.append("'").append((char)a).append("'"); + else if ( elemAreChar ) buf.append("'").appendCodePoint(a).append("'"); else buf.append(a); } else { - if ( elemAreChar ) buf.append("'").append((char)a).append("'..'").append((char)b).append("'"); + if ( elemAreChar ) buf.append("'").appendCodePoint(a).append("'..'").appendCodePoint(b).append("'"); else buf.append(a).append("..").append(b); } if ( iter.hasNext() ) { |