ICU4C 79
Loading...
Searching...
No Matches
ucharstrie.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, International Business Machines
6* Corporation and others. All Rights Reserved.
7*******************************************************************************
8* file name: ucharstrie.h
9* encoding: UTF-8
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 2010nov14
14* created by: Markus W. Scherer
15*/
16
17#ifndef __UCHARSTRIE_H__
18#define __UCHARSTRIE_H__
19
25
26#include "unicode/utypes.h"
27
28#if U_SHOW_CPLUSPLUS_API
29
30#include "unicode/unistr.h"
31#include "unicode/uobject.h"
32#include "unicode/ustringtrie.h"
33
34U_NAMESPACE_BEGIN
35
36class Appendable;
37class UCharsTrieBuilder;
38class UVector32;
39
54public:
69 UCharsTrie(ConstChar16Ptr trieUChars U_LIFETIME_BOUND)
70 : ownedArray_(nullptr), uchars_(trieUChars),
71 pos_(uchars_), remainingMatchLength_(-1) {}
72
78
85 UCharsTrie(const UCharsTrie &other)
86 : ownedArray_(nullptr), uchars_(other.uchars_),
87 pos_(other.pos_), remainingMatchLength_(other.remainingMatchLength_) {}
88
94 UCharsTrie& reset() U_LIFETIME_BOUND {
95 pos_=uchars_;
96 remainingMatchLength_=-1;
97 return *this;
98 }
99
108 uint64_t getState64() const {
109 return (static_cast<uint64_t>(remainingMatchLength_ + 2) << kState64RemainingShift) |
110 static_cast<uint64_t>(pos_ - uchars_);
111 }
112
127 UCharsTrie& resetToState64(uint64_t state) U_LIFETIME_BOUND {
128 remainingMatchLength_ = static_cast<int32_t>(state >> kState64RemainingShift) - 2;
129 pos_ = uchars_ + (state & kState64PosMask);
130 return *this;
131 }
132
138 class State : public UMemory {
139 public:
144 State() { uchars=nullptr; }
145 private:
146 friend class UCharsTrie;
147
148 const char16_t *uchars;
149 const char16_t *pos;
150 int32_t remainingMatchLength;
151 };
152
160 const UCharsTrie& saveState(State& state) const U_LIFETIME_BOUND {
161 state.uchars=uchars_;
162 state.pos=pos_;
163 state.remainingMatchLength=remainingMatchLength_;
164 return *this;
165 }
166
177 UCharsTrie& resetToState(const State& state) U_LIFETIME_BOUND {
178 if(uchars_==state.uchars && uchars_!=nullptr) {
179 pos_=state.pos;
180 remainingMatchLength_=state.remainingMatchLength;
181 }
182 return *this;
183 }
184
192
200 inline UStringTrieResult first(int32_t uchar) {
201 remainingMatchLength_=-1;
202 return nextImpl(uchars_, uchar);
203 }
204
214
221 UStringTrieResult next(int32_t uchar);
222
231
248
258 inline int32_t getValue() const {
259 const char16_t *pos=pos_;
260 int32_t leadUnit=*pos++;
261 // U_ASSERT(leadUnit>=kMinValueLead);
262 return leadUnit&kValueIsFinal ?
263 readValue(pos, leadUnit&0x7fff) : readNodeValue(pos, leadUnit);
264 }
265
275 inline UBool hasUniqueValue(int32_t &uniqueValue) const {
276 const char16_t *pos=pos_;
277 // Skip the rest of a pending linear-match node.
278 return pos!=nullptr && findUniqueValue(pos+remainingMatchLength_+1, false, uniqueValue);
279 }
280
288 int32_t getNextUChars(Appendable &out) const;
289
295 public:
307 Iterator(ConstChar16Ptr trieUChars U_LIFETIME_BOUND, int32_t maxStringLength, UErrorCode& errorCode);
308
320 Iterator(const UCharsTrie &trie, int32_t maxStringLength, UErrorCode &errorCode);
321
327
333 Iterator& reset() U_LIFETIME_BOUND;
334
339 UBool hasNext() const;
340
355 UBool next(UErrorCode &errorCode);
356
361 const UnicodeString& getString() const U_LIFETIME_BOUND { return str_; }
366 int32_t getValue() const { return value_; }
367
368 private:
369 UBool truncateAndStop() {
370 pos_=nullptr;
371 value_=-1; // no real value for str
372 return true;
373 }
374
375 const char16_t* branchNext(const char16_t* pos U_LIFETIME_BOUND,
376 int32_t length,
377 UErrorCode& errorCode);
378
379 const char16_t *uchars_;
380 const char16_t *pos_;
381 const char16_t *initialPos_;
382 int32_t remainingMatchLength_;
383 int32_t initialRemainingMatchLength_;
384 UBool skipValue_; // Skip intermediate value which was already delivered.
385
386 UnicodeString str_;
387 int32_t maxLength_;
388 int32_t value_;
389
390 // The stack stores pairs of integers for backtracking to another
391 // outbound edge of a branch node.
392 // The first integer is an offset from uchars_.
393 // The second integer has the str_.length() from before the node in bits 15..0,
394 // and the remaining branch length in bits 31..16.
395 // (We could store the remaining branch length minus 1 in bits 30..16 and not use the sign bit,
396 // but the code looks more confusing that way.)
397 UVector32 *stack_;
398 };
399
400private:
401 friend class UCharsTrieBuilder;
402
409 UCharsTrie(char16_t *adoptUChars, const char16_t *trieUChars)
410 : ownedArray_(adoptUChars), uchars_(trieUChars),
411 pos_(uchars_), remainingMatchLength_(-1) {}
412
413 // No assignment operator.
414 UCharsTrie &operator=(const UCharsTrie &other) = delete;
415
416 inline void stop() {
417 pos_=nullptr;
418 }
419
420 // Reads a compact 32-bit integer.
421 // pos is already after the leadUnit, and the lead unit has bit 15 reset.
422 static inline int32_t readValue(const char16_t *pos, int32_t leadUnit) {
423 int32_t value;
424 if(leadUnit<kMinTwoUnitValueLead) {
425 value=leadUnit;
426 } else if(leadUnit<kThreeUnitValueLead) {
427 value=((leadUnit-kMinTwoUnitValueLead)<<16)|*pos;
428 } else {
429 value=(pos[0]<<16)|pos[1];
430 }
431 return value;
432 }
433 static inline const char16_t* skipValue(const char16_t* pos U_LIFETIME_BOUND,
434 int32_t leadUnit) {
435 if(leadUnit>=kMinTwoUnitValueLead) {
436 if(leadUnit<kThreeUnitValueLead) {
437 ++pos;
438 } else {
439 pos+=2;
440 }
441 }
442 return pos;
443 }
444 static inline const char16_t* skipValue(const char16_t* pos U_LIFETIME_BOUND) {
445 int32_t leadUnit=*pos++;
446 return skipValue(pos, leadUnit&0x7fff);
447 }
448
449 static inline int32_t readNodeValue(const char16_t *pos, int32_t leadUnit) {
450 // U_ASSERT(kMinValueLead<=leadUnit && leadUnit<kValueIsFinal);
451 int32_t value;
452 if(leadUnit<kMinTwoUnitNodeValueLead) {
453 value=(leadUnit>>6)-1;
454 } else if(leadUnit<kThreeUnitNodeValueLead) {
455 value=(((leadUnit&0x7fc0)-kMinTwoUnitNodeValueLead)<<10)|*pos;
456 } else {
457 value=(pos[0]<<16)|pos[1];
458 }
459 return value;
460 }
461 static inline const char16_t* skipNodeValue(const char16_t* pos U_LIFETIME_BOUND,
462 int32_t leadUnit) {
463 // U_ASSERT(kMinValueLead<=leadUnit && leadUnit<kValueIsFinal);
464 if(leadUnit>=kMinTwoUnitNodeValueLead) {
465 if(leadUnit<kThreeUnitNodeValueLead) {
466 ++pos;
467 } else {
468 pos+=2;
469 }
470 }
471 return pos;
472 }
473
474 static inline const char16_t* jumpByDelta(const char16_t* pos U_LIFETIME_BOUND) {
475 int32_t delta=*pos++;
476 if(delta>=kMinTwoUnitDeltaLead) {
477 if(delta==kThreeUnitDeltaLead) {
478 delta=(pos[0]<<16)|pos[1];
479 pos+=2;
480 } else {
481 delta=((delta-kMinTwoUnitDeltaLead)<<16)|*pos++;
482 }
483 }
484 return pos+delta;
485 }
486
487 static const char16_t* skipDelta(const char16_t* pos U_LIFETIME_BOUND) {
488 int32_t delta=*pos++;
489 if(delta>=kMinTwoUnitDeltaLead) {
490 if(delta==kThreeUnitDeltaLead) {
491 pos+=2;
492 } else {
493 ++pos;
494 }
495 }
496 return pos;
497 }
498
499 static inline UStringTrieResult valueResult(int32_t node) {
500 return static_cast<UStringTrieResult>(USTRINGTRIE_INTERMEDIATE_VALUE - (node >> 15));
501 }
502
503 // Handles a branch node for both next(uchar) and next(string).
504 UStringTrieResult branchNext(const char16_t *pos, int32_t length, int32_t uchar);
505
506 // Requires remainingLength_<0.
507 UStringTrieResult nextImpl(const char16_t *pos, int32_t uchar);
508
509 // Helper functions for hasUniqueValue().
510 // Recursively finds a unique value (or whether there is not a unique one)
511 // from a branch.
512 static const char16_t* findUniqueValueFromBranch(const char16_t* pos U_LIFETIME_BOUND,
513 int32_t length,
514 UBool haveUniqueValue,
515 int32_t& uniqueValue);
516 // Recursively finds a unique value (or whether there is not a unique one)
517 // starting from a position on a node lead unit.
518 static UBool findUniqueValue(const char16_t *pos, UBool haveUniqueValue, int32_t &uniqueValue);
519
520 // Helper functions for getNextUChars().
521 // getNextUChars() when pos is on a branch node.
522 static void getNextBranchUChars(const char16_t *pos, int32_t length, Appendable &out);
523
524 // UCharsTrie data structure
525 //
526 // The trie consists of a series of char16_t-serialized nodes for incremental
527 // Unicode string/char16_t sequence matching. (char16_t=16-bit unsigned integer)
528 // The root node is at the beginning of the trie data.
529 //
530 // Types of nodes are distinguished by their node lead unit ranges.
531 // After each node, except a final-value node, another node follows to
532 // encode match values or continue matching further units.
533 //
534 // Node types:
535 // - Final-value node: Stores a 32-bit integer in a compact, variable-length format.
536 // The value is for the string/char16_t sequence so far.
537 // - Match node, optionally with an intermediate value in a different compact format.
538 // The value, if present, is for the string/char16_t sequence so far.
539 //
540 // Aside from the value, which uses the node lead unit's high bits:
541 //
542 // - Linear-match node: Matches a number of units.
543 // - Branch node: Branches to other nodes according to the current input unit.
544 // The node unit is the length of the branch (number of units to select from)
545 // minus 1. It is followed by a sub-node:
546 // - If the length is at most kMaxBranchLinearSubNodeLength, then
547 // there are length-1 (key, value) pairs and then one more comparison unit.
548 // If one of the key units matches, then the value is either a final value for
549 // the string so far, or a "jump" delta to the next node.
550 // If the last unit matches, then matching continues with the next node.
551 // (Values have the same encoding as final-value nodes.)
552 // - If the length is greater than kMaxBranchLinearSubNodeLength, then
553 // there is one unit and one "jump" delta.
554 // If the input unit is less than the sub-node unit, then "jump" by delta to
555 // the next sub-node which will have a length of length/2.
556 // (The delta has its own compact encoding.)
557 // Otherwise, skip the "jump" delta to the next sub-node
558 // which will have a length of length-length/2.
559
560 // Match-node lead unit values, after masking off intermediate-value bits:
561
562 // 0000..002f: Branch node. If node!=0 then the length is node+1, otherwise
563 // the length is one more than the next unit.
564
565 // For a branch sub-node with at most this many entries, we drop down
566 // to a linear search.
567 static const int32_t kMaxBranchLinearSubNodeLength=5;
568
569 // 0030..003f: Linear-match node, match 1..16 units and continue reading the next node.
570 static const int32_t kMinLinearMatch=0x30;
571 static const int32_t kMaxLinearMatchLength=0x10;
572
573 // Match-node lead unit bits 14..6 for the optional intermediate value.
574 // If these bits are 0, then there is no intermediate value.
575 // Otherwise, see the *NodeValue* constants below.
576 static const int32_t kMinValueLead=kMinLinearMatch+kMaxLinearMatchLength; // 0x0040
577 static const int32_t kNodeTypeMask=kMinValueLead-1; // 0x003f
578
579 // A final-value node has bit 15 set.
580 static const int32_t kValueIsFinal=0x8000;
581
582 // Compact value: After testing and masking off bit 15, use the following thresholds.
583 static const int32_t kMaxOneUnitValue=0x3fff;
584
585 static const int32_t kMinTwoUnitValueLead=kMaxOneUnitValue+1; // 0x4000
586 static const int32_t kThreeUnitValueLead=0x7fff;
587
588 static const int32_t kMaxTwoUnitValue=((kThreeUnitValueLead-kMinTwoUnitValueLead)<<16)-1; // 0x3ffeffff
589
590 // Compact intermediate-value integer, lead unit shared with a branch or linear-match node.
591 static const int32_t kMaxOneUnitNodeValue=0xff;
592 static const int32_t kMinTwoUnitNodeValueLead=kMinValueLead+((kMaxOneUnitNodeValue+1)<<6); // 0x4040
593 static const int32_t kThreeUnitNodeValueLead=0x7fc0;
594
595 static const int32_t kMaxTwoUnitNodeValue=
596 ((kThreeUnitNodeValueLead-kMinTwoUnitNodeValueLead)<<10)-1; // 0xfdffff
597
598 // Compact delta integers.
599 static const int32_t kMaxOneUnitDelta=0xfbff;
600 static const int32_t kMinTwoUnitDeltaLead=kMaxOneUnitDelta+1; // 0xfc00
601 static const int32_t kThreeUnitDeltaLead=0xffff;
602
603 static const int32_t kMaxTwoUnitDelta=((kThreeUnitDeltaLead-kMinTwoUnitDeltaLead)<<16)-1; // 0x03feffff
604
605 // For getState64():
606 // The remainingMatchLength_ is -1..14=(kMaxLinearMatchLength=0x10)-2
607 // so we need at least 5 bits for that.
608 // We add 2 to store it as a positive value 1..16=kMaxLinearMatchLength.
609 static constexpr int32_t kState64RemainingShift = 59;
610 static constexpr uint64_t kState64PosMask = (UINT64_C(1) << kState64RemainingShift) - 1;
611
612 char16_t *ownedArray_;
613
614 // Fixed value referencing the UCharsTrie words.
615 const char16_t *uchars_;
616
617 // Iterator variables.
618
619 // Pointer to next trie unit to read. nullptr if no more matches.
620 const char16_t *pos_;
621 // Remaining length of a linear-match node, minus 1. Negative if not in such a node.
622 int32_t remainingMatchLength_;
623};
624
625U_NAMESPACE_END
626
627#endif /* U_SHOW_CPLUSPLUS_API */
628
629#endif // __UCHARSTRIE_H__
const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition char16ptr.h:156
Iterator(ConstChar16Ptr trieUChars, int32_t maxStringLength, UErrorCode &errorCode)
Iterates from the root of a char16_t-serialized UCharsTrie.
Iterator & reset()
Resets this iterator to its initial state.
const UnicodeString & getString() const
Definition ucharstrie.h:361
int32_t getValue() const
Definition ucharstrie.h:366
UBool next(UErrorCode &errorCode)
Finds the next (string, value) pair if there is one.
Iterator(const UCharsTrie &trie, int32_t maxStringLength, UErrorCode &errorCode)
Iterates from the current state of the specified UCharsTrie.
UCharsTrie state object, for saving a trie's current state and resetting the trie back to this state ...
Definition ucharstrie.h:138
State()
Constructs an empty State.
Definition ucharstrie.h:144
const UCharsTrie & saveState(State &state) const
Saves the state of this trie.
Definition ucharstrie.h:160
UCharsTrie(ConstChar16Ptr trieUChars)
Constructs a UCharsTrie reader instance.
Definition ucharstrie.h:69
UCharsTrie(const UCharsTrie &other)
Copy constructor, copies the other trie reader object and its state, but not the char16_t array which...
Definition ucharstrie.h:85
UBool hasUniqueValue(int32_t &uniqueValue) const
Determines whether all strings reachable from the current state map to the same value.
Definition ucharstrie.h:275
uint64_t getState64() const
Returns the state of this trie as a 64-bit integer.
Definition ucharstrie.h:108
UCharsTrie & reset()
Resets this trie to its initial state.
Definition ucharstrie.h:94
UCharsTrie & resetToState(const State &state)
Resets this trie to the saved state.
Definition ucharstrie.h:177
UStringTrieResult next(int32_t uchar)
Traverses the trie from the current state for this input char16_t.
UStringTrieResult nextForCodePoint(UChar32 cp)
Traverses the trie from the current state for the one or two UTF-16 code units for this input code po...
int32_t getValue() const
Returns a matching string's value if called immediately after current()/first()/next() returned USTRI...
Definition ucharstrie.h:258
UStringTrieResult firstForCodePoint(UChar32 cp)
Traverses the trie from the initial state for the one or two UTF-16 code units for this input code po...
UStringTrieResult current() const
Determines whether the string so far matches, whether it has a value, and whether another input char1...
UStringTrieResult first(int32_t uchar)
Traverses the trie from the initial state for this input char16_t.
Definition ucharstrie.h:200
UCharsTrie & resetToState64(uint64_t state)
Resets this trie to the saved state.
Definition ucharstrie.h:127
~UCharsTrie()
Destructor.
UStringTrieResult next(ConstChar16Ptr s, int32_t length)
Traverses the trie from the current state for this string.
int32_t getNextUChars(Appendable &out) const
Finds each char16_t which continues the string from the current state.
UMemory is the common ICU base class.
Definition uobject.h:115
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:303
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition umachine.h:449
#define UINT64_C(c)
Provides a platform independent way to specify an unsigned 64-bit integer constant.
Definition umachine.h:241
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:269
C++ API: Unicode String.
C++ API: Common ICU base class UObject.
C API: Helper definitions for dictionary trie APIs.
UStringTrieResult
Return values for BytesTrie::next(), UCharsTrie::next() and similar methods.
Definition ustringtrie.h:35
@ USTRINGTRIE_INTERMEDIATE_VALUE
The input unit(s) continued a matching string and there is a value for the string so far.
Definition ustringtrie.h:66
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