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, int32_t limit, int32_t unitIndex,
UErrorCode &errorCode);
106 Node *makeBranchSubNode(int32_t start, int32_t limit, int32_t unitIndex,
111 virtual int32_t getElementStringLength(int32_t i)
const = 0;
113 virtual char16_t getElementUnit(int32_t i, int32_t unitIndex)
const = 0;
115 virtual int32_t getElementValue(int32_t i)
const = 0;
120 virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex)
const = 0;
124 virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex)
const = 0;
126 virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count)
const = 0;
128 virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex,
char16_t unit)
const = 0;
131 virtual UBool matchNodesCanHaveValues()
const = 0;
134 virtual int32_t getMaxBranchLinearSubNodeLength()
const = 0;
136 virtual int32_t getMinLinearMatch()
const = 0;
138 virtual int32_t getMaxLinearMatchLength()
const = 0;
140#ifndef U_HIDE_INTERNAL_API
143 static const int32_t kMaxBranchLinearSubNodeLength=5;
148 static const int32_t kMaxSplitBranchLevels=14;
160 Node *registerNode(Node *newNode,
UErrorCode &errorCode);
171 Node *registerFinalValue(int32_t value,
UErrorCode &errorCode);
200 class Node :
public UObject {
202 Node(int32_t initialHash) : hash(initialHash), offset(0) {}
203 inline int32_t hashCode()
const {
return hash; }
205 static inline int32_t hashCode(
const Node *node) {
return node==
nullptr ? 0 : node->hashCode(); }
207 virtual bool operator==(
const Node &other)
const;
236 virtual int32_t markRightEdgesFirst(int32_t edgeNumber);
238 virtual void write(StringTrieBuilder &builder) = 0;
240 inline void writeUnlessInsideRightEdge(int32_t firstRight, int32_t lastRight,
241 StringTrieBuilder &builder) {
247 if(offset<0 && (offset<lastRight || firstRight<offset)) {
251 inline int32_t getOffset()
const {
return offset; }
257#ifndef U_HIDE_INTERNAL_API
265 class FinalValueNode :
public Node {
267 FinalValueNode(int32_t v) : Node(0x111111u*37u+v), value(v) {}
268 virtual bool operator==(
const Node &other)
const override;
269 virtual void write(StringTrieBuilder &builder)
override;
280 class ValueNode :
public Node {
282 ValueNode(int32_t initialHash) : Node(initialHash), hasValue(false), value(0) {}
283 virtual bool operator==(
const Node &other)
const override;
284 void setValue(int32_t v) {
294#ifndef U_HIDE_INTERNAL_API
298 class IntermediateValueNode :
public ValueNode {
300 IntermediateValueNode(int32_t v, Node *nextNode)
301 : ValueNode(0x222222u*37u+hashCode(nextNode)), next(nextNode) { setValue(v); }
302 virtual bool operator==(
const Node &other)
const override;
303 virtual int32_t markRightEdgesFirst(int32_t edgeNumber)
override;
304 virtual void write(StringTrieBuilder &builder)
override;
315 class LinearMatchNode :
public ValueNode {
317 LinearMatchNode(int32_t len, Node *nextNode)
318 : ValueNode((0x333333u*37u+len)*37u+hashCode(nextNode)),
319 length(len), next(nextNode) {}
320 virtual bool operator==(
const Node &other)
const override;
321 virtual int32_t markRightEdgesFirst(int32_t edgeNumber)
override;
327#ifndef U_HIDE_INTERNAL_API
331 class BranchNode :
public Node {
333 BranchNode(int32_t initialHash) : Node(initialHash) {}
335 int32_t firstEdgeNumber;
341 class ListBranchNode :
public BranchNode {
343 ListBranchNode() : BranchNode(0x444444), length(0) {}
344 virtual bool operator==(
const Node &other)
const override;
345 virtual int32_t markRightEdgesFirst(int32_t edgeNumber)
override;
346 virtual void write(StringTrieBuilder &builder)
override;
348 void add(int32_t c, int32_t value) {
349 units[length] =
static_cast<char16_t>(c);
350 equal[length]=
nullptr;
351 values[length]=value;
353 hash=(hash*37u+c)*37u+value;
356 void add(int32_t c, Node *node) {
357 units[length] =
static_cast<char16_t>(c);
361 hash=(hash*37u+c)*37u+hashCode(node);
364 Node *equal[kMaxBranchLinearSubNodeLength];
366 int32_t values[kMaxBranchLinearSubNodeLength];
367 char16_t units[kMaxBranchLinearSubNodeLength];
373 class SplitBranchNode :
public BranchNode {
375 SplitBranchNode(
char16_t middleUnit, Node *lessThanNode, Node *greaterOrEqualNode)
376 : BranchNode(((0x555555u*37u+middleUnit)*37u+
377 hashCode(lessThanNode))*37u+hashCode(greaterOrEqualNode)),
378 unit(middleUnit), lessThan(lessThanNode), greaterOrEqual(greaterOrEqualNode) {}
379 virtual bool operator==(
const Node &other)
const override;
380 virtual int32_t markRightEdgesFirst(int32_t edgeNumber)
override;
381 virtual void write(StringTrieBuilder &builder)
override;
385 Node *greaterOrEqual;
390 class BranchHeadNode :
public ValueNode {
392 BranchHeadNode(int32_t len, Node *subNode)
393 : ValueNode((0x666666u*37u+len)*37u+hashCode(subNode)),
394 length(len), next(subNode) {}
395 virtual bool operator==(
const Node &other)
const override;
396 virtual int32_t markRightEdgesFirst(int32_t edgeNumber)
override;
397 virtual void write(StringTrieBuilder &builder)
override;
407 virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length,
408 Node *nextNode)
const = 0;
411 virtual int32_t write(int32_t unit) = 0;
413 virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length) = 0;
415 virtual int32_t writeValueAndFinal(int32_t i,
UBool isFinal) = 0;
417 virtual int32_t writeValueAndType(
UBool hasValue, int32_t value, int32_t node) = 0;
419 virtual int32_t writeDeltaTo(int32_t jumpTarget) = 0;
struct UHashtable UHashtable
U_EXPORT 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.