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