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-2016, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 ******************************************************************************* 8 * file name: ucharstriebuilder.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 __UCHARSTRIEBUILDER_H__ 18 #define __UCHARSTRIEBUILDER_H__ 19 20 #include "unicode/utypes.h" 21 22 #if U_SHOW_CPLUSPLUS_API 23 24 #include "unicode/stringtriebuilder.h" 25 #include "unicode/ucharstrie.h" 26 #include "unicode/unistr.h" 27 28 /** 29 * \file 30 * \brief C++ API: Builder for icu::UCharsTrie 31 */ 32 33 U_NAMESPACE_BEGIN 34 35 class UCharsTrieElement; 36 37 /** 38 * Builder class for UCharsTrie. 39 * 40 * This class is not intended for public subclassing. 41 * @stable ICU 4.8 42 */ 43 class U_COMMON_API UCharsTrieBuilder : public StringTrieBuilder { 44 public: 45 /** 46 * Constructs an empty builder. 47 * @param errorCode Standard ICU error code. 48 * @stable ICU 4.8 49 */ 50 UCharsTrieBuilder(UErrorCode &errorCode); 51 52 /** 53 * Destructor. 54 * @stable ICU 4.8 55 */ 56 virtual ~UCharsTrieBuilder(); 57 58 /** 59 * Adds a (string, value) pair. 60 * The string must be unique. 61 * The string contents will be copied; the builder does not keep 62 * a reference to the input UnicodeString or its buffer. 63 * @param s The input string. 64 * @param value The value associated with this string. 65 * @param errorCode Standard ICU error code. Its input value must 66 * pass the U_SUCCESS() test, or else the function returns 67 * immediately. Check for U_FAILURE() on output or use with 68 * function chaining. (See User Guide for details.) 69 * @return *this 70 * @stable ICU 4.8 71 */ 72 UCharsTrieBuilder &add(const UnicodeString &s, int32_t value, UErrorCode &errorCode); 73 74 /** 75 * Builds a UCharsTrie for the add()ed data. 76 * Once built, no further data can be add()ed until clear() is called. 77 * 78 * A UCharsTrie cannot be empty. At least one (string, value) pair 79 * must have been add()ed. 80 * 81 * This method passes ownership of the builder's internal result array to the new trie object. 82 * Another call to any build() variant will re-serialize the trie. 83 * After clear() has been called, a new array will be used as well. 84 * @param buildOption Build option, see UStringTrieBuildOption. 85 * @param errorCode Standard ICU error code. Its input value must 86 * pass the U_SUCCESS() test, or else the function returns 87 * immediately. Check for U_FAILURE() on output or use with 88 * function chaining. (See User Guide for details.) 89 * @return A new UCharsTrie for the add()ed data. 90 * @stable ICU 4.8 91 */ 92 UCharsTrie *build(UStringTrieBuildOption buildOption, UErrorCode &errorCode); 93 94 /** 95 * Builds a UCharsTrie for the add()ed data and char16_t-serializes it. 96 * Once built, no further data can be add()ed until clear() is called. 97 * 98 * A UCharsTrie cannot be empty. At least one (string, value) pair 99 * must have been add()ed. 100 * 101 * Multiple calls to buildUnicodeString() set the UnicodeStrings to the 102 * builder's same char16_t array, without rebuilding. 103 * If buildUnicodeString() is called after build(), the trie will be 104 * re-serialized into a new array. 105 * If build() is called after buildUnicodeString(), the trie object will become 106 * the owner of the previously returned array. 107 * After clear() has been called, a new array will be used as well. 108 * @param buildOption Build option, see UStringTrieBuildOption. 109 * @param result A UnicodeString which will be set to the char16_t-serialized 110 * UCharsTrie for the add()ed data. 111 * @param errorCode Standard ICU error code. Its input value must 112 * pass the U_SUCCESS() test, or else the function returns 113 * immediately. Check for U_FAILURE() on output or use with 114 * function chaining. (See User Guide for details.) 115 * @return result 116 * @stable ICU 4.8 117 */ 118 UnicodeString &buildUnicodeString(UStringTrieBuildOption buildOption, UnicodeString &result, 119 UErrorCode &errorCode); 120 121 /** 122 * Removes all (string, value) pairs. 123 * New data can then be add()ed and a new trie can be built. 124 * @return *this 125 * @stable ICU 4.8 126 */ clear()127 UCharsTrieBuilder &clear() { 128 strings.remove(); 129 elementsLength=0; 130 ucharsLength=0; 131 return *this; 132 } 133 134 private: 135 UCharsTrieBuilder(const UCharsTrieBuilder &other); // no copy constructor 136 UCharsTrieBuilder &operator=(const UCharsTrieBuilder &other); // no assignment operator 137 138 void buildUChars(UStringTrieBuildOption buildOption, UErrorCode &errorCode); 139 140 virtual int32_t getElementStringLength(int32_t i) const; 141 virtual char16_t getElementUnit(int32_t i, int32_t unitIndex) const; 142 virtual int32_t getElementValue(int32_t i) const; 143 144 virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const; 145 146 virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const; 147 virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const; 148 virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, char16_t unit) const; 149 matchNodesCanHaveValues()150 virtual UBool matchNodesCanHaveValues() const { return TRUE; } 151 getMaxBranchLinearSubNodeLength()152 virtual int32_t getMaxBranchLinearSubNodeLength() const { return UCharsTrie::kMaxBranchLinearSubNodeLength; } getMinLinearMatch()153 virtual int32_t getMinLinearMatch() const { return UCharsTrie::kMinLinearMatch; } getMaxLinearMatchLength()154 virtual int32_t getMaxLinearMatchLength() const { return UCharsTrie::kMaxLinearMatchLength; } 155 156 class UCTLinearMatchNode : public LinearMatchNode { 157 public: 158 UCTLinearMatchNode(const char16_t *units, int32_t len, Node *nextNode); 159 virtual UBool operator==(const Node &other) const; 160 virtual void write(StringTrieBuilder &builder); 161 private: 162 const char16_t *s; 163 }; 164 165 virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length, 166 Node *nextNode) const; 167 168 UBool ensureCapacity(int32_t length); 169 virtual int32_t write(int32_t unit); 170 int32_t write(const char16_t *s, int32_t length); 171 virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length); 172 virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal); 173 virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node); 174 virtual int32_t writeDeltaTo(int32_t jumpTarget); 175 176 UnicodeString strings; 177 UCharsTrieElement *elements; 178 int32_t elementsCapacity; 179 int32_t elementsLength; 180 181 // char16_t serialization of the trie. 182 // Grows from the back: ucharsLength measures from the end of the buffer! 183 char16_t *uchars; 184 int32_t ucharsCapacity; 185 int32_t ucharsLength; 186 }; 187 188 U_NAMESPACE_END 189 190 #endif /* U_SHOW_CPLUSPLUS_API */ 191 192 #endif // __UCHARSTRIEBUILDER_H__ 193