1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 // umutablecptrie.h (split out of ucptrie.h)
5 // created: 2018jan24 Markus W. Scherer
7 #ifndef __UMUTABLECPTRIE_H__
8 #define __UMUTABLECPTRIE_H__
10 #include "unicode/utypes.h"
12 #include "unicode/ucpmap.h"
13 #include "unicode/ucptrie.h"
14 #include "unicode/utf8.h"
16 #if U_SHOW_CPLUSPLUS_API
17 #include "unicode/localpointer.h"
18 #endif // U_SHOW_CPLUSPLUS_API
25 * This file defines a mutable Unicode code point trie.
32 * Mutable Unicode code point trie.
33 * Fast map from Unicode code points (U+0000..U+10FFFF) to 32-bit integer values.
34 * For details see https://icu.unicode.org/design/struct/utrie
36 * Setting values (especially ranges) and lookup is fast.
37 * The mutable trie is only somewhat space-efficient.
38 * It builds a compacted, immutable UCPTrie.
40 * This trie can be modified while iterating over its contents.
41 * For example, it is possible to merge its values with those from another
42 * set of ranges (e.g., another mutable or immutable trie):
43 * Iterate over those source ranges; for each of them iterate over this trie;
44 * add the source value into the value of each trie range.
47 * @see umutablecptrie_buildImmutable
50 typedef struct UMutableCPTrie UMutableCPTrie;
53 * Creates a mutable trie that initially maps each Unicode code point to the same value.
54 * It uses 32-bit data values until umutablecptrie_buildImmutable() is called.
55 * umutablecptrie_buildImmutable() takes a valueWidth parameter which
56 * determines the number of bits in the data value in the resulting UCPTrie.
57 * You must umutablecptrie_close() the trie once you are done using it.
59 * @param initialValue the initial value that is set for all code points
60 * @param errorValue the value for out-of-range code points and ill-formed UTF-8/16
61 * @param pErrorCode an in/out ICU UErrorCode
65 U_CAPI UMutableCPTrie * U_EXPORT2
66 umutablecptrie_open(uint32_t initialValue, uint32_t errorValue, UErrorCode *pErrorCode);
69 * Clones a mutable trie.
70 * You must umutablecptrie_close() the clone once you are done using it.
72 * @param other the trie to clone
73 * @param pErrorCode an in/out ICU UErrorCode
74 * @return the trie clone
77 U_CAPI UMutableCPTrie * U_EXPORT2
78 umutablecptrie_clone(const UMutableCPTrie *other, UErrorCode *pErrorCode);
81 * Closes a mutable trie and releases associated memory.
83 * @param trie the trie
87 umutablecptrie_close(UMutableCPTrie *trie);
90 * Creates a mutable trie with the same contents as the UCPMap.
91 * You must umutablecptrie_close() the mutable trie once you are done using it.
93 * @param map the source map
94 * @param pErrorCode an in/out ICU UErrorCode
95 * @return the mutable trie
98 U_CAPI UMutableCPTrie * U_EXPORT2
99 umutablecptrie_fromUCPMap(const UCPMap *map, UErrorCode *pErrorCode);
102 * Creates a mutable trie with the same contents as the immutable one.
103 * You must umutablecptrie_close() the mutable trie once you are done using it.
105 * @param trie the immutable trie
106 * @param pErrorCode an in/out ICU UErrorCode
107 * @return the mutable trie
110 U_CAPI UMutableCPTrie * U_EXPORT2
111 umutablecptrie_fromUCPTrie(const UCPTrie *trie, UErrorCode *pErrorCode);
114 * Returns the value for a code point as stored in the trie.
116 * @param trie the trie
117 * @param c the code point
121 U_CAPI uint32_t U_EXPORT2
122 umutablecptrie_get(const UMutableCPTrie *trie, UChar32 c);
125 * Returns the last code point such that all those from start to there have the same value.
126 * Can be used to efficiently iterate over all same-value ranges in a trie.
127 * (This is normally faster than iterating over code points and get()ting each value,
128 * but much slower than a data structure that stores ranges directly.)
130 * The trie can be modified between calls to this function.
132 * If the UCPMapValueFilter function pointer is not NULL, then
133 * the value to be delivered is passed through that function, and the return value is the end
134 * of the range where all values are modified to the same actual value.
135 * The value is unchanged if that function pointer is NULL.
137 * See the same-signature ucptrie_getRange() for a code sample.
139 * @param trie the trie
140 * @param start range start
141 * @param option defines whether surrogates are treated normally,
142 * or as having the surrogateValue; usually UCPMAP_RANGE_NORMAL
143 * @param surrogateValue value for surrogates; ignored if option==UCPMAP_RANGE_NORMAL
144 * @param filter a pointer to a function that may modify the trie data value,
145 * or NULL if the values from the trie are to be used unmodified
146 * @param context an opaque pointer that is passed on to the filter function
147 * @param pValue if not NULL, receives the value that every code point start..end has;
148 * may have been modified by filter(context, trie value)
149 * if that function pointer is not NULL
150 * @return the range end code point, or -1 if start is not a valid code point
153 U_CAPI UChar32 U_EXPORT2
154 umutablecptrie_getRange(const UMutableCPTrie *trie, UChar32 start,
155 UCPMapRangeOption option, uint32_t surrogateValue,
156 UCPMapValueFilter *filter, const void *context, uint32_t *pValue);
159 * Sets a value for a code point.
161 * @param trie the trie
162 * @param c the code point
163 * @param value the value
164 * @param pErrorCode an in/out ICU UErrorCode
167 U_CAPI void U_EXPORT2
168 umutablecptrie_set(UMutableCPTrie *trie, UChar32 c, uint32_t value, UErrorCode *pErrorCode);
171 * Sets a value for each code point [start..end].
172 * Faster and more space-efficient than setting the value for each code point separately.
174 * @param trie the trie
175 * @param start the first code point to get the value
176 * @param end the last code point to get the value (inclusive)
177 * @param value the value
178 * @param pErrorCode an in/out ICU UErrorCode
181 U_CAPI void U_EXPORT2
182 umutablecptrie_setRange(UMutableCPTrie *trie,
183 UChar32 start, UChar32 end,
184 uint32_t value, UErrorCode *pErrorCode);
187 * Compacts the data and builds an immutable UCPTrie according to the parameters.
188 * After this, the mutable trie will be empty.
190 * The mutable trie stores 32-bit values until buildImmutable() is called.
191 * If values shorter than 32 bits are to be stored in the immutable trie,
192 * then the upper bits are discarded.
193 * For example, when the mutable trie contains values 0x81, -0x7f, and 0xa581,
194 * and the value width is 8 bits, then each of these is stored as 0x81
195 * and the immutable trie will return that as an unsigned value.
196 * (Some implementations may want to make productive temporary use of the upper bits
197 * until buildImmutable() discards them.)
199 * Not every possible set of mappings can be built into a UCPTrie,
200 * because of limitations resulting from speed and space optimizations.
201 * Every Unicode assigned character can be mapped to a unique value.
202 * Typical data yields data structures far smaller than the limitations.
204 * It is possible to construct extremely unusual mappings that exceed the data structure limits.
205 * In such a case this function will fail with a U_INDEX_OUTOFBOUNDS_ERROR.
207 * @param trie the trie trie
208 * @param type selects the trie type
209 * @param valueWidth selects the number of bits in a trie data value; if smaller than 32 bits,
210 * then the values stored in the trie will be truncated first
211 * @param pErrorCode an in/out ICU UErrorCode
213 * @see umutablecptrie_fromUCPTrie
216 U_CAPI UCPTrie * U_EXPORT2
217 umutablecptrie_buildImmutable(UMutableCPTrie *trie, UCPTrieType type, UCPTrieValueWidth valueWidth,
218 UErrorCode *pErrorCode);
222 #if U_SHOW_CPLUSPLUS_API
227 * \class LocalUMutableCPTriePointer
228 * "Smart pointer" class, closes a UMutableCPTrie via umutablecptrie_close().
229 * For most methods see the LocalPointerBase base class.
231 * @see LocalPointerBase
235 U_DEFINE_LOCAL_OPEN_POINTER(LocalUMutableCPTriePointer, UMutableCPTrie, umutablecptrie_close);