10596faeddefbf198de137d5e893708495ab1584cFredrik Roubert// © 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* collationrootelements.h
9fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*
10fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius* created on: 2013mar01
11fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius* created by: Markus W. Scherer
12fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*/
13fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
14fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#ifndef __COLLATIONROOTELEMENTS_H__
15fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#define __COLLATIONROOTELEMENTS_H__
16fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
17fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#include "unicode/utypes.h"
18fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
19fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#if !UCONFIG_NO_COLLATION
20fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
21fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#include "unicode/uobject.h"
22fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#include "collation.h"
23fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
24fceb39872958b9fa2505e63f8b8699a9e0f882f4ccorneliusU_NAMESPACE_BEGIN
25fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
26fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius/**
27fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius * Container and access methods for collation elements and weights
28fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius * that occur in the root collator.
29fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius * Needed for finding boundaries for building a tailoring.
30fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius *
31fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius * This class takes and returns 16-bit secondary and tertiary weights.
32fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius */
33fceb39872958b9fa2505e63f8b8699a9e0f882f4ccorneliusclass U_I18N_API CollationRootElements : public UMemory {
34fceb39872958b9fa2505e63f8b8699a9e0f882f4ccorneliuspublic:
35fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    CollationRootElements(const uint32_t *rootElements, int32_t rootElementsLength)
36fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius            : elements(rootElements), length(rootElementsLength) {}
37fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
38fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
39fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Higher than any root primary.
40fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
41fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    static const uint32_t PRIMARY_SENTINEL = 0xffffff00;
42fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
43fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
44fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Flag in a root element, set if the element contains secondary & tertiary weights,
45fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * rather than a primary.
46fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
47fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    static const uint32_t SEC_TER_DELTA_FLAG = 0x80;
48fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
49fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Mask for getting the primary range step value from a primary-range-end element.
50fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
51fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    static const uint8_t PRIMARY_STEP_MASK = 0x7f;
52fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
53fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    enum {
54fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        /**
55fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * Index of the first CE with a non-zero tertiary weight.
56fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * Same as the start of the compact root elements table.
57fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         */
58fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        IX_FIRST_TERTIARY_INDEX,
59fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        /**
60fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * Index of the first CE with a non-zero secondary weight.
61fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         */
62fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        IX_FIRST_SECONDARY_INDEX,
63fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        /**
64fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * Index of the first CE with a non-zero primary weight.
65fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         */
66fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        IX_FIRST_PRIMARY_INDEX,
67fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        /**
68fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * Must match Collation::COMMON_SEC_AND_TER_CE.
69fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         */
70fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        IX_COMMON_SEC_AND_TER_CE,
71fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        /**
72fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * Secondary & tertiary boundaries.
73fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * Bits 31..24: [fixed last secondary common byte 45]
74fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * Bits 23..16: [fixed first ignorable secondary byte 80]
75fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * Bits 15.. 8: reserved, 0
76fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * Bits  7.. 0: [fixed first ignorable tertiary byte 3C]
77fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         */
78fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        IX_SEC_TER_BOUNDARIES,
79fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        /**
80fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * The current number of indexes.
81fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         * Currently the same as elements[IX_FIRST_TERTIARY_INDEX].
82fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius         */
83fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        IX_COUNT
84fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    };
85fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
86fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
87fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the boundary between tertiary weights of primary/secondary CEs
88fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * and those of tertiary CEs.
89fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * This is the upper limit for tertiaries of primary/secondary CEs.
90fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * This minus one is the lower limit for tertiaries of tertiary CEs.
91fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
92fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getTertiaryBoundary() const {
93fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        return (elements[IX_SEC_TER_BOUNDARIES] << 8) & 0xff00;
94fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
95fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
96fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
97fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the first assigned tertiary CE.
98fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
99fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getFirstTertiaryCE() const {
100fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        return elements[elements[IX_FIRST_TERTIARY_INDEX]] & ~SEC_TER_DELTA_FLAG;
101fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
102fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
103fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
104fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the last assigned tertiary CE.
105fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
106fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getLastTertiaryCE() const {
107fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        return elements[elements[IX_FIRST_SECONDARY_INDEX] - 1] & ~SEC_TER_DELTA_FLAG;
108fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
109fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
110fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
111fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the last common secondary weight.
112fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * This is the lower limit for secondaries of primary CEs.
113fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
114fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getLastCommonSecondary() const {
115fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        return (elements[IX_SEC_TER_BOUNDARIES] >> 16) & 0xff00;
116fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
117fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
118fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
119fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the boundary between secondary weights of primary CEs
120fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * and those of secondary CEs.
121fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * This is the upper limit for secondaries of primary CEs.
122fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * This minus one is the lower limit for secondaries of secondary CEs.
123fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
124fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getSecondaryBoundary() const {
125fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        return (elements[IX_SEC_TER_BOUNDARIES] >> 8) & 0xff00;
126fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
127fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
128fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
129fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the first assigned secondary CE.
130fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
131fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getFirstSecondaryCE() const {
132fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        return elements[elements[IX_FIRST_SECONDARY_INDEX]] & ~SEC_TER_DELTA_FLAG;
133fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
134fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
135fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
136fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the last assigned secondary CE.
137fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
138fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getLastSecondaryCE() const {
139fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        return elements[elements[IX_FIRST_PRIMARY_INDEX] - 1] & ~SEC_TER_DELTA_FLAG;
140fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
141fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
142fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
143fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the first assigned primary weight.
144fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
145fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getFirstPrimary() const {
146fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        return elements[elements[IX_FIRST_PRIMARY_INDEX]];  // step=0: cannot be a range end
147fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
148fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
149fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
150fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the first assigned primary CE.
151fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
152fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    int64_t getFirstPrimaryCE() const {
153fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        return Collation::makeCE(getFirstPrimary());
154fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
155fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
156fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
157fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the last root CE with a primary weight before p.
158fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Intended only for reordering group boundaries.
159fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
160fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    int64_t lastCEWithPrimaryBefore(uint32_t p) const;
161fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
162fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
163fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the first root CE with a primary weight of at least p.
164fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Intended only for reordering group boundaries.
165fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
166fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    int64_t firstCEWithPrimaryAtLeast(uint32_t p) const;
167fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
168fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
169fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the primary weight before p.
170fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * p must be greater than the first root primary.
171fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
172fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getPrimaryBefore(uint32_t p, UBool isCompressible) const;
173fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
174fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /** Returns the secondary weight before [p, s]. */
175fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getSecondaryBefore(uint32_t p, uint32_t s) const;
176fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
177fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /** Returns the tertiary weight before [p, s, t]. */
178fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getTertiaryBefore(uint32_t p, uint32_t s, uint32_t t) const;
179fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
180fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
181fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Finds the index of the input primary.
182fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * p must occur as a root primary, and must not be 0.
183fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
184fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    int32_t findPrimary(uint32_t p) const;
185fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
186fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
187fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the primary weight after p where index=findPrimary(p).
188fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * p must be at least the first root primary.
189fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
190fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getPrimaryAfter(uint32_t p, int32_t index, UBool isCompressible) const;
191fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
192fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the secondary weight after [p, s] where index=findPrimary(p)
193fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * except use index=0 for p=0.
1941b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     *
1951b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * Must return a weight for every root [p, s] as well as for every weight
1961b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * returned by getSecondaryBefore(). If p!=0 then s can be BEFORE_WEIGHT16.
1971b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     *
1981b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * Exception: [0, 0] is handled by the CollationBuilder:
1991b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * Both its lower and upper boundaries are special.
200fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
201fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getSecondaryAfter(int32_t index, uint32_t s) const;
202fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
203fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Returns the tertiary weight after [p, s, t] where index=findPrimary(p)
204fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * except use index=0 for p=0.
2051b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     *
2061b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * Must return a weight for every root [p, s, t] as well as for every weight
2071b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * returned by getTertiaryBefore(). If s!=0 then t can be BEFORE_WEIGHT16.
2081b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     *
2091b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * Exception: [0, 0, 0] is handled by the CollationBuilder:
2101b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * Both its lower and upper boundaries are special.
211fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
212fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t getTertiaryAfter(int32_t index, uint32_t s, uint32_t t) const;
213fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
214fceb39872958b9fa2505e63f8b8699a9e0f882f4ccorneliusprivate:
215fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
2161b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * Returns the first secondary & tertiary weights for p where index=findPrimary(p)+1.
2171b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     */
2181b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert    uint32_t getFirstSecTerForPrimary(int32_t index) const;
2191b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert
2201b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert    /**
221fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Finds the largest index i where elements[i]<=p.
222fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Requires first primary<=p<0xffffff00 (PRIMARY_SENTINEL).
223fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Does not require that p is a root collator primary.
224fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
225fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    int32_t findP(uint32_t p) const;
226fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
227fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    static inline UBool isEndOfPrimaryRange(uint32_t q) {
228fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        return (q & SEC_TER_DELTA_FLAG) == 0 && (q & PRIMARY_STEP_MASK) != 0;
229fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
230fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
231fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
232fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Data structure:
233fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
234fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * The first few entries are indexes, up to elements[IX_FIRST_TERTIARY_INDEX].
235fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * See the comments on the IX_ constants.
236fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
237fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * All other elements are a compact form of the root collator CEs
2381b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * in mostly collation order.
239fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
2401b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * A sequence of one or more root CEs with the same primary weight is stored as
2411b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * one element with the primary weight, with the SEC_TER_DELTA_FLAG flag not set,
2421b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * followed by elements with only the secondary/tertiary weights,
2431b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * each with that flag set.
2441b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * If the lowest secondary/tertiary combination is Collation::COMMON_SEC_AND_TER_CE,
2451b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * then the element for that combination is omitted.
246fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
2471b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * Note: If the first actual secondary/tertiary combination is higher than
2481b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * Collation::COMMON_SEC_AND_TER_CE (which is unusual),
2491b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert     * the runtime code will assume anyway that Collation::COMMON_SEC_AND_TER_CE is present.
250fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
251fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * A range of only-primary CEs with a consistent "step" increment
252fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * from each primary to the next may be stored as a range.
253fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Only the first and last primary are stored, and the last has the step
254fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * value in the low bits (PRIMARY_STEP_MASK).
255fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
256fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * An range-end element may also either start a new range or be followed by
257fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * elements with secondary/tertiary deltas.
258fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
259fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * A primary element that is not a range end has zero step bits.
260fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
261fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * There is no element for the completely ignorable CE (all weights 0).
262fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
263fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Before elements[IX_FIRST_PRIMARY_INDEX], all elements are secondary/tertiary deltas,
264fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * for all of the ignorable root CEs.
265fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
266fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * There are no elements for unassigned-implicit primary CEs.
267fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * All primaries stored here are at most 3 bytes long.
268fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
269fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    const uint32_t *elements;
270fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    int32_t length;
271fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius};
272fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
273fceb39872958b9fa2505e63f8b8699a9e0f882f4ccorneliusU_NAMESPACE_END
274fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
275fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#endif  // !UCONFIG_NO_COLLATION
276fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#endif  // __COLLATIONROOTELEMENTS_H__
277