ucharstrie.h
Go to the documentation of this file.
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2010-2012, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
* file name: ucharstrie.h
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2010nov14
* created by: Markus W. Scherer
*/
#ifndef __UCHARSTRIE_H__
#define __UCHARSTRIE_H__
#include "unicode/utypes.h"
#if U_SHOW_CPLUSPLUS_API
#include "unicode/unistr.h"
#include "unicode/uobject.h"
#include "unicode/ustringtrie.h"
U_NAMESPACE_BEGIN
class Appendable;
class UCharsTrieBuilder;
class UVector32;
class U_COMMON_API UCharsTrie : public UMemory {
public:
UCharsTrie(ConstChar16Ptr trieUChars)
: ownedArray_(nullptr), uchars_(trieUChars),
pos_(uchars_), remainingMatchLength_(-1) {}
~UCharsTrie();
UCharsTrie(const UCharsTrie &other)
: ownedArray_(nullptr), uchars_(other.uchars_),
pos_(other.pos_), remainingMatchLength_(other.remainingMatchLength_) {}
UCharsTrie &reset() {
pos_=uchars_;
remainingMatchLength_=-1;
return *this;
}
uint64_t getState64() const {
return (static_cast<uint64_t>(remainingMatchLength_ + 2) << kState64RemainingShift) |
(uint64_t)(pos_ - uchars_);
}
UCharsTrie &resetToState64(uint64_t state) {
remainingMatchLength_ = static_cast<int32_t>(state >> kState64RemainingShift) - 2;
pos_ = uchars_ + (state & kState64PosMask);
return *this;
}
class State : public UMemory {
public:
State() { uchars=nullptr; }
private:
friend class UCharsTrie;
const char16_t *uchars;
const char16_t *pos;
int32_t remainingMatchLength;
};
const UCharsTrie &saveState(State &state) const {
state.uchars=uchars_;
state.pos=pos_;
state.remainingMatchLength=remainingMatchLength_;
return *this;
}
UCharsTrie &resetToState(const State &state) {
if(uchars_==state.uchars && uchars_!=nullptr) {
pos_=state.pos;
remainingMatchLength_=state.remainingMatchLength;
}
return *this;
}
UStringTrieResult current() const;
inline UStringTrieResult first(int32_t uchar) {
remainingMatchLength_=-1;
return nextImpl(uchars_, uchar);
}
UStringTrieResult firstForCodePoint(UChar32 cp);
UStringTrieResult next(int32_t uchar);
UStringTrieResult nextForCodePoint(UChar32 cp);
UStringTrieResult next(ConstChar16Ptr s, int32_t length);
inline int32_t getValue() const {
const char16_t *pos=pos_;
int32_t leadUnit=*pos++;
// U_ASSERT(leadUnit>=kMinValueLead);
return leadUnit&kValueIsFinal ?
readValue(pos, leadUnit&0x7fff) : readNodeValue(pos, leadUnit);
}
inline UBool hasUniqueValue(int32_t &uniqueValue) const {
const char16_t *pos=pos_;
// Skip the rest of a pending linear-match node.
return pos!=nullptr && findUniqueValue(pos+remainingMatchLength_+1, false, uniqueValue);
}
int32_t getNextUChars(Appendable &out) const;
class U_COMMON_API Iterator : public UMemory {
public:
Iterator(ConstChar16Ptr trieUChars, int32_t maxStringLength, UErrorCode &errorCode);
Iterator(const UCharsTrie &trie, int32_t maxStringLength, UErrorCode &errorCode);
~Iterator();
Iterator &reset();
UBool hasNext() const;
UBool next(UErrorCode &errorCode);
const UnicodeString &getString() const { return str_; }
int32_t getValue() const { return value_; }
private:
UBool truncateAndStop() {
pos_=nullptr;
value_=-1; // no real value for str
return true;
}
const char16_t *branchNext(const char16_t *pos, int32_t length, UErrorCode &errorCode);
const char16_t *uchars_;
const char16_t *pos_;
const char16_t *initialPos_;
int32_t remainingMatchLength_;
int32_t initialRemainingMatchLength_;
UBool skipValue_; // Skip intermediate value which was already delivered.
UnicodeString str_;
int32_t maxLength_;
int32_t value_;
// The stack stores pairs of integers for backtracking to another
// outbound edge of a branch node.
// The first integer is an offset from uchars_.
// The second integer has the str_.length() from before the node in bits 15..0,
// and the remaining branch length in bits 31..16.
// (We could store the remaining branch length minus 1 in bits 30..16 and not use the sign bit,
// but the code looks more confusing that way.)
UVector32 *stack_;
};
private:
friend class UCharsTrieBuilder;
UCharsTrie(char16_t *adoptUChars, const char16_t *trieUChars)
: ownedArray_(adoptUChars), uchars_(trieUChars),
pos_(uchars_), remainingMatchLength_(-1) {}
// No assignment operator.
UCharsTrie &operator=(const UCharsTrie &other) = delete;
inline void stop() {
pos_=nullptr;
}
// Reads a compact 32-bit integer.
// pos is already after the leadUnit, and the lead unit has bit 15 reset.
static inline int32_t readValue(const char16_t *pos, int32_t leadUnit) {
int32_t value;
if(leadUnit<kMinTwoUnitValueLead) {
value=leadUnit;
} else if(leadUnit<kThreeUnitValueLead) {
value=((leadUnit-kMinTwoUnitValueLead)<<16)|*pos;
} else {
value=(pos[0]<<16)|pos[1];
}
return value;
}
static inline const char16_t *skipValue(const char16_t *pos, int32_t leadUnit) {
if(leadUnit>=kMinTwoUnitValueLead) {
if(leadUnit<kThreeUnitValueLead) {
++pos;
} else {
pos+=2;
}
}
return pos;
}
static inline const char16_t *skipValue(const char16_t *pos) {
int32_t leadUnit=*pos++;
return skipValue(pos, leadUnit&0x7fff);
}
static inline int32_t readNodeValue(const char16_t *pos, int32_t leadUnit) {
// U_ASSERT(kMinValueLead<=leadUnit && leadUnit<kValueIsFinal);
int32_t value;
if(leadUnit<kMinTwoUnitNodeValueLead) {
value=(leadUnit>>6)-1;
} else if(leadUnit<kThreeUnitNodeValueLead) {
value=(((leadUnit&0x7fc0)-kMinTwoUnitNodeValueLead)<<10)|*pos;
} else {
value=(pos[0]<<16)|pos[1];
}
return value;
}
static inline const char16_t *skipNodeValue(const char16_t *pos, int32_t leadUnit) {
// U_ASSERT(kMinValueLead<=leadUnit && leadUnit<kValueIsFinal);
if(leadUnit>=kMinTwoUnitNodeValueLead) {
if(leadUnit<kThreeUnitNodeValueLead) {
++pos;
} else {
pos+=2;
}
}
return pos;
}
static inline const char16_t *jumpByDelta(const char16_t *pos) {
int32_t delta=*pos++;
if(delta>=kMinTwoUnitDeltaLead) {
if(delta==kThreeUnitDeltaLead) {
delta=(pos[0]<<16)|pos[1];
pos+=2;
} else {
delta=((delta-kMinTwoUnitDeltaLead)<<16)|*pos++;
}
}
return pos+delta;
}
static const char16_t *skipDelta(const char16_t *pos) {
int32_t delta=*pos++;
if(delta>=kMinTwoUnitDeltaLead) {
if(delta==kThreeUnitDeltaLead) {
pos+=2;
} else {
++pos;
}
}
return pos;
}
static inline UStringTrieResult valueResult(int32_t node) {
return (UStringTrieResult)(USTRINGTRIE_INTERMEDIATE_VALUE-(node>>15));
}
// Handles a branch node for both next(uchar) and next(string).
UStringTrieResult branchNext(const char16_t *pos, int32_t length, int32_t uchar);
// Requires remainingLength_<0.
UStringTrieResult nextImpl(const char16_t *pos, int32_t uchar);
// Helper functions for hasUniqueValue().
// Recursively finds a unique value (or whether there is not a unique one)
// from a branch.
static const char16_t *findUniqueValueFromBranch(const char16_t *pos, int32_t length,
UBool haveUniqueValue, int32_t &uniqueValue);
// Recursively finds a unique value (or whether there is not a unique one)
// starting from a position on a node lead unit.
static UBool findUniqueValue(const char16_t *pos, UBool haveUniqueValue, int32_t &uniqueValue);
// Helper functions for getNextUChars().
// getNextUChars() when pos is on a branch node.
static void getNextBranchUChars(const char16_t *pos, int32_t length, Appendable &out);
// UCharsTrie data structure
//
// The trie consists of a series of char16_t-serialized nodes for incremental
// Unicode string/char16_t sequence matching. (char16_t=16-bit unsigned integer)
// The root node is at the beginning of the trie data.
//
// Types of nodes are distinguished by their node lead unit ranges.
// After each node, except a final-value node, another node follows to
// encode match values or continue matching further units.
//
// Node types:
// - Final-value node: Stores a 32-bit integer in a compact, variable-length format.
// The value is for the string/char16_t sequence so far.
// - Match node, optionally with an intermediate value in a different compact format.
// The value, if present, is for the string/char16_t sequence so far.
//
// Aside from the value, which uses the node lead unit's high bits:
//
// - Linear-match node: Matches a number of units.
// - Branch node: Branches to other nodes according to the current input unit.
// The node unit is the length of the branch (number of units to select from)
// minus 1. It is followed by a sub-node:
// - If the length is at most kMaxBranchLinearSubNodeLength, then
// there are length-1 (key, value) pairs and then one more comparison unit.
// If one of the key units matches, then the value is either a final value for
// the string so far, or a "jump" delta to the next node.
// If the last unit matches, then matching continues with the next node.
// (Values have the same encoding as final-value nodes.)
// - If the length is greater than kMaxBranchLinearSubNodeLength, then
// there is one unit and one "jump" delta.
// If the input unit is less than the sub-node unit, then "jump" by delta to
// the next sub-node which will have a length of length/2.
// (The delta has its own compact encoding.)
// Otherwise, skip the "jump" delta to the next sub-node
// which will have a length of length-length/2.
// Match-node lead unit values, after masking off intermediate-value bits:
// 0000..002f: Branch node. If node!=0 then the length is node+1, otherwise
// the length is one more than the next unit.
// For a branch sub-node with at most this many entries, we drop down
// to a linear search.
static const int32_t kMaxBranchLinearSubNodeLength=5;
// 0030..003f: Linear-match node, match 1..16 units and continue reading the next node.
static const int32_t kMinLinearMatch=0x30;
static const int32_t kMaxLinearMatchLength=0x10;
// Match-node lead unit bits 14..6 for the optional intermediate value.
// If these bits are 0, then there is no intermediate value.
// Otherwise, see the *NodeValue* constants below.
static const int32_t kMinValueLead=kMinLinearMatch+kMaxLinearMatchLength; // 0x0040
static const int32_t kNodeTypeMask=kMinValueLead-1; // 0x003f
// A final-value node has bit 15 set.
static const int32_t kValueIsFinal=0x8000;
// Compact value: After testing and masking off bit 15, use the following thresholds.
static const int32_t kMaxOneUnitValue=0x3fff;
static const int32_t kMinTwoUnitValueLead=kMaxOneUnitValue+1; // 0x4000
static const int32_t kThreeUnitValueLead=0x7fff;
static const int32_t kMaxTwoUnitValue=((kThreeUnitValueLead-kMinTwoUnitValueLead)<<16)-1; // 0x3ffeffff
// Compact intermediate-value integer, lead unit shared with a branch or linear-match node.
static const int32_t kMaxOneUnitNodeValue=0xff;
static const int32_t kMinTwoUnitNodeValueLead=kMinValueLead+((kMaxOneUnitNodeValue+1)<<6); // 0x4040
static const int32_t kThreeUnitNodeValueLead=0x7fc0;
static const int32_t kMaxTwoUnitNodeValue=
((kThreeUnitNodeValueLead-kMinTwoUnitNodeValueLead)<<10)-1; // 0xfdffff
// Compact delta integers.
static const int32_t kMaxOneUnitDelta=0xfbff;
static const int32_t kMinTwoUnitDeltaLead=kMaxOneUnitDelta+1; // 0xfc00
static const int32_t kThreeUnitDeltaLead=0xffff;
static const int32_t kMaxTwoUnitDelta=((kThreeUnitDeltaLead-kMinTwoUnitDeltaLead)<<16)-1; // 0x03feffff
// For getState64():
// The remainingMatchLength_ is -1..14=(kMaxLinearMatchLength=0x10)-2
// so we need at least 5 bits for that.
// We add 2 to store it as a positive value 1..16=kMaxLinearMatchLength.
static constexpr int32_t kState64RemainingShift = 59;
static constexpr uint64_t kState64PosMask = (UINT64_C(1) << kState64RemainingShift) - 1;
char16_t *ownedArray_;
// Fixed value referencing the UCharsTrie words.
const char16_t *uchars_;
// Iterator variables.
// Pointer to next trie unit to read. nullptr if no more matches.
const char16_t *pos_;
// Remaining length of a linear-match node, minus 1. Negative if not in such a node.
int32_t remainingMatchLength_;
};
U_NAMESPACE_END
#endif /* U_SHOW_CPLUSPLUS_API */
#endif // __UCHARSTRIE_H__
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
icu::ConstChar16Ptr
const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition: char16ptr.h:149
icu::UCharsTrie::Iterator
Iterator for all of the (string, value) pairs in a UCharsTrie.
Definition: ucharstrie.h:294
icu::UCharsTrie::Iterator::Iterator
Iterator(ConstChar16Ptr trieUChars, int32_t maxStringLength, UErrorCode &errorCode)
Iterates from the root of a char16_t-serialized UCharsTrie.
icu::UCharsTrie::Iterator::reset
Iterator & reset()
Resets this iterator to its initial state.
icu::UCharsTrie::Iterator::hasNext
UBool hasNext() const
icu::UCharsTrie::Iterator::~Iterator
~Iterator()
Destructor.
icu::UCharsTrie::Iterator::getValue
int32_t getValue() const
Definition: ucharstrie.h:366
icu::UCharsTrie::Iterator::next
UBool next(UErrorCode &errorCode)
Finds the next (string, value) pair if there is one.
icu::UCharsTrie::Iterator::getString
const UnicodeString & getString() const
Definition: ucharstrie.h:361
icu::UCharsTrie::Iterator::Iterator
Iterator(const UCharsTrie &trie, int32_t maxStringLength, UErrorCode &errorCode)
Iterates from the current state of the specified UCharsTrie.
icu::UCharsTrie::State
UCharsTrie state object, for saving a trie's current state and resetting the trie back to this state ...
Definition: ucharstrie.h:138
icu::UCharsTrie::State::State
State()
Constructs an empty State.
Definition: ucharstrie.h:144
icu::UCharsTrie
Light-weight, non-const reader class for a UCharsTrie.
Definition: ucharstrie.h:53
icu::UCharsTrie::UCharsTrie
UCharsTrie(ConstChar16Ptr trieUChars)
Constructs a UCharsTrie reader instance.
Definition: ucharstrie.h:69
icu::UCharsTrie::UCharsTrie
UCharsTrie(const UCharsTrie &other)
Copy constructor, copies the other trie reader object and its state, but not the char16_t array which...
Definition: ucharstrie.h:85
icu::UCharsTrie::hasUniqueValue
UBool hasUniqueValue(int32_t &uniqueValue) const
Determines whether all strings reachable from the current state map to the same value.
Definition: ucharstrie.h:275
icu::UCharsTrie::getState64
uint64_t getState64() const
Returns the state of this trie as a 64-bit integer.
Definition: ucharstrie.h:108
icu::UCharsTrie::reset
UCharsTrie & reset()
Resets this trie to its initial state.
Definition: ucharstrie.h:94
icu::UCharsTrie::resetToState64
UCharsTrie & resetToState64(uint64_t state)
Resets this trie to the saved state.
Definition: ucharstrie.h:127
icu::UCharsTrie::next
UStringTrieResult next(int32_t uchar)
Traverses the trie from the current state for this input char16_t.
icu::UCharsTrie::nextForCodePoint
UStringTrieResult nextForCodePoint(UChar32 cp)
Traverses the trie from the current state for the one or two UTF-16 code units for this input code po...
icu::UCharsTrie::getValue
int32_t getValue() const
Returns a matching string's value if called immediately after current()/first()/next() returned USTRI...
Definition: ucharstrie.h:258
icu::UCharsTrie::firstForCodePoint
UStringTrieResult firstForCodePoint(UChar32 cp)
Traverses the trie from the initial state for the one or two UTF-16 code units for this input code po...
icu::UCharsTrie::current
UStringTrieResult current() const
Determines whether the string so far matches, whether it has a value, and whether another input char1...
icu::UCharsTrie::resetToState
UCharsTrie & resetToState(const State &state)
Resets this trie to the saved state.
Definition: ucharstrie.h:177
icu::UCharsTrie::first
UStringTrieResult first(int32_t uchar)
Traverses the trie from the initial state for this input char16_t.
Definition: ucharstrie.h:200
icu::UCharsTrie::~UCharsTrie
~UCharsTrie()
Destructor.
icu::UCharsTrie::next
UStringTrieResult next(ConstChar16Ptr s, int32_t length)
Traverses the trie from the current state for this string.
icu::UCharsTrie::saveState
const UCharsTrie & saveState(State &state) const
Saves the state of this trie.
Definition: ucharstrie.h:160
icu::UCharsTrie::getNextUChars
int32_t getNextUChars(Appendable &out) const
Finds each char16_t which continues the string from the current state.
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
UChar32
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:435
UINT64_C
#define UINT64_C(c)
Provides a platform independent way to specify an unsigned 64-bit integer constant.
Definition: umachine.h:219
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
unistr.h
C++ API: Unicode String.
uobject.h
C++ API: Common ICU base class UObject.
ustringtrie.h
C API: Helper definitions for dictionary trie APIs.
UStringTrieResult
UStringTrieResult
Return values for BytesTrie::next(), UCharsTrie::next() and similar methods.
Definition: ustringtrie.h:35
USTRINGTRIE_INTERMEDIATE_VALUE
@ USTRINGTRIE_INTERMEDIATE_VALUE
The input unit(s) continued a matching string and there is a value for the string so far.
Definition: ustringtrie.h:66
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
U_COMMON_API
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition: utypes.h:300