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