public final class FSABuilder
extends java.lang.Object
FSA that is a tradeoff between construction speed and
memory consumption. Use serializers to compress the returned automaton into
more compact form.FSASerializer| Modifier and Type | Class and Description |
|---|---|
static class |
FSABuilder.InfoEntry
Debug and information constants.
|
| Modifier and Type | Field and Description |
|---|---|
private int[] |
activePath
States on the "active path" (still mutable).
|
private int |
activePathLen
Current length of the active path.
|
private static int |
BUFFER_GROWTH_SIZE
Internal serialized FSA buffer expand ratio.
|
private int |
bufferGrowthSize
Internal serialized FSA buffer expand ratio.
|
private int |
epsilon
An epsilon state.
|
private int[] |
hashSet
Hash set of state addresses in
serialized, hashed by
hash(int, int). |
private int |
hashSize
Number of entries currently stored in
hashSet. |
private java.util.TreeMap<FSABuilder.InfoEntry,java.lang.Object> |
info
Information about the automaton and its compilation.
|
static java.util.Comparator<byte[]> |
LEXICAL_ORDERING
A comparator comparing full byte arrays.
|
private static int |
MAX_LABELS
Maximum number of labels from a single state.
|
private static int |
MB
A megabyte.
|
private int[] |
nextArcOffset
The next offset at which an arc will be added to the given state on
activePath. |
private byte[] |
previous
Previous sequence added to the automaton in
add(byte[], int, int). |
private int |
previousLength
previous sequence's length, used in assertions only. |
private int |
root
Root state.
|
private int |
serializationBufferReallocations
Number of serialization buffer reallocations.
|
private byte[] |
serialized
Holds serialized and mutable states.
|
private int |
size
Number of bytes already taken in
serialized. |
| Constructor and Description |
|---|
FSABuilder() |
FSABuilder(int bufferGrowthSize) |
| Modifier and Type | Method and Description |
|---|---|
void |
add(byte[] sequence,
int start,
int len)
Add a single sequence of bytes to the FSA.
|
private int |
allocateState(int labels)
Allocate space for a state with the given number of outgoing labels.
|
static FSA |
build(byte[][] input)
Build a minimal, deterministic automaton from a sorted list of byte
sequences.
|
static FSA |
build(java.lang.Iterable<byte[]> input)
Build a minimal, deterministic automaton from an iterable list of byte
sequences.
|
private int |
commonPrefix(byte[] sequence,
int start,
int len) |
private static int |
compare(byte[] s1,
int start1,
int lens1,
byte[] s2,
int start2,
int lens2)
Lexicographic order of input sequences.
|
FSA |
complete() |
private boolean |
equivalent(int start1,
int start2,
int len)
Return
true if two regions in serialized are
identical. |
private void |
expandActivePath(int size)
Append a new mutable state to the active path.
|
private void |
expandAndRehash()
Reallocate and rehash the hash set.
|
private void |
expandBuffers()
Expand internal buffers for the next state.
|
private int |
freezeState(int activePathIndex)
Freeze a state: try to find an equivalent state in the interned states
dictionary first, if found, return it, otherwise, serialize the mutable
state at
activePathIndex and return it. |
private byte |
getArcLabel(int arc)
Get label's arc.
|
private int |
getArcTarget(int arc)
Returns the address of an arc.
|
java.util.Map<FSABuilder.InfoEntry,java.lang.Object> |
getInfo() |
private int |
hash(int start,
int byteCount)
Hash code of a fragment of
serialized array. |
private boolean |
isArcFinal(int arc)
Is this arc final?
|
private boolean |
isArcLast(int arc)
Is this arc the state's last?
|
private int |
serialize(int activePathIndex)
Serialize a given state on the active path.
|
private void |
setArcTarget(int arc,
int state)
Fills the target state address of an arc.
|
private boolean |
setPrevious(byte[] sequence,
int start,
int length)
Copy
current into an internal buffer. |
private int |
stateLength(int state)
The total length of the serialized state data (all arcs).
|
private static final int MB
private static final int BUFFER_GROWTH_SIZE
private static final int MAX_LABELS
public static final java.util.Comparator<byte[]> LEXICAL_ORDERING
private final int bufferGrowthSize
private byte[] serialized
#BIT_ARC_LAST.private int size
serialized. Start from 1 to keep
0 a sentinel value (for the hash set and final state).private int[] activePath
private int activePathLen
private int[] nextArcOffset
activePath.private int root
private int epsilon
private int[] hashSet
serialized, hashed by
hash(int, int). Zero reserved for an unoccupied slot.private int hashSize
hashSet.private byte[] previous
add(byte[], int, int).
Used in assertions only.private java.util.TreeMap<FSABuilder.InfoEntry,java.lang.Object> info
private int previousLength
previous sequence's length, used in assertions only.private int serializationBufferReallocations
public FSABuilder()
public FSABuilder(int bufferGrowthSize)
bufferGrowthSize - Buffer growth size (in bytes) when constructing the automaton.public void add(byte[] sequence,
int start,
int len)
sequence - The array holding input sequence of bytes.start - Starting offset (inclusive)len - Length of the input sequence (at least 1 byte).public FSA complete()
public static FSA build(byte[][] input)
input - Input sequences to build automaton from.public static FSA build(java.lang.Iterable<byte[]> input)
input - Input sequences to build automaton from.public java.util.Map<FSABuilder.InfoEntry,java.lang.Object> getInfo()
FSABuilder.InfoEntryprivate boolean isArcLast(int arc)
private boolean isArcFinal(int arc)
private byte getArcLabel(int arc)
private void setArcTarget(int arc,
int state)
private int getArcTarget(int arc)
private int commonPrefix(byte[] sequence,
int start,
int len)
private int freezeState(int activePathIndex)
activePathIndex and return it.private void expandAndRehash()
private int stateLength(int state)
private boolean equivalent(int start1,
int start2,
int len)
true if two regions in serialized are
identical.private int serialize(int activePathIndex)
private int hash(int start,
int byteCount)
serialized array.private void expandActivePath(int size)
private void expandBuffers()
private int allocateState(int labels)
private boolean setPrevious(byte[] sequence,
int start,
int length)
current into an internal buffer.private static int compare(byte[] s1,
int start1,
int lens1,
byte[] s2,
int start2,
int lens2)