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