jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ******************************************************************************* jpayne@69: * Copyright (C) 2010-2012, International Business Machines jpayne@69: * Corporation and others. All Rights Reserved. jpayne@69: ******************************************************************************* jpayne@69: * file name: ucharstrie.h jpayne@69: * encoding: UTF-8 jpayne@69: * tab size: 8 (not used) jpayne@69: * indentation:4 jpayne@69: * jpayne@69: * created on: 2010nov14 jpayne@69: * created by: Markus W. Scherer jpayne@69: */ jpayne@69: jpayne@69: #ifndef __UCHARSTRIE_H__ jpayne@69: #define __UCHARSTRIE_H__ jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C++ API: Trie for mapping Unicode strings (or 16-bit-unit sequences) jpayne@69: * to integer values. jpayne@69: */ jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: #include "unicode/unistr.h" jpayne@69: #include "unicode/uobject.h" jpayne@69: #include "unicode/ustringtrie.h" jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: class Appendable; jpayne@69: class UCharsTrieBuilder; jpayne@69: class UVector32; jpayne@69: jpayne@69: /** jpayne@69: * Light-weight, non-const reader class for a UCharsTrie. jpayne@69: * Traverses a char16_t-serialized data structure with minimal state, jpayne@69: * for mapping strings (16-bit-unit sequences) to non-negative integer values. jpayne@69: * jpayne@69: * This class owns the serialized trie data only if it was constructed by jpayne@69: * the builder's build() method. jpayne@69: * The public constructor and the copy constructor only alias the data (only copy the pointer). jpayne@69: * There is no assignment operator. jpayne@69: * jpayne@69: * This class is not intended for public subclassing. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: class U_COMMON_API UCharsTrie : public UMemory { jpayne@69: public: jpayne@69: /** jpayne@69: * Constructs a UCharsTrie reader instance. jpayne@69: * jpayne@69: * The trieUChars must contain a copy of a char16_t sequence from the UCharsTrieBuilder, jpayne@69: * starting with the first char16_t of that sequence. jpayne@69: * The UCharsTrie object will not read more char16_ts than jpayne@69: * the UCharsTrieBuilder generated in the corresponding build() call. jpayne@69: * jpayne@69: * The array is not copied/cloned and must not be modified while jpayne@69: * the UCharsTrie object is in use. jpayne@69: * jpayne@69: * @param trieUChars The char16_t array that contains the serialized trie. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UCharsTrie(ConstChar16Ptr trieUChars) jpayne@69: : ownedArray_(NULL), uchars_(trieUChars), jpayne@69: pos_(uchars_), remainingMatchLength_(-1) {} jpayne@69: jpayne@69: /** jpayne@69: * Destructor. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: ~UCharsTrie(); jpayne@69: jpayne@69: /** jpayne@69: * Copy constructor, copies the other trie reader object and its state, jpayne@69: * but not the char16_t array which will be shared. (Shallow copy.) jpayne@69: * @param other Another UCharsTrie object. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UCharsTrie(const UCharsTrie &other) jpayne@69: : ownedArray_(NULL), uchars_(other.uchars_), jpayne@69: pos_(other.pos_), remainingMatchLength_(other.remainingMatchLength_) {} jpayne@69: jpayne@69: /** jpayne@69: * Resets this trie to its initial state. jpayne@69: * @return *this jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UCharsTrie &reset() { jpayne@69: pos_=uchars_; jpayne@69: remainingMatchLength_=-1; jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: #ifndef U_HIDE_DRAFT_API jpayne@69: /** jpayne@69: * Returns the state of this trie as a 64-bit integer. jpayne@69: * The state value is never 0. jpayne@69: * jpayne@69: * @return opaque state value jpayne@69: * @see resetToState64 jpayne@69: * @draft ICU 65 jpayne@69: */ jpayne@69: uint64_t getState64() const { jpayne@69: return (static_cast(remainingMatchLength_ + 2) << kState64RemainingShift) | jpayne@69: (uint64_t)(pos_ - uchars_); jpayne@69: } jpayne@69: jpayne@69: /** jpayne@69: * Resets this trie to the saved state. jpayne@69: * Unlike resetToState(State), the 64-bit state value jpayne@69: * must be from getState64() from the same trie object or jpayne@69: * from one initialized the exact same way. jpayne@69: * Because of no validation, this method is faster. jpayne@69: * jpayne@69: * @param state The opaque trie state value from getState64(). jpayne@69: * @return *this jpayne@69: * @see getState64 jpayne@69: * @see resetToState jpayne@69: * @see reset jpayne@69: * @draft ICU 65 jpayne@69: */ jpayne@69: UCharsTrie &resetToState64(uint64_t state) { jpayne@69: remainingMatchLength_ = static_cast(state >> kState64RemainingShift) - 2; jpayne@69: pos_ = uchars_ + (state & kState64PosMask); jpayne@69: return *this; jpayne@69: } jpayne@69: #endif /* U_HIDE_DRAFT_API */ jpayne@69: jpayne@69: /** jpayne@69: * UCharsTrie state object, for saving a trie's current state jpayne@69: * and resetting the trie back to this state later. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: class State : public UMemory { jpayne@69: public: jpayne@69: /** jpayne@69: * Constructs an empty State. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: State() { uchars=NULL; } jpayne@69: private: jpayne@69: friend class UCharsTrie; jpayne@69: jpayne@69: const char16_t *uchars; jpayne@69: const char16_t *pos; jpayne@69: int32_t remainingMatchLength; jpayne@69: }; jpayne@69: jpayne@69: /** jpayne@69: * Saves the state of this trie. jpayne@69: * @param state The State object to hold the trie's state. jpayne@69: * @return *this jpayne@69: * @see resetToState jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: const UCharsTrie &saveState(State &state) const { jpayne@69: state.uchars=uchars_; jpayne@69: state.pos=pos_; jpayne@69: state.remainingMatchLength=remainingMatchLength_; jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: /** jpayne@69: * Resets this trie to the saved state. jpayne@69: * If the state object contains no state, or the state of a different trie, jpayne@69: * then this trie remains unchanged. jpayne@69: * @param state The State object which holds a saved trie state. jpayne@69: * @return *this jpayne@69: * @see saveState jpayne@69: * @see reset jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UCharsTrie &resetToState(const State &state) { jpayne@69: if(uchars_==state.uchars && uchars_!=NULL) { jpayne@69: pos_=state.pos; jpayne@69: remainingMatchLength_=state.remainingMatchLength; jpayne@69: } jpayne@69: return *this; jpayne@69: } jpayne@69: jpayne@69: /** jpayne@69: * Determines whether the string so far matches, whether it has a value, jpayne@69: * and whether another input char16_t can continue a matching string. jpayne@69: * @return The match/value Result. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UStringTrieResult current() const; jpayne@69: jpayne@69: /** jpayne@69: * Traverses the trie from the initial state for this input char16_t. jpayne@69: * Equivalent to reset().next(uchar). jpayne@69: * @param uchar Input char value. Values below 0 and above 0xffff will never match. jpayne@69: * @return The match/value Result. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: inline UStringTrieResult first(int32_t uchar) { jpayne@69: remainingMatchLength_=-1; jpayne@69: return nextImpl(uchars_, uchar); jpayne@69: } jpayne@69: jpayne@69: /** jpayne@69: * Traverses the trie from the initial state for the jpayne@69: * one or two UTF-16 code units for this input code point. jpayne@69: * Equivalent to reset().nextForCodePoint(cp). jpayne@69: * @param cp A Unicode code point 0..0x10ffff. jpayne@69: * @return The match/value Result. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UStringTrieResult firstForCodePoint(UChar32 cp); jpayne@69: jpayne@69: /** jpayne@69: * Traverses the trie from the current state for this input char16_t. jpayne@69: * @param uchar Input char value. Values below 0 and above 0xffff will never match. jpayne@69: * @return The match/value Result. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UStringTrieResult next(int32_t uchar); jpayne@69: jpayne@69: /** jpayne@69: * Traverses the trie from the current state for the jpayne@69: * one or two UTF-16 code units for this input code point. jpayne@69: * @param cp A Unicode code point 0..0x10ffff. jpayne@69: * @return The match/value Result. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UStringTrieResult nextForCodePoint(UChar32 cp); jpayne@69: jpayne@69: /** jpayne@69: * Traverses the trie from the current state for this string. jpayne@69: * Equivalent to jpayne@69: * \code jpayne@69: * Result result=current(); jpayne@69: * for(each c in s) jpayne@69: * if(!USTRINGTRIE_HAS_NEXT(result)) return USTRINGTRIE_NO_MATCH; jpayne@69: * result=next(c); jpayne@69: * return result; jpayne@69: * \endcode jpayne@69: * @param s A string. Can be NULL if length is 0. jpayne@69: * @param length The length of the string. Can be -1 if NUL-terminated. jpayne@69: * @return The match/value Result. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UStringTrieResult next(ConstChar16Ptr s, int32_t length); jpayne@69: jpayne@69: /** jpayne@69: * Returns a matching string's value if called immediately after jpayne@69: * current()/first()/next() returned USTRINGTRIE_INTERMEDIATE_VALUE or USTRINGTRIE_FINAL_VALUE. jpayne@69: * getValue() can be called multiple times. jpayne@69: * jpayne@69: * Do not call getValue() after USTRINGTRIE_NO_MATCH or USTRINGTRIE_NO_VALUE! jpayne@69: * @return The value for the string so far. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: inline int32_t getValue() const { jpayne@69: const char16_t *pos=pos_; jpayne@69: int32_t leadUnit=*pos++; jpayne@69: // U_ASSERT(leadUnit>=kMinValueLead); jpayne@69: return leadUnit&kValueIsFinal ? jpayne@69: readValue(pos, leadUnit&0x7fff) : readNodeValue(pos, leadUnit); jpayne@69: } jpayne@69: jpayne@69: /** jpayne@69: * Determines whether all strings reachable from the current state jpayne@69: * map to the same value. jpayne@69: * @param uniqueValue Receives the unique value, if this function returns TRUE. jpayne@69: * (output-only) jpayne@69: * @return TRUE if all strings reachable from the current state jpayne@69: * map to the same value. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: inline UBool hasUniqueValue(int32_t &uniqueValue) const { jpayne@69: const char16_t *pos=pos_; jpayne@69: // Skip the rest of a pending linear-match node. jpayne@69: return pos!=NULL && findUniqueValue(pos+remainingMatchLength_+1, FALSE, uniqueValue); jpayne@69: } jpayne@69: jpayne@69: /** jpayne@69: * Finds each char16_t which continues the string from the current state. jpayne@69: * That is, each char16_t c for which it would be next(c)!=USTRINGTRIE_NO_MATCH now. jpayne@69: * @param out Each next char16_t is appended to this object. jpayne@69: * @return the number of char16_ts which continue the string from here jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: int32_t getNextUChars(Appendable &out) const; jpayne@69: jpayne@69: /** jpayne@69: * Iterator for all of the (string, value) pairs in a UCharsTrie. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: class U_COMMON_API Iterator : public UMemory { jpayne@69: public: jpayne@69: /** jpayne@69: * Iterates from the root of a char16_t-serialized UCharsTrie. jpayne@69: * @param trieUChars The trie char16_ts. jpayne@69: * @param maxStringLength If 0, the iterator returns full strings. jpayne@69: * Otherwise, the iterator returns strings with this maximum length. jpayne@69: * @param errorCode Standard ICU error code. Its input value must jpayne@69: * pass the U_SUCCESS() test, or else the function returns jpayne@69: * immediately. Check for U_FAILURE() on output or use with jpayne@69: * function chaining. (See User Guide for details.) jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: Iterator(ConstChar16Ptr trieUChars, int32_t maxStringLength, UErrorCode &errorCode); jpayne@69: jpayne@69: /** jpayne@69: * Iterates from the current state of the specified UCharsTrie. jpayne@69: * @param trie The trie whose state will be copied for iteration. jpayne@69: * @param maxStringLength If 0, the iterator returns full strings. jpayne@69: * Otherwise, the iterator returns strings with this maximum length. jpayne@69: * @param errorCode Standard ICU error code. Its input value must jpayne@69: * pass the U_SUCCESS() test, or else the function returns jpayne@69: * immediately. Check for U_FAILURE() on output or use with jpayne@69: * function chaining. (See User Guide for details.) jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: Iterator(const UCharsTrie &trie, int32_t maxStringLength, UErrorCode &errorCode); jpayne@69: jpayne@69: /** jpayne@69: * Destructor. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: ~Iterator(); jpayne@69: jpayne@69: /** jpayne@69: * Resets this iterator to its initial state. jpayne@69: * @return *this jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: Iterator &reset(); jpayne@69: jpayne@69: /** jpayne@69: * @return TRUE if there are more elements. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UBool hasNext() const; jpayne@69: jpayne@69: /** jpayne@69: * Finds the next (string, value) pair if there is one. jpayne@69: * jpayne@69: * If the string is truncated to the maximum length and does not jpayne@69: * have a real value, then the value is set to -1. jpayne@69: * In this case, this "not a real value" is indistinguishable from jpayne@69: * a real value of -1. jpayne@69: * @param errorCode Standard ICU error code. Its input value must jpayne@69: * pass the U_SUCCESS() test, or else the function returns jpayne@69: * immediately. Check for U_FAILURE() on output or use with jpayne@69: * function chaining. (See User Guide for details.) jpayne@69: * @return TRUE if there is another element. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: UBool next(UErrorCode &errorCode); jpayne@69: jpayne@69: /** jpayne@69: * @return The string for the last successful next(). jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: const UnicodeString &getString() const { return str_; } jpayne@69: /** jpayne@69: * @return The value for the last successful next(). jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: int32_t getValue() const { return value_; } jpayne@69: jpayne@69: private: jpayne@69: UBool truncateAndStop() { jpayne@69: pos_=NULL; jpayne@69: value_=-1; // no real value for str jpayne@69: return TRUE; jpayne@69: } jpayne@69: jpayne@69: const char16_t *branchNext(const char16_t *pos, int32_t length, UErrorCode &errorCode); jpayne@69: jpayne@69: const char16_t *uchars_; jpayne@69: const char16_t *pos_; jpayne@69: const char16_t *initialPos_; jpayne@69: int32_t remainingMatchLength_; jpayne@69: int32_t initialRemainingMatchLength_; jpayne@69: UBool skipValue_; // Skip intermediate value which was already delivered. jpayne@69: jpayne@69: UnicodeString str_; jpayne@69: int32_t maxLength_; jpayne@69: int32_t value_; jpayne@69: jpayne@69: // The stack stores pairs of integers for backtracking to another jpayne@69: // outbound edge of a branch node. jpayne@69: // The first integer is an offset from uchars_. jpayne@69: // The second integer has the str_.length() from before the node in bits 15..0, jpayne@69: // and the remaining branch length in bits 31..16. jpayne@69: // (We could store the remaining branch length minus 1 in bits 30..16 and not use the sign bit, jpayne@69: // but the code looks more confusing that way.) jpayne@69: UVector32 *stack_; jpayne@69: }; jpayne@69: jpayne@69: private: jpayne@69: friend class UCharsTrieBuilder; jpayne@69: jpayne@69: /** jpayne@69: * Constructs a UCharsTrie reader instance. jpayne@69: * Unlike the public constructor which just aliases an array, jpayne@69: * this constructor adopts the builder's array. jpayne@69: * This constructor is only called by the builder. jpayne@69: */ jpayne@69: UCharsTrie(char16_t *adoptUChars, const char16_t *trieUChars) jpayne@69: : ownedArray_(adoptUChars), uchars_(trieUChars), jpayne@69: pos_(uchars_), remainingMatchLength_(-1) {} jpayne@69: jpayne@69: // No assignment operator. jpayne@69: UCharsTrie &operator=(const UCharsTrie &other); jpayne@69: jpayne@69: inline void stop() { jpayne@69: pos_=NULL; jpayne@69: } jpayne@69: jpayne@69: // Reads a compact 32-bit integer. jpayne@69: // pos is already after the leadUnit, and the lead unit has bit 15 reset. jpayne@69: static inline int32_t readValue(const char16_t *pos, int32_t leadUnit) { jpayne@69: int32_t value; jpayne@69: if(leadUnit=kMinTwoUnitValueLead) { jpayne@69: if(leadUnit>6)-1; jpayne@69: } else if(leadUnit=kMinTwoUnitNodeValueLead) { jpayne@69: if(leadUnit=kMinTwoUnitDeltaLead) { jpayne@69: if(delta==kThreeUnitDeltaLead) { jpayne@69: delta=(pos[0]<<16)|pos[1]; jpayne@69: pos+=2; jpayne@69: } else { jpayne@69: delta=((delta-kMinTwoUnitDeltaLead)<<16)|*pos++; jpayne@69: } jpayne@69: } jpayne@69: return pos+delta; jpayne@69: } jpayne@69: jpayne@69: static const char16_t *skipDelta(const char16_t *pos) { jpayne@69: int32_t delta=*pos++; jpayne@69: if(delta>=kMinTwoUnitDeltaLead) { jpayne@69: if(delta==kThreeUnitDeltaLead) { jpayne@69: pos+=2; jpayne@69: } else { jpayne@69: ++pos; jpayne@69: } jpayne@69: } jpayne@69: return pos; jpayne@69: } jpayne@69: jpayne@69: static inline UStringTrieResult valueResult(int32_t node) { jpayne@69: return (UStringTrieResult)(USTRINGTRIE_INTERMEDIATE_VALUE-(node>>15)); jpayne@69: } jpayne@69: jpayne@69: // Handles a branch node for both next(uchar) and next(string). jpayne@69: UStringTrieResult branchNext(const char16_t *pos, int32_t length, int32_t uchar); jpayne@69: jpayne@69: // Requires remainingLength_<0. jpayne@69: UStringTrieResult nextImpl(const char16_t *pos, int32_t uchar); jpayne@69: jpayne@69: // Helper functions for hasUniqueValue(). jpayne@69: // Recursively finds a unique value (or whether there is not a unique one) jpayne@69: // from a branch. jpayne@69: static const char16_t *findUniqueValueFromBranch(const char16_t *pos, int32_t length, jpayne@69: UBool haveUniqueValue, int32_t &uniqueValue); jpayne@69: // Recursively finds a unique value (or whether there is not a unique one) jpayne@69: // starting from a position on a node lead unit. jpayne@69: static UBool findUniqueValue(const char16_t *pos, UBool haveUniqueValue, int32_t &uniqueValue); jpayne@69: jpayne@69: // Helper functions for getNextUChars(). jpayne@69: // getNextUChars() when pos is on a branch node. jpayne@69: static void getNextBranchUChars(const char16_t *pos, int32_t length, Appendable &out); jpayne@69: jpayne@69: // UCharsTrie data structure jpayne@69: // jpayne@69: // The trie consists of a series of char16_t-serialized nodes for incremental jpayne@69: // Unicode string/char16_t sequence matching. (char16_t=16-bit unsigned integer) jpayne@69: // The root node is at the beginning of the trie data. jpayne@69: // jpayne@69: // Types of nodes are distinguished by their node lead unit ranges. jpayne@69: // After each node, except a final-value node, another node follows to jpayne@69: // encode match values or continue matching further units. jpayne@69: // jpayne@69: // Node types: jpayne@69: // - Final-value node: Stores a 32-bit integer in a compact, variable-length format. jpayne@69: // The value is for the string/char16_t sequence so far. jpayne@69: // - Match node, optionally with an intermediate value in a different compact format. jpayne@69: // The value, if present, is for the string/char16_t sequence so far. jpayne@69: // jpayne@69: // Aside from the value, which uses the node lead unit's high bits: jpayne@69: // jpayne@69: // - Linear-match node: Matches a number of units. jpayne@69: // - Branch node: Branches to other nodes according to the current input unit. jpayne@69: // The node unit is the length of the branch (number of units to select from) jpayne@69: // minus 1. It is followed by a sub-node: jpayne@69: // - If the length is at most kMaxBranchLinearSubNodeLength, then jpayne@69: // there are length-1 (key, value) pairs and then one more comparison unit. jpayne@69: // If one of the key units matches, then the value is either a final value for jpayne@69: // the string so far, or a "jump" delta to the next node. jpayne@69: // If the last unit matches, then matching continues with the next node. jpayne@69: // (Values have the same encoding as final-value nodes.) jpayne@69: // - If the length is greater than kMaxBranchLinearSubNodeLength, then jpayne@69: // there is one unit and one "jump" delta. jpayne@69: // If the input unit is less than the sub-node unit, then "jump" by delta to jpayne@69: // the next sub-node which will have a length of length/2. jpayne@69: // (The delta has its own compact encoding.) jpayne@69: // Otherwise, skip the "jump" delta to the next sub-node jpayne@69: // which will have a length of length-length/2. jpayne@69: jpayne@69: // Match-node lead unit values, after masking off intermediate-value bits: jpayne@69: jpayne@69: // 0000..002f: Branch node. If node!=0 then the length is node+1, otherwise jpayne@69: // the length is one more than the next unit. jpayne@69: jpayne@69: // For a branch sub-node with at most this many entries, we drop down jpayne@69: // to a linear search. jpayne@69: static const int32_t kMaxBranchLinearSubNodeLength=5; jpayne@69: jpayne@69: // 0030..003f: Linear-match node, match 1..16 units and continue reading the next node. jpayne@69: static const int32_t kMinLinearMatch=0x30; jpayne@69: static const int32_t kMaxLinearMatchLength=0x10; jpayne@69: jpayne@69: // Match-node lead unit bits 14..6 for the optional intermediate value. jpayne@69: // If these bits are 0, then there is no intermediate value. jpayne@69: // Otherwise, see the *NodeValue* constants below. jpayne@69: static const int32_t kMinValueLead=kMinLinearMatch+kMaxLinearMatchLength; // 0x0040 jpayne@69: static const int32_t kNodeTypeMask=kMinValueLead-1; // 0x003f jpayne@69: jpayne@69: // A final-value node has bit 15 set. jpayne@69: static const int32_t kValueIsFinal=0x8000; jpayne@69: jpayne@69: // Compact value: After testing and masking off bit 15, use the following thresholds. jpayne@69: static const int32_t kMaxOneUnitValue=0x3fff; jpayne@69: jpayne@69: static const int32_t kMinTwoUnitValueLead=kMaxOneUnitValue+1; // 0x4000 jpayne@69: static const int32_t kThreeUnitValueLead=0x7fff; jpayne@69: jpayne@69: static const int32_t kMaxTwoUnitValue=((kThreeUnitValueLead-kMinTwoUnitValueLead)<<16)-1; // 0x3ffeffff jpayne@69: jpayne@69: // Compact intermediate-value integer, lead unit shared with a branch or linear-match node. jpayne@69: static const int32_t kMaxOneUnitNodeValue=0xff; jpayne@69: static const int32_t kMinTwoUnitNodeValueLead=kMinValueLead+((kMaxOneUnitNodeValue+1)<<6); // 0x4040 jpayne@69: static const int32_t kThreeUnitNodeValueLead=0x7fc0; jpayne@69: jpayne@69: static const int32_t kMaxTwoUnitNodeValue= jpayne@69: ((kThreeUnitNodeValueLead-kMinTwoUnitNodeValueLead)<<10)-1; // 0xfdffff jpayne@69: jpayne@69: // Compact delta integers. jpayne@69: static const int32_t kMaxOneUnitDelta=0xfbff; jpayne@69: static const int32_t kMinTwoUnitDeltaLead=kMaxOneUnitDelta+1; // 0xfc00 jpayne@69: static const int32_t kThreeUnitDeltaLead=0xffff; jpayne@69: jpayne@69: static const int32_t kMaxTwoUnitDelta=((kThreeUnitDeltaLead-kMinTwoUnitDeltaLead)<<16)-1; // 0x03feffff jpayne@69: jpayne@69: // For getState64(): jpayne@69: // The remainingMatchLength_ is -1..14=(kMaxLinearMatchLength=0x10)-2 jpayne@69: // so we need at least 5 bits for that. jpayne@69: // We add 2 to store it as a positive value 1..16=kMaxLinearMatchLength. jpayne@69: static constexpr int32_t kState64RemainingShift = 59; jpayne@69: static constexpr uint64_t kState64PosMask = (UINT64_C(1) << kState64RemainingShift) - 1; jpayne@69: jpayne@69: char16_t *ownedArray_; jpayne@69: jpayne@69: // Fixed value referencing the UCharsTrie words. jpayne@69: const char16_t *uchars_; jpayne@69: jpayne@69: // Iterator variables. jpayne@69: jpayne@69: // Pointer to next trie unit to read. NULL if no more matches. jpayne@69: const char16_t *pos_; jpayne@69: // Remaining length of a linear-match node, minus 1. Negative if not in such a node. jpayne@69: int32_t remainingMatchLength_; jpayne@69: }; jpayne@69: jpayne@69: U_NAMESPACE_END jpayne@69: jpayne@69: #endif /* U_SHOW_CPLUSPLUS_API */ jpayne@69: jpayne@69: #endif // __UCHARSTRIE_H__