Package jflex.state
Class StateSet
- java.lang.Object
-
- jflex.state.StateSet
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Integer>
public final class StateSet extends java.lang.Object implements java.lang.Iterable<java.lang.Integer>A set of NFA states (= ints).Similar to
BitSet, but tuned for sets of states. Can hold at most2^64elements, but is only useful for much smaller sets (ideally not more than tens of thousands).Provides an Integer iterator and a native int enumerator.
- Version:
- JFlex 1.9.1
- See Also:
StateSetEnumerator
-
-
Constructor Summary
Constructors Constructor Description StateSet()Construct an empty StateSet with default memory backing.StateSet(int size)Construct an empty StateSet with specified memory backing.StateSet(int size, int state)Construct an StateSet with specified initial element and memory backing.StateSet(StateSet set)Copy the specified StateSet to create a new one.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(StateSet set)Add all elements of the specified StateSet to this one.voidaddState(int state)Add an element (a state) to the set.voidclear()Remove all elements from this set.StateSetcomplement(StateSet univ)Returns the complement of this set with respect to the specified set, that is, the set of elements that are contained in the specified set but are not contained in this set.booleancontains(StateSet set)Determine if the given set is a subset of this set.booleancontainsElements()Determine if the State set contains elements.StateSetcopy()Return a copy of this StateSet.voidcopy(StateSet set)Copy specified StateSet into this.static StateSetemptySet(int length)Return a new StateSet of the specified length.booleanequals(java.lang.Object b)intgetAndRemoveElement()Returns an element of the set and removes it.intgetCurrentMaxState()Provide the max value that can be stored without a resizebooleanhasElement(int state)Determine if a given state is an element of the set.inthashCode()voidintersect(StateSet set)Remove all states fromthisthat are not contained in the providedStateSet.java.util.Iterator<java.lang.Integer>iterator()Construct an Integer iterator for this StateSet.(package private) static intnbits2size(int length)Compute a set size that will lead to an array of the given length.voidremove(int state)Remove a given state from the set.private voidresize(int size)Resize this set so it can hold at leastsizeelements.(package private) static intsize2nbits(int size)Compute the array size for a given set size.StateSetEnumeratorstates()Construct an enumerator for this set.java.lang.StringtoString()
-
-
-
Field Detail
-
DEBUG
private final boolean DEBUG
Compile timeDEBUGsetting, local toStateSet- See Also:
- Constant Field Values
-
BITS
static final int BITS
2^BITSper word- See Also:
- Constant Field Values
-
MASK
static final int MASK
- See Also:
- Constant Field Values
-
bits
long[] bits
Content of theStateSet, one bit per int, i.e. bit 0 ofbits[0]stands for 0, bit 1 ofbits[0]stands for 1, bit 1 ofbits[1]stands for 65, etc.
-
-
Constructor Detail
-
StateSet
public StateSet()
Construct an empty StateSet with default memory backing.
-
StateSet
public StateSet(int size)
Construct an empty StateSet with specified memory backing.- Parameters:
size- an int specifying the largest number this set will store. The StateSet will automatically resize of a larger number is added; specifying the size avoids re-allocation.
-
StateSet
public StateSet(int size, int state)Construct an StateSet with specified initial element and memory backing.- Parameters:
size- an int specifying the largest number this set will store. The StateSet will automatically resize of a larger number is added; specifying the size avoids re-allocation.state- the element the set should contain.
-
-
Method Detail
-
emptySet
public static StateSet emptySet(int length)
Return a new StateSet of the specified length.
-
addState
public void addState(int state)
Add an element (a state) to the set. Will automatically resize the set representation if necessary.- Parameters:
state- the element to add.
-
size2nbits
static int size2nbits(int size)
Compute the array size for a given set size.- Parameters:
size- the desired size of the set.- Returns:
- an array size such that the set can hold at least
sizeelements.
-
nbits2size
static int nbits2size(int length)
Compute a set size that will lead to an array of the given length.Precondition: length > 0 && length <= 2^58 (58=64-BITS)
- Parameters:
length- desired length of the StateSet array- Returns:
- an int
valsuch thatsize2nbits(val) = length
-
resize
private void resize(int size)
Resize this set so it can hold at leastsizeelements.- Parameters:
size- new maximum element
-
clear
public void clear()
Remove all elements from this set.
-
hasElement
public boolean hasElement(int state)
Determine if a given state is an element of the set.- Parameters:
state- the element to check for.- Returns:
- true iff this set has the element
state.
-
getAndRemoveElement
public int getAndRemoveElement()
Returns an element of the set and removes it.Precondition: the set is not empty.
- Returns:
- an element of the set.
-
remove
public void remove(int state)
Remove a given state from the set.- Parameters:
state- the element to remove.
-
intersect
public void intersect(StateSet set)
Remove all states fromthisthat are not contained in the providedStateSet.- Parameters:
set- theStateSetobject to intersect with.
-
complement
@Nullable public StateSet complement(StateSet univ)
Returns the complement of this set with respect to the specified set, that is, the set of elements that are contained in the specified set but are not contained in this set.Does not change this set.
-
add
public void add(StateSet set)
Add all elements of the specified StateSet to this one.- Parameters:
set- aStateSetobject to be added.
-
equals
public boolean equals(@Nullable java.lang.Object b)- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
states
public StateSetEnumerator states()
Construct an enumerator for this set.- Returns:
- a
StateSetEnumeratorobject for this set.
-
containsElements
public boolean containsElements()
Determine if the State set contains elements.- Returns:
- true iff the set is not empty.
-
contains
public boolean contains(StateSet set)
Determine if the given set is a subset of this set.- Parameters:
set- the set to check containment of- Returns:
- true iff
setis contained in this set.
-
copy
public StateSet copy()
Return a copy of this StateSet.- Returns:
- a
StateSetobject with the same content as this.
-
copy
public void copy(StateSet set)
Copy specified StateSet into this.- Parameters:
set- the state set to copy.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
iterator
public java.util.Iterator<java.lang.Integer> iterator()
Construct an Integer iterator for this StateSet.- Specified by:
iteratorin interfacejava.lang.Iterable<java.lang.Integer>- Returns:
- an iterator for this StateSet.
-
getCurrentMaxState
public int getCurrentMaxState()
Provide the max value that can be stored without a resize- Returns:
- an int of the max value
-
-