ICU4C 79
Loading...
Searching...
No Matches
stringtriebuilder.h
Go to the documentation of this file.
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4*******************************************************************************
5* Copyright (C) 2010-2012,2014, International Business Machines
6* Corporation and others. All Rights Reserved.
7*******************************************************************************
8* file name: stringtriebuilder.h
9* encoding: UTF-8
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 2010dec24
14* created by: Markus W. Scherer
15*/
16
17#ifndef __STRINGTRIEBUILDER_H__
18#define __STRINGTRIEBUILDER_H__
19
20#include "unicode/utypes.h"
21
22#if U_SHOW_CPLUSPLUS_API
23
24#include "unicode/uobject.h"
25
30
31// Forward declaration.
33struct UHashtable;
34typedef struct UHashtable UHashtable;
36
59
60U_NAMESPACE_BEGIN
61
68class U_COMMON_API StringTrieBuilder : public UObject {
69public:
70#ifndef U_HIDE_INTERNAL_API
72 static int32_t hashNode(const void *node);
74 static UBool equalNodes(const void *left, const void *right);
75#endif /* U_HIDE_INTERNAL_API */
76
77protected:
78 // Do not enclose the protected default constructor with #ifndef U_HIDE_INTERNAL_API
79 // or else the compiler will create a public default constructor.
81 StringTrieBuilder();
83 virtual ~StringTrieBuilder();
84
85#ifndef U_HIDE_INTERNAL_API
87 void createCompactBuilder(int32_t sizeGuess, UErrorCode &errorCode);
89 void deleteCompactBuilder();
90
92 void build(UStringTrieBuildOption buildOption, int32_t elementsLength, UErrorCode &errorCode);
93
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);
98#endif /* U_HIDE_INTERNAL_API */
99
100 class Node;
101
102#ifndef U_HIDE_INTERNAL_API
104 Node* makeNode(int32_t start,
105 int32_t limit,
106 int32_t unitIndex,
107 UErrorCode& errorCode) U_LIFETIME_BOUND;
109 Node* makeBranchSubNode(int32_t start,
110 int32_t limit,
111 int32_t unitIndex,
112 int32_t length,
113 UErrorCode& errorCode) U_LIFETIME_BOUND;
114#endif /* U_HIDE_INTERNAL_API */
115
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;
122
123 // Finds the first unit index after this one where
124 // the first and last element have different units again.
126 virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const = 0;
127
128 // Number of different units at unitIndex.
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;
135
137 virtual UBool matchNodesCanHaveValues() const = 0;
138
140 virtual int32_t getMaxBranchLinearSubNodeLength() const = 0;
142 virtual int32_t getMinLinearMatch() const = 0;
144 virtual int32_t getMaxLinearMatchLength() const = 0;
145
146#ifndef U_HIDE_INTERNAL_API
147 // max(BytesTrie::kMaxBranchLinearSubNodeLength, UCharsTrie::kMaxBranchLinearSubNodeLength).
149 static const int32_t kMaxBranchLinearSubNodeLength=5;
150
151 // Maximum number of nested split-branch levels for a branch on all 2^16 possible char16_t units.
152 // log2(2^16/kMaxBranchLinearSubNodeLength) rounded up.
154 static const int32_t kMaxSplitBranchLevels=14;
155
166 Node* registerNode(Node* newNode, UErrorCode& errorCode) U_LIFETIME_BOUND;
177 Node* registerFinalValue(int32_t value, UErrorCode& errorCode) U_LIFETIME_BOUND;
178#endif /* U_HIDE_INTERNAL_API */
179
180 /*
181 * C++ note:
182 * registerNode() and registerFinalValue() take ownership of their input nodes,
183 * and only return owned nodes.
184 * If they see a failure UErrorCode, they will delete the input node.
185 * If they get a nullptr pointer, they will record a U_MEMORY_ALLOCATION_ERROR.
186 * If there is a failure, they return nullptr.
187 *
188 * nullptr Node pointers can be safely passed into other Nodes because
189 * they call the static Node::hashCode() which checks for a nullptr pointer first.
190 *
191 * Therefore, as long as builder functions register a new node,
192 * they need to check for failures only before explicitly dereferencing
193 * a Node pointer, or before setting a new UErrorCode.
194 */
195
196 // Hash set of nodes, maps from nodes to integer 1.
198 UHashtable *nodes;
199
200 // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API,
201 // it is needed for layout of other objects.
206 class Node : public UObject {
207 public:
208 Node(int32_t initialHash) : hash(initialHash), offset(0) {}
209 inline int32_t hashCode() const { return hash; }
210 // Handles node==nullptr.
211 static inline int32_t hashCode(const Node *node) { return node==nullptr ? 0 : node->hashCode(); }
212 // Base class operator==() compares the actual class types.
213 virtual bool operator==(const Node &other) const;
214 inline bool operator!=(const Node &other) const { return !operator==(other); }
242 virtual int32_t markRightEdgesFirst(int32_t edgeNumber);
243 // write() must set the offset to a positive value.
244 virtual void write(StringTrieBuilder &builder) = 0;
245 // See markRightEdgesFirst.
246 inline void writeUnlessInsideRightEdge(int32_t firstRight, int32_t lastRight,
247 StringTrieBuilder &builder) {
248 // Note: Edge numbers are negative, lastRight<=firstRight.
249 // If offset>0 then this node and its sub-nodes have been written already
250 // and we need not write them again.
251 // If this node is part of the unwritten right branch edge,
252 // then we wait until that is written.
253 if(offset<0 && (offset<lastRight || firstRight<offset)) {
254 write(builder);
255 }
256 }
257 inline int32_t getOffset() const { return offset; }
258 protected:
259 int32_t hash;
260 int32_t offset;
261 };
262
263#ifndef U_HIDE_INTERNAL_API
264 // This class should not be overridden because
265 // registerFinalValue() compares a stack-allocated FinalValueNode
266 // (stack-allocated so that we don't unnecessarily create lots of duplicate nodes)
267 // with the input node, and the
268 // !Node::operator==(other) used inside FinalValueNode::operator==(other)
269 // will be false if the typeid's are different.
271 class FinalValueNode : public Node {
272 public:
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;
276 protected:
277 int32_t value;
278 };
279#endif /* U_HIDE_INTERNAL_API */
280
281 // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API,
282 // it is needed for layout of other objects.
286 class ValueNode : public Node {
287 public:
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) {
291 hasValue=true;
292 value=v;
293 hash=hash*37u+v;
294 }
295 protected:
296 UBool hasValue;
297 int32_t value;
298 };
299
300#ifndef U_HIDE_INTERNAL_API
304 class IntermediateValueNode : public ValueNode {
305 public:
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;
311 protected:
312 Node *next;
313 };
314#endif /* U_HIDE_INTERNAL_API */
315
316 // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API,
317 // it is needed for layout of other objects.
321 class LinearMatchNode : public ValueNode {
322 public:
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;
328 protected:
329 int32_t length;
330 Node *next;
331 };
332
333#ifndef U_HIDE_INTERNAL_API
337 class BranchNode : public Node {
338 public:
339 BranchNode(int32_t initialHash) : Node(initialHash) {}
340 protected:
341 int32_t firstEdgeNumber;
342 };
343
347 class ListBranchNode : public BranchNode {
348 public:
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;
353 // Adds a unit with a final value.
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;
358 ++length;
359 hash=(hash*37u+c)*37u+value;
360 }
361 // Adds a unit which leads to another match node.
362 void add(int32_t c, Node *node) {
363 units[length] = static_cast<char16_t>(c);
364 equal[length]=node;
365 values[length]=0;
366 ++length;
367 hash=(hash*37u+c)*37u+hashCode(node);
368 }
369 protected:
370 Node *equal[kMaxBranchLinearSubNodeLength]; // nullptr means "has final value".
371 int32_t length;
372 int32_t values[kMaxBranchLinearSubNodeLength];
373 char16_t units[kMaxBranchLinearSubNodeLength];
374 };
375
379 class SplitBranchNode : public BranchNode {
380 public:
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;
388 protected:
389 char16_t unit;
390 Node *lessThan;
391 Node *greaterOrEqual;
392 };
393
394 // Branch head node, for writing the actual node lead unit.
396 class BranchHeadNode : public ValueNode {
397 public:
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;
404 protected:
405 int32_t length;
406 Node *next; // A branch sub-node.
407 };
408
409#endif /* U_HIDE_INTERNAL_API */
411
413 virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length,
414 Node *nextNode) const = 0;
415
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;
426};
427
428U_NAMESPACE_END
429
430#endif /* U_SHOW_CPLUSPLUS_API */
431
432#endif // __STRINGTRIEBUILDER_H__
struct UHashtable UHashtable
Definition msgfmt.h:43
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.
Definition umachine.h:269
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.
Definition utypes.h:509
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition utypes.h:315