Package com.ibm.icu.util
Class BytesTrie
- java.lang.Object
-
- com.ibm.icu.util.BytesTrie
-
- All Implemented Interfaces:
java.lang.Cloneable
,java.lang.Iterable<BytesTrie.Entry>
public final class BytesTrie extends java.lang.Object implements java.lang.Cloneable, java.lang.Iterable<BytesTrie.Entry>
Light-weight, non-const reader class for a BytesTrie. Traverses a byte-serialized data structure with minimal state, for mapping byte sequences to non-negative integer values.This class is not intended for public subclassing.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BytesTrie.Entry
Return value type for the Iterator.static class
BytesTrie.Iterator
Iterator for all of the (byte sequence, value) pairs in a BytesTrie.static class
BytesTrie.Result
Return values for BytesTrie.next(), CharsTrie.next() and similar methods.static class
BytesTrie.State
BytesTrie state object, for saving a trie's current state and resetting the trie back to this state later.
-
Field Summary
Fields Modifier and Type Field Description private byte[]
bytes_
(package private) static int
kFiveByteDeltaLead
(package private) static int
kFiveByteValueLead
(package private) static int
kFourByteDeltaLead
(package private) static int
kFourByteValueLead
(package private) static int
kMaxBranchLinearSubNodeLength
(package private) static int
kMaxLinearMatchLength
(package private) static int
kMaxOneByteDelta
(package private) static int
kMaxOneByteValue
(package private) static int
kMaxThreeByteDelta
(package private) static int
kMaxThreeByteValue
(package private) static int
kMaxTwoByteDelta
(package private) static int
kMaxTwoByteValue
(package private) static int
kMinLinearMatch
(package private) static int
kMinOneByteValueLead
(package private) static int
kMinThreeByteDeltaLead
(package private) static int
kMinThreeByteValueLead
(package private) static int
kMinTwoByteDeltaLead
(package private) static int
kMinTwoByteValueLead
(package private) static int
kMinValueLead
private static int
kValueIsFinal
private int
pos_
private int
remainingMatchLength_
private int
root_
private static BytesTrie.Result[]
valueResults_
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static void
append(java.lang.Appendable out, int c)
private BytesTrie.Result
branchNext(int pos, int length, int inByte)
BytesTrie
clone()
Clones this trie reader object and its state, but not the byte array which will be shared.BytesTrie.Result
current()
Determines whether the byte sequence so far matches, whether it has a value, and whether another input byte can continue a matching byte sequence.private static long
findUniqueValue(byte[] bytes, int pos, long uniqueValue)
private static long
findUniqueValueFromBranch(byte[] bytes, int pos, int length, long uniqueValue)
BytesTrie.Result
first(int inByte)
Traverses the trie from the initial state for this input byte.private static void
getNextBranchBytes(byte[] bytes, int pos, int length, java.lang.Appendable out)
int
getNextBytes(java.lang.Appendable out)
Finds each byte which continues the byte sequence from the current state.long
getState64()
Returns the state of this trie as a 64-bit integer.long
getUniqueValue()
Determines whether all byte sequences reachable from the current state map to the same value, and if so, returns that value.int
getValue()
Returns a matching byte sequence's value if called immediately after current()/first()/next() returned Result.INTERMEDIATE_VALUE or Result.FINAL_VALUE.BytesTrie.Iterator
iterator()
Iterates from the current state of this trie.static BytesTrie.Iterator
iterator(byte[] trieBytes, int offset, int maxStringLength)
Iterates from the root of a byte-serialized BytesTrie.BytesTrie.Iterator
iterator(int maxStringLength)
Iterates from the current state of this trie.static int
jumpByDelta(byte[] bytes, int pos)
Deprecated.This API is ICU internal only.BytesTrie.Result
next(byte[] s, int sIndex, int sLimit)
Traverses the trie from the current state for this byte sequence.BytesTrie.Result
next(int inByte)
Traverses the trie from the current state for this input byte.private BytesTrie.Result
nextImpl(int pos, int inByte)
private static int
readValue(byte[] bytes, int pos, int leadByte)
BytesTrie
reset()
Resets this trie to its initial state.BytesTrie
resetToState(BytesTrie.State state)
Resets this trie to the saved state.BytesTrie
resetToState64(long state)
Resets this trie to the saved state.BytesTrie
saveState(BytesTrie.State state)
Saves the state of this trie.private static int
skipDelta(byte[] bytes, int pos)
private static int
skipValue(byte[] bytes, int pos)
private static int
skipValue(int pos, int leadByte)
private void
stop()
-
-
-
Field Detail
-
valueResults_
private static BytesTrie.Result[] valueResults_
-
kMaxBranchLinearSubNodeLength
static final int kMaxBranchLinearSubNodeLength
- See Also:
- Constant Field Values
-
kMinLinearMatch
static final int kMinLinearMatch
- See Also:
- Constant Field Values
-
kMaxLinearMatchLength
static final int kMaxLinearMatchLength
- See Also:
- Constant Field Values
-
kMinValueLead
static final int kMinValueLead
- See Also:
- Constant Field Values
-
kValueIsFinal
private static final int kValueIsFinal
- See Also:
- Constant Field Values
-
kMinOneByteValueLead
static final int kMinOneByteValueLead
- See Also:
- Constant Field Values
-
kMaxOneByteValue
static final int kMaxOneByteValue
- See Also:
- Constant Field Values
-
kMinTwoByteValueLead
static final int kMinTwoByteValueLead
- See Also:
- Constant Field Values
-
kMaxTwoByteValue
static final int kMaxTwoByteValue
- See Also:
- Constant Field Values
-
kMinThreeByteValueLead
static final int kMinThreeByteValueLead
- See Also:
- Constant Field Values
-
kFourByteValueLead
static final int kFourByteValueLead
- See Also:
- Constant Field Values
-
kMaxThreeByteValue
static final int kMaxThreeByteValue
- See Also:
- Constant Field Values
-
kFiveByteValueLead
static final int kFiveByteValueLead
- See Also:
- Constant Field Values
-
kMaxOneByteDelta
static final int kMaxOneByteDelta
- See Also:
- Constant Field Values
-
kMinTwoByteDeltaLead
static final int kMinTwoByteDeltaLead
- See Also:
- Constant Field Values
-
kMinThreeByteDeltaLead
static final int kMinThreeByteDeltaLead
- See Also:
- Constant Field Values
-
kFourByteDeltaLead
static final int kFourByteDeltaLead
- See Also:
- Constant Field Values
-
kFiveByteDeltaLead
static final int kFiveByteDeltaLead
- See Also:
- Constant Field Values
-
kMaxTwoByteDelta
static final int kMaxTwoByteDelta
- See Also:
- Constant Field Values
-
kMaxThreeByteDelta
static final int kMaxThreeByteDelta
- See Also:
- Constant Field Values
-
bytes_
private byte[] bytes_
-
root_
private int root_
-
pos_
private int pos_
-
remainingMatchLength_
private int remainingMatchLength_
-
-
Constructor Detail
-
BytesTrie
public BytesTrie(byte[] trieBytes, int offset)
Constructs a BytesTrie reader instance.The array must contain a copy of a byte sequence from the BytesTrieBuilder, with the offset indicating the first byte of that sequence. The BytesTrie object will not read more bytes than the BytesTrieBuilder generated in the corresponding build() call.
The array is not copied/cloned and must not be modified while the BytesTrie object is in use.
- Parameters:
trieBytes
- Bytes array that contains the serialized trie.offset
- Root offset of the trie in the array.
-
BytesTrie
public BytesTrie(BytesTrie other)
Copy constructor. Makes a shallow copy of the other trie reader object and its state. Does not copy the byte array which will be shared. Same as clone() but without the throws clause.
-
-
Method Detail
-
clone
public BytesTrie clone() throws java.lang.CloneNotSupportedException
Clones this trie reader object and its state, but not the byte array which will be shared.- Overrides:
clone
in classjava.lang.Object
- Returns:
- A shallow clone of this trie.
- Throws:
java.lang.CloneNotSupportedException
-
reset
public BytesTrie reset()
Resets this trie to its initial state.- Returns:
- this
-
getState64
public long getState64()
Returns the state of this trie as a 64-bit integer. The state value is never 0.- Returns:
- opaque state value
- See Also:
resetToState64(long)
-
resetToState64
public BytesTrie resetToState64(long state)
Resets this trie to the saved state. UnlikeresetToState(State)
, the 64-bit state value must be fromgetState64()
from the same trie object or from one initialized the exact same way. Because of no validation, this method is faster.- Parameters:
state
- The opaque trie state value from getState64().- Returns:
- this
- See Also:
getState64()
,resetToState(com.ibm.icu.util.BytesTrie.State)
,reset()
-
saveState
public BytesTrie saveState(BytesTrie.State state)
Saves the state of this trie.- Parameters:
state
- The State object to hold the trie's state.- Returns:
- this
- See Also:
resetToState(com.ibm.icu.util.BytesTrie.State)
-
resetToState
public BytesTrie resetToState(BytesTrie.State state)
Resets this trie to the saved state. Slower thanresetToState64(long)
which does not validate the state value.- Parameters:
state
- The State object which holds a saved trie state.- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if the state object contains no state, or the state of a different trie- See Also:
saveState(com.ibm.icu.util.BytesTrie.State)
,reset()
-
current
public BytesTrie.Result current()
Determines whether the byte sequence so far matches, whether it has a value, and whether another input byte can continue a matching byte sequence.- Returns:
- The match/value Result.
-
first
public BytesTrie.Result first(int inByte)
Traverses the trie from the initial state for this input byte. Equivalent to reset().next(inByte).- Parameters:
inByte
- Input byte value. Values -0x100..-1 are treated like 0..0xff. Values below -0x100 and above 0xff will never match.- Returns:
- The match/value Result.
-
next
public BytesTrie.Result next(int inByte)
Traverses the trie from the current state for this input byte.- Parameters:
inByte
- Input byte value. Values -0x100..-1 are treated like 0..0xff. Values below -0x100 and above 0xff will never match.- Returns:
- The match/value Result.
-
next
public BytesTrie.Result next(byte[] s, int sIndex, int sLimit)
Traverses the trie from the current state for this byte sequence. Equivalent toResult result=current(); for(each c in s) if(!result.hasNext()) return Result.NO_MATCH; result=next(c); return result;
- Parameters:
s
- Contains a string or byte sequence.sIndex
- The start index of the byte sequence in s.sLimit
- The (exclusive) end index of the byte sequence in s.- Returns:
- The match/value Result.
-
getValue
public int getValue()
Returns a matching byte sequence's value if called immediately after current()/first()/next() returned Result.INTERMEDIATE_VALUE or Result.FINAL_VALUE. getValue() can be called multiple times. Do not call getValue() after Result.NO_MATCH or Result.NO_VALUE!- Returns:
- The value for the byte sequence so far.
-
getUniqueValue
public long getUniqueValue()
Determines whether all byte sequences reachable from the current state map to the same value, and if so, returns that value.- Returns:
- The unique value in bits 32..1 with bit 0 set, if all byte sequences reachable from the current state map to the same value; otherwise returns 0.
-
getNextBytes
public int getNextBytes(java.lang.Appendable out)
Finds each byte which continues the byte sequence from the current state. That is, each byte b for which it would be next(b)!=Result.NO_MATCH now.- Parameters:
out
- Each next byte is 0-extended to a char and appended to this object. (Only uses the out.append(c) method.)- Returns:
- The number of bytes which continue the byte sequence from here.
-
iterator
public BytesTrie.Iterator iterator()
Iterates from the current state of this trie.- Specified by:
iterator
in interfacejava.lang.Iterable<BytesTrie.Entry>
- Returns:
- A new BytesTrie.Iterator.
-
iterator
public BytesTrie.Iterator iterator(int maxStringLength)
Iterates from the current state of this trie.- Parameters:
maxStringLength
- If 0, the iterator returns full strings/byte sequences. Otherwise, the iterator returns strings with this maximum length.- Returns:
- A new BytesTrie.Iterator.
-
iterator
public static BytesTrie.Iterator iterator(byte[] trieBytes, int offset, int maxStringLength)
Iterates from the root of a byte-serialized BytesTrie.- Parameters:
trieBytes
- Bytes array that contains the serialized trie.offset
- Root offset of the trie in the array.maxStringLength
- If 0, the iterator returns full strings/byte sequences. Otherwise, the iterator returns strings with this maximum length.- Returns:
- A new BytesTrie.Iterator.
-
stop
private void stop()
-
readValue
private static int readValue(byte[] bytes, int pos, int leadByte)
-
skipValue
private static int skipValue(int pos, int leadByte)
-
skipValue
private static int skipValue(byte[] bytes, int pos)
-
jumpByDelta
@Deprecated public static int jumpByDelta(byte[] bytes, int pos)
Deprecated.This API is ICU internal only.Reads a jump delta and jumps.
-
skipDelta
private static int skipDelta(byte[] bytes, int pos)
-
branchNext
private BytesTrie.Result branchNext(int pos, int length, int inByte)
-
nextImpl
private BytesTrie.Result nextImpl(int pos, int inByte)
-
findUniqueValueFromBranch
private static long findUniqueValueFromBranch(byte[] bytes, int pos, int length, long uniqueValue)
-
findUniqueValue
private static long findUniqueValue(byte[] bytes, int pos, long uniqueValue)
-
getNextBranchBytes
private static void getNextBranchBytes(byte[] bytes, int pos, int length, java.lang.Appendable out)
-
append
private static void append(java.lang.Appendable out, int c)
-
-