17#ifndef __STRINGTRIEBUILDER_H__
18#define __STRINGTRIEBUILDER_H__
22#if U_SHOW_CPLUSPLUS_API
70#ifndef U_HIDE_INTERNAL_API
72 static int32_t hashNode(
const void *node);
74 static UBool equalNodes(
const void *left,
const void *right);
83 virtual ~StringTrieBuilder();
85#ifndef U_HIDE_INTERNAL_API
87 void createCompactBuilder(int32_t sizeGuess,
UErrorCode &errorCode);
89 void deleteCompactBuilder();
95 int32_t writeNode(int32_t start, int32_t limit, int32_t unitIndex);
97 int32_t writeBranchSubNode(int32_t start, int32_t limit, int32_t unitIndex, int32_t length);
102#ifndef U_HIDE_INTERNAL_API
104 Node* makeNode(int32_t start,
109 Node* makeBranchSubNode(int32_t start,
117 virtual int32_t getElementStringLength(int32_t i)
const = 0;
119 virtual char16_t getElementUnit(int32_t i, int32_t unitIndex)
const = 0;
121 virtual int32_t getElementValue(int32_t i)
const = 0;
126 virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex)
const = 0;
130 virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex)
const = 0;
132 virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count)
const = 0;
134 virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex,
char16_t unit)
const = 0;
137 virtual UBool matchNodesCanHaveValues()
const = 0;
140 virtual int32_t getMaxBranchLinearSubNodeLength()
const = 0;
142 virtual int32_t getMinLinearMatch()
const = 0;
144 virtual int32_t getMaxLinearMatchLength()
const = 0;
146#ifndef U_HIDE_INTERNAL_API
149 static const int32_t kMaxBranchLinearSubNodeLength=5;
154 static const int32_t kMaxSplitBranchLevels=14;
166 Node* registerNode(Node* newNode,
UErrorCode& errorCode) U_LIFETIME_BOUND;
177 Node* registerFinalValue(int32_t value,
UErrorCode& errorCode) U_LIFETIME_BOUND;
206 class Node :
public UObject {
208 Node(int32_t initialHash) : hash(initialHash), offset(0) {}
209 inline int32_t hashCode()
const {
return hash; }
211 static inline int32_t hashCode(
const Node *node) {
return node==
nullptr ? 0 : node->hashCode(); }
213 virtual bool operator==(
const Node &other)
const;
242 virtual int32_t markRightEdgesFirst(int32_t edgeNumber);
244 virtual void write(StringTrieBuilder &builder) = 0;
246 inline void writeUnlessInsideRightEdge(int32_t firstRight, int32_t lastRight,
247 StringTrieBuilder &builder) {
253 if(offset<0 && (offset<lastRight || firstRight<offset)) {
257 inline int32_t getOffset()
const {
return offset; }
263#ifndef U_HIDE_INTERNAL_API
271 class FinalValueNode :
public Node {
273 FinalValueNode(int32_t v) : Node(0x111111u*37u+v), value(v) {}
274 virtual bool operator==(
const Node &other)
const override;
275 virtual void write(StringTrieBuilder &builder)
override;
286 class ValueNode :
public Node {
288 ValueNode(int32_t initialHash) : Node(initialHash), hasValue(false), value(0) {}
289 virtual bool operator==(
const Node &other)
const override;
290 void setValue(int32_t v) {
300#ifndef U_HIDE_INTERNAL_API
304 class IntermediateValueNode :
public ValueNode {
306 IntermediateValueNode(int32_t v, Node *nextNode)
307 : ValueNode(0x222222u*37u+hashCode(nextNode)), next(nextNode) { setValue(v); }
308 virtual bool operator==(
const Node &other)
const override;
309 virtual int32_t markRightEdgesFirst(int32_t edgeNumber)
override;
310 virtual void write(StringTrieBuilder &builder)
override;
321 class LinearMatchNode :
public ValueNode {
323 LinearMatchNode(int32_t len, Node *nextNode)
324 : ValueNode((0x333333u*37u+len)*37u+hashCode(nextNode)),
325 length(len), next(nextNode) {}
326 virtual bool operator==(
const Node &other)
const override;
327 virtual int32_t markRightEdgesFirst(int32_t edgeNumber)
override;
333#ifndef U_HIDE_INTERNAL_API
337 class BranchNode :
public Node {
339 BranchNode(int32_t initialHash) : Node(initialHash) {}
341 int32_t firstEdgeNumber;
347 class ListBranchNode :
public BranchNode {
349 ListBranchNode() : BranchNode(0x444444), length(0) {}
350 virtual bool operator==(
const Node &other)
const override;
351 virtual int32_t markRightEdgesFirst(int32_t edgeNumber)
override;
352 virtual void write(StringTrieBuilder &builder)
override;
354 void add(int32_t c, int32_t value) {
355 units[length] =
static_cast<char16_t>(c);
356 equal[length]=
nullptr;
357 values[length]=value;
359 hash=(hash*37u+c)*37u+value;
362 void add(int32_t c, Node *node) {
363 units[length] =
static_cast<char16_t>(c);
367 hash=(hash*37u+c)*37u+hashCode(node);
370 Node *equal[kMaxBranchLinearSubNodeLength];
372 int32_t values[kMaxBranchLinearSubNodeLength];
373 char16_t units[kMaxBranchLinearSubNodeLength];
379 class SplitBranchNode :
public BranchNode {
381 SplitBranchNode(
char16_t middleUnit, Node *lessThanNode, Node *greaterOrEqualNode)
382 : BranchNode(((0x555555u*37u+middleUnit)*37u+
383 hashCode(lessThanNode))*37u+hashCode(greaterOrEqualNode)),
384 unit(middleUnit), lessThan(lessThanNode), greaterOrEqual(greaterOrEqualNode) {}
385 virtual bool operator==(
const Node &other)
const override;
386 virtual int32_t markRightEdgesFirst(int32_t edgeNumber)
override;
387 virtual void write(StringTrieBuilder &builder)
override;
391 Node *greaterOrEqual;
396 class BranchHeadNode :
public ValueNode {
398 BranchHeadNode(int32_t len, Node *subNode)
399 : ValueNode((0x666666u*37u+len)*37u+hashCode(subNode)),
400 length(len), next(subNode) {}
401 virtual bool operator==(
const Node &other)
const override;
402 virtual int32_t markRightEdgesFirst(int32_t edgeNumber)
override;
403 virtual void write(StringTrieBuilder &builder)
override;
413 virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length,
414 Node *nextNode)
const = 0;
417 virtual int32_t write(int32_t unit) = 0;
419 virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length) = 0;
421 virtual int32_t writeValueAndFinal(int32_t i,
UBool isFinal) = 0;
423 virtual int32_t writeValueAndType(
UBool hasValue, int32_t value, int32_t node) = 0;
425 virtual int32_t writeDeltaTo(int32_t jumpTarget) = 0;
struct UHashtable UHashtable
U_COMMON_API UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
UStringTrieBuildOption
Build options for BytesTrieBuilder and CharsTrieBuilder.
@ USTRINGTRIE_BUILD_SMALL
Builds a trie more slowly, attempting to generate a shorter but equivalent serialization.
@ USTRINGTRIE_BUILD_FAST
Builds a trie quickly.
int8_t UBool
The ICU boolean type, a signed-byte integer.
C++ API: Common ICU base class UObject.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.