Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/ucharstrie.h
48773 views
// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3*******************************************************************************4* Copyright (C) 2010-2012, International Business Machines5* Corporation and others. All Rights Reserved.6*******************************************************************************7* file name: ucharstrie.h8* encoding: UTF-89* tab size: 8 (not used)10* indentation:411*12* created on: 2010nov1413* created by: Markus W. Scherer14*/1516#ifndef __UCHARSTRIE_H__17#define __UCHARSTRIE_H__1819/**20* \file21* \brief C++ API: Trie for mapping Unicode strings (or 16-bit-unit sequences)22* to integer values.23*/2425#include "unicode/utypes.h"26#include "unicode/unistr.h"27#include "unicode/uobject.h"28#include "unicode/ustringtrie.h"2930U_NAMESPACE_BEGIN3132class Appendable;33class UCharsTrieBuilder;34class UVector32;3536/**37* Light-weight, non-const reader class for a UCharsTrie.38* Traverses a char16_t-serialized data structure with minimal state,39* for mapping strings (16-bit-unit sequences) to non-negative integer values.40*41* This class owns the serialized trie data only if it was constructed by42* the builder's build() method.43* The public constructor and the copy constructor only alias the data (only copy the pointer).44* There is no assignment operator.45*46* This class is not intended for public subclassing.47* @stable ICU 4.848*/49class U_COMMON_API UCharsTrie : public UMemory {50public:51/**52* Constructs a UCharsTrie reader instance.53*54* The trieUChars must contain a copy of a char16_t sequence from the UCharsTrieBuilder,55* starting with the first char16_t of that sequence.56* The UCharsTrie object will not read more char16_ts than57* the UCharsTrieBuilder generated in the corresponding build() call.58*59* The array is not copied/cloned and must not be modified while60* the UCharsTrie object is in use.61*62* @param trieUChars The char16_t array that contains the serialized trie.63* @stable ICU 4.864*/65UCharsTrie(ConstChar16Ptr trieUChars)66: ownedArray_(NULL), uchars_(trieUChars),67pos_(uchars_), remainingMatchLength_(-1) {}6869/**70* Destructor.71* @stable ICU 4.872*/73~UCharsTrie();7475/**76* Copy constructor, copies the other trie reader object and its state,77* but not the char16_t array which will be shared. (Shallow copy.)78* @param other Another UCharsTrie object.79* @stable ICU 4.880*/81UCharsTrie(const UCharsTrie &other)82: ownedArray_(NULL), uchars_(other.uchars_),83pos_(other.pos_), remainingMatchLength_(other.remainingMatchLength_) {}8485/**86* Resets this trie to its initial state.87* @return *this88* @stable ICU 4.889*/90UCharsTrie &reset() {91pos_=uchars_;92remainingMatchLength_=-1;93return *this;94}9596/**97* UCharsTrie state object, for saving a trie's current state98* and resetting the trie back to this state later.99* @stable ICU 4.8100*/101class State : public UMemory {102public:103/**104* Constructs an empty State.105* @stable ICU 4.8106*/107State() { uchars=NULL; }108private:109friend class UCharsTrie;110111const char16_t *uchars;112const char16_t *pos;113int32_t remainingMatchLength;114};115116/**117* Saves the state of this trie.118* @param state The State object to hold the trie's state.119* @return *this120* @see resetToState121* @stable ICU 4.8122*/123const UCharsTrie &saveState(State &state) const {124state.uchars=uchars_;125state.pos=pos_;126state.remainingMatchLength=remainingMatchLength_;127return *this;128}129130/**131* Resets this trie to the saved state.132* If the state object contains no state, or the state of a different trie,133* then this trie remains unchanged.134* @param state The State object which holds a saved trie state.135* @return *this136* @see saveState137* @see reset138* @stable ICU 4.8139*/140UCharsTrie &resetToState(const State &state) {141if(uchars_==state.uchars && uchars_!=NULL) {142pos_=state.pos;143remainingMatchLength_=state.remainingMatchLength;144}145return *this;146}147148/**149* Determines whether the string so far matches, whether it has a value,150* and whether another input char16_t can continue a matching string.151* @return The match/value Result.152* @stable ICU 4.8153*/154UStringTrieResult current() const;155156/**157* Traverses the trie from the initial state for this input char16_t.158* Equivalent to reset().next(uchar).159* @param uchar Input char value. Values below 0 and above 0xffff will never match.160* @return The match/value Result.161* @stable ICU 4.8162*/163inline UStringTrieResult first(int32_t uchar) {164remainingMatchLength_=-1;165return nextImpl(uchars_, uchar);166}167168/**169* Traverses the trie from the initial state for the170* one or two UTF-16 code units for this input code point.171* Equivalent to reset().nextForCodePoint(cp).172* @param cp A Unicode code point 0..0x10ffff.173* @return The match/value Result.174* @stable ICU 4.8175*/176UStringTrieResult firstForCodePoint(UChar32 cp);177178/**179* Traverses the trie from the current state for this input char16_t.180* @param uchar Input char value. Values below 0 and above 0xffff will never match.181* @return The match/value Result.182* @stable ICU 4.8183*/184UStringTrieResult next(int32_t uchar);185186/**187* Traverses the trie from the current state for the188* one or two UTF-16 code units for this input code point.189* @param cp A Unicode code point 0..0x10ffff.190* @return The match/value Result.191* @stable ICU 4.8192*/193UStringTrieResult nextForCodePoint(UChar32 cp);194195/**196* Traverses the trie from the current state for this string.197* Equivalent to198* \code199* Result result=current();200* for(each c in s)201* if(!USTRINGTRIE_HAS_NEXT(result)) return USTRINGTRIE_NO_MATCH;202* result=next(c);203* return result;204* \endcode205* @param s A string. Can be NULL if length is 0.206* @param length The length of the string. Can be -1 if NUL-terminated.207* @return The match/value Result.208* @stable ICU 4.8209*/210UStringTrieResult next(ConstChar16Ptr s, int32_t length);211212/**213* Returns a matching string's value if called immediately after214* current()/first()/next() returned USTRINGTRIE_INTERMEDIATE_VALUE or USTRINGTRIE_FINAL_VALUE.215* getValue() can be called multiple times.216*217* Do not call getValue() after USTRINGTRIE_NO_MATCH or USTRINGTRIE_NO_VALUE!218* @return The value for the string so far.219* @stable ICU 4.8220*/221inline int32_t getValue() const {222const char16_t *pos=pos_;223int32_t leadUnit=*pos++;224// U_ASSERT(leadUnit>=kMinValueLead);225return leadUnit&kValueIsFinal ?226readValue(pos, leadUnit&0x7fff) : readNodeValue(pos, leadUnit);227}228229/**230* Determines whether all strings reachable from the current state231* map to the same value.232* @param uniqueValue Receives the unique value, if this function returns TRUE.233* (output-only)234* @return TRUE if all strings reachable from the current state235* map to the same value.236* @stable ICU 4.8237*/238inline UBool hasUniqueValue(int32_t &uniqueValue) const {239const char16_t *pos=pos_;240// Skip the rest of a pending linear-match node.241return pos!=NULL && findUniqueValue(pos+remainingMatchLength_+1, FALSE, uniqueValue);242}243244/**245* Finds each char16_t which continues the string from the current state.246* That is, each char16_t c for which it would be next(c)!=USTRINGTRIE_NO_MATCH now.247* @param out Each next char16_t is appended to this object.248* @return the number of char16_ts which continue the string from here249* @stable ICU 4.8250*/251int32_t getNextUChars(Appendable &out) const;252253/**254* Iterator for all of the (string, value) pairs in a UCharsTrie.255* @stable ICU 4.8256*/257class U_COMMON_API Iterator : public UMemory {258public:259/**260* Iterates from the root of a char16_t-serialized UCharsTrie.261* @param trieUChars The trie char16_ts.262* @param maxStringLength If 0, the iterator returns full strings.263* Otherwise, the iterator returns strings with this maximum length.264* @param errorCode Standard ICU error code. Its input value must265* pass the U_SUCCESS() test, or else the function returns266* immediately. Check for U_FAILURE() on output or use with267* function chaining. (See User Guide for details.)268* @stable ICU 4.8269*/270Iterator(ConstChar16Ptr trieUChars, int32_t maxStringLength, UErrorCode &errorCode);271272/**273* Iterates from the current state of the specified UCharsTrie.274* @param trie The trie whose state will be copied for iteration.275* @param maxStringLength If 0, the iterator returns full strings.276* Otherwise, the iterator returns strings with this maximum length.277* @param errorCode Standard ICU error code. Its input value must278* pass the U_SUCCESS() test, or else the function returns279* immediately. Check for U_FAILURE() on output or use with280* function chaining. (See User Guide for details.)281* @stable ICU 4.8282*/283Iterator(const UCharsTrie &trie, int32_t maxStringLength, UErrorCode &errorCode);284285/**286* Destructor.287* @stable ICU 4.8288*/289~Iterator();290291/**292* Resets this iterator to its initial state.293* @return *this294* @stable ICU 4.8295*/296Iterator &reset();297298/**299* @return TRUE if there are more elements.300* @stable ICU 4.8301*/302UBool hasNext() const;303304/**305* Finds the next (string, value) pair if there is one.306*307* If the string is truncated to the maximum length and does not308* have a real value, then the value is set to -1.309* In this case, this "not a real value" is indistinguishable from310* a real value of -1.311* @param errorCode Standard ICU error code. Its input value must312* pass the U_SUCCESS() test, or else the function returns313* immediately. Check for U_FAILURE() on output or use with314* function chaining. (See User Guide for details.)315* @return TRUE if there is another element.316* @stable ICU 4.8317*/318UBool next(UErrorCode &errorCode);319320/**321* @return The string for the last successful next().322* @stable ICU 4.8323*/324const UnicodeString &getString() const { return str_; }325/**326* @return The value for the last successful next().327* @stable ICU 4.8328*/329int32_t getValue() const { return value_; }330331private:332UBool truncateAndStop() {333pos_=NULL;334value_=-1; // no real value for str335return TRUE;336}337338const char16_t *branchNext(const char16_t *pos, int32_t length, UErrorCode &errorCode);339340const char16_t *uchars_;341const char16_t *pos_;342const char16_t *initialPos_;343int32_t remainingMatchLength_;344int32_t initialRemainingMatchLength_;345UBool skipValue_; // Skip intermediate value which was already delivered.346347UnicodeString str_;348int32_t maxLength_;349int32_t value_;350351// The stack stores pairs of integers for backtracking to another352// outbound edge of a branch node.353// The first integer is an offset from uchars_.354// The second integer has the str_.length() from before the node in bits 15..0,355// and the remaining branch length in bits 31..16.356// (We could store the remaining branch length minus 1 in bits 30..16 and not use the sign bit,357// but the code looks more confusing that way.)358UVector32 *stack_;359};360361private:362friend class UCharsTrieBuilder;363364/**365* Constructs a UCharsTrie reader instance.366* Unlike the public constructor which just aliases an array,367* this constructor adopts the builder's array.368* This constructor is only called by the builder.369*/370UCharsTrie(char16_t *adoptUChars, const char16_t *trieUChars)371: ownedArray_(adoptUChars), uchars_(trieUChars),372pos_(uchars_), remainingMatchLength_(-1) {}373374// No assignment operator.375UCharsTrie &operator=(const UCharsTrie &other);376377inline void stop() {378pos_=NULL;379}380381// Reads a compact 32-bit integer.382// pos is already after the leadUnit, and the lead unit has bit 15 reset.383static inline int32_t readValue(const char16_t *pos, int32_t leadUnit) {384int32_t value;385if(leadUnit<kMinTwoUnitValueLead) {386value=leadUnit;387} else if(leadUnit<kThreeUnitValueLead) {388value=((leadUnit-kMinTwoUnitValueLead)<<16)|*pos;389} else {390value=(pos[0]<<16)|pos[1];391}392return value;393}394static inline const char16_t *skipValue(const char16_t *pos, int32_t leadUnit) {395if(leadUnit>=kMinTwoUnitValueLead) {396if(leadUnit<kThreeUnitValueLead) {397++pos;398} else {399pos+=2;400}401}402return pos;403}404static inline const char16_t *skipValue(const char16_t *pos) {405int32_t leadUnit=*pos++;406return skipValue(pos, leadUnit&0x7fff);407}408409static inline int32_t readNodeValue(const char16_t *pos, int32_t leadUnit) {410// U_ASSERT(kMinValueLead<=leadUnit && leadUnit<kValueIsFinal);411int32_t value;412if(leadUnit<kMinTwoUnitNodeValueLead) {413value=(leadUnit>>6)-1;414} else if(leadUnit<kThreeUnitNodeValueLead) {415value=(((leadUnit&0x7fc0)-kMinTwoUnitNodeValueLead)<<10)|*pos;416} else {417value=(pos[0]<<16)|pos[1];418}419return value;420}421static inline const char16_t *skipNodeValue(const char16_t *pos, int32_t leadUnit) {422// U_ASSERT(kMinValueLead<=leadUnit && leadUnit<kValueIsFinal);423if(leadUnit>=kMinTwoUnitNodeValueLead) {424if(leadUnit<kThreeUnitNodeValueLead) {425++pos;426} else {427pos+=2;428}429}430return pos;431}432433static inline const char16_t *jumpByDelta(const char16_t *pos) {434int32_t delta=*pos++;435if(delta>=kMinTwoUnitDeltaLead) {436if(delta==kThreeUnitDeltaLead) {437delta=(pos[0]<<16)|pos[1];438pos+=2;439} else {440delta=((delta-kMinTwoUnitDeltaLead)<<16)|*pos++;441}442}443return pos+delta;444}445446static const char16_t *skipDelta(const char16_t *pos) {447int32_t delta=*pos++;448if(delta>=kMinTwoUnitDeltaLead) {449if(delta==kThreeUnitDeltaLead) {450pos+=2;451} else {452++pos;453}454}455return pos;456}457458static inline UStringTrieResult valueResult(int32_t node) {459return (UStringTrieResult)(USTRINGTRIE_INTERMEDIATE_VALUE-(node>>15));460}461462// Handles a branch node for both next(uchar) and next(string).463UStringTrieResult branchNext(const char16_t *pos, int32_t length, int32_t uchar);464465// Requires remainingLength_<0.466UStringTrieResult nextImpl(const char16_t *pos, int32_t uchar);467468// Helper functions for hasUniqueValue().469// Recursively finds a unique value (or whether there is not a unique one)470// from a branch.471static const char16_t *findUniqueValueFromBranch(const char16_t *pos, int32_t length,472UBool haveUniqueValue, int32_t &uniqueValue);473// Recursively finds a unique value (or whether there is not a unique one)474// starting from a position on a node lead unit.475static UBool findUniqueValue(const char16_t *pos, UBool haveUniqueValue, int32_t &uniqueValue);476477// Helper functions for getNextUChars().478// getNextUChars() when pos is on a branch node.479static void getNextBranchUChars(const char16_t *pos, int32_t length, Appendable &out);480481// UCharsTrie data structure482//483// The trie consists of a series of char16_t-serialized nodes for incremental484// Unicode string/char16_t sequence matching. (char16_t=16-bit unsigned integer)485// The root node is at the beginning of the trie data.486//487// Types of nodes are distinguished by their node lead unit ranges.488// After each node, except a final-value node, another node follows to489// encode match values or continue matching further units.490//491// Node types:492// - Final-value node: Stores a 32-bit integer in a compact, variable-length format.493// The value is for the string/char16_t sequence so far.494// - Match node, optionally with an intermediate value in a different compact format.495// The value, if present, is for the string/char16_t sequence so far.496//497// Aside from the value, which uses the node lead unit's high bits:498//499// - Linear-match node: Matches a number of units.500// - Branch node: Branches to other nodes according to the current input unit.501// The node unit is the length of the branch (number of units to select from)502// minus 1. It is followed by a sub-node:503// - If the length is at most kMaxBranchLinearSubNodeLength, then504// there are length-1 (key, value) pairs and then one more comparison unit.505// If one of the key units matches, then the value is either a final value for506// the string so far, or a "jump" delta to the next node.507// If the last unit matches, then matching continues with the next node.508// (Values have the same encoding as final-value nodes.)509// - If the length is greater than kMaxBranchLinearSubNodeLength, then510// there is one unit and one "jump" delta.511// If the input unit is less than the sub-node unit, then "jump" by delta to512// the next sub-node which will have a length of length/2.513// (The delta has its own compact encoding.)514// Otherwise, skip the "jump" delta to the next sub-node515// which will have a length of length-length/2.516517// Match-node lead unit values, after masking off intermediate-value bits:518519// 0000..002f: Branch node. If node!=0 then the length is node+1, otherwise520// the length is one more than the next unit.521522// For a branch sub-node with at most this many entries, we drop down523// to a linear search.524static const int32_t kMaxBranchLinearSubNodeLength=5;525526// 0030..003f: Linear-match node, match 1..16 units and continue reading the next node.527static const int32_t kMinLinearMatch=0x30;528static const int32_t kMaxLinearMatchLength=0x10;529530// Match-node lead unit bits 14..6 for the optional intermediate value.531// If these bits are 0, then there is no intermediate value.532// Otherwise, see the *NodeValue* constants below.533static const int32_t kMinValueLead=kMinLinearMatch+kMaxLinearMatchLength; // 0x0040534static const int32_t kNodeTypeMask=kMinValueLead-1; // 0x003f535536// A final-value node has bit 15 set.537static const int32_t kValueIsFinal=0x8000;538539// Compact value: After testing and masking off bit 15, use the following thresholds.540static const int32_t kMaxOneUnitValue=0x3fff;541542static const int32_t kMinTwoUnitValueLead=kMaxOneUnitValue+1; // 0x4000543static const int32_t kThreeUnitValueLead=0x7fff;544545static const int32_t kMaxTwoUnitValue=((kThreeUnitValueLead-kMinTwoUnitValueLead)<<16)-1; // 0x3ffeffff546547// Compact intermediate-value integer, lead unit shared with a branch or linear-match node.548static const int32_t kMaxOneUnitNodeValue=0xff;549static const int32_t kMinTwoUnitNodeValueLead=kMinValueLead+((kMaxOneUnitNodeValue+1)<<6); // 0x4040550static const int32_t kThreeUnitNodeValueLead=0x7fc0;551552static const int32_t kMaxTwoUnitNodeValue=553((kThreeUnitNodeValueLead-kMinTwoUnitNodeValueLead)<<10)-1; // 0xfdffff554555// Compact delta integers.556static const int32_t kMaxOneUnitDelta=0xfbff;557static const int32_t kMinTwoUnitDeltaLead=kMaxOneUnitDelta+1; // 0xfc00558static const int32_t kThreeUnitDeltaLead=0xffff;559560static const int32_t kMaxTwoUnitDelta=((kThreeUnitDeltaLead-kMinTwoUnitDeltaLead)<<16)-1; // 0x03feffff561562char16_t *ownedArray_;563564// Fixed value referencing the UCharsTrie words.565const char16_t *uchars_;566567// Iterator variables.568569// Pointer to next trie unit to read. NULL if no more matches.570const char16_t *pos_;571// Remaining length of a linear-match node, minus 1. Negative if not in such a node.572int32_t remainingMatchLength_;573};574575U_NAMESPACE_END576577#endif // __UCHARSTRIE_H__578579580