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