umutablecptrie.h 8.3 KB

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