umutablecptrie.h 8.2 KB

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