Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 1 | // © 2016 and later: Unicode, Inc. and others. |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 2 | // License & terms of use: http://www.unicode.org/copyright.html |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 3 | /* |
| 4 | ******************************************************************************* |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 5 | * Copyright (C) 2010-2016, International Business Machines |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 6 | * Corporation and others. All Rights Reserved. |
| 7 | ******************************************************************************* |
| 8 | * file name: ucharstriebuilder.h |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 9 | * encoding: UTF-8 |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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" |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 21 | |
| 22 | #if U_SHOW_CPLUSPLUS_API |
| 23 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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 | /** |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 95 | * Builds a UCharsTrie for the add()ed data and char16_t-serializes it. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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 |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 102 | * builder's same char16_t array, without rebuilding. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 103 | * If buildUnicodeString() is called after build(), the trie will be |
Frank Tang | f90543d | 2020-10-30 19:02:04 -0700 | [diff] [blame] | 104 | * re-serialized into a new array (because build() passes on ownership). |
| 105 | * If build() is called after buildUnicodeString(), the trie object returned |
| 106 | * by build() will become the owner of the underlying data for the |
| 107 | * previously returned UnicodeString. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 108 | * After clear() has been called, a new array will be used as well. |
| 109 | * @param buildOption Build option, see UStringTrieBuildOption. |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 110 | * @param result A UnicodeString which will be set to the char16_t-serialized |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 111 | * UCharsTrie for the add()ed data. |
| 112 | * @param errorCode Standard ICU error code. Its input value must |
| 113 | * pass the U_SUCCESS() test, or else the function returns |
| 114 | * immediately. Check for U_FAILURE() on output or use with |
| 115 | * function chaining. (See User Guide for details.) |
| 116 | * @return result |
| 117 | * @stable ICU 4.8 |
| 118 | */ |
| 119 | UnicodeString &buildUnicodeString(UStringTrieBuildOption buildOption, UnicodeString &result, |
| 120 | UErrorCode &errorCode); |
| 121 | |
| 122 | /** |
| 123 | * Removes all (string, value) pairs. |
| 124 | * New data can then be add()ed and a new trie can be built. |
| 125 | * @return *this |
| 126 | * @stable ICU 4.8 |
| 127 | */ |
| 128 | UCharsTrieBuilder &clear() { |
| 129 | strings.remove(); |
| 130 | elementsLength=0; |
| 131 | ucharsLength=0; |
| 132 | return *this; |
| 133 | } |
| 134 | |
| 135 | private: |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 136 | UCharsTrieBuilder(const UCharsTrieBuilder &other) = delete; // no copy constructor |
| 137 | UCharsTrieBuilder &operator=(const UCharsTrieBuilder &other) = delete; // no assignment operator |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 138 | |
| 139 | void buildUChars(UStringTrieBuildOption buildOption, UErrorCode &errorCode); |
| 140 | |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 141 | virtual int32_t getElementStringLength(int32_t i) const override; |
| 142 | virtual char16_t getElementUnit(int32_t i, int32_t unitIndex) const override; |
| 143 | virtual int32_t getElementValue(int32_t i) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 144 | |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 145 | virtual int32_t getLimitOfLinearMatch(int32_t first, int32_t last, int32_t unitIndex) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 146 | |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 147 | virtual int32_t countElementUnits(int32_t start, int32_t limit, int32_t unitIndex) const override; |
| 148 | virtual int32_t skipElementsBySomeUnits(int32_t i, int32_t unitIndex, int32_t count) const override; |
| 149 | virtual int32_t indexOfElementWithNextUnit(int32_t i, int32_t unitIndex, char16_t unit) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 150 | |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 151 | virtual UBool matchNodesCanHaveValues() const override { return true; } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 152 | |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 153 | virtual int32_t getMaxBranchLinearSubNodeLength() const override { return UCharsTrie::kMaxBranchLinearSubNodeLength; } |
| 154 | virtual int32_t getMinLinearMatch() const override { return UCharsTrie::kMinLinearMatch; } |
| 155 | virtual int32_t getMaxLinearMatchLength() const override { return UCharsTrie::kMaxLinearMatchLength; } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 156 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 157 | class UCTLinearMatchNode : public LinearMatchNode { |
| 158 | public: |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 159 | UCTLinearMatchNode(const char16_t *units, int32_t len, Node *nextNode); |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 160 | virtual bool operator==(const Node &other) const override; |
| 161 | virtual void write(StringTrieBuilder &builder) override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 162 | private: |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 163 | const char16_t *s; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 164 | }; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 165 | |
| 166 | virtual Node *createLinearMatchNode(int32_t i, int32_t unitIndex, int32_t length, |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 167 | Node *nextNode) const override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 168 | |
| 169 | UBool ensureCapacity(int32_t length); |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 170 | virtual int32_t write(int32_t unit) override; |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 171 | int32_t write(const char16_t *s, int32_t length); |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 172 | virtual int32_t writeElementUnits(int32_t i, int32_t unitIndex, int32_t length) override; |
| 173 | virtual int32_t writeValueAndFinal(int32_t i, UBool isFinal) override; |
| 174 | virtual int32_t writeValueAndType(UBool hasValue, int32_t value, int32_t node) override; |
| 175 | virtual int32_t writeDeltaTo(int32_t jumpTarget) override; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 176 | |
| 177 | UnicodeString strings; |
| 178 | UCharsTrieElement *elements; |
| 179 | int32_t elementsCapacity; |
| 180 | int32_t elementsLength; |
| 181 | |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 182 | // char16_t serialization of the trie. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 183 | // Grows from the back: ucharsLength measures from the end of the buffer! |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 184 | char16_t *uchars; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 185 | int32_t ucharsCapacity; |
| 186 | int32_t ucharsLength; |
| 187 | }; |
| 188 | |
| 189 | U_NAMESPACE_END |
| 190 | |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 191 | #endif /* U_SHOW_CPLUSPLUS_API */ |
| 192 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 193 | #endif // __UCHARSTRIEBUILDER_H__ |