| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
struct UHashtable; |
| 34 |
typedef struct UHashtable UHashtable; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
enum UStringTrieBuildOption { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
USTRINGTRIE_BUILD_FAST, |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
USTRINGTRIE_BUILD_SMALL |
| 58 |
}; |
| 59 |
|
| 60 |
U_NAMESPACE_BEGIN |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
class U_COMMON_API StringTrieBuilder : public UObject { |
| 69 |
public: |
| 70 |
#ifndef U_HIDE_INTERNAL_API |
| 71 |
|
| 72 |
static int32_t hashNode(const void *node); |
| 73 |
|
| 74 |
static UBool equalNodes(const void *left, const void *right); |
| 75 |
#endif |
| 76 |
|
| 77 |
protected: |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
StringTrieBuilder(); |
| 82 |
|
| 83 |
virtual ~StringTrieBuilder(); |
| 84 |
|
| 85 |
#ifndef U_HIDE_INTERNAL_API |
| 86 |
|
| 87 |
void createCompactBuilder(int32_t sizeGuess, UErrorCode &errorCode); |
| 88 |
|
| 89 |
void deleteCompactBuilder(); |
| 90 |
|
| 91 |
|
| 92 |
void build(UStringTrieBuildOption buildOption, int32_t elementsLength, UErrorCode &errorCode); |
| 93 |
|
| 94 |
|
| 95 |
int32_t writeNode(int32_t start, int32_t limit, int32_t unitIndex); |
| 96 |
|
| 97 |
int32_t writeBranchSubNode(int32_t start, int32_t limit, int32_t unitIndex, int32_t length); |
| 98 |
#endif |
| 99 |
|
| 100 |
class Node; |
| 101 |
|
| 102 |
#ifndef U_HIDE_INTERNAL_API |
| 103 |
|
| 104 |
Node *makeNode(int32_t start, int32_t limit, int32_t unitIndex, UErrorCode &errorCode); |
| 105 |
|
| 106 |
Node *makeBranchSubNode(int32_t start, int32_t limit, int32_t unitIndex, |
| 107 |
int32_t length, UErrorCode &errorCode); |
| 108 |
#endif |
| 109 |
|
| 110 |
|
| 111 |
virtual int32_t getElementStringLength(int32_t i) const = 0; |
| 112 |
|
| 113 |
virtual char16_t getElementUnit(int32_t i, int32_t unitIndex) const = 0; |
| 114 |
|
| 115 |
virtual int32_t getElementValue(int32_t i) const = 0; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const = 0; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const = 0; |
| 125 |
|
| 126 |
virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const = 0; |
| 127 |
|
| 128 |
virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, char16_t unit) const = 0; |
| 129 |
|
| 130 |
|
| 131 |
virtual UBool matchNodesCanHaveValues() const = 0; |
| 132 |
|
| 133 |
|
| 134 |
virtual int32_t getMaxBranchLinearSubNodeLength() const = 0; |
| 135 |
|
| 136 |
virtual int32_t getMinLinearMatch() const = 0; |
| 137 |
|
| 138 |
virtual int32_t getMaxLinearMatchLength() const = 0; |
| 139 |
|
| 140 |
#ifndef U_HIDE_INTERNAL_API |
| 141 |
|
| 142 |
|
| 143 |
static const int32_t kMaxBranchLinearSubNodeLength=5; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
static const int32_t kMaxSplitBranchLevels=14; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
Node *registerNode(Node *newNode, UErrorCode &errorCode); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
Node *registerFinalValue(int32_t value, UErrorCode &errorCode); |
| 172 |
#endif |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
UHashtable *nodes; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
class Node : public UObject { |
| 201 |
public: |
| 202 |
Node(int32_t initialHash) : hash(initialHash), offset(0) {} |
| 203 |
inline int32_t hashCode() const { return hash; } |
| 204 |
|
| 205 |
static inline int32_t hashCode(const Node *node) { return node==nullptr ? 0 : node->hashCode(); } |
| 206 |
|
| 207 |
virtual bool operator==(const Node &other) const; |
| 208 |
inline bool operator!=(const Node &other) const { return !operator==(other); } |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
virtual int32_t markRightEdgesFirst(int32_t edgeNumber); |
| 237 |
|
| 238 |
virtual void write(StringTrieBuilder &builder) = 0; |
| 239 |
|
| 240 |
inline void writeUnlessInsideRightEdge(int32_t firstRight, int32_t lastRight, |
| 241 |
StringTrieBuilder &builder) { |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if(offset<0 && (offset<lastRight || firstRight<offset)) { |
| 248 |
write(builder); |
| 249 |
} |
| 250 |
} |
| 251 |
inline int32_t getOffset() const { return offset; } |
| 252 |
protected: |
| 253 |
int32_t hash; |
| 254 |
int32_t offset; |
| 255 |
}; |
| 256 |
|
| 257 |
#ifndef U_HIDE_INTERNAL_API |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
class FinalValueNode : public Node { |
| 266 |
public: |
| 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; |
| 270 |
protected: |
| 271 |
int32_t value; |
| 272 |
}; |
| 273 |
#endif |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
class ValueNode : public Node { |
| 281 |
public: |
| 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) { |
| 285 |
hasValue=true; |
| 286 |
value=v; |
| 287 |
hash=hash*37u+v; |
| 288 |
} |
| 289 |
protected: |
| 290 |
UBool hasValue; |
| 291 |
int32_t value; |
| 292 |
}; |
| 293 |
|
| 294 |
#ifndef U_HIDE_INTERNAL_API |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
class IntermediateValueNode : public ValueNode { |
| 299 |
public: |
| 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; |
| 305 |
protected: |
| 306 |
Node *next; |
| 307 |
}; |
| 308 |
#endif |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
class LinearMatchNode : public ValueNode { |
| 316 |
public: |
| 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; |
| 322 |
protected: |
| 323 |
int32_t length; |
| 324 |
Node *next; |
| 325 |
}; |
| 326 |
|
| 327 |
#ifndef U_HIDE_INTERNAL_API |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
class BranchNode : public Node { |
| 332 |
public: |
| 333 |
BranchNode(int32_t initialHash) : Node(initialHash) {} |
| 334 |
protected: |
| 335 |
int32_t firstEdgeNumber; |
| 336 |
}; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
class ListBranchNode : public BranchNode { |
| 342 |
public: |
| 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; |
| 347 |
|
| 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; |
| 352 |
++length; |
| 353 |
hash=(hash*37u+c)*37u+value; |
| 354 |
} |
| 355 |
|
| 356 |
void add(int32_t c, Node *node) { |
| 357 |
units[length] = static_cast<char16_t>(c); |
| 358 |
equal[length]=node; |
| 359 |
values[length]=0; |
| 360 |
++length; |
| 361 |
hash=(hash*37u+c)*37u+hashCode(node); |
| 362 |
} |
| 363 |
protected: |
| 364 |
Node *equal[kMaxBranchLinearSubNodeLength]; |
| 365 |
int32_t length; |
| 366 |
int32_t values[kMaxBranchLinearSubNodeLength]; |
| 367 |
char16_t units[kMaxBranchLinearSubNodeLength]; |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
class SplitBranchNode : public BranchNode { |
| 374 |
public: |
| 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; |
| 382 |
protected: |
| 383 |
char16_t unit; |
| 384 |
Node *lessThan; |
| 385 |
Node *greaterOrEqual; |
| 386 |
}; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
class BranchHeadNode : public ValueNode { |
| 391 |
public: |
| 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; |
| 398 |
protected: |
| 399 |
int32_t length; |
| 400 |
Node *next; |
| 401 |
}; |
| 402 |
|
| 403 |
#endif |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length, |
| 408 |
Node *nextNode) const = 0; |
| 409 |
|
| 410 |
|
| 411 |
virtual int32_t write(int32_t unit) = 0; |
| 412 |
|
| 413 |
virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length) = 0; |
| 414 |
|
| 415 |
virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal) = 0; |
| 416 |
|
| 417 |
virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node) = 0; |
| 418 |
|
| 419 |
virtual int32_t writeDeltaTo(int32_t jumpTarget) = 0; |
| 420 |
}; |
| 421 |
|
| 422 |
U_NAMESPACE_END |
| 423 |
|
| 424 |
#endif |
| 425 |
|
| 426 |
#endif |