1fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius/*
2fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*******************************************************************************
3fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*
4fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*   Copyright (C) 1999-2014, International Business Machines
5fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*   Corporation and others.  All Rights Reserved.
6fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*
7fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*******************************************************************************
8fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*   file name:  collationweights.h
9fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*   encoding:   US-ASCII
10fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*   tab size:   8 (not used)
11fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*   indentation:4
12fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*
13fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*   created on: 2001mar08 as ucol_wgt.h
14fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*   created by: Markus W. Scherer
15fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius*/
16fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
17fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#ifndef __COLLATIONWEIGHTS_H__
18fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#define __COLLATIONWEIGHTS_H__
19fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
20fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#include "unicode/utypes.h"
21fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
22fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#if !UCONFIG_NO_COLLATION
23fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
24fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#include "unicode/uobject.h"
25fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
26fceb39872958b9fa2505e63f8b8699a9e0f882f4ccorneliusU_NAMESPACE_BEGIN
27fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
28fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius/**
29fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius * Allocates n collation element weights between two exclusive limits.
30fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius * Used only internally by the collation tailoring builder.
31fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius */
32fceb39872958b9fa2505e63f8b8699a9e0f882f4ccorneliusclass U_I18N_API CollationWeights : public UMemory {
33fceb39872958b9fa2505e63f8b8699a9e0f882f4ccorneliuspublic:
34fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    CollationWeights();
35fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
36fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    static inline int32_t lengthOfWeight(uint32_t weight) {
37fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        if((weight&0xffffff)==0) {
38fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius            return 1;
39fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        } else if((weight&0xffff)==0) {
40fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius            return 2;
41fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        } else if((weight&0xff)==0) {
42fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius            return 3;
43fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        } else {
44fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius            return 4;
45fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        }
46fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
47fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
48fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    void initForPrimary(UBool compressible);
49fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    void initForSecondary();
50fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    void initForTertiary();
51fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
52fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
53fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Determine heuristically
54fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * what ranges to use for a given number of weights between (excluding)
55fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * two limits.
56fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
57fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * @param lowerLimit A collation element weight; the ranges will be filled to cover
58fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *                   weights greater than this one.
59fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * @param upperLimit A collation element weight; the ranges will be filled to cover
60fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *                   weights less than this one.
61fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * @param n          The number of collation element weights w necessary such that
62fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *                   lowerLimit<w<upperLimit in lexical order.
63fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * @return TRUE if it is possible to fit n elements between the limits
64fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
65fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    UBool allocWeights(uint32_t lowerLimit, uint32_t upperLimit, int32_t n);
66fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
67fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
68fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Given a set of ranges calculated by allocWeights(),
69fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * iterate through the weights.
70fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * The ranges are modified to keep the current iteration state.
71fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     *
72fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * @return The next weight in the ranges, or 0xffffffff if there is none left.
73fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
74fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t nextWeight();
75fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
76fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /** @internal */
77fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    struct WeightRange {
78fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        uint32_t start, end;
79fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        int32_t length, count;
80fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    };
81fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
82fceb39872958b9fa2505e63f8b8699a9e0f882f4ccorneliusprivate:
83fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /** @return number of usable byte values for byte idx */
84fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    inline int32_t countBytes(int32_t idx) const {
85fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius        return (int32_t)(maxBytes[idx] - minBytes[idx] + 1);
86fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    }
87fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
88fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t incWeight(uint32_t weight, int32_t length) const;
89fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t incWeightByOffset(uint32_t weight, int32_t length, int32_t offset) const;
90fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    void lengthenRange(WeightRange &range) const;
91fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    /**
92fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * Takes two CE weights and calculates the
93fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * possible ranges of weights between the two limits, excluding them.
94fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     * For weights with up to 4 bytes there are up to 2*4-1=7 ranges.
95fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius     */
96fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    UBool getWeightRanges(uint32_t lowerLimit, uint32_t upperLimit);
97fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    UBool allocWeightsInShortRanges(int32_t n, int32_t minLength);
98fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    UBool allocWeightsInMinLengthRanges(int32_t n, int32_t minLength);
99fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
100fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    int32_t middleLength;
101fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t minBytes[5];  // for byte 1, 2, 3, 4
102fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    uint32_t maxBytes[5];
103fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    WeightRange ranges[7];
104fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    int32_t rangeIndex;
105fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    int32_t rangeCount;
106fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius};
107fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
108fceb39872958b9fa2505e63f8b8699a9e0f882f4ccorneliusU_NAMESPACE_END
109fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius
110fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#endif  // !UCONFIG_NO_COLLATION
111fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius#endif  // __COLLATIONWEIGHTS_H__
112