1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 // umutablecptrie.h (split out of ucptrie.h)
5 // created: 2018jan24 Markus W. Scherer
6 
7 #ifndef __UMUTABLECPTRIE_H__
8 #define __UMUTABLECPTRIE_H__
9 
10 #include "unicode/utypes.h"
11 
12 #include "unicode/ucpmap.h"
13 #include "unicode/ucptrie.h"
14 #include "unicode/utf8.h"
15 
16 #if U_SHOW_CPLUSPLUS_API
17 #include "unicode/localpointer.h"
18 #endif   // U_SHOW_CPLUSPLUS_API
19 
20 U_CDECL_BEGIN
21 
22 /**
23  * \file
24  *
25  * This file defines a mutable Unicode code point trie.
26  *
27  * @see UCPTrie
28  * @see UMutableCPTrie
29  */
30 
31 /**
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 http://site.icu-project.org/design/struct/utrie
35  *
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.
39  *
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.
45  *
46  * @see UCPTrie
47  * @see umutablecptrie_buildImmutable
48  * @stable ICU 63
49  */
50 typedef struct UMutableCPTrie UMutableCPTrie;
51 
52 /**
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.
58  *
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
62  * @return the trie
63  * @stable ICU 63
64  */
65 U_CAPI UMutableCPTrie * U_EXPORT2
66 umutablecptrie_open(uint32_t initialValue, uint32_t errorValue, UErrorCode *pErrorCode);
67 
68 /**
69  * Clones a mutable trie.
70  * You must umutablecptrie_close() the clone once you are done using it.
71  *
72  * @param other the trie to clone
73  * @param pErrorCode an in/out ICU UErrorCode
74  * @return the trie clone
75  * @stable ICU 63
76  */
77 U_CAPI UMutableCPTrie * U_EXPORT2
78 umutablecptrie_clone(const UMutableCPTrie *other, UErrorCode *pErrorCode);
79 
80 /**
81  * Closes a mutable trie and releases associated memory.
82  *
83  * @param trie the trie
84  * @stable ICU 63
85  */
86 U_CAPI void U_EXPORT2
87 umutablecptrie_close(UMutableCPTrie *trie);
88 
89 /**
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.
92  *
93  * @param map the source map
94  * @param pErrorCode an in/out ICU UErrorCode
95  * @return the mutable trie
96  * @stable ICU 63
97  */
98 U_CAPI UMutableCPTrie * U_EXPORT2
99 umutablecptrie_fromUCPMap(const UCPMap *map, UErrorCode *pErrorCode);
100 
101 /**
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.
104  *
105  * @param trie the immutable trie
106  * @param pErrorCode an in/out ICU UErrorCode
107  * @return the mutable trie
108  * @stable ICU 63
109  */
110 U_CAPI UMutableCPTrie * U_EXPORT2
111 umutablecptrie_fromUCPTrie(const UCPTrie *trie, UErrorCode *pErrorCode);
112 
113 /**
114  * Returns the value for a code point as stored in the trie.
115  *
116  * @param trie the trie
117  * @param c the code point
118  * @return the value
119  * @stable ICU 63
120  */
121 U_CAPI uint32_t U_EXPORT2
122 umutablecptrie_get(const UMutableCPTrie *trie, UChar32 c);
123 
124 /**
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.)
129  *
130  * The trie can be modified between calls to this function.
131  *
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.
136  *
137  * See the same-signature ucptrie_getRange() for a code sample.
138  *
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
151  * @stable ICU 63
152  */
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);
157 
158 /**
159  * Sets a value for a code point.
160  *
161  * @param trie the trie
162  * @param c the code point
163  * @param value the value
164  * @param pErrorCode an in/out ICU UErrorCode
165  * @stable ICU 63
166  */
167 U_CAPI void U_EXPORT2
168 umutablecptrie_set(UMutableCPTrie *trie, UChar32 c, uint32_t value, UErrorCode *pErrorCode);
169 
170 /**
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.
173  *
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
179  * @stable ICU 63
180  */
181 U_CAPI void U_EXPORT2
182 umutablecptrie_setRange(UMutableCPTrie *trie,
183                         UChar32 start, UChar32 end,
184                         uint32_t value, UErrorCode *pErrorCode);
185 
186 /**
187  * Compacts the data and builds an immutable UCPTrie according to the parameters.
188  * After this, the mutable trie will be empty.
189  *
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.)
198  *
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.
203  *
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.
206  *
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
212  *
213  * @see umutablecptrie_fromUCPTrie
214  * @stable ICU 63
215  */
216 U_CAPI UCPTrie * U_EXPORT2
217 umutablecptrie_buildImmutable(UMutableCPTrie *trie, UCPTrieType type, UCPTrieValueWidth valueWidth,
218                               UErrorCode *pErrorCode);
219 
220 U_CDECL_END
221 
222 #if U_SHOW_CPLUSPLUS_API
223 
224 U_NAMESPACE_BEGIN
225 
226 /**
227  * \class LocalUMutableCPTriePointer
228  * "Smart pointer" class, closes a UMutableCPTrie via umutablecptrie_close().
229  * For most methods see the LocalPointerBase base class.
230  *
231  * @see LocalPointerBase
232  * @see LocalPointer
233  * @stable ICU 63
234  */
235 U_DEFINE_LOCAL_OPEN_POINTER(LocalUMutableCPTriePointer, UMutableCPTrie, umutablecptrie_close);
236 
237 U_NAMESPACE_END
238 
239 #endif
240 
241 #endif
242