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