Warning, file /include/unicode/stringtriebuilder.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef __STRINGTRIEBUILDER_H__
0018 #define __STRINGTRIEBUILDER_H__
0019
0020 #include "unicode/utypes.h"
0021
0022 #if U_SHOW_CPLUSPLUS_API
0023
0024 #include "unicode/uobject.h"
0025
0026
0027
0028
0029
0030
0031
0032
0033 struct UHashtable;
0034 typedef struct UHashtable UHashtable;
0035
0036
0037
0038
0039
0040
0041 enum UStringTrieBuildOption {
0042
0043
0044
0045
0046 USTRINGTRIE_BUILD_FAST,
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 USTRINGTRIE_BUILD_SMALL
0058 };
0059
0060 U_NAMESPACE_BEGIN
0061
0062
0063
0064
0065
0066
0067
0068 class U_COMMON_API StringTrieBuilder : public UObject {
0069 public:
0070 #ifndef U_HIDE_INTERNAL_API
0071
0072 static int32_t hashNode(const void *node);
0073
0074 static UBool equalNodes(const void *left, const void *right);
0075 #endif
0076
0077 protected:
0078
0079
0080
0081 StringTrieBuilder();
0082
0083 virtual ~StringTrieBuilder();
0084
0085 #ifndef U_HIDE_INTERNAL_API
0086
0087 void createCompactBuilder(int32_t sizeGuess, UErrorCode &errorCode);
0088
0089 void deleteCompactBuilder();
0090
0091
0092 void build(UStringTrieBuildOption buildOption, int32_t elementsLength, UErrorCode &errorCode);
0093
0094
0095 int32_t writeNode(int32_t start, int32_t limit, int32_t unitIndex);
0096
0097 int32_t writeBranchSubNode(int32_t start, int32_t limit, int32_t unitIndex, int32_t length);
0098 #endif
0099
0100 class Node;
0101
0102 #ifndef U_HIDE_INTERNAL_API
0103
0104 Node *makeNode(int32_t start, int32_t limit, int32_t unitIndex, UErrorCode &errorCode);
0105
0106 Node *makeBranchSubNode(int32_t start, int32_t limit, int32_t unitIndex,
0107 int32_t length, UErrorCode &errorCode);
0108 #endif
0109
0110
0111 virtual int32_t getElementStringLength(int32_t i) const = 0;
0112
0113 virtual char16_t getElementUnit(int32_t i, int32_t unitIndex) const = 0;
0114
0115 virtual int32_t getElementValue(int32_t i) const = 0;
0116
0117
0118
0119
0120 virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const = 0;
0121
0122
0123
0124 virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const = 0;
0125
0126 virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const = 0;
0127
0128 virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, char16_t unit) const = 0;
0129
0130
0131 virtual UBool matchNodesCanHaveValues() const = 0;
0132
0133
0134 virtual int32_t getMaxBranchLinearSubNodeLength() const = 0;
0135
0136 virtual int32_t getMinLinearMatch() const = 0;
0137
0138 virtual int32_t getMaxLinearMatchLength() const = 0;
0139
0140 #ifndef U_HIDE_INTERNAL_API
0141
0142
0143 static const int32_t kMaxBranchLinearSubNodeLength=5;
0144
0145
0146
0147
0148 static const int32_t kMaxSplitBranchLevels=14;
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160 Node *registerNode(Node *newNode, UErrorCode &errorCode);
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171 Node *registerFinalValue(int32_t value, UErrorCode &errorCode);
0172 #endif
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192 UHashtable *nodes;
0193
0194
0195
0196
0197
0198
0199
0200 class Node : public UObject {
0201 public:
0202 Node(int32_t initialHash) : hash(initialHash), offset(0) {}
0203 inline int32_t hashCode() const { return hash; }
0204
0205 static inline int32_t hashCode(const Node *node) { return node==nullptr ? 0 : node->hashCode(); }
0206
0207 virtual bool operator==(const Node &other) const;
0208 inline bool operator!=(const Node &other) const { return !operator==(other); }
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236 virtual int32_t markRightEdgesFirst(int32_t edgeNumber);
0237
0238 virtual void write(StringTrieBuilder &builder) = 0;
0239
0240 inline void writeUnlessInsideRightEdge(int32_t firstRight, int32_t lastRight,
0241 StringTrieBuilder &builder) {
0242
0243
0244
0245
0246
0247 if(offset<0 && (offset<lastRight || firstRight<offset)) {
0248 write(builder);
0249 }
0250 }
0251 inline int32_t getOffset() const { return offset; }
0252 protected:
0253 int32_t hash;
0254 int32_t offset;
0255 };
0256
0257 #ifndef U_HIDE_INTERNAL_API
0258
0259
0260
0261
0262
0263
0264
0265 class FinalValueNode : public Node {
0266 public:
0267 FinalValueNode(int32_t v) : Node(0x111111u*37u+v), value(v) {}
0268 virtual bool operator==(const Node &other) const override;
0269 virtual void write(StringTrieBuilder &builder) override;
0270 protected:
0271 int32_t value;
0272 };
0273 #endif
0274
0275
0276
0277
0278
0279
0280 class ValueNode : public Node {
0281 public:
0282 ValueNode(int32_t initialHash) : Node(initialHash), hasValue(false), value(0) {}
0283 virtual bool operator==(const Node &other) const override;
0284 void setValue(int32_t v) {
0285 hasValue=true;
0286 value=v;
0287 hash=hash*37u+v;
0288 }
0289 protected:
0290 UBool hasValue;
0291 int32_t value;
0292 };
0293
0294 #ifndef U_HIDE_INTERNAL_API
0295
0296
0297
0298 class IntermediateValueNode : public ValueNode {
0299 public:
0300 IntermediateValueNode(int32_t v, Node *nextNode)
0301 : ValueNode(0x222222u*37u+hashCode(nextNode)), next(nextNode) { setValue(v); }
0302 virtual bool operator==(const Node &other) const override;
0303 virtual int32_t markRightEdgesFirst(int32_t edgeNumber) override;
0304 virtual void write(StringTrieBuilder &builder) override;
0305 protected:
0306 Node *next;
0307 };
0308 #endif
0309
0310
0311
0312
0313
0314
0315 class LinearMatchNode : public ValueNode {
0316 public:
0317 LinearMatchNode(int32_t len, Node *nextNode)
0318 : ValueNode((0x333333u*37u+len)*37u+hashCode(nextNode)),
0319 length(len), next(nextNode) {}
0320 virtual bool operator==(const Node &other) const override;
0321 virtual int32_t markRightEdgesFirst(int32_t edgeNumber) override;
0322 protected:
0323 int32_t length;
0324 Node *next;
0325 };
0326
0327 #ifndef U_HIDE_INTERNAL_API
0328
0329
0330
0331 class BranchNode : public Node {
0332 public:
0333 BranchNode(int32_t initialHash) : Node(initialHash) {}
0334 protected:
0335 int32_t firstEdgeNumber;
0336 };
0337
0338
0339
0340
0341 class ListBranchNode : public BranchNode {
0342 public:
0343 ListBranchNode() : BranchNode(0x444444), length(0) {}
0344 virtual bool operator==(const Node &other) const override;
0345 virtual int32_t markRightEdgesFirst(int32_t edgeNumber) override;
0346 virtual void write(StringTrieBuilder &builder) override;
0347
0348 void add(int32_t c, int32_t value) {
0349 units[length]=(char16_t)c;
0350 equal[length]=nullptr;
0351 values[length]=value;
0352 ++length;
0353 hash=(hash*37u+c)*37u+value;
0354 }
0355
0356 void add(int32_t c, Node *node) {
0357 units[length]=(char16_t)c;
0358 equal[length]=node;
0359 values[length]=0;
0360 ++length;
0361 hash=(hash*37u+c)*37u+hashCode(node);
0362 }
0363 protected:
0364 Node *equal[kMaxBranchLinearSubNodeLength];
0365 int32_t length;
0366 int32_t values[kMaxBranchLinearSubNodeLength];
0367 char16_t units[kMaxBranchLinearSubNodeLength];
0368 };
0369
0370
0371
0372
0373 class SplitBranchNode : public BranchNode {
0374 public:
0375 SplitBranchNode(char16_t middleUnit, Node *lessThanNode, Node *greaterOrEqualNode)
0376 : BranchNode(((0x555555u*37u+middleUnit)*37u+
0377 hashCode(lessThanNode))*37u+hashCode(greaterOrEqualNode)),
0378 unit(middleUnit), lessThan(lessThanNode), greaterOrEqual(greaterOrEqualNode) {}
0379 virtual bool operator==(const Node &other) const override;
0380 virtual int32_t markRightEdgesFirst(int32_t edgeNumber) override;
0381 virtual void write(StringTrieBuilder &builder) override;
0382 protected:
0383 char16_t unit;
0384 Node *lessThan;
0385 Node *greaterOrEqual;
0386 };
0387
0388
0389
0390 class BranchHeadNode : public ValueNode {
0391 public:
0392 BranchHeadNode(int32_t len, Node *subNode)
0393 : ValueNode((0x666666u*37u+len)*37u+hashCode(subNode)),
0394 length(len), next(subNode) {}
0395 virtual bool operator==(const Node &other) const override;
0396 virtual int32_t markRightEdgesFirst(int32_t edgeNumber) override;
0397 virtual void write(StringTrieBuilder &builder) override;
0398 protected:
0399 int32_t length;
0400 Node *next;
0401 };
0402
0403 #endif
0404
0405
0406
0407 virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length,
0408 Node *nextNode) const = 0;
0409
0410
0411 virtual int32_t write(int32_t unit) = 0;
0412
0413 virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length) = 0;
0414
0415 virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal) = 0;
0416
0417 virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node) = 0;
0418
0419 virtual int32_t writeDeltaTo(int32_t jumpTarget) = 0;
0420 };
0421
0422 U_NAMESPACE_END
0423
0424 #endif
0425
0426 #endif