public class IntervalSet extends Object implements IntSet
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).
Modifier and Type | Field and Description |
---|---|
static IntervalSet |
COMPLETE_CHAR_SET |
static IntervalSet |
EMPTY_SET |
protected List<Interval> |
intervals
The list of sorted, disjoint intervals.
|
protected boolean |
readonly |
Constructor and Description |
---|
IntervalSet(int... els) |
IntervalSet(IntervalSet set) |
IntervalSet(List<Interval> intervals) |
Modifier and Type | Method and Description |
---|---|
void |
add(int el)
Add a single element to the set.
|
protected void |
add(Interval addition) |
void |
add(int a,
int b)
Add interval; i.e., add all integers from a to b to set.
|
IntervalSet |
addAll(IntSet set)
Modify the current
IntSet object to contain all elements that are
present in itself, the specified set , or both. |
IntervalSet |
and(IntSet other)
Return a new
IntSet object containing all elements that are
present in both the current set and the specified set a . |
void |
clear() |
IntervalSet |
complement(int minElement,
int maxElement) |
IntervalSet |
complement(IntSet vocabulary)
Return a new
IntSet object containing all elements that are
present in elements but not present in the current set. |
boolean |
contains(int el)
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) |
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.
|
int |
get(int i)
Get the ith element of ordered set.
|
List<Interval> |
getIntervals()
Return a list of Interval objects.
|
int |
getMaxElement()
Returns the maximum value contained in the set if not isNil().
|
int |
getMinElement()
Returns the minimum value contained in the set if not isNil().
|
int |
hashCode() |
boolean |
isNil()
Returns
true if this set contains no elements. |
boolean |
isReadonly() |
static IntervalSet |
of(int a)
Create a set with a single element, el.
|
static IntervalSet |
of(int a,
int b)
Create a set with all ints within range [a..b] (inclusive)
|
static IntervalSet |
or(IntervalSet[] sets)
combine all sets in the array returned the or'd value
|
IntervalSet |
or(IntSet a)
Return a new
IntSet object containing all elements that are
present in the current set, the specified set a , or both. |
void |
remove(int el)
Removes the specified value from the current set.
|
void |
setReadonly(boolean readonly) |
int |
size()
Return the total number of elements represented by the current set.
|
static IntervalSet |
subtract(IntervalSet left,
IntervalSet right)
Compute the set difference between two interval sets.
|
IntervalSet |
subtract(IntSet a)
Return a new
IntSet object containing all elements that are
present in the current set but not present in the input set a . |
int[] |
toArray() |
IntegerList |
toIntegerList() |
List<Integer> |
toList()
Return a list containing the elements represented by the current set.
|
Set<Integer> |
toSet() |
String |
toString() |
String |
toString(boolean elemAreChar) |
String |
toString(String[] tokenNames)
Deprecated.
Use
toString(Vocabulary) instead. |
String |
toString(Vocabulary vocabulary) |
public static final IntervalSet COMPLETE_CHAR_SET
public static final IntervalSet EMPTY_SET
protected boolean readonly
public IntervalSet(IntervalSet set)
public IntervalSet(int... els)
public static IntervalSet of(int a)
public static IntervalSet of(int a, int b)
public void clear()
public void add(int el)
public void add(int a, int b)
protected void add(Interval addition)
public static IntervalSet or(IntervalSet[] sets)
public IntervalSet addAll(IntSet set)
IntSet
IntSet
object to contain all elements that are
present in itself, the specified set
, or both.public IntervalSet complement(int minElement, int maxElement)
public IntervalSet complement(IntSet vocabulary)
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)
complement
in interface IntSet
vocabulary
- The set to compare with the current set. A null
argument is treated as though it were an empty set.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.public IntervalSet subtract(IntSet a)
IntSet
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)
subtract
in interface IntSet
a
- The set to compare with the current set. A null
argument is treated as though it were an empty set.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.public static IntervalSet subtract(IntervalSet left, IntervalSet right)
left - right
. If either of the input sets is
null
, it is treated as though it was an empty set.public IntervalSet or(IntSet a)
IntSet
IntSet
object containing all elements that are
present in the current set, the specified set a
, or both.
This method is similar to IntSet.addAll(IntSet)
, but returns a new
IntSet
instance instead of modifying the current set.
public IntervalSet and(IntSet other)
IntSet
object containing all elements that are
present in both the current set and the specified set a
.and
in interface IntSet
other
- The set to intersect with the current set. A null
argument is treated as though it were an empty set.IntSet
instance containing the intersection of the
current set and a
. The value null
may be returned in
place of an empty result set.public boolean contains(int el)
true
if the set contains the specified element.public boolean isNil()
true
if this set contains no elements.public int getMaxElement()
RuntimeException
- if set is emptypublic int getMinElement()
RuntimeException
- if set is emptypublic boolean equals(Object obj)
public String toString(boolean elemAreChar)
@Deprecated public String toString(String[] tokenNames)
toString(Vocabulary)
instead.public String toString(Vocabulary vocabulary)
@Deprecated protected String elementName(String[] tokenNames, int a)
elementName(Vocabulary, int)
instead.protected String elementName(Vocabulary vocabulary, int a)
public int size()
IntSet
public IntegerList toIntegerList()
public List<Integer> toList()
IntSet
public int get(int i)
public int[] toArray()
public void remove(int el)
IntSet
public boolean isReadonly()
public void setReadonly(boolean readonly)
Copyright © 1992–2021 ANTLR. All rights reserved.