collationbuilder.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2013-2014, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. * collationbuilder.h
  9. *
  10. * created on: 2013may06
  11. * created by: Markus W. Scherer
  12. */
  13. #ifndef __COLLATIONBUILDER_H__
  14. #define __COLLATIONBUILDER_H__
  15. #include "unicode/utypes.h"
  16. #if !UCONFIG_NO_COLLATION
  17. #include "unicode/uniset.h"
  18. #include "unicode/unistr.h"
  19. #include "collationrootelements.h"
  20. #include "collationruleparser.h"
  21. #include "uvectr32.h"
  22. #include "uvectr64.h"
  23. struct UParseError;
  24. U_NAMESPACE_BEGIN
  25. struct CollationData;
  26. struct CollationTailoring;
  27. class CEFinalizer;
  28. class CollationDataBuilder;
  29. class Normalizer2;
  30. class Normalizer2Impl;
  31. class U_I18N_API CollationBuilder : public CollationRuleParser::Sink {
  32. public:
  33. CollationBuilder(const CollationTailoring *base, UErrorCode &errorCode);
  34. virtual ~CollationBuilder();
  35. void disableFastLatin() { fastLatinEnabled = FALSE; }
  36. CollationTailoring *parseAndBuild(const UnicodeString &ruleString,
  37. const UVersionInfo rulesVersion,
  38. CollationRuleParser::Importer *importer,
  39. UParseError *outParseError,
  40. UErrorCode &errorCode);
  41. const char *getErrorReason() const { return errorReason; }
  42. private:
  43. friend class CEFinalizer;
  44. /** Implements CollationRuleParser::Sink. */
  45. virtual void addReset(int32_t strength, const UnicodeString &str,
  46. const char *&errorReason, UErrorCode &errorCode);
  47. /**
  48. * Returns the secondary or tertiary weight preceding the current node's weight.
  49. * node=nodes[index].
  50. */
  51. uint32_t getWeight16Before(int32_t index, int64_t node, int32_t level);
  52. int64_t getSpecialResetPosition(const UnicodeString &str,
  53. const char *&parserErrorReason, UErrorCode &errorCode);
  54. /** Implements CollationRuleParser::Sink. */
  55. virtual void addRelation(int32_t strength, const UnicodeString &prefix,
  56. const UnicodeString &str, const UnicodeString &extension,
  57. const char *&errorReason, UErrorCode &errorCode);
  58. /**
  59. * Picks one of the current CEs and finds or inserts a node in the graph
  60. * for the CE + strength.
  61. */
  62. int32_t findOrInsertNodeForCEs(int32_t strength, const char *&parserErrorReason,
  63. UErrorCode &errorCode);
  64. int32_t findOrInsertNodeForRootCE(int64_t ce, int32_t strength, UErrorCode &errorCode);
  65. /** Finds or inserts the node for a root CE's primary weight. */
  66. int32_t findOrInsertNodeForPrimary(uint32_t p, UErrorCode &errorCode);
  67. /** Finds or inserts the node for a secondary or tertiary weight. */
  68. int32_t findOrInsertWeakNode(int32_t index, uint32_t weight16, int32_t level,
  69. UErrorCode &errorCode);
  70. /**
  71. * Makes and inserts a new tailored node into the list, after the one at index.
  72. * Skips over nodes of weaker strength to maintain collation order
  73. * ("postpone insertion").
  74. * @return the new node's index
  75. */
  76. int32_t insertTailoredNodeAfter(int32_t index, int32_t strength, UErrorCode &errorCode);
  77. /**
  78. * Inserts a new node into the list, between list-adjacent items.
  79. * The node's previous and next indexes must not be set yet.
  80. * @return the new node's index
  81. */
  82. int32_t insertNodeBetween(int32_t index, int32_t nextIndex, int64_t node,
  83. UErrorCode &errorCode);
  84. /**
  85. * Finds the node which implies or contains a common=05 weight of the given strength
  86. * (secondary or tertiary), if the current node is stronger.
  87. * Skips weaker nodes and tailored nodes if the current node is stronger
  88. * and is followed by an explicit-common-weight node.
  89. * Always returns the input index if that node is no stronger than the given strength.
  90. */
  91. int32_t findCommonNode(int32_t index, int32_t strength) const;
  92. void setCaseBits(const UnicodeString &nfdString,
  93. const char *&parserErrorReason, UErrorCode &errorCode);
  94. /** Implements CollationRuleParser::Sink. */
  95. virtual void suppressContractions(const UnicodeSet &set, const char *&parserErrorReason,
  96. UErrorCode &errorCode);
  97. /** Implements CollationRuleParser::Sink. */
  98. virtual void optimize(const UnicodeSet &set, const char *&parserErrorReason,
  99. UErrorCode &errorCode);
  100. /**
  101. * Adds the mapping and its canonical closure.
  102. * Takes ce32=dataBuilder->encodeCEs(...) so that the data builder
  103. * need not re-encode the CEs multiple times.
  104. */
  105. uint32_t addWithClosure(const UnicodeString &nfdPrefix, const UnicodeString &nfdString,
  106. const int64_t newCEs[], int32_t newCEsLength, uint32_t ce32,
  107. UErrorCode &errorCode);
  108. uint32_t addOnlyClosure(const UnicodeString &nfdPrefix, const UnicodeString &nfdString,
  109. const int64_t newCEs[], int32_t newCEsLength, uint32_t ce32,
  110. UErrorCode &errorCode);
  111. void addTailComposites(const UnicodeString &nfdPrefix, const UnicodeString &nfdString,
  112. UErrorCode &errorCode);
  113. UBool mergeCompositeIntoString(const UnicodeString &nfdString, int32_t indexAfterLastStarter,
  114. UChar32 composite, const UnicodeString &decomp,
  115. UnicodeString &newNFDString, UnicodeString &newString,
  116. UErrorCode &errorCode) const;
  117. UBool ignorePrefix(const UnicodeString &s, UErrorCode &errorCode) const;
  118. UBool ignoreString(const UnicodeString &s, UErrorCode &errorCode) const;
  119. UBool isFCD(const UnicodeString &s, UErrorCode &errorCode) const;
  120. void closeOverComposites(UErrorCode &errorCode);
  121. uint32_t addIfDifferent(const UnicodeString &prefix, const UnicodeString &str,
  122. const int64_t newCEs[], int32_t newCEsLength, uint32_t ce32,
  123. UErrorCode &errorCode);
  124. static UBool sameCEs(const int64_t ces1[], int32_t ces1Length,
  125. const int64_t ces2[], int32_t ces2Length);
  126. /**
  127. * Walks the tailoring graph and overwrites tailored nodes with new CEs.
  128. * After this, the graph is destroyed.
  129. * The nodes array can then be used only as a source of tailored CEs.
  130. */
  131. void makeTailoredCEs(UErrorCode &errorCode);
  132. /**
  133. * Counts the tailored nodes of the given strength up to the next node
  134. * which is either stronger or has an explicit weight of this strength.
  135. */
  136. static int32_t countTailoredNodes(const int64_t *nodesArray, int32_t i, int32_t strength);
  137. /** Replaces temporary CEs with the final CEs they point to. */
  138. void finalizeCEs(UErrorCode &errorCode);
  139. /**
  140. * Encodes "temporary CE" data into a CE that fits into the CE32 data structure,
  141. * with 2-byte primary, 1-byte secondary and 6-bit tertiary,
  142. * with valid CE byte values.
  143. *
  144. * The index must not exceed 20 bits (0xfffff).
  145. * The strength must fit into 2 bits (UCOL_PRIMARY..UCOL_QUATERNARY).
  146. *
  147. * Temporary CEs are distinguished from real CEs by their use of
  148. * secondary weights 06..45 which are otherwise reserved for compressed sort keys.
  149. *
  150. * The case bits are unused and available.
  151. */
  152. static inline int64_t tempCEFromIndexAndStrength(int32_t index, int32_t strength) {
  153. return
  154. // CE byte offsets, to ensure valid CE bytes, and case bits 11
  155. INT64_C(0x4040000006002000) +
  156. // index bits 19..13 -> primary byte 1 = CE bits 63..56 (byte values 40..BF)
  157. ((int64_t)(index & 0xfe000) << 43) +
  158. // index bits 12..6 -> primary byte 2 = CE bits 55..48 (byte values 40..BF)
  159. ((int64_t)(index & 0x1fc0) << 42) +
  160. // index bits 5..0 -> secondary byte 1 = CE bits 31..24 (byte values 06..45)
  161. ((index & 0x3f) << 24) +
  162. // strength bits 1..0 -> tertiary byte 1 = CE bits 13..8 (byte values 20..23)
  163. (strength << 8);
  164. }
  165. static inline int32_t indexFromTempCE(int64_t tempCE) {
  166. tempCE -= INT64_C(0x4040000006002000);
  167. return
  168. ((int32_t)(tempCE >> 43) & 0xfe000) |
  169. ((int32_t)(tempCE >> 42) & 0x1fc0) |
  170. ((int32_t)(tempCE >> 24) & 0x3f);
  171. }
  172. static inline int32_t strengthFromTempCE(int64_t tempCE) {
  173. return ((int32_t)tempCE >> 8) & 3;
  174. }
  175. static inline UBool isTempCE(int64_t ce) {
  176. uint32_t sec = (uint32_t)ce >> 24;
  177. return 6 <= sec && sec <= 0x45;
  178. }
  179. static inline int32_t indexFromTempCE32(uint32_t tempCE32) {
  180. tempCE32 -= 0x40400620;
  181. return
  182. ((int32_t)(tempCE32 >> 11) & 0xfe000) |
  183. ((int32_t)(tempCE32 >> 10) & 0x1fc0) |
  184. ((int32_t)(tempCE32 >> 8) & 0x3f);
  185. }
  186. static inline UBool isTempCE32(uint32_t ce32) {
  187. return
  188. (ce32 & 0xff) >= 2 && // not a long-primary/long-secondary CE32
  189. 6 <= ((ce32 >> 8) & 0xff) && ((ce32 >> 8) & 0xff) <= 0x45;
  190. }
  191. static int32_t ceStrength(int64_t ce);
  192. /** At most 1M nodes, limited by the 20 bits in node bit fields. */
  193. static const int32_t MAX_INDEX = 0xfffff;
  194. /**
  195. * Node bit 6 is set on a primary node if there are nodes
  196. * with secondary values below the common secondary weight (05).
  197. */
  198. static const int32_t HAS_BEFORE2 = 0x40;
  199. /**
  200. * Node bit 5 is set on a primary or secondary node if there are nodes
  201. * with tertiary values below the common tertiary weight (05).
  202. */
  203. static const int32_t HAS_BEFORE3 = 0x20;
  204. /**
  205. * Node bit 3 distinguishes a tailored node, which has no weight value,
  206. * from a node with an explicit (root or default) weight.
  207. */
  208. static const int32_t IS_TAILORED = 8;
  209. static inline int64_t nodeFromWeight32(uint32_t weight32) {
  210. return (int64_t)weight32 << 32;
  211. }
  212. static inline int64_t nodeFromWeight16(uint32_t weight16) {
  213. return (int64_t)weight16 << 48;
  214. }
  215. static inline int64_t nodeFromPreviousIndex(int32_t previous) {
  216. return (int64_t)previous << 28;
  217. }
  218. static inline int64_t nodeFromNextIndex(int32_t next) {
  219. return next << 8;
  220. }
  221. static inline int64_t nodeFromStrength(int32_t strength) {
  222. return strength;
  223. }
  224. static inline uint32_t weight32FromNode(int64_t node) {
  225. return (uint32_t)(node >> 32);
  226. }
  227. static inline uint32_t weight16FromNode(int64_t node) {
  228. return (uint32_t)(node >> 48) & 0xffff;
  229. }
  230. static inline int32_t previousIndexFromNode(int64_t node) {
  231. return (int32_t)(node >> 28) & MAX_INDEX;
  232. }
  233. static inline int32_t nextIndexFromNode(int64_t node) {
  234. return ((int32_t)node >> 8) & MAX_INDEX;
  235. }
  236. static inline int32_t strengthFromNode(int64_t node) {
  237. return (int32_t)node & 3;
  238. }
  239. static inline UBool nodeHasBefore2(int64_t node) {
  240. return (node & HAS_BEFORE2) != 0;
  241. }
  242. static inline UBool nodeHasBefore3(int64_t node) {
  243. return (node & HAS_BEFORE3) != 0;
  244. }
  245. static inline UBool nodeHasAnyBefore(int64_t node) {
  246. return (node & (HAS_BEFORE2 | HAS_BEFORE3)) != 0;
  247. }
  248. static inline UBool isTailoredNode(int64_t node) {
  249. return (node & IS_TAILORED) != 0;
  250. }
  251. static inline int64_t changeNodePreviousIndex(int64_t node, int32_t previous) {
  252. return (node & INT64_C(0xffff00000fffffff)) | nodeFromPreviousIndex(previous);
  253. }
  254. static inline int64_t changeNodeNextIndex(int64_t node, int32_t next) {
  255. return (node & INT64_C(0xfffffffff00000ff)) | nodeFromNextIndex(next);
  256. }
  257. const Normalizer2 &nfd, &fcd;
  258. const Normalizer2Impl &nfcImpl;
  259. const CollationTailoring *base;
  260. const CollationData *baseData;
  261. const CollationRootElements rootElements;
  262. uint32_t variableTop;
  263. CollationDataBuilder *dataBuilder;
  264. UBool fastLatinEnabled;
  265. UnicodeSet optimizeSet;
  266. const char *errorReason;
  267. int64_t ces[Collation::MAX_EXPANSION_LENGTH];
  268. int32_t cesLength;
  269. /**
  270. * Indexes of nodes with root primary weights, sorted by primary.
  271. * Compact form of a TreeMap from root primary to node index.
  272. *
  273. * This is a performance optimization for finding reset positions.
  274. * Without this, we would have to search through the entire nodes list.
  275. * It also allows storing root primary weights in list head nodes,
  276. * without previous index, leaving room in root primary nodes for 32-bit primary weights.
  277. */
  278. UVector32 rootPrimaryIndexes;
  279. /**
  280. * Data structure for assigning tailored weights and CEs.
  281. * Doubly-linked lists of nodes in mostly collation order.
  282. * Each list starts with a root primary node and ends with a nextIndex of 0.
  283. *
  284. * When there are any nodes in the list, then there is always a root primary node at index 0.
  285. * This allows some code not to have to check explicitly for nextIndex==0.
  286. *
  287. * Root primary nodes have 32-bit weights but do not have previous indexes.
  288. * All other nodes have at most 16-bit weights and do have previous indexes.
  289. *
  290. * Nodes with explicit weights store root collator weights,
  291. * or default weak weights (e.g., secondary 05) for stronger nodes.
  292. * "Tailored" nodes, with the IS_TAILORED bit set,
  293. * do not store explicit weights but rather
  294. * create a difference of a certain strength from the preceding node.
  295. *
  296. * A root node is followed by either
  297. * - a root/default node of the same strength, or
  298. * - a root/default node of the next-weaker strength, or
  299. * - a tailored node of the same strength.
  300. *
  301. * A node of a given strength normally implies "common" weights on weaker levels.
  302. *
  303. * A node with HAS_BEFORE2 must be immediately followed by
  304. * a secondary node with an explicit below-common weight, then a secondary tailored node,
  305. * and later an explicit common-secondary node.
  306. * The below-common weight can be a root weight,
  307. * or it can be BEFORE_WEIGHT16 for tailoring before an implied common weight
  308. * or before the lowest root weight.
  309. * (&[before 2] resets to an explicit secondary node so that
  310. * the following addRelation(secondary) tailors right after that.
  311. * If we did not have this node and instead were to reset on the primary node,
  312. * then addRelation(secondary) would skip forward to the the COMMON_WEIGHT16 node.)
  313. *
  314. * If the flag is not set, then there are no explicit secondary nodes
  315. * with the common or lower weights.
  316. *
  317. * Same for HAS_BEFORE3 for tertiary nodes and weights.
  318. * A node must not have both flags set.
  319. *
  320. * Tailored CEs are initially represented in a CollationDataBuilder as temporary CEs
  321. * which point to stable indexes in this list,
  322. * and temporary CEs stored in a CollationDataBuilder only point to tailored nodes.
  323. *
  324. * A temporary CE in the ces[] array may point to a non-tailored reset-before-position node,
  325. * until the next relation is added.
  326. *
  327. * At the end, the tailored weights are allocated as necessary,
  328. * then the tailored nodes are replaced with final CEs,
  329. * and the CollationData is rewritten by replacing temporary CEs with final ones.
  330. *
  331. * We cannot simply insert new nodes in the middle of the array
  332. * because that would invalidate the indexes stored in existing temporary CEs.
  333. * We need to use a linked graph with stable indexes to existing nodes.
  334. * A doubly-linked list seems easiest to maintain.
  335. *
  336. * Each node is stored as an int64_t, with its fields stored as bit fields.
  337. *
  338. * Root primary node:
  339. * - primary weight: 32 bits 63..32
  340. * - reserved/unused/zero: 4 bits 31..28
  341. *
  342. * Weaker root nodes & tailored nodes:
  343. * - a weight: 16 bits 63..48
  344. * + a root or default weight for a non-tailored node
  345. * + unused/zero for a tailored node
  346. * - index to the previous node: 20 bits 47..28
  347. *
  348. * All types of nodes:
  349. * - index to the next node: 20 bits 27..8
  350. * + nextIndex=0 in last node per root-primary list
  351. * - reserved/unused/zero bits: bits 7, 4, 2
  352. * - HAS_BEFORE2: bit 6
  353. * - HAS_BEFORE3: bit 5
  354. * - IS_TAILORED: bit 3
  355. * - the difference strength (primary/secondary/tertiary/quaternary): 2 bits 1..0
  356. *
  357. * We could allocate structs with pointers, but we would have to store them
  358. * in a pointer list so that they can be indexed from temporary CEs,
  359. * and they would require more memory allocations.
  360. */
  361. UVector64 nodes;
  362. };
  363. U_NAMESPACE_END
  364. #endif // !UCONFIG_NO_COLLATION
  365. #endif // __COLLATIONBUILDER_H__