Home | History | Annotate | Download | only in unicode
      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 #ifndef U_HIDE_DRAFT_API
     13 
     14 #include "unicode/localpointer.h"
     15 #include "unicode/ucpmap.h"
     16 #include "unicode/ucptrie.h"
     17 #include "unicode/utf8.h"
     18 
     19 U_CDECL_BEGIN
     20 
     21 /**
     22  * \file
     23  *
     24  * 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 http://site.icu-project.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  * @draft 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  * @draft 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  * @draft 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  * @draft ICU 63
     84  */
     85 U_CAPI void U_EXPORT2
     86 umutablecptrie_close(UMutableCPTrie *trie);
     87 
     88 #if U_SHOW_CPLUSPLUS_API
     89 
     90 U_NAMESPACE_BEGIN
     91 
     92 /**
     93  * \class LocalUMutableCPTriePointer
     94  * "Smart pointer" class, closes a UMutableCPTrie via umutablecptrie_close().
     95  * For most methods see the LocalPointerBase base class.
     96  *
     97  * @see LocalPointerBase
     98  * @see LocalPointer
     99  * @draft ICU 63
    100  */
    101 U_DEFINE_LOCAL_OPEN_POINTER(LocalUMutableCPTriePointer, UMutableCPTrie, umutablecptrie_close);
    102 
    103 U_NAMESPACE_END
    104 
    105 #endif
    106 
    107 /**
    108  * Creates a mutable trie with the same contents as the UCPMap.
    109  * You must umutablecptrie_close() the mutable trie once you are done using it.
    110  *
    111  * @param map the source map
    112  * @param pErrorCode an in/out ICU UErrorCode
    113  * @return the mutable trie
    114  * @draft ICU 63
    115  */
    116 U_CAPI UMutableCPTrie * U_EXPORT2
    117 umutablecptrie_fromUCPMap(const UCPMap *map, UErrorCode *pErrorCode);
    118 
    119 /**
    120  * Creates a mutable trie with the same contents as the immutable one.
    121  * You must umutablecptrie_close() the mutable trie once you are done using it.
    122  *
    123  * @param trie the immutable trie
    124  * @param pErrorCode an in/out ICU UErrorCode
    125  * @return the mutable trie
    126  * @draft ICU 63
    127  */
    128 U_CAPI UMutableCPTrie * U_EXPORT2
    129 umutablecptrie_fromUCPTrie(const UCPTrie *trie, UErrorCode *pErrorCode);
    130 
    131 /**
    132  * Returns the value for a code point as stored in the trie.
    133  *
    134  * @param trie the trie
    135  * @param c the code point
    136  * @return the value
    137  * @draft ICU 63
    138  */
    139 U_CAPI uint32_t U_EXPORT2
    140 umutablecptrie_get(const UMutableCPTrie *trie, UChar32 c);
    141 
    142 /**
    143  * Returns the last code point such that all those from start to there have the same value.
    144  * Can be used to efficiently iterate over all same-value ranges in a trie.
    145  * (This is normally faster than iterating over code points and get()ting each value,
    146  * but much slower than a data structure that stores ranges directly.)
    147  *
    148  * The trie can be modified between calls to this function.
    149  *
    150  * If the UCPMapValueFilter function pointer is not NULL, then
    151  * the value to be delivered is passed through that function, and the return value is the end
    152  * of the range where all values are modified to the same actual value.
    153  * The value is unchanged if that function pointer is NULL.
    154  *
    155  * See the same-signature ucptrie_getRange() for a code sample.
    156  *
    157  * @param trie the trie
    158  * @param start range start
    159  * @param option defines whether surrogates are treated normally,
    160  *               or as having the surrogateValue; usually UCPMAP_RANGE_NORMAL
    161  * @param surrogateValue value for surrogates; ignored if option==UCPMAP_RANGE_NORMAL
    162  * @param filter a pointer to a function that may modify the trie data value,
    163  *     or NULL if the values from the trie are to be used unmodified
    164  * @param context an opaque pointer that is passed on to the filter function
    165  * @param pValue if not NULL, receives the value that every code point start..end has;
    166  *     may have been modified by filter(context, trie value)
    167  *     if that function pointer is not NULL
    168  * @return the range end code point, or -1 if start is not a valid code point
    169  * @draft ICU 63
    170  */
    171 U_CAPI UChar32 U_EXPORT2
    172 umutablecptrie_getRange(const UMutableCPTrie *trie, UChar32 start,
    173                         UCPMapRangeOption option, uint32_t surrogateValue,
    174                         UCPMapValueFilter *filter, const void *context, uint32_t *pValue);
    175 
    176 /**
    177  * Sets a value for a code point.
    178  *
    179  * @param trie the trie
    180  * @param c the code point
    181  * @param value the value
    182  * @param pErrorCode an in/out ICU UErrorCode
    183  * @draft ICU 63
    184  */
    185 U_CAPI void U_EXPORT2
    186 umutablecptrie_set(UMutableCPTrie *trie, UChar32 c, uint32_t value, UErrorCode *pErrorCode);
    187 
    188 /**
    189  * Sets a value for each code point [start..end].
    190  * Faster and more space-efficient than setting the value for each code point separately.
    191  *
    192  * @param trie the trie
    193  * @param start the first code point to get the value
    194  * @param end the last code point to get the value (inclusive)
    195  * @param value the value
    196  * @param pErrorCode an in/out ICU UErrorCode
    197  * @draft ICU 63
    198  */
    199 U_CAPI void U_EXPORT2
    200 umutablecptrie_setRange(UMutableCPTrie *trie,
    201                         UChar32 start, UChar32 end,
    202                         uint32_t value, UErrorCode *pErrorCode);
    203 
    204 /**
    205  * Compacts the data and builds an immutable UCPTrie according to the parameters.
    206  * After this, the mutable trie will be empty.
    207  *
    208  * The mutable trie stores 32-bit values until buildImmutable() is called.
    209  * If values shorter than 32 bits are to be stored in the immutable trie,
    210  * then the upper bits are discarded.
    211  * For example, when the mutable trie contains values 0x81, -0x7f, and 0xa581,
    212  * and the value width is 8 bits, then each of these is stored as 0x81
    213  * and the immutable trie will return that as an unsigned value.
    214  * (Some implementations may want to make productive temporary use of the upper bits
    215  * until buildImmutable() discards them.)
    216  *
    217  * Not every possible set of mappings can be built into a UCPTrie,
    218  * because of limitations resulting from speed and space optimizations.
    219  * Every Unicode assigned character can be mapped to a unique value.
    220  * Typical data yields data structures far smaller than the limitations.
    221  *
    222  * It is possible to construct extremely unusual mappings that exceed the data structure limits.
    223  * In such a case this function will fail with a U_INDEX_OUTOFBOUNDS_ERROR.
    224  *
    225  * @param trie the trie trie
    226  * @param type selects the trie type
    227  * @param valueWidth selects the number of bits in a trie data value; if smaller than 32 bits,
    228  *                   then the values stored in the trie will be truncated first
    229  * @param pErrorCode an in/out ICU UErrorCode
    230  *
    231  * @see umutablecptrie_fromUCPTrie
    232  * @draft ICU 63
    233  */
    234 U_CAPI UCPTrie * U_EXPORT2
    235 umutablecptrie_buildImmutable(UMutableCPTrie *trie, UCPTrieType type, UCPTrieValueWidth valueWidth,
    236                               UErrorCode *pErrorCode);
    237 
    238 U_CDECL_END
    239 
    240 #endif  // U_HIDE_DRAFT_API
    241 #endif
    242