Package com.ibm.icu.util
Class CharsTrie
- java.lang.Object
-
- com.ibm.icu.util.CharsTrie
-
- All Implemented Interfaces:
java.lang.Cloneable
,java.lang.Iterable<CharsTrie.Entry>
public final class CharsTrie extends java.lang.Object implements java.lang.Cloneable, java.lang.Iterable<CharsTrie.Entry>
Light-weight, non-const reader class for a CharsTrie. Traverses a char-serialized data structure with minimal state, for mapping strings (16-bit-unit 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
CharsTrie.Entry
Return value type for the Iterator.static class
CharsTrie.Iterator
Iterator for all of the (string, value) pairs in a CharsTrie.static class
CharsTrie.State
CharsTrie 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 java.lang.CharSequence
chars_
(package private) static int
kMaxBranchLinearSubNodeLength
(package private) static int
kMaxLinearMatchLength
(package private) static int
kMaxOneUnitDelta
(package private) static int
kMaxOneUnitNodeValue
(package private) static int
kMaxOneUnitValue
(package private) static int
kMaxTwoUnitDelta
(package private) static int
kMaxTwoUnitNodeValue
(package private) static int
kMaxTwoUnitValue
(package private) static int
kMinLinearMatch
(package private) static int
kMinTwoUnitDeltaLead
(package private) static int
kMinTwoUnitNodeValueLead
(package private) static int
kMinTwoUnitValueLead
(package private) static int
kMinValueLead
(package private) static int
kNodeTypeMask
(package private) static int
kThreeUnitDeltaLead
(package private) static int
kThreeUnitNodeValueLead
(package private) static int
kThreeUnitValueLead
(package 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 Modifier and Type Method Description private static void
append(java.lang.Appendable out, int c)
private BytesTrie.Result
branchNext(int pos, int length, int inUnit)
CharsTrie
clone()
Clones this trie reader object and its state, but not the char array which will be shared.BytesTrie.Result
current()
Determines whether the string so far matches, whether it has a value, and whether another input char can continue a matching string.private static long
findUniqueValue(java.lang.CharSequence chars, int pos, long uniqueValue)
private static long
findUniqueValueFromBranch(java.lang.CharSequence chars, int pos, int length, long uniqueValue)
BytesTrie.Result
first(int inUnit)
Traverses the trie from the initial state for this input char.BytesTrie.Result
firstForCodePoint(int cp)
Traverses the trie from the initial state for the one or two UTF-16 code units for this input code point.private static void
getNextBranchChars(java.lang.CharSequence chars, int pos, int length, java.lang.Appendable out)
int
getNextChars(java.lang.Appendable out)
Finds each char which continues the string from the current state.long
getState64()
Returns the state of this trie as a 64-bit integer.long
getUniqueValue()
Determines whether all strings reachable from the current state map to the same value, and if so, returns that value.int
getValue()
Returns a matching string's value if called immediately after current()/first()/next() returned Result.INTERMEDIATE_VALUE or Result.FINAL_VALUE.CharsTrie.Iterator
iterator()
Iterates from the current state of this trie.CharsTrie.Iterator
iterator(int maxStringLength)
Iterates from the current state of this trie.static CharsTrie.Iterator
iterator(java.lang.CharSequence trieChars, int offset, int maxStringLength)
Iterates from the root of a char-serialized BytesTrie.private static int
jumpByDelta(java.lang.CharSequence chars, int pos)
BytesTrie.Result
next(int inUnit)
Traverses the trie from the current state for this input char.BytesTrie.Result
next(java.lang.CharSequence s, int sIndex, int sLimit)
Traverses the trie from the current state for this string.BytesTrie.Result
nextForCodePoint(int cp)
Traverses the trie from the current state for the one or two UTF-16 code units for this input code point.private BytesTrie.Result
nextImpl(int pos, int inUnit)
private static int
readNodeValue(java.lang.CharSequence chars, int pos, int leadUnit)
private static int
readValue(java.lang.CharSequence chars, int pos, int leadUnit)
CharsTrie
reset()
Resets this trie to its initial state.CharsTrie
resetToState(CharsTrie.State state)
Resets this trie to the saved state.CharsTrie
resetToState64(long state)
Resets this trie to the saved state.CharsTrie
saveState(CharsTrie.State state)
Saves the state of this trie.private static int
skipDelta(java.lang.CharSequence chars, int pos)
private static int
skipNodeValue(int pos, int leadUnit)
private static int
skipValue(int pos, int leadUnit)
private static int
skipValue(java.lang.CharSequence chars, int pos)
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
-
kNodeTypeMask
static final int kNodeTypeMask
- See Also:
- Constant Field Values
-
kValueIsFinal
static final int kValueIsFinal
- See Also:
- Constant Field Values
-
kMaxOneUnitValue
static final int kMaxOneUnitValue
- See Also:
- Constant Field Values
-
kMinTwoUnitValueLead
static final int kMinTwoUnitValueLead
- See Also:
- Constant Field Values
-
kThreeUnitValueLead
static final int kThreeUnitValueLead
- See Also:
- Constant Field Values
-
kMaxTwoUnitValue
static final int kMaxTwoUnitValue
- See Also:
- Constant Field Values
-
kMaxOneUnitNodeValue
static final int kMaxOneUnitNodeValue
- See Also:
- Constant Field Values
-
kMinTwoUnitNodeValueLead
static final int kMinTwoUnitNodeValueLead
- See Also:
- Constant Field Values
-
kThreeUnitNodeValueLead
static final int kThreeUnitNodeValueLead
- See Also:
- Constant Field Values
-
kMaxTwoUnitNodeValue
static final int kMaxTwoUnitNodeValue
- See Also:
- Constant Field Values
-
kMaxOneUnitDelta
static final int kMaxOneUnitDelta
- See Also:
- Constant Field Values
-
kMinTwoUnitDeltaLead
static final int kMinTwoUnitDeltaLead
- See Also:
- Constant Field Values
-
kThreeUnitDeltaLead
static final int kThreeUnitDeltaLead
- See Also:
- Constant Field Values
-
kMaxTwoUnitDelta
static final int kMaxTwoUnitDelta
- See Also:
- Constant Field Values
-
chars_
private java.lang.CharSequence chars_
-
root_
private int root_
-
pos_
private int pos_
-
remainingMatchLength_
private int remainingMatchLength_
-
-
Constructor Detail
-
CharsTrie
public CharsTrie(java.lang.CharSequence trieChars, int offset)
Constructs a CharsTrie reader instance.The CharSequence must contain a copy of a char sequence from the CharsTrieBuilder, with the offset indicating the first char of that sequence. The CharsTrie object will not read more chars than the CharsTrieBuilder generated in the corresponding build() call.
The CharSequence is not copied/cloned and must not be modified while the CharsTrie object is in use.
- Parameters:
trieChars
- CharSequence that contains the serialized trie.offset
- Root offset of the trie in the CharSequence.
-
CharsTrie
public CharsTrie(CharsTrie other)
Copy constructor. Makes a shallow copy of the other trie reader object and its state. Does not copy the char array which will be shared. Same as clone() but without the throws clause.
-
-
Method Detail
-
clone
public CharsTrie clone() throws java.lang.CloneNotSupportedException
Clones this trie reader object and its state, but not the char array which will be shared.- Overrides:
clone
in classjava.lang.Object
- Returns:
- A shallow clone of this trie.
- Throws:
java.lang.CloneNotSupportedException
-
reset
public CharsTrie 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 CharsTrie 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.CharsTrie.State)
,reset()
-
saveState
public CharsTrie saveState(CharsTrie.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.CharsTrie.State)
-
resetToState
public CharsTrie resetToState(CharsTrie.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.CharsTrie.State)
,reset()
-
current
public BytesTrie.Result current()
Determines whether the string so far matches, whether it has a value, and whether another input char can continue a matching string.- Returns:
- The match/value Result.
-
first
public BytesTrie.Result first(int inUnit)
Traverses the trie from the initial state for this input char. Equivalent to reset().next(inUnit).- Parameters:
inUnit
- Input char value. Values below 0 and above 0xffff will never match.- Returns:
- The match/value Result.
-
firstForCodePoint
public BytesTrie.Result firstForCodePoint(int cp)
Traverses the trie from the initial state for the one or two UTF-16 code units for this input code point. Equivalent to reset().nextForCodePoint(cp).- Parameters:
cp
- A Unicode code point 0..0x10ffff.- Returns:
- The match/value Result.
-
next
public BytesTrie.Result next(int inUnit)
Traverses the trie from the current state for this input char.- Parameters:
inUnit
- Input char value. Values below 0 and above 0xffff will never match.- Returns:
- The match/value Result.
-
nextForCodePoint
public BytesTrie.Result nextForCodePoint(int cp)
Traverses the trie from the current state for the one or two UTF-16 code units for this input code point.- Parameters:
cp
- A Unicode code point 0..0x10ffff.- Returns:
- The match/value Result.
-
next
public BytesTrie.Result next(java.lang.CharSequence s, int sIndex, int sLimit)
Traverses the trie from the current state for this string. 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.sIndex
- The start index of the string in s.sLimit
- The (exclusive) end index of the string in s.- Returns:
- The match/value Result.
-
getValue
public int getValue()
Returns a matching string'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 string so far.
-
getUniqueValue
public long getUniqueValue()
Determines whether all strings 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 strings reachable from the current state map to the same value; otherwise returns 0.
-
getNextChars
public int getNextChars(java.lang.Appendable out)
Finds each char which continues the string from the current state. That is, each char c for which it would be next(c)!=Result.NO_MATCH now.- Parameters:
out
- Each next char is appended to this object. (Only uses the out.append(c) method.)- Returns:
- The number of chars which continue the string from here.
-
iterator
public CharsTrie.Iterator iterator()
Iterates from the current state of this trie.- Specified by:
iterator
in interfacejava.lang.Iterable<CharsTrie.Entry>
- Returns:
- A new CharsTrie.Iterator.
-
iterator
public CharsTrie.Iterator iterator(int maxStringLength)
Iterates from the current state of this trie.- Parameters:
maxStringLength
- If 0, the iterator returns full strings. Otherwise, the iterator returns strings with this maximum length.- Returns:
- A new CharsTrie.Iterator.
-
iterator
public static CharsTrie.Iterator iterator(java.lang.CharSequence trieChars, int offset, int maxStringLength)
Iterates from the root of a char-serialized BytesTrie.- Parameters:
trieChars
- CharSequence that contains the serialized trie.offset
- Root offset of the trie in the CharSequence.maxStringLength
- If 0, the iterator returns full strings. Otherwise, the iterator returns strings with this maximum length.- Returns:
- A new CharsTrie.Iterator.
-
stop
private void stop()
-
readValue
private static int readValue(java.lang.CharSequence chars, int pos, int leadUnit)
-
skipValue
private static int skipValue(int pos, int leadUnit)
-
skipValue
private static int skipValue(java.lang.CharSequence chars, int pos)
-
readNodeValue
private static int readNodeValue(java.lang.CharSequence chars, int pos, int leadUnit)
-
skipNodeValue
private static int skipNodeValue(int pos, int leadUnit)
-
jumpByDelta
private static int jumpByDelta(java.lang.CharSequence chars, int pos)
-
skipDelta
private static int skipDelta(java.lang.CharSequence chars, int pos)
-
branchNext
private BytesTrie.Result branchNext(int pos, int length, int inUnit)
-
nextImpl
private BytesTrie.Result nextImpl(int pos, int inUnit)
-
findUniqueValueFromBranch
private static long findUniqueValueFromBranch(java.lang.CharSequence chars, int pos, int length, long uniqueValue)
-
findUniqueValue
private static long findUniqueValue(java.lang.CharSequence chars, int pos, long uniqueValue)
-
getNextBranchChars
private static void getNextBranchChars(java.lang.CharSequence chars, int pos, int length, java.lang.Appendable out)
-
append
private static void append(java.lang.Appendable out, int c)
-
-