Package org.antlr.misc
Class IntervalSet
java.lang.Object
org.antlr.misc.IntervalSet
- All Implemented Interfaces:
IntSet
A set of integers that relies on ranges being common to do
"run-length-encoded" like compression (if you view an IntSet like
a BitSet with runs of 0s and 1s). Only ranges are recorded so that
a few ints up near value 1000 don't cause massive bitsets, just two
integer intervals.
element values may be negative. Useful for sets of EPSILON and EOF.
0..9 char range is index pair ['0','9'].
Multiple ranges are encoded with multiple index pairs. Isolated
elements are encoded with an index pair where both intervals are the same.
The ranges are ordered and disjoint so that 2..6 appears before 101..103.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final IntervalSet
The list of sorted, disjoint intervals. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(int el) Add a single element to the set.void
add
(int a, int b) Add interval; i.e., add all integers from a to b to set.protected void
void
Add all elements from incoming set to this set.Return a new set with the intersection of this set with other.complement
(int minElement, int maxElement) complement
(IntSet vocabulary) Given the set of possible values (rather than, say UNICODE or MAXINT), return a new set containing all elements in vocabulary, but not in this.boolean
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.Return a list of Interval objects.int
int
Return minimum element >= 0int
If this set is a single integer, return it otherwise Label.INVALIDboolean
isNil()
return true if this set has no membersboolean
member
(int el) Is el in any range of this set?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)TODO: implement this!void
remove
(int el) remove this element from this setint
size()
Return the size of this set (not the underlying implementation's allocated memory size, for example).Compute this-other via this&~other.int[]
toArray()
toList()
toString()
-
Field Details
-
COMPLETE_SET
-
intervals
The list of sorted, disjoint intervals.
-
-
Constructor Details
-
IntervalSet
public IntervalSet()Create a set with no elements -
IntervalSet
-
-
Method Details
-
of
Create a set with a single element, el. -
of
Create a set with all ints within range [a..b] (inclusive) -
add
public void add(int el) Add a single element to the set. An isolated element is stored as a range el..el. -
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
-
addAll
Description copied from interface:IntSet
Add all elements from incoming set to this set. Can limit to set of its own type. -
complement
-
complement
Given the set of possible values (rather than, say UNICODE or MAXINT), return a new set containing all elements in vocabulary, but not in this. The computation is (vocabulary - this). 'this' is assumed to be either a subset or equal to vocabulary.- Specified by:
complement
in interfaceIntSet
-
subtract
Compute this-other via this&~other. Return a new set containing all elements in this but not in other. other is assumed to be a subset of this; anything that is in other but not in this will be ignored. -
or
TODO: implement this! -
and
Return a new set with the intersection of this set with other. Because the intervals are sorted, we can use an iterator for each list and just walk them together. This is roughly O(min(n,m)) for interval list lengths n and m. -
member
public boolean member(int el) Is el in any range of this set? -
isNil
public boolean isNil()return true if this set has no members -
getSingleElement
public int getSingleElement()If this set is a single integer, return it otherwise Label.INVALID- Specified by:
getSingleElement
in interfaceIntSet
-
getMaxElement
public int getMaxElement() -
getMinElement
public int getMinElement()Return minimum element >= 0 -
getIntervals
Return a list of Interval objects. -
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
-
size
public int size()Description copied from interface:IntSet
Return the size of this set (not the underlying implementation's allocated memory size, for example). -
toList
-
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() -
toRuntimeBitSet
-
remove
public void remove(int el) Description copied from interface:IntSet
remove this element from this set
-