Top Description Inners Fields Constructors Methods
com.ibm.icu.util

public final Class CharsTrie

extends Object
implements Cloneable, Iterable<CharsTrie.Entry>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable, java.lang.Cloneable
Imports
java.io.IOException, java.util.ArrayList, .NoSuchElementException, com.ibm.icu.text.UTF16, com.ibm.icu.util.BytesTrie.Result

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.

Author
Markus W. Scherer
ICU Status
Stable since ICU 4.8.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
CharsTrie.Entry

Return value type for the Iterator.

public static class
CharsTrie.Iterator

Iterator for all of the (string, value) pairs in a CharsTrie.

public 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

Modifier and TypeField and Description
private CharSequence
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
private int
private int
private int
private static BytesTrie.Result[]

Constructor Summary

AccessConstructor and Description
public
CharsTrie(CharSequence
CharSequence that contains the serialized trie.
trieChars
,
int
Root offset of the trie in the CharSequence.
offset
)

Constructs a CharsTrie reader instance.

public
CharsTrie(CharsTrie other)

Copy constructor.

Method Summary

Modifier and TypeMethod and Description
private static void
append(Appendable out, int c)

private BytesTrie.Result
branchNext(int pos, int length, int inUnit)

public CharsTrie

Returns:

A shallow clone of this trie.
clone
()

Overrides java.lang.Object.clone.

Clones this trie reader object and its state, but not the char array which will be shared.
public BytesTrie.Result

Returns:

The match/value 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(CharSequence chars, int pos, long uniqueValue)

private static long
findUniqueValueFromBranch(CharSequence chars, int pos, int length, long uniqueValue)

public BytesTrie.Result

Returns:

The match/value Result.
first
(int
Input char value. Values below 0 and above 0xffff will never match.
inUnit
)

Traverses the trie from the initial state for this input char.

public BytesTrie.Result

Returns:

The match/value Result.
firstForCodePoint
(int
A Unicode code point 0..0x10ffff.
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(CharSequence chars, int pos, int length, Appendable out)

public int

Returns:

The number of chars which continue the string from here.
getNextChars
(Appendable
Each next char is appended to this object. (Only uses the out.append(c) method.)
out
)

Finds each char which continues the string from the current state.

public long

Returns:

opaque state value
getState64
()

Returns the state of this trie as a 64-bit integer.

public long

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.
getUniqueValue
()

Determines whether all strings reachable from the current state map to the same value, and if so, returns that value.

public int

Returns:

The value for the string so far.
getValue
()

Returns a matching string's value if called immediately after current()/first()/next() returned Result.INTERMEDIATE_VALUE or Result.FINAL_VALUE.

public CharsTrie.Iterator

Returns:

A new CharsTrie.Iterator.
iterator
()

Implements java.lang.Iterable.iterator.

Iterates from the current state of this trie.
public CharsTrie.Iterator

Returns:

A new CharsTrie.Iterator.
iterator
(int
If 0, the iterator returns full strings. Otherwise, the iterator returns strings with this maximum length.
maxStringLength
)

Iterates from the current state of this trie.

public static CharsTrie.Iterator

Returns:

A new CharsTrie.Iterator.
iterator
(CharSequence
CharSequence that contains the serialized trie.
trieChars
,
int
Root offset of the trie in the CharSequence.
offset
,
int
If 0, the iterator returns full strings. Otherwise, the iterator returns strings with this maximum length.
maxStringLength
)

Iterates from the root of a char-serialized BytesTrie.

private static int
jumpByDelta(CharSequence chars, int pos)

public BytesTrie.Result

Returns:

The match/value Result.
next
(int
Input char value. Values below 0 and above 0xffff will never match.
inUnit
)

Traverses the trie from the current state for this input char.

public BytesTrie.Result

Returns:

The match/value Result.
next
(CharSequence
Contains a string.
s
,
int
The start index of the string in s.
sIndex
,
int
The (exclusive) end index of the string in s.
sLimit
)

Traverses the trie from the current state for this string.

public BytesTrie.Result

Returns:

The match/value Result.
nextForCodePoint
(int
A Unicode code point 0..0x10ffff.
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(CharSequence chars, int pos, int leadUnit)

private static int
readValue(CharSequence chars, int pos, int leadUnit)

public CharsTrie

Returns:

this
reset
()

Resets this trie to its initial state.

public CharsTrie

Returns:

this
resetToState
(CharsTrie.State
The State object which holds a saved trie state.
state
)

Resets this trie to the saved state.

public CharsTrie

Returns:

this
resetToState64
(long
The opaque trie state value from getState64().
state
)

Resets this trie to the saved state.

public CharsTrie

Returns:

this
saveState
(CharsTrie.State
The State object to hold the trie's state.
state
)

Saves the state of this trie.

private static int
skipDelta(CharSequence chars, int pos)

private static int
skipNodeValue(int pos, int leadUnit)

private static int
skipValue(int pos, int leadUnit)

private static int
skipValue(CharSequence chars, int pos)

private void
stop()

Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

chars_back to summary
private CharSequence chars_
kMaxBranchLinearSubNodeLengthback to summary
pack-priv static final int kMaxBranchLinearSubNodeLength
kMaxLinearMatchLengthback to summary
pack-priv static final int kMaxLinearMatchLength
kMaxOneUnitDeltaback to summary
pack-priv static final int kMaxOneUnitDelta
kMaxOneUnitNodeValueback to summary
pack-priv static final int kMaxOneUnitNodeValue
kMaxOneUnitValueback to summary
pack-priv static final int kMaxOneUnitValue
kMaxTwoUnitDeltaback to summary
pack-priv static final int kMaxTwoUnitDelta
kMaxTwoUnitNodeValueback to summary
pack-priv static final int kMaxTwoUnitNodeValue
kMaxTwoUnitValueback to summary
pack-priv static final int kMaxTwoUnitValue
kMinLinearMatchback to summary
pack-priv static final int kMinLinearMatch
kMinTwoUnitDeltaLeadback to summary
pack-priv static final int kMinTwoUnitDeltaLead
kMinTwoUnitNodeValueLeadback to summary
pack-priv static final int kMinTwoUnitNodeValueLead
kMinTwoUnitValueLeadback to summary
pack-priv static final int kMinTwoUnitValueLead
kMinValueLeadback to summary
pack-priv static final int kMinValueLead
kNodeTypeMaskback to summary
pack-priv static final int kNodeTypeMask
kThreeUnitDeltaLeadback to summary
pack-priv static final int kThreeUnitDeltaLead
kThreeUnitNodeValueLeadback to summary
pack-priv static final int kThreeUnitNodeValueLead
kThreeUnitValueLeadback to summary
pack-priv static final int kThreeUnitValueLead
kValueIsFinalback to summary
pack-priv static final int kValueIsFinal
pos_back to summary
private int pos_
remainingMatchLength_back to summary
private int remainingMatchLength_
root_back to summary
private int root_
valueResults_back to summary
private static BytesTrie.Result[] valueResults_

Constructor Detail

CharsTrieback to summary
public CharsTrie(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

CharSequence that contains the serialized trie.

offset:int

Root offset of the trie in the CharSequence.

ICU Status
Stable since ICU 4.8.
CharsTrieback to summary
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.

ICU Status
Stable since ICU 64.

Method Detail

appendback to summary
private static void append(Appendable out, int c)
branchNextback to summary
private BytesTrie.Result branchNext(int pos, int length, int inUnit)
cloneback to summary
public CharsTrie clone() throws CloneNotSupportedException

Overrides java.lang.Object.clone.

Clones this trie reader object and its state, but not the char array which will be shared.

Returns:CharsTrie

A shallow clone of this trie.

Annotations
@Override
Exceptions
CloneNotSupportedException:

Doc from java.lang.Object.clone.

if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.

ICU Status
Stable since ICU 4.8.
currentback to summary
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:BytesTrie.Result

The match/value Result.

ICU Status
Stable since ICU 4.8.
findUniqueValueback to summary
private static long findUniqueValue(CharSequence chars, int pos, long uniqueValue)
findUniqueValueFromBranchback to summary
private static long findUniqueValueFromBranch(CharSequence chars, int pos, int length, long uniqueValue)
firstback to summary
public BytesTrie.Result first(int inUnit)

Traverses the trie from the initial state for this input char. Equivalent to reset().next(inUnit).

Parameters
inUnit:int

Input char value. Values below 0 and above 0xffff will never match.

Returns:BytesTrie.Result

The match/value Result.

ICU Status
Stable since ICU 4.8.
firstForCodePointback to summary
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:int

A Unicode code point 0..0x10ffff.

Returns:BytesTrie.Result

The match/value Result.

ICU Status
Stable since ICU 4.8.
getNextBranchCharsback to summary
private static void getNextBranchChars(CharSequence chars, int pos, int length, Appendable out)
getNextCharsback to summary
public int getNextChars(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:Appendable

Each next char is appended to this object. (Only uses the out.append(c) method.)

Returns:int

The number of chars which continue the string from here.

ICU Status
Stable since ICU 4.8.
getState64back to summary
public long getState64()

Returns the state of this trie as a 64-bit integer. The state value is never 0.

Returns:long

opaque state value

ICU Status
Stable since ICU 64.
See Also
resetToState64
getUniqueValueback to summary
public long getUniqueValue()

Determines whether all strings reachable from the current state map to the same value, and if so, returns that value.

Returns:long

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.

ICU Status
Stable since ICU 4.8.
getValueback to summary
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:int

The value for the string so far.

ICU Status
Stable since ICU 4.8.
iteratorback to summary
public CharsTrie.Iterator iterator()

Implements java.lang.Iterable.iterator.

Iterates from the current state of this trie.

Returns:CharsTrie.Iterator

A new CharsTrie.Iterator.

Annotations
@Override
ICU Status
Stable since ICU 4.8.
iteratorback to summary
public CharsTrie.Iterator iterator(int maxStringLength)

Iterates from the current state of this trie.

Parameters
maxStringLength:int

If 0, the iterator returns full strings. Otherwise, the iterator returns strings with this maximum length.

Returns:CharsTrie.Iterator

A new CharsTrie.Iterator.

ICU Status
Stable since ICU 4.8.
iteratorback to summary
public static CharsTrie.Iterator iterator(CharSequence trieChars, int offset, int maxStringLength)

Iterates from the root of a char-serialized BytesTrie.

Parameters
trieChars:CharSequence

CharSequence that contains the serialized trie.

offset:int

Root offset of the trie in the CharSequence.

maxStringLength:int

If 0, the iterator returns full strings. Otherwise, the iterator returns strings with this maximum length.

Returns:CharsTrie.Iterator

A new CharsTrie.Iterator.

ICU Status
Stable since ICU 4.8.
jumpByDeltaback to summary
private static int jumpByDelta(CharSequence chars, int pos)
nextback to summary
public BytesTrie.Result next(int inUnit)

Traverses the trie from the current state for this input char.

Parameters
inUnit:int

Input char value. Values below 0 and above 0xffff will never match.

Returns:BytesTrie.Result

The match/value Result.

ICU Status
Stable since ICU 4.8.
nextback to summary
public BytesTrie.Result next(CharSequence s, int sIndex, int sLimit)

Traverses the trie from the current state for this string. Equivalent to

Result result=current();
for(each c in s)
  if(!result.hasNext()) return Result.NO_MATCH;
  result=next(c);
return result;
Parameters
s:CharSequence

Contains a string.

sIndex:int

The start index of the string in s.

sLimit:int

The (exclusive) end index of the string in s.

Returns:BytesTrie.Result

The match/value Result.

ICU Status
Stable since ICU 4.8.
nextForCodePointback to summary
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:int

A Unicode code point 0..0x10ffff.

Returns:BytesTrie.Result

The match/value Result.

ICU Status
Stable since ICU 4.8.
nextImplback to summary
private BytesTrie.Result nextImpl(int pos, int inUnit)
readNodeValueback to summary
private static int readNodeValue(CharSequence chars, int pos, int leadUnit)
readValueback to summary
private static int readValue(CharSequence chars, int pos, int leadUnit)
resetback to summary
public CharsTrie reset()

Resets this trie to its initial state.

Returns:CharsTrie

this

ICU Status
Stable since ICU 4.8.
resetToStateback to summary
public CharsTrie resetToState(CharsTrie.State state)

Resets this trie to the saved state. Slower than resetToState64(long) which does not validate the state value.

Parameters
state:CharsTrie.State

The State object which holds a saved trie state.

Returns:CharsTrie

this

Exceptions
IllegalArgumentException:
if the state object contains no state, or the state of a different trie
ICU Status
Stable since ICU 4.8.
See Also
saveState, reset
resetToState64back to summary
public CharsTrie resetToState64(long state)

Resets this trie to the saved state. Unlike resetToState(State), the 64-bit state value must be from getState64() from the same trie object or from one initialized the exact same way. Because of no validation, this method is faster.

Parameters
state:long

The opaque trie state value from getState64().

Returns:CharsTrie

this

ICU Status
Stable since ICU 64.
See Also
getState64, resetToState, reset
saveStateback to summary
public CharsTrie saveState(CharsTrie.State state)

Saves the state of this trie.

Parameters
state:CharsTrie.State

The State object to hold the trie's state.

Returns:CharsTrie

this

ICU Status
Stable since ICU 4.8.
See Also
resetToState
skipDeltaback to summary
private static int skipDelta(CharSequence chars, int pos)
skipNodeValueback to summary
private static int skipNodeValue(int pos, int leadUnit)
skipValueback to summary
private static int skipValue(int pos, int leadUnit)
skipValueback to summary
private static int skipValue(CharSequence chars, int pos)
stopback to summary
private void stop()
com.ibm.icu.util back to summary

public final Class CharsTrie.Entry

extends Object
Class Inheritance

Return value type for the Iterator.
ICU Status
Stable since ICU 4.8.

Field Summary

Modifier and TypeField and Description
public CharSequence
chars

The string.

public int
value

The value associated with the string.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

charsback to summary
public CharSequence chars

The string.

ICU Status
Stable since ICU 4.8.
valueback to summary
public int value

The value associated with the string.

ICU Status
Stable since ICU 4.8.

Constructor Detail

Entryback to summary
private Entry()
com.ibm.icu.util back to summary

public final Class CharsTrie.Iterator

extends Object
implements Iterator<CharsTrie.Entry>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

Iterator for all of the (string, value) pairs in a CharsTrie.
ICU Status
Stable since ICU 4.8.

Field Summary

Modifier and TypeField and Description
private CharSequence
private CharsTrie.Entry
private int
private int
private int
private int
private int
private boolean
private ArrayList<Long>
private StringBuilder

Constructor Summary

AccessConstructor and Description
private
Iterator(CharSequence trieChars, int offset, int remainingMatchLength, int maxStringLength)

Method Summary

Modifier and TypeMethod and Description
private int
branchNext(int pos, int length)

public boolean

Returns:

true if there are more elements.
hasNext
()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
public CharsTrie.Entry

Returns:

An Entry with the string and value of the next element.
next
()

Implements java.util.Iterator.next.

Finds the next (string, value) pair if there is one.
public void
remove()

Overrides default java.util.Iterator.remove.

Iterator.remove() is not supported.
public CharsTrie.Iterator

Returns:

this
reset
()

Resets this iterator to its initial state.

private CharsTrie.Entry
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

chars_back to summary
private CharSequence chars_
entry_back to summary
private CharsTrie.Entry entry_
initialPos_back to summary
private int initialPos_
initialRemainingMatchLength_back to summary
private int initialRemainingMatchLength_
maxLength_back to summary
private int maxLength_
pos_back to summary
private int pos_
remainingMatchLength_back to summary
private int remainingMatchLength_
skipValue_back to summary
private boolean skipValue_
stack_back to summary
private ArrayList<Long> stack_
str_back to summary
private StringBuilder str_

Constructor Detail

Iteratorback to summary
private Iterator(CharSequence trieChars, int offset, int remainingMatchLength, int maxStringLength)

Method Detail

branchNextback to summary
private int branchNext(int pos, int length)
hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if there are more elements.

Annotations
@Override
ICU Status
Stable since ICU 4.8.
nextback to summary
public CharsTrie.Entry next()

Implements java.util.Iterator.next.

Finds the next (string, value) pair if there is one. If the string is truncated to the maximum length and does not have a real value, then the value is set to -1. In this case, this "not a real value" is indistinguishable from a real value of -1.

Returns:CharsTrie.Entry

An Entry with the string and value of the next element.

Annotations
@Override
Exceptions
NoSuchElementException:
- iteration has no more elements.
ICU Status
Stable since ICU 4.8.
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Iterator.remove() is not supported.

Annotations
@Override
Exceptions
UnsupportedOperationException:
(always)
ICU Status
Stable since ICU 4.8.
resetback to summary
public CharsTrie.Iterator reset()

Resets this iterator to its initial state.

Returns:CharsTrie.Iterator

this

ICU Status
Stable since ICU 4.8.
truncateAndStopback to summary
private CharsTrie.Entry truncateAndStop()
com.ibm.icu.util back to summary

public final Class CharsTrie.State

extends Object
Class Inheritance

CharsTrie state object, for saving a trie's current state and resetting the trie back to this state later.
ICU Status
Stable since ICU 4.8.

Field Summary

Modifier and TypeField and Description
private CharSequence
private int
private int
private int

Constructor Summary

AccessConstructor and Description
public
State()

Constructs an empty State.

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

charsback to summary
private CharSequence chars
posback to summary
private int pos
remainingMatchLengthback to summary
private int remainingMatchLength
rootback to summary
private int root

Constructor Detail

Stateback to summary
public State()

Constructs an empty State.

ICU Status
Stable since ICU 4.8.