Class IntervalSet
java.lang.Object
org.graalvm.shadowed.org.antlr.v4.runtime.misc.IntervalSet
- All Implemented Interfaces:
IntSet
This class implements the
IntSet backed 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_VALUE to Integer.MAX_VALUE
(inclusive).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final IntervalSetstatic final IntervalSetThe list of sorted, disjoint intervals.protected boolean -
Constructor Summary
ConstructorsConstructorDescriptionIntervalSet(int... els) IntervalSet(List<Interval> intervals) IntervalSet(IntervalSet set) -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(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 voidModify the currentIntSetobject to contain all elements that are present in itself, the specifiedset, or both.Return a newIntSetobject containing all elements that are present in both the current set and the specified seta.voidclear()complement(int minElement, int maxElement) complement(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.protected StringelementName(Vocabulary vocabulary, int a) booleanAre 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.Return a list of Interval objects.intReturns the maximum value contained in the set if not isNil().intReturns the minimum value contained in the set if not isNil().inthashCode()booleanisNil()Returnstrueif this set contains no elements.booleanstatic 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 valueReturn 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.Return a newIntSetobject containing all elements that are present in the current set but not present in the input seta.int[]toArray()toList()Return a list containing the elements represented by the current set.toSet()toString()toString(boolean elemAreChar) Deprecated.UsetoString(Vocabulary)instead.toString(Vocabulary vocabulary)
-
Field Details
-
COMPLETE_CHAR_SET
-
EMPTY_SET
-
intervals
-
readonly
protected boolean readonly
-
-
Constructor Details
-
IntervalSet
-
IntervalSet
-
IntervalSet
public IntervalSet(int... els)
-
-
Method Details
-
of
Create a set with a single element, el. -
of
Create a set with all ints within range [a..b] (inclusive) -
clear
public void clear() -
add
-
add
public 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}. -
add
-
or
combine all sets in the array returned the or'd value -
addAll
-
complement
-
complement
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 interfaceIntSet- Parameters:
vocabulary- The set to compare with the current set. Anullargument 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.
-
subtract
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 interfaceIntSet- Parameters:
a- The set to compare with the current set. Anullargument 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.
-
subtract
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. -
or
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. -
and
Return a newIntSetobject containing all elements that are present in both the current set and the specified seta.- Specified by:
andin interfaceIntSet- Parameters:
other- The set to intersect with the current set. Anullargument 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.
-
contains
-
isNil
-
getMaxElement
public 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
-
getMinElement
public 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
-
getIntervals
-
hashCode
-
equals
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. -
toString
-
toString
-
toString
Deprecated.UsetoString(Vocabulary)instead. -
toString
-
elementName
Deprecated.UseelementName(Vocabulary, int)instead. -
elementName
-
size
-
toIntegerList
-
toList
Description copied from interface:IntSetReturn a list containing the elements represented by the current set. The list is returned in ascending numerical order. -
toSet
-
get
public 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. -
toArray
public int[] toArray() -
remove
-
isReadonly
public boolean isReadonly() -
setReadonly
public void setReadonly(boolean readonly)
-
elementName(Vocabulary, int)instead.