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