12ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/* GENERATED SOURCE. DO NOT MODIFY. */
2f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert// © 2016 and later: Unicode, Inc. and others.
3f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert// License & terms of use: http://www.unicode.org/copyright.html#License
4f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert/*
52ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*******************************************************************************
62ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*
72ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*   Copyright (C) 1999-2015, International Business Machines
82ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*   Corporation and others.  All Rights Reserved.
92ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*
102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*******************************************************************************
112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*   CollationWeights.java, ported from collationweights.h/.cpp
122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*
132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*   C++ version created on: 2001mar08 as ucol_wgt.h
142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*   created by: Markus W. Scherer
152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller*/
162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpackage android.icu.impl.coll;
182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.util.Arrays;
202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/**
222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Allocates n collation element weights between two exclusive limits.
232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Used only internally by the collation tailoring builder.
24836e6b40a94ec3fb7545a76cb072960442b7eee9Neil Fuller * @hide Only a subset of ICU is exposed in Android
252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */
262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpublic final class CollationWeights {
272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public CollationWeights() {}
282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public void initForPrimary(boolean compressible) {
302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        middleLength=1;
312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        minBytes[1] = Collation.MERGE_SEPARATOR_BYTE + 1;
322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        maxBytes[1] = Collation.TRAIL_WEIGHT_BYTE;
332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(compressible) {
342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            minBytes[2] = Collation.PRIMARY_COMPRESSION_LOW_BYTE + 1;
352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            maxBytes[2] = Collation.PRIMARY_COMPRESSION_HIGH_BYTE - 1;
362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            minBytes[2] = 2;
382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            maxBytes[2] = 0xff;
392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        minBytes[3] = 2;
412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        maxBytes[3] = 0xff;
422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        minBytes[4] = 2;
432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        maxBytes[4] = 0xff;
442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public void initForSecondary() {
472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // We use only the lower 16 bits for secondary weights.
482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        middleLength=3;
492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        minBytes[1] = 0;
502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        maxBytes[1] = 0;
512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        minBytes[2] = 0;
522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        maxBytes[2] = 0;
532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        minBytes[3] = Collation.LEVEL_SEPARATOR_BYTE + 1;
542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        maxBytes[3] = 0xff;
552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        minBytes[4] = 2;
562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        maxBytes[4] = 0xff;
572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public void initForTertiary() {
602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // We use only the lower 16 bits for tertiary weights.
612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        middleLength=3;
622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        minBytes[1] = 0;
632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        maxBytes[1] = 0;
642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        minBytes[2] = 0;
652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        maxBytes[2] = 0;
662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // We use only 6 bits per byte.
672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // The other bits are used for case & quaternary weights.
682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        minBytes[3] = Collation.LEVEL_SEPARATOR_BYTE + 1;
692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        maxBytes[3] = 0x3f;
702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        minBytes[4] = 2;
712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        maxBytes[4] = 0x3f;
722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Determine heuristically
762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * what ranges to use for a given number of weights between (excluding)
772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * two limits.
782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param lowerLimit A collation element weight; the ranges will be filled to cover
802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *                   weights greater than this one.
812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param upperLimit A collation element weight; the ranges will be filled to cover
822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *                   weights less than this one.
832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @param n          The number of collation element weights w necessary such that
842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *                   lowerLimit<w<upperLimit in lexical order.
852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return true if it is possible to fit n elements between the limits
862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public boolean allocWeights(long lowerLimit, long upperLimit, int n) {
882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // Call getWeightRanges() and then determine heuristically
892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // which ranges to use for a given number of weights between (excluding)
902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // two limits.
912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // puts("");
922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(!getWeightRanges(lowerLimit, upperLimit)) {
942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // printf("error: unable to get Weight ranges\n");
952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return false;
962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        /* try until we find suitably large ranges */
992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(;;) {
1002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            /* get the smallest number of bytes in a range */
1012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            int minLength=ranges[0].length;
1022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(allocWeightsInShortRanges(n, minLength)) { break; }
1042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(minLength == 4) {
1062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // printf("error: the maximum number of %ld weights is insufficient for n=%ld\n",
1072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                //       minLengthCount, n);
1082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return false;
1092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(allocWeightsInMinLengthRanges(n, minLength)) { break; }
1122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            /* no good match, lengthen all minLength ranges and iterate */
1142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // printf("lengthen the short ranges from %ld bytes to %ld and iterate\n", minLength, minLength+1);
11505fa7802d0874812c234a29745586677ee5837eaFredrik Roubert            for (int i = 0; i < rangeCount && ranges[i].length == minLength; ++i) {
1162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                lengthenRange(ranges[i]);
1172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        /* puts("final ranges:");
1212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(int i=0; i<rangeCount; ++i) {
1222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            printf("ranges[%ld] .start=0x%08lx .end=0x%08lx .length=%ld .count=%ld\n",
1232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                  i, ranges[i].start, ranges[i].end, ranges[i].length, ranges[i].count);
1242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } */
1252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        rangeIndex = 0;
1272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(rangeCount < ranges.length) {
1282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ranges[rangeCount] = null;  // force a crash when going out of bounds
1292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return true;
1312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
1342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Given a set of ranges calculated by allocWeights(),
1352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * iterate through the weights.
1362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * The ranges are modified to keep the current iteration state.
1372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     *
1382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * @return The next weight in the ranges, or 0xffffffff if there is none left.
1392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
1402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public long nextWeight() {
1412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(rangeIndex >= rangeCount) {
1422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return 0xffffffffL;
1432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
1442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            /* get the next weight */
1452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            WeightRange range = ranges[rangeIndex];
1462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            long weight = range.start;
1472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(--range.count == 0) {
1482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                /* this range is finished */
1492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ++rangeIndex;
1502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else {
1512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                /* increment the weight for the next value */
1522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                range.start = incWeight(weight, range.length);
1532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                assert(range.start <= range.end);
1542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return weight;
1572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1601537b2f39245c07b00aa78c3600f7aebcb172490Neil Fuller    /** @hide draft / provisional / internal are hidden on Android*/
1612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static final class WeightRange implements Comparable<WeightRange> {
1622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        long start, end;
1632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int length, count;
1642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
165f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert        @Override
1662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        public int compareTo(WeightRange other) {
1672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            long l=start;
1682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            long r=other.start;
1692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(l<r) {
1702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return -1;
1712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else if(l>r) {
1722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return 1;
1732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else {
1742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return 0;
1752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
1762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /* helper functions for CE weights */
1802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static int lengthOfWeight(long weight) {
1822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if((weight&0xffffff)==0) {
1832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return 1;
1842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else if((weight&0xffff)==0) {
1852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return 2;
1862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else if((weight&0xff)==0) {
1872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return 3;
1882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
1892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return 4;
1902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
1912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static int getWeightTrail(long weight, int length) {
1942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return (int)(weight>>(8*(4-length)))&0xff;
1952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
1962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
1972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static long setWeightTrail(long weight, int length, int trail) {
1982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        length=8*(4-length);
1992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return (weight&(0xffffff00L<<length))|((long)trail<<length);
2002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static int getWeightByte(long weight, int idx) {
2032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return getWeightTrail(weight, idx); /* same calculation */
2042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static long setWeightByte(long weight, int idx, int b) {
2072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        long mask; /* 0xffffffff except a 00 "hole" for the index-th byte */
2082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        idx*=8;
2102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(idx<32) {
2112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            mask=0xffffffffL>>idx;
2122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
2132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // Do not use int>>32 because on some platforms that does not shift at all
2142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // while we need it to become 0.
2152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // PowerPC: 0xffffffff>>32 = 0           (wanted)
2162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // x86:     0xffffffff>>32 = 0xffffffff  (not wanted)
2172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            //
2182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // ANSI C99 6.5.7 Bitwise shift operators:
2192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // "If the value of the right operand is negative
2202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // or is greater than or equal to the width of the promoted left operand,
2212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // the behavior is undefined."
2222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            mask=0;
2232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
2242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        idx=32-idx;
2252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        mask|=0xffffff00L<<idx;
2262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return (weight&mask)|((long)b<<idx);
2272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static long truncateWeight(long weight, int length) {
2302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return weight&(0xffffffffL<<(8*(4-length)));
2312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static long incWeightTrail(long weight, int length) {
2342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return weight+(1L<<(8*(4-length)));
2352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static long decWeightTrail(long weight, int length) {
2382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return weight-(1L<<(8*(4-length)));
2392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /** @return number of usable byte values for byte idx */
2422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int countBytes(int idx) {
2432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return maxBytes[idx] - minBytes[idx] + 1;
2442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private long incWeight(long weight, int length) {
2472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(;;) {
2482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            int b=getWeightByte(weight, length);
2492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(b<maxBytes[length]) {
2502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return setWeightByte(weight, length, b+1);
2512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else {
2522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Roll over, set this byte to the minimum and increment the previous one.
2532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                weight=setWeightByte(weight, length, minBytes[length]);
2542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                --length;
2552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                assert(length > 0);
2562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
2572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
2582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private long incWeightByOffset(long weight, int length, int offset) {
2612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(;;) {
2622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            offset += getWeightByte(weight, length);
2632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(offset <= maxBytes[length]) {
2642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return setWeightByte(weight, length, offset);
2652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            } else {
2662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Split the offset between this byte and the previous one.
2672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                offset -= minBytes[length];
2682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                weight = setWeightByte(weight, length, minBytes[length] + offset % countBytes(length));
2692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                offset /= countBytes(length);
2702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                --length;
2712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                assert(length > 0);
2722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
2732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
2742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private void lengthenRange(WeightRange range) {
2772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int length=range.length+1;
2782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        range.start=setWeightTrail(range.start, length, minBytes[length]);
2792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        range.end=setWeightTrail(range.end, length, maxBytes[length]);
2802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        range.count*=countBytes(length);
2812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        range.length=length;
2822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
2832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    /**
2852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * Takes two CE weights and calculates the
2862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * possible ranges of weights between the two limits, excluding them.
2872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     * For weights with up to 4 bytes there are up to 2*4-1=7 ranges.
2882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller     */
2892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private boolean getWeightRanges(long lowerLimit, long upperLimit) {
2902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        assert(lowerLimit != 0);
2912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        assert(upperLimit != 0);
2922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        /* get the lengths of the limits */
2942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int lowerLength=lengthOfWeight(lowerLimit);
2952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int upperLength=lengthOfWeight(upperLimit);
2962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
2972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // printf("length of lower limit 0x%08lx is %ld\n", lowerLimit, lowerLength);
2982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // printf("length of upper limit 0x%08lx is %ld\n", upperLimit, upperLength);
2992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        assert(lowerLength>=middleLength);
3002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // Permit upperLength<middleLength: The upper limit for secondaries is 0x10000.
3012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(lowerLimit>=upperLimit) {
3032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // printf("error: no space between lower & upper limits\n");
3042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            return false;
3052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
3062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        /* check that neither is a prefix of the other */
3082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(lowerLength<upperLength) {
3092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(lowerLimit==truncateWeight(upperLimit, lowerLength)) {
3102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // printf("error: lower limit 0x%08lx is a prefix of upper limit 0x%08lx\n", lowerLimit, upperLimit);
3112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return false;
3122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
3132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
3142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        /* if the upper limit is a prefix of the lower limit then the earlier test lowerLimit>=upperLimit has caught it */
3152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        WeightRange[] lower = new WeightRange[5]; /* [0] and [1] are not used - this simplifies indexing */
3172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        WeightRange middle = new WeightRange();
3182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        WeightRange[] upper = new WeightRange[5];
3192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        /*
3212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         * With the limit lengths of 1..4, there are up to 7 ranges for allocation:
3222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         * range     minimum length
3232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         * lower[4]  4
3242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         * lower[3]  3
3252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         * lower[2]  2
3262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         * middle    1
3272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         * upper[2]  2
3282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         * upper[3]  3
3292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         * upper[4]  4
3302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         *
3312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         * We are now going to calculate up to 7 ranges.
3322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         * Some of them will typically overlap, so we will then have to merge and eliminate ranges.
3332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller         */
3342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        long weight=lowerLimit;
3352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(int length=lowerLength; length>middleLength; --length) {
3362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            int trail=getWeightTrail(weight, length);
3372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(trail<maxBytes[length]) {
3382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                lower[length] = new WeightRange();
3392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                lower[length].start=incWeightTrail(weight, length);
3402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                lower[length].end=setWeightTrail(weight, length, maxBytes[length]);
3412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                lower[length].length=length;
3422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                lower[length].count=maxBytes[length]-trail;
3432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
3442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            weight=truncateWeight(weight, length-1);
3452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
3462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(weight<0xff000000L) {
3472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            middle.start=incWeightTrail(weight, middleLength);
3482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
3492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // Prevent overflow for primary lead byte FF
3502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // which would yield a middle range starting at 0.
3512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            middle.start=0xffffffffL;  // no middle range
3522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
3532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        weight=upperLimit;
3552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(int length=upperLength; length>middleLength; --length) {
3562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            int trail=getWeightTrail(weight, length);
3572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(trail>minBytes[length]) {
3582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                upper[length] = new WeightRange();
3592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                upper[length].start=setWeightTrail(weight, length, minBytes[length]);
3602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                upper[length].end=decWeightTrail(weight, length);
3612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                upper[length].length=length;
3622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                upper[length].count=trail-minBytes[length];
3632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
3642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            weight=truncateWeight(weight, length-1);
3652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
3662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        middle.end=decWeightTrail(weight, middleLength);
3672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        /* set the middle range */
3692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        middle.length=middleLength;
3702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(middle.end>=middle.start) {
3712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            middle.count=(int)((middle.end-middle.start)>>(8*(4-middleLength)))+1;
3722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
3732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            /* no middle range, eliminate overlaps */
3742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            for(int length=4; length>middleLength; --length) {
3752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if(lower[length] != null && upper[length] != null &&
3762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        lower[length].count>0 && upper[length].count>0) {
3772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    // Note: The lowerEnd and upperStart weights are versions of
3782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    // lowerLimit and upperLimit (which are lowerLimit<upperLimit),
3792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    // truncated (still less-or-equal)
3802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    // and then with their last bytes changed to the
3812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    // maxByte (for lowerEnd) or minByte (for upperStart).
3822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    final long lowerEnd=lower[length].end;
3832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    final long upperStart=upper[length].start;
3842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    boolean merged=false;
3852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
3862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    if(lowerEnd>upperStart) {
3872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // These two lower and upper ranges collide.
3882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // Since lowerLimit<upperLimit and lowerEnd and upperStart
3892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // are versions with only their last bytes modified
3902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // (and following ones removed/reset to 0),
3912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // lowerEnd>upperStart is only possible
3922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // if the leading bytes are equal
3932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // and lastByte(lowerEnd)>lastByte(upperStart).
3942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        assert(truncateWeight(lowerEnd, length-1)==
3952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                                truncateWeight(upperStart, length-1));
3962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // Intersect these two ranges.
3972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        lower[length].end=upper[length].end;
3982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        lower[length].count=
3992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                                getWeightTrail(lower[length].end, length)-
4002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                                getWeightTrail(lower[length].start, length)+1;
4012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // count might be <=0 in which case there is no room,
4022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // and the range-collecting code below will ignore this range.
4032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        merged=true;
4042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    } else if(lowerEnd==upperStart) {
4052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // Not possible, unless minByte==maxByte which is not allowed.
4062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        assert(minBytes[length]<maxBytes[length]);
4072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    } else /* lowerEnd<upperStart */ {
4082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        if(incWeight(lowerEnd, length)==upperStart) {
4092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                            // Merge adjacent ranges.
4102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                            lower[length].end=upper[length].end;
4112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                            lower[length].count+=upper[length].count;  // might be >countBytes
4122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                            merged=true;
4132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        }
4142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    }
4152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    if(merged) {
4162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // Remove all shorter ranges.
4172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        // There was no room available for them between the ranges we just merged.
4182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        upper[length].count=0;
4192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        while(--length>middleLength) {
4202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                            lower[length]=upper[length]=null;
4212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        }
4222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                        break;
4232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    }
4242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
4252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
4262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
4272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
4282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        /* print ranges
4292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(int length=4; length>=2; --length) {
4302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(lower[length].count>0) {
4312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                printf("lower[%ld] .start=0x%08lx .end=0x%08lx .count=%ld\n", length, lower[length].start, lower[length].end, lower[length].count);
4322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
4332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
4342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(middle.count>0) {
4352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            printf("middle   .start=0x%08lx .end=0x%08lx .count=%ld\n", middle.start, middle.end, middle.count);
4362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
4372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(int length=2; length<=4; ++length) {
4382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(upper[length].count>0) {
4392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                printf("upper[%ld] .start=0x%08lx .end=0x%08lx .count=%ld\n", length, upper[length].start, upper[length].end, upper[length].count);
4402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
4412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } */
4422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
4432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        /* copy the ranges, shortest first, into the result array */
4442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        rangeCount=0;
4452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(middle.count>0) {
4462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ranges[0] = middle;
4472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            rangeCount=1;
4482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
4492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(int length=middleLength+1; length<=4; ++length) {
4502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            /* copy upper first so that later the middle range is more likely the first one to use */
4512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(upper[length] != null && upper[length].count>0) {
4522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ranges[rangeCount++]=upper[length];
4532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
4542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(lower[length] != null && lower[length].count>0) {
4552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ranges[rangeCount++]=lower[length];
4562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
4572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
4582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return rangeCount>0;
4592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
4602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
4612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private boolean allocWeightsInShortRanges(int n, int minLength) {
4622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // See if the first few minLength and minLength+1 ranges have enough weights.
4632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(int i = 0; i < rangeCount && ranges[i].length <= (minLength + 1); ++i) {
4642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(n <= ranges[i].count) {
4652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // Use the first few minLength and minLength+1 ranges.
4662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if(ranges[i].length > minLength) {
4672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    // Reduce the number of weights from the last minLength+1 range
4682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    // which might sort before some minLength ranges,
4692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    // so that we use all weights in the minLength ranges.
4702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    ranges[i].count = n;
4712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
4722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                rangeCount = i + 1;
4732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                // printf("take first %ld ranges\n", rangeCount);
4742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
4752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                if(rangeCount>1) {
4762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    /* sort the ranges by weight values */
4772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    Arrays.sort(ranges, 0, rangeCount);
4782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                }
4792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                return true;
4802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
4812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            n -= ranges[i].count;  // still >0
4822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
4832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return false;
4842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
4852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
4862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private boolean allocWeightsInMinLengthRanges(int n, int minLength) {
4872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // See if the minLength ranges have enough weights
4882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // when we split one and lengthen the following ones.
4892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int count = 0;
4902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int minLengthRangeCount;
4912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(minLengthRangeCount = 0;
4922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                minLengthRangeCount < rangeCount &&
4932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                    ranges[minLengthRangeCount].length == minLength;
4942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ++minLengthRangeCount) {
4952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            count += ranges[minLengthRangeCount].count;
4962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
4972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
4982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int nextCountBytes = countBytes(minLength + 1);
4992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(n > count * nextCountBytes) { return false; }
5002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
5012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // Use the minLength ranges. Merge them, and then split again as necessary.
5022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        long start = ranges[0].start;
5032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        long end = ranges[0].end;
5042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for(int i = 1; i < minLengthRangeCount; ++i) {
5052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(ranges[i].start < start) { start = ranges[i].start; }
5062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(ranges[i].end > end) { end = ranges[i].end; }
5072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
5082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
5092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // Calculate how to split the range between minLength (count1) and minLength+1 (count2).
5102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // Goal:
5112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        //   count1 + count2 * nextCountBytes = n
5122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        //   count1 + count2 = count
5132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // These turn into
5142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        //   (count - count2) + count2 * nextCountBytes = n
5152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // and then into the following count1 & count2 computations.
5162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int count2 = (n - count) / (nextCountBytes - 1);  // number of weights to be lengthened
5172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int count1 = count - count2;  // number of minLength weights
5182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(count2 == 0 || (count1 + count2 * nextCountBytes) < n) {
5192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // round up
5202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ++count2;
5212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            --count1;
5222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            assert((count1 + count2 * nextCountBytes) >= n);
5232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
5242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
5252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        ranges[0].start = start;
5262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
5272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if(count1 == 0) {
5282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // Make one long range.
5292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ranges[0].end = end;
5302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ranges[0].count = count;
5312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            lengthenRange(ranges[0]);
5322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            rangeCount = 1;
5332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
5342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // Split the range, lengthen the second part.
5352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // printf("split the range number %ld (out of %ld minLength ranges) by %ld:%ld\n",
5362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            //       splitRange, rangeCount, count1, count2);
5372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
5382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            // Next start = start + count1. First end = 1 before that.
5392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ranges[0].end = incWeightByOffset(start, minLength, count1 - 1);
5402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ranges[0].count = count1;
5412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
5422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            if(ranges[1] == null) {
5432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller                ranges[1] = new WeightRange();
5442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            }
5452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ranges[1].start = incWeight(ranges[0].end, minLength);
5462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ranges[1].end = end;
5472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ranges[1].length = minLength;  // +1 when lengthened
5482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ranges[1].count = count2;  // *countBytes when lengthened
5492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            lengthenRange(ranges[1]);
5502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            rangeCount = 2;
5512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
5522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return true;
5532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
5542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
5552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int middleLength;
5562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int[] minBytes = new int[5];  // for byte 1, 2, 3, 4
5572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int[] maxBytes = new int[5];
5582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private WeightRange[] ranges = new WeightRange[7];
5592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int rangeIndex;
5602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private int rangeCount;
5612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller}
562