Top Description Fields Constructors Methods
org.antlr.v4.runtime.misc

public Class IntervalSet

extends Object
implements IntSet
Class Inheritance
All Implemented Interfaces
org.antlr.v4.runtime.misc.IntSet
Imports
org.antlr.v4.runtime.Lexer, .Token, .Vocabulary, .VocabularyImpl, java.util.ArrayList, .HashSet, .Iterator, .List, .ListIterator, .Set

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

Modifier and TypeField and Description
public static final IntervalSet
public static final IntervalSet
protected List<Interval>
intervals

The list of sorted, disjoint intervals.

protected boolean

Constructor Summary

AccessConstructor and Description
public
public
public
IntervalSet(int... els)

Method Summary

Modifier and TypeMethod and Description
public void
add(int
the value to add
el
)

Implements org.antlr.v4.runtime.misc.IntSet.add.

Add a single element to the set.
public void
add(int a, int b)

Add interval; i.e., add all integers from a to b to set.

protected void
add(Interval addition)

public IntervalSet
addAll(IntSet
The set to add to the current set. A null argument is treated as though it were an empty set.
set
)

Implements org.antlr.v4.runtime.misc.IntSet.addAll.

Modify the current IntSet object to contain all elements that are present in itself, the specified set, or both.
public IntervalSet
and(IntSet
The set to intersect with the current set. A null argument is treated as though it were an empty set.
other
)

Implements org.antlr.v4.runtime.misc.IntSet.and.

Return a new IntSet object containing all elements that are present in both the current set and the specified set a.
public void
public IntervalSet
complement(int minElement, int maxElement)

public IntervalSet
complement(IntSet
The set to compare with the current set. A null argument is treated as though it were an empty set.
vocabulary
)

Implements org.antlr.v4.runtime.misc.IntSet.complement.

Return a new IntSet object containing all elements that are present in elements but not present in the current set.
public boolean
contains(int
The element to check for.
el
)

Implements org.antlr.v4.runtime.misc.IntSet.contains.

Returns true if the set contains the specified element.
protected String
elementName(String[] tokenNames, int a)

Deprecated Use elementName(Vocabulary, int) instead.
protected String
elementName(Vocabulary vocabulary, int a)

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Implements org.antlr.v4.runtime.misc.IntSet.equals.

Are two IntervalSets equal?
public int
get(int i)

Get the ith element of ordered set.

public List<Interval>
getIntervals()

Return a list of Interval objects.

public int

Returns:

the maximum value contained in the set.
getMaxElement
()

Returns the maximum value contained in the set if not isNil().

public int

Returns:

the minimum value contained in the set.
getMinElement
()

Returns the minimum value contained in the set if not isNil().

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public boolean
isNil()

Implements org.antlr.v4.runtime.misc.IntSet.isNil.

Returns true if this set contains no elements.
public boolean
public static IntervalSet
of(int a)

Create a set with a single element, el.

public static IntervalSet
of(int a, int b)

Create a set with all ints within range [a..b] (inclusive)

public static IntervalSet
or(IntervalSet[] sets)

combine all sets in the array returned the or'd value

public IntervalSet
or(IntSet
The set to union with the current set. A null argument is treated as though it were an empty set.
a
)

Implements org.antlr.v4.runtime.misc.IntSet.or.

Return a new IntSet object containing all elements that are present in the current set, the specified set a, or both.
public void
remove(int
the value to remove
el
)

Implements org.antlr.v4.runtime.misc.IntSet.remove.

Removes the specified value from the current set.
public void
setReadonly(boolean readonly)

public int
size()

Implements org.antlr.v4.runtime.misc.IntSet.size.

Return the total number of elements represented by the current set.
public IntervalSet
subtract(IntSet
The set to compare with the current set. A null argument is treated as though it were an empty set.
a
)

Implements org.antlr.v4.runtime.misc.IntSet.subtract.

Return a new IntSet object containing all elements that are present in the current set but not present in the input set a.
public static IntervalSet
subtract(IntervalSet left, IntervalSet right)

Compute the set difference between two interval sets.

public int[]
public IntegerList
public List<Integer>
toList()

Implements org.antlr.v4.runtime.misc.IntSet.toList.

Return a list containing the elements represented by the current set.
public Set<Integer>
public String
public String
toString(boolean elemAreChar)

public String
toString(String[] tokenNames)

Deprecated Use toString(Vocabulary) instead.
public String
toString(Vocabulary vocabulary)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

COMPLETE_CHAR_SETback to summary
public static final IntervalSet COMPLETE_CHAR_SET
EMPTY_SETback to summary
public static final IntervalSet EMPTY_SET
intervalsback to summary
protected List<Interval> intervals

The list of sorted, disjoint intervals.

readonlyback to summary
protected boolean readonly

Constructor Detail

IntervalSetback to summary
public IntervalSet(List<Interval> intervals)
IntervalSetback to summary
public IntervalSet(IntervalSet set)
IntervalSetback to summary
public IntervalSet(int... els)

Method Detail

addback to summary
public void add(int el)

Implements org.antlr.v4.runtime.misc.IntSet.add.

Add a single element to the set. An isolated element is stored as a range el..el.

Parameters
el:int

Doc from org.antlr.v4.runtime.misc.IntSet.add.

the value to add

Annotations
@Override
addback to summary
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}.

addback to summary
protected void add(Interval addition)
addAllback to summary
public IntervalSet addAll(IntSet set)

Implements org.antlr.v4.runtime.misc.IntSet.addAll.

Doc from org.antlr.v4.runtime.misc.IntSet.addAll.

Modify the current IntSet object to contain all elements that are present in itself, the specified set, or both.

Parameters
set:IntSet

The set to add to the current set. A null argument is treated as though it were an empty set.

Returns:IntervalSet

this (to support chained calls)

Annotations
@Override
andback to summary
public IntervalSet and(IntSet other)

Implements org.antlr.v4.runtime.misc.IntSet.and.

Doc from org.antlr.v4.runtime.misc.IntSet.and.

Return a new IntSet object containing all elements that are present in both the current set and the specified set a.

Parameters
other:IntSet

The set to intersect with the current set. A null argument is treated as though it were an empty set.

Returns:IntervalSet

A new IntSet instance containing the intersection of the current set and a. The value null may be returned in place of an empty result set.

Annotations
@Override
clearback to summary
public void clear()
complementback to summary
public IntervalSet complement(int minElement, int maxElement)
complementback to summary
public IntervalSet complement(IntSet vocabulary)

Implements org.antlr.v4.runtime.misc.IntSet.complement.

Doc from org.antlr.v4.runtime.misc.IntSet.complement.

Return a new IntSet object containing all elements that are present in elements but not present in the current set. The following expressions are equivalent for input non-null IntSet instances x and y.

  • x.complement(y)
  • y.subtract(x)
Parameters
vocabulary:IntSet

The set to compare with the current set. A null argument is treated as though it were an empty set.

Returns:IntervalSet

A new IntSet instance containing the elements present in elements but not present in the current set. The value null may be returned in place of an empty result set.

Annotations
@Override
containsback to summary
public boolean contains(int el)

Implements org.antlr.v4.runtime.misc.IntSet.contains.

Doc from org.antlr.v4.runtime.misc.IntSet.contains.

Returns true if the set contains the specified element.

Parameters
el:int

The element to check for.

Returns:boolean

true if the set contains el; otherwise false.

Annotations
@Override
elementNameback to summary
protected String elementName(String[] tokenNames, int a)

Deprecated

Use elementName(Vocabulary, int) instead.

Annotations
@Deprecated
elementNameback to summary
protected String elementName(Vocabulary vocabulary, int a)
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Implements org.antlr.v4.runtime.misc.IntSet.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.

Parameters
obj:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getback to summary
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.

getIntervalsback to summary
public List<Interval> getIntervals()

Return a list of Interval objects.

getMaxElementback to summary
public int getMaxElement()

Returns the maximum value contained in the set if not isNil().

Returns:int

the maximum value contained in the set.

Exceptions
RuntimeException:
if set is empty
getMinElementback to summary
public int getMinElement()

Returns the minimum value contained in the set if not isNil().

Returns:int

the minimum value contained in the set.

Exceptions
RuntimeException:
if set is empty
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
isNilback to summary
public boolean isNil()

Implements org.antlr.v4.runtime.misc.IntSet.isNil.

Doc from org.antlr.v4.runtime.misc.IntSet.isNil.

Returns true if this set contains no elements.

Returns:boolean

true if the current set contains no elements; otherwise, false.

Annotations
@Override
isReadonlyback to summary
public boolean isReadonly()
ofback to summary
public static IntervalSet of(int a)

Create a set with a single element, el.

ofback to summary
public static IntervalSet of(int a, int b)

Create a set with all ints within range [a..b] (inclusive)

orback to summary
public static IntervalSet or(IntervalSet[] sets)

combine all sets in the array returned the or'd value

orback to summary
public IntervalSet or(IntSet a)

Implements org.antlr.v4.runtime.misc.IntSet.or.

Doc from org.antlr.v4.runtime.misc.IntSet.or.

Return a new IntSet object containing all elements that are present in the current set, the specified set a, or both.

This method is similar to addAll(IntSet), but returns a new IntSet instance instead of modifying the current set.

Parameters
a:IntSet

The set to union with the current set. A null argument is treated as though it were an empty set.

Returns:IntervalSet

A new IntSet instance containing the union of the current set and a. The value null may be returned in place of an empty result set.

Annotations
@Override
removeback to summary
public void remove(int el)

Implements org.antlr.v4.runtime.misc.IntSet.remove.

Doc from org.antlr.v4.runtime.misc.IntSet.remove.

Removes the specified value from the current set. If the current set does not contain the element, no changes are made.

Parameters
el:int

the value to remove

Annotations
@Override
setReadonlyback to summary
public void setReadonly(boolean readonly)
sizeback to summary
public int size()

Implements org.antlr.v4.runtime.misc.IntSet.size.

Doc from org.antlr.v4.runtime.misc.IntSet.size.

Return the total number of elements represented by the current set.

Returns:int

the total number of elements represented by the current set, regardless of the manner in which the elements are stored.

Annotations
@Override
subtractback to summary
public IntervalSet subtract(IntSet a)

Implements org.antlr.v4.runtime.misc.IntSet.subtract.

Doc from org.antlr.v4.runtime.misc.IntSet.subtract.

Return a new IntSet object containing all elements that are present in the current set but not present in the input set a. The following expressions are equivalent for input non-null IntSet instances x and y.

  • y.subtract(x)
  • x.complement(y)
Parameters
a:IntSet

The set to compare with the current set. A null argument is treated as though it were an empty set.

Returns:IntervalSet

A new IntSet instance containing the elements present in elements but not present in the current set. The value null may be returned in place of an empty result set.

Annotations
@Override
subtractback to summary
public static IntervalSet subtract(IntervalSet left, IntervalSet right)

Compute the set difference between two interval sets. The specific operation is left - right. If either of the input sets is null, it is treated as though it was an empty set.

toArrayback to summary
public int[] toArray()
toIntegerListback to summary
public IntegerList toIntegerList()
toListback to summary
public List<Integer> toList()

Implements org.antlr.v4.runtime.misc.IntSet.toList.

Doc from org.antlr.v4.runtime.misc.IntSet.toList.

Return a list containing the elements represented by the current set. The list is returned in ascending numerical order.

Returns:List<Integer>

A list containing all element present in the current set, sorted in ascending numerical order.

Annotations
@Override
toSetback to summary
public Set<Integer> toSet()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Implements org.antlr.v4.runtime.misc.IntSet.toString.

Doc from org.antlr.v4.runtime.misc.IntSet.toString.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override
toStringback to summary
public String toString(boolean elemAreChar)
toStringback to summary
public String toString(String[] tokenNames)

Deprecated

Use toString(Vocabulary) instead.

Annotations
@Deprecated
toStringback to summary
public String toString(Vocabulary vocabulary)