jpayne@69: // © 2017 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: jpayne@69: // umutablecptrie.h (split out of ucptrie.h) jpayne@69: // created: 2018jan24 Markus W. Scherer jpayne@69: jpayne@69: #ifndef __UMUTABLECPTRIE_H__ jpayne@69: #define __UMUTABLECPTRIE_H__ jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #include "unicode/localpointer.h" jpayne@69: #include "unicode/ucpmap.h" jpayne@69: #include "unicode/ucptrie.h" jpayne@69: #include "unicode/utf8.h" jpayne@69: jpayne@69: U_CDECL_BEGIN jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * jpayne@69: * This file defines a mutable Unicode code point trie. jpayne@69: * jpayne@69: * @see UCPTrie jpayne@69: * @see UMutableCPTrie jpayne@69: */ jpayne@69: jpayne@69: /** jpayne@69: * Mutable Unicode code point trie. jpayne@69: * Fast map from Unicode code points (U+0000..U+10FFFF) to 32-bit integer values. jpayne@69: * For details see http://site.icu-project.org/design/struct/utrie jpayne@69: * jpayne@69: * Setting values (especially ranges) and lookup is fast. jpayne@69: * The mutable trie is only somewhat space-efficient. jpayne@69: * It builds a compacted, immutable UCPTrie. jpayne@69: * jpayne@69: * This trie can be modified while iterating over its contents. jpayne@69: * For example, it is possible to merge its values with those from another jpayne@69: * set of ranges (e.g., another mutable or immutable trie): jpayne@69: * Iterate over those source ranges; for each of them iterate over this trie; jpayne@69: * add the source value into the value of each trie range. jpayne@69: * jpayne@69: * @see UCPTrie jpayne@69: * @see umutablecptrie_buildImmutable jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: typedef struct UMutableCPTrie UMutableCPTrie; jpayne@69: jpayne@69: /** jpayne@69: * Creates a mutable trie that initially maps each Unicode code point to the same value. jpayne@69: * It uses 32-bit data values until umutablecptrie_buildImmutable() is called. jpayne@69: * umutablecptrie_buildImmutable() takes a valueWidth parameter which jpayne@69: * determines the number of bits in the data value in the resulting UCPTrie. jpayne@69: * You must umutablecptrie_close() the trie once you are done using it. jpayne@69: * jpayne@69: * @param initialValue the initial value that is set for all code points jpayne@69: * @param errorValue the value for out-of-range code points and ill-formed UTF-8/16 jpayne@69: * @param pErrorCode an in/out ICU UErrorCode jpayne@69: * @return the trie jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: U_CAPI UMutableCPTrie * U_EXPORT2 jpayne@69: umutablecptrie_open(uint32_t initialValue, uint32_t errorValue, UErrorCode *pErrorCode); jpayne@69: jpayne@69: /** jpayne@69: * Clones a mutable trie. jpayne@69: * You must umutablecptrie_close() the clone once you are done using it. jpayne@69: * jpayne@69: * @param other the trie to clone jpayne@69: * @param pErrorCode an in/out ICU UErrorCode jpayne@69: * @return the trie clone jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: U_CAPI UMutableCPTrie * U_EXPORT2 jpayne@69: umutablecptrie_clone(const UMutableCPTrie *other, UErrorCode *pErrorCode); jpayne@69: jpayne@69: /** jpayne@69: * Closes a mutable trie and releases associated memory. jpayne@69: * jpayne@69: * @param trie the trie jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: U_CAPI void U_EXPORT2 jpayne@69: umutablecptrie_close(UMutableCPTrie *trie); jpayne@69: jpayne@69: /** jpayne@69: * Creates a mutable trie with the same contents as the UCPMap. jpayne@69: * You must umutablecptrie_close() the mutable trie once you are done using it. jpayne@69: * jpayne@69: * @param map the source map jpayne@69: * @param pErrorCode an in/out ICU UErrorCode jpayne@69: * @return the mutable trie jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: U_CAPI UMutableCPTrie * U_EXPORT2 jpayne@69: umutablecptrie_fromUCPMap(const UCPMap *map, UErrorCode *pErrorCode); jpayne@69: jpayne@69: /** jpayne@69: * Creates a mutable trie with the same contents as the immutable one. jpayne@69: * You must umutablecptrie_close() the mutable trie once you are done using it. jpayne@69: * jpayne@69: * @param trie the immutable trie jpayne@69: * @param pErrorCode an in/out ICU UErrorCode jpayne@69: * @return the mutable trie jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: U_CAPI UMutableCPTrie * U_EXPORT2 jpayne@69: umutablecptrie_fromUCPTrie(const UCPTrie *trie, UErrorCode *pErrorCode); jpayne@69: jpayne@69: /** jpayne@69: * Returns the value for a code point as stored in the trie. jpayne@69: * jpayne@69: * @param trie the trie jpayne@69: * @param c the code point jpayne@69: * @return the value jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: U_CAPI uint32_t U_EXPORT2 jpayne@69: umutablecptrie_get(const UMutableCPTrie *trie, UChar32 c); jpayne@69: jpayne@69: /** jpayne@69: * Returns the last code point such that all those from start to there have the same value. jpayne@69: * Can be used to efficiently iterate over all same-value ranges in a trie. jpayne@69: * (This is normally faster than iterating over code points and get()ting each value, jpayne@69: * but much slower than a data structure that stores ranges directly.) jpayne@69: * jpayne@69: * The trie can be modified between calls to this function. jpayne@69: * jpayne@69: * If the UCPMapValueFilter function pointer is not NULL, then jpayne@69: * the value to be delivered is passed through that function, and the return value is the end jpayne@69: * of the range where all values are modified to the same actual value. jpayne@69: * The value is unchanged if that function pointer is NULL. jpayne@69: * jpayne@69: * See the same-signature ucptrie_getRange() for a code sample. jpayne@69: * jpayne@69: * @param trie the trie jpayne@69: * @param start range start jpayne@69: * @param option defines whether surrogates are treated normally, jpayne@69: * or as having the surrogateValue; usually UCPMAP_RANGE_NORMAL jpayne@69: * @param surrogateValue value for surrogates; ignored if option==UCPMAP_RANGE_NORMAL jpayne@69: * @param filter a pointer to a function that may modify the trie data value, jpayne@69: * or NULL if the values from the trie are to be used unmodified jpayne@69: * @param context an opaque pointer that is passed on to the filter function jpayne@69: * @param pValue if not NULL, receives the value that every code point start..end has; jpayne@69: * may have been modified by filter(context, trie value) jpayne@69: * if that function pointer is not NULL jpayne@69: * @return the range end code point, or -1 if start is not a valid code point jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: U_CAPI UChar32 U_EXPORT2 jpayne@69: umutablecptrie_getRange(const UMutableCPTrie *trie, UChar32 start, jpayne@69: UCPMapRangeOption option, uint32_t surrogateValue, jpayne@69: UCPMapValueFilter *filter, const void *context, uint32_t *pValue); jpayne@69: jpayne@69: /** jpayne@69: * Sets a value for a code point. jpayne@69: * jpayne@69: * @param trie the trie jpayne@69: * @param c the code point jpayne@69: * @param value the value jpayne@69: * @param pErrorCode an in/out ICU UErrorCode jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: U_CAPI void U_EXPORT2 jpayne@69: umutablecptrie_set(UMutableCPTrie *trie, UChar32 c, uint32_t value, UErrorCode *pErrorCode); jpayne@69: jpayne@69: /** jpayne@69: * Sets a value for each code point [start..end]. jpayne@69: * Faster and more space-efficient than setting the value for each code point separately. jpayne@69: * jpayne@69: * @param trie the trie jpayne@69: * @param start the first code point to get the value jpayne@69: * @param end the last code point to get the value (inclusive) jpayne@69: * @param value the value jpayne@69: * @param pErrorCode an in/out ICU UErrorCode jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: U_CAPI void U_EXPORT2 jpayne@69: umutablecptrie_setRange(UMutableCPTrie *trie, jpayne@69: UChar32 start, UChar32 end, jpayne@69: uint32_t value, UErrorCode *pErrorCode); jpayne@69: jpayne@69: /** jpayne@69: * Compacts the data and builds an immutable UCPTrie according to the parameters. jpayne@69: * After this, the mutable trie will be empty. jpayne@69: * jpayne@69: * The mutable trie stores 32-bit values until buildImmutable() is called. jpayne@69: * If values shorter than 32 bits are to be stored in the immutable trie, jpayne@69: * then the upper bits are discarded. jpayne@69: * For example, when the mutable trie contains values 0x81, -0x7f, and 0xa581, jpayne@69: * and the value width is 8 bits, then each of these is stored as 0x81 jpayne@69: * and the immutable trie will return that as an unsigned value. jpayne@69: * (Some implementations may want to make productive temporary use of the upper bits jpayne@69: * until buildImmutable() discards them.) jpayne@69: * jpayne@69: * Not every possible set of mappings can be built into a UCPTrie, jpayne@69: * because of limitations resulting from speed and space optimizations. jpayne@69: * Every Unicode assigned character can be mapped to a unique value. jpayne@69: * Typical data yields data structures far smaller than the limitations. jpayne@69: * jpayne@69: * It is possible to construct extremely unusual mappings that exceed the data structure limits. jpayne@69: * In such a case this function will fail with a U_INDEX_OUTOFBOUNDS_ERROR. jpayne@69: * jpayne@69: * @param trie the trie trie jpayne@69: * @param type selects the trie type jpayne@69: * @param valueWidth selects the number of bits in a trie data value; if smaller than 32 bits, jpayne@69: * then the values stored in the trie will be truncated first jpayne@69: * @param pErrorCode an in/out ICU UErrorCode jpayne@69: * jpayne@69: * @see umutablecptrie_fromUCPTrie jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: U_CAPI UCPTrie * U_EXPORT2 jpayne@69: umutablecptrie_buildImmutable(UMutableCPTrie *trie, UCPTrieType type, UCPTrieValueWidth valueWidth, jpayne@69: UErrorCode *pErrorCode); jpayne@69: jpayne@69: U_CDECL_END jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: /** jpayne@69: * \class LocalUMutableCPTriePointer jpayne@69: * "Smart pointer" class, closes a UMutableCPTrie via umutablecptrie_close(). jpayne@69: * For most methods see the LocalPointerBase base class. jpayne@69: * jpayne@69: * @see LocalPointerBase jpayne@69: * @see LocalPointer jpayne@69: * @stable ICU 63 jpayne@69: */ jpayne@69: U_DEFINE_LOCAL_OPEN_POINTER(LocalUMutableCPTriePointer, UMutableCPTrie, umutablecptrie_close); jpayne@69: jpayne@69: U_NAMESPACE_END jpayne@69: jpayne@69: #endif jpayne@69: jpayne@69: #endif