Package org.antlr.v4.runtime.misc
Class IntervalSet
- java.lang.Object
- 
- org.antlr.v4.runtime.misc.IntervalSet
 
- 
- All Implemented Interfaces:
- IntSet
 
 public class IntervalSet extends Object implements IntSet This class implements theIntSetbacked by a sorted array of non-overlapping intervals. It is particularly efficient for representing large collections of numbers, where the majority of elements appear as part of a sequential range of numbers that are all part of the set. For example, the set { 1, 2, 3, 4, 7, 8 } may be represented as { [1, 4], [7, 8] }.This class is able to represent sets containing any combination of values in the range Integer.MIN_VALUEtoInteger.MAX_VALUE(inclusive).
- 
- 
Field SummaryFields Modifier and Type Field Description static IntervalSetCOMPLETE_CHAR_SETstatic IntervalSetEMPTY_SETprotected List<Interval>intervalsThe list of sorted, disjoint intervals.protected booleanreadonly
 - 
Constructor SummaryConstructors Constructor Description IntervalSet(int... els)IntervalSet(List<Interval> intervals)IntervalSet(IntervalSet set)
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidadd(int el)Add a single element to the set.voidadd(int a, int b)Add interval; i.e., add all integers from a to b to set.protected voidadd(Interval addition)IntervalSetaddAll(IntSet set)Modify the currentIntSetobject to contain all elements that are present in itself, the specifiedset, or both.IntervalSetand(IntSet other)Return a newIntSetobject containing all elements that are present in both the current set and the specified seta.voidclear()IntervalSetcomplement(int minElement, int maxElement)IntervalSetcomplement(IntSet vocabulary)Return a newIntSetobject containing all elements that are present inelementsbut not present in the current set.booleancontains(int el)Returnstrueif the set contains the specified element.protected StringelementName(String[] tokenNames, int a)Deprecated.UseelementName(Vocabulary, int)instead.protected StringelementName(Vocabulary vocabulary, int a)booleanequals(Object obj)Are two IntervalSets equal? Because all intervals are sorted and disjoint, equals is a simple linear walk over both lists to make sure they are the same.intget(int i)Get the ith element of ordered set.List<Interval>getIntervals()Return a list of Interval objects.intgetMaxElement()Returns the maximum value contained in the set if not isNil().intgetMinElement()Returns the minimum value contained in the set if not isNil().inthashCode()booleanisNil()Returnstrueif this set contains no elements.booleanisReadonly()static IntervalSetof(int a)Create a set with a single element, el.static IntervalSetof(int a, int b)Create a set with all ints within range [a..b] (inclusive)static IntervalSetor(IntervalSet[] sets)combine all sets in the array returned the or'd valueIntervalSetor(IntSet a)Return a newIntSetobject containing all elements that are present in the current set, the specified seta, or both.voidremove(int el)Removes the specified value from the current set.voidsetReadonly(boolean readonly)intsize()Return the total number of elements represented by the current set.static IntervalSetsubtract(IntervalSet left, IntervalSet right)Compute the set difference between two interval sets.IntervalSetsubtract(IntSet a)Return a newIntSetobject containing all elements that are present in the current set but not present in the input seta.int[]toArray()IntegerListtoIntegerList()List<Integer>toList()Return a list containing the elements represented by the current set.Set<Integer>toSet()StringtoString()StringtoString(boolean elemAreChar)StringtoString(String[] tokenNames)Deprecated.UsetoString(Vocabulary)instead.StringtoString(Vocabulary vocabulary)
 
- 
- 
- 
Field Detail- 
COMPLETE_CHAR_SETpublic static final IntervalSet COMPLETE_CHAR_SET 
 - 
EMPTY_SETpublic static final IntervalSet EMPTY_SET 
 - 
readonlyprotected boolean readonly 
 
- 
 - 
Constructor Detail- 
IntervalSetpublic IntervalSet(IntervalSet set) 
 - 
IntervalSetpublic IntervalSet(int... els) 
 
- 
 - 
Method Detail- 
ofpublic static IntervalSet of(int a) Create a set with a single element, el.
 - 
ofpublic static IntervalSet of(int a, int b) Create a set with all ints within range [a..b] (inclusive)
 - 
clearpublic void clear() 
 - 
addpublic void add(int el) Add a single element to the set. An isolated element is stored as a range el..el.
 - 
addpublic void add(int a, int b)Add interval; i.e., add all integers from a to b to set. If b<a, do nothing. Keep list in sorted order (by left range value). If overlap, combine ranges. For example, If this is {1..5, 10..20}, adding 6..7 yields {1..5, 6..7, 10..20}. Adding 4..8 yields {1..8, 10..20}.
 - 
addprotected void add(Interval addition) 
 - 
orpublic static IntervalSet or(IntervalSet[] sets) combine all sets in the array returned the or'd value
 - 
addAllpublic IntervalSet addAll(IntSet set) Description copied from interface:IntSetModify the currentIntSetobject to contain all elements that are present in itself, the specifiedset, or both.
 - 
complementpublic IntervalSet complement(int minElement, int maxElement) 
 - 
complementpublic IntervalSet complement(IntSet vocabulary) Return a newIntSetobject containing all elements that are present inelementsbut not present in the current set. The following expressions are equivalent for input non-nullIntSetinstancesxandy.- x.complement(y)
- y.subtract(x)
 - Specified by:
- complementin interface- IntSet
- Parameters:
- vocabulary- The set to compare with the current set. A- nullargument is treated as though it were an empty set.
- Returns:
- A new IntSetinstance containing the elements present inelementsbut not present in the current set. The valuenullmay be returned in place of an empty result set.
 
 - 
subtractpublic IntervalSet subtract(IntSet a) Description copied from interface:IntSetReturn a newIntSetobject containing all elements that are present in the current set but not present in the input seta. The following expressions are equivalent for input non-nullIntSetinstancesxandy.- y.subtract(x)
- x.complement(y)
 - Specified by:
- subtractin interface- IntSet
- Parameters:
- a- The set to compare with the current set. A- nullargument is treated as though it were an empty set.
- Returns:
- A new IntSetinstance containing the elements present inelementsbut not present in the current set. The valuenullmay be returned in place of an empty result set.
 
 - 
subtractpublic static IntervalSet subtract(IntervalSet left, IntervalSet right) Compute the set difference between two interval sets. The specific operation isleft - right. If either of the input sets isnull, it is treated as though it was an empty set.
 - 
orpublic IntervalSet or(IntSet a) Description copied from interface:IntSetReturn a newIntSetobject containing all elements that are present in the current set, the specified seta, or both.This method is similar to IntSet.addAll(IntSet), but returns a newIntSetinstance instead of modifying the current set.
 - 
andpublic IntervalSet and(IntSet other) Return a newIntSetobject containing all elements that are present in both the current set and the specified seta.- Specified by:
- andin interface- IntSet
- Parameters:
- other- The set to intersect with the current set. A- nullargument is treated as though it were an empty set.
- Returns:
- A new IntSetinstance containing the intersection of the current set anda. The valuenullmay be returned in place of an empty result set.
 
 - 
containspublic boolean contains(int el) Returnstrueif the set contains the specified element.
 - 
isNilpublic boolean isNil() Returnstrueif this set contains no elements.
 - 
getMaxElementpublic int getMaxElement() Returns the maximum value contained in the set if not isNil().- Returns:
- the maximum value contained in the set.
- Throws:
- RuntimeException- if set is empty
 
 - 
getMinElementpublic int getMinElement() Returns the minimum value contained in the set if not isNil().- Returns:
- the minimum value contained in the set.
- Throws:
- RuntimeException- if set is empty
 
 - 
equalspublic boolean equals(Object obj) Are two IntervalSets equal? Because all intervals are sorted and disjoint, equals is a simple linear walk over both lists to make sure they are the same. Interval.equals() is used by the List.equals() method to check the ranges.
 - 
toStringpublic String toString(boolean elemAreChar) 
 - 
toString@Deprecated public String toString(String[] tokenNames) Deprecated.UsetoString(Vocabulary)instead.
 - 
toStringpublic String toString(Vocabulary vocabulary) 
 - 
elementName@Deprecated protected String elementName(String[] tokenNames, int a) Deprecated.UseelementName(Vocabulary, int)instead.
 - 
elementNameprotected String elementName(Vocabulary vocabulary, int a) 
 - 
sizepublic int size() Description copied from interface:IntSetReturn the total number of elements represented by the current set.
 - 
toIntegerListpublic IntegerList toIntegerList() 
 - 
toListpublic List<Integer> toList() Description copied from interface:IntSetReturn a list containing the elements represented by the current set. The list is returned in ascending numerical order.
 - 
getpublic int get(int i) Get the ith element of ordered set. Used only by RandomPhrase so don't bother to implement if you're not doing that for a new ANTLR code gen target.
 - 
toArraypublic int[] toArray() 
 - 
removepublic void remove(int el) Description copied from interface:IntSetRemoves the specified value from the current set. If the current set does not contain the element, no changes are made.
 - 
isReadonlypublic boolean isReadonly() 
 - 
setReadonlypublic void setReadonly(boolean readonly) 
 
- 
 
-