jdk/src/share/classes/com/sun/java/util/jar/pack/PopulationCoding.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7192 445c518364c4
child 7816 55a18147b4bf
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7192
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    28
import java.io.ByteArrayOutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    29
import java.io.IOException;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    30
import java.io.InputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    31
import java.io.OutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    32
import java.util.Arrays;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    33
import java.util.HashSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Population-based coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * See the section "Encodings of Uncorrelated Values" in the Pack200 spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
// This tactic alone reduces the final zipped rt.jar by about a percent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class PopulationCoding implements Constants, CodingMethod {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    Histogram vHist;   // histogram of all values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    int[]     fValues; // list of favored values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    int       fVlen;   // inclusive max index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    long[]    symtab;  // int map of favored value -> token [1..#fValues]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    CodingMethod favoredCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    CodingMethod tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    CodingMethod unfavoredCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    int L = -1;  //preferred L value for tokenCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public void setFavoredValues(int[] fValues, int fVlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // Note:  {f} is allFavoredValues[1..fvlen], not [0..fvlen-1].
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // This is because zero is an exceptional favored value index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        assert(fValues[0] == 0);  // must be empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        assert(this.fValues == null);  // do not do this twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.fValues = fValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.fVlen   = fVlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (L >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            setL(L);  // reassert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public void setFavoredValues(int[] fValues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int fVlen = fValues.length-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        setFavoredValues(fValues, fVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public void setHistogram(Histogram vHist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.vHist = vHist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public void setL(int L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.L = L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (L >= 0 && fValues != null && tokenCoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            tokenCoding = fitTokenCoding(fVlen, L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            assert(tokenCoding != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static Coding fitTokenCoding(int fVlen, int L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // Find the smallest B s.t. (B,H,0) covers fVlen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (fVlen < 256)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            // H/L do not matter when B==1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return BandStructure.BYTE1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        Coding longest = BandStructure.UNSIGNED5.setL(L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (!longest.canRepresentUnsigned(fVlen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return null;  // failure; L is too sharp and fVlen too large
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Coding tc = longest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        for (Coding shorter = longest; ; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            shorter = shorter.setB(shorter.B()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (shorter.umax() < fVlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            tc = shorter;  // shorten it by reducing B
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return tc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public void setFavoredCoding(CodingMethod favoredCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.favoredCoding = favoredCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void setTokenCoding(CodingMethod tokenCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.tokenCoding = tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.L = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (tokenCoding instanceof Coding && fValues != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            Coding tc = (Coding) tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (tc == fitTokenCoding(fVlen, tc.L()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                this.L = tc.L();;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            // Otherwise, it's a non-default coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void setUnfavoredCoding(CodingMethod unfavoredCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.unfavoredCoding = unfavoredCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public int favoredValueMaxLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (L == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return BandStructure.UNSIGNED5.setL(L).umax();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public void resortFavoredValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Coding tc = (Coding) tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // Make a local copy before reordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        fValues = BandStructure.realloc(fValues, 1+fVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // Resort favoredValues within each byte-size cadre.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int fillp = 1;  // skip initial zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        for (int n = 1; n <= tc.B(); n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            int nmax = tc.byteMax(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (nmax > fVlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                nmax = fVlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (nmax < tc.byteMin(n))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            int low = fillp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int high = nmax+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (high == low)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            assert(high > low)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                : high+"!>"+low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            assert(tc.getLength(low) == n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                : n+" != len("+(low)+") == "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                  tc.getLength(low);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            assert(tc.getLength(high-1) == n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                : n+" != len("+(high-1)+") == "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                  tc.getLength(high-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            int midTarget = low + (high-low)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            int mid = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            // Divide the values into cadres, and sort within each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            int prevCount = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            int prevLimit = low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            for (int i = low; i < high; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                int val = fValues[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                int count = vHist.getFrequency(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (prevCount != count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    if (n == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        // For the single-byte encoding, keep strict order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        // among frequency groups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        Arrays.sort(fValues, prevLimit, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    } else if (Math.abs(mid - midTarget) >
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                               Math.abs(i   - midTarget)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        // Find a single inflection point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        // close to the middle of the byte-size cadre.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        mid = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    prevCount = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    prevLimit = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (n == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                Arrays.sort(fValues, prevLimit, high);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                // Sort up to the midpoint, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                Arrays.sort(fValues, low, mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                Arrays.sort(fValues, mid, high);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            assert(tc.getLength(low) == tc.getLength(mid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            assert(tc.getLength(low) == tc.getLength(high-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            fillp = nmax+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        assert(fillp == fValues.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // Reset symtab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        symtab = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public int getToken(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (symtab == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            symtab = makeSymtab();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int pos = Arrays.binarySearch(symtab, (long)value << 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (pos < 0)  pos = -pos-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (pos < symtab.length && value == (int)(symtab[pos] >>> 32))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return (int)symtab[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public int[][] encodeValues(int[] values, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // Compute token sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int[] tokens = new int[end-start];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        int nuv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        for (int i = 0; i < tokens.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            int val = values[start+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            int tok = getToken(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (tok != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                tokens[i] = tok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                nuv += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // Compute unfavored value sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int[] unfavoredValues = new int[nuv];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        nuv = 0;  // reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        for (int i = 0; i < tokens.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (tokens[i] != 0)  continue;  // already covered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            int val = values[start+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            unfavoredValues[nuv++] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        assert(nuv == unfavoredValues.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return new int[][]{ tokens, unfavoredValues };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private long[] makeSymtab() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        long[] symtab = new long[fVlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for (int token = 1; token <= fVlen; token++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            symtab[token-1] = ((long)fValues[token] << 32) | token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // Index by value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        Arrays.sort(symtab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return symtab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    private Coding getTailCoding(CodingMethod c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        while (c instanceof AdaptiveCoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            c = ((AdaptiveCoding)c).tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return (Coding) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    // CodingMethod methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public void writeArrayTo(OutputStream out, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int[][] vals = encodeValues(a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        writeSequencesTo(out, vals[0], vals[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    void writeSequencesTo(OutputStream out, int[] tokens, int[] uValues) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        favoredCoding.writeArrayTo(out, fValues, 1, 1+fVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        getTailCoding(favoredCoding).writeTo(out, computeSentinelValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        tokenCoding.writeArrayTo(out, tokens, 0, tokens.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (uValues.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            unfavoredCoding.writeArrayTo(out, uValues, 0, uValues.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
   int computeSentinelValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        Coding fc = getTailCoding(favoredCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (fc.isDelta()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // repeat the last favored value, using delta=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // else repeat the shorter of the min or last value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            int min = fValues[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            int last = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // (remember that fVlen is an inclusive limit in fValues)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            for (int i = 2; i <= fVlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                last = fValues[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                min = moreCentral(min, last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            int endVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (fc.getLength(min) <= fc.getLength(last))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                return last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public void readArrayFrom(InputStream in, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // Parameters are fCode, L, uCode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        setFavoredValues(readFavoredValuesFrom(in, end-start));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // Read the tokens.  Read them into the final array, for the moment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        tokenCoding.readArrayFrom(in, a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // Decode the favored tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        int headp = 0, tailp = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int uVlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            int tok = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (tok == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                // Make a linked list, and decode in a second pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                if (tailp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    headp = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    a[tailp] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                tailp = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                uVlen += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                a[i] = fValues[tok];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // Walk the linked list of "zero" locations, decoding unfavored vals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int[] uValues = new int[uVlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (uVlen > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            unfavoredCoding.readArrayFrom(in, uValues, 0, uVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        for (int i = 0; i < uVlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            int nextp = a[headp];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            a[headp] = uValues[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            headp = nextp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    int[] readFavoredValuesFrom(InputStream in, int maxForDebug) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        int[] fValues = new int[1000];  // realloc as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // The set uniqueValuesForDebug records all favored values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // As each new value is added, we assert that the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // was not already in the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        HashSet uniqueValuesForDebug = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        assert((uniqueValuesForDebug = new HashSet()) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        int fillp = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        maxForDebug += fillp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int min = Integer.MIN_VALUE;  // farthest from the center
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        //int min2 = Integer.MIN_VALUE;  // emulate buggy 150.7 spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        CodingMethod fcm = favoredCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        while (fcm instanceof AdaptiveCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            AdaptiveCoding ac = (AdaptiveCoding) fcm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            int len = ac.headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            while (fillp + len > fValues.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                fValues = BandStructure.realloc(fValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            int newFillp = fillp + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            ac.headCoding.readArrayFrom(in, fValues, fillp, newFillp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            while (fillp < newFillp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                int val = fValues[fillp++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                assert(uniqueValuesForDebug.add(new Integer(val)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                assert(fillp <= maxForDebug);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                last = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                min = moreCentral(min, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                //min2 = moreCentral2(min2, val, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            fcm = ac.tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        Coding fc = (Coding) fcm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (fc.isDelta()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            for (long state = 0;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                // Read a new value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                state += fc.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                int val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (fc.isSubrange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    val = fc.reduceToUnsignedRange(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    val = (int)state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                state = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if (fillp > 1 && (val == last || val == min)) //|| val == min2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                if (fillp == fValues.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    fValues = BandStructure.realloc(fValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                fValues[fillp++] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                assert(uniqueValuesForDebug.add(new Integer(val)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                assert(fillp <= maxForDebug);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                last = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                min = moreCentral(min, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                //min2 = moreCentral(min2, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                int val = fc.readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                if (fillp > 1 && (val == last || val == min)) //|| val == min2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                if (fillp == fValues.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    fValues = BandStructure.realloc(fValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                fValues[fillp++] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                assert(uniqueValuesForDebug.add(new Integer(val)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                assert(fillp <= maxForDebug);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                last = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                min = moreCentral(min, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                //min2 = moreCentral2(min2, val, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return BandStructure.realloc(fValues, fillp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    private static int moreCentral(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        int kx = (x >> 31) ^ (x << 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int ky = (y >> 31) ^ (y << 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        // bias kx/ky to get an unsigned comparison:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        kx -= Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        ky -= Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int xy = (kx < ky? x: y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // assert that this ALU-ish version is the same:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        assert(xy == moreCentralSlow(x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return xy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
//  private static int moreCentral2(int x, int y, int min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
//      // Strict implementation of buggy 150.7 specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
//      // The bug is that the spec. says absolute-value ties are broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
//      // in favor of positive numbers, but the suggested implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
//      // (also mentioned in the spec.) breaks ties in favor of negatives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
//      if (x + y == 0)  return (x > y? x : y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
//      return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
//  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    private static int moreCentralSlow(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        int ax = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (ax < 0)  ax = -ax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (ax < 0)  return y;  //x is MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        int ay = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (ay < 0)  ay = -ay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (ay < 0)  return x;  //y is MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (ax < ay)  return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (ax > ay)  return y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // At this point the absolute values agree, and the negative wins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return x < y ? x : y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    static final int[] LValuesCoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        = { -1, 4, 8, 16, 32, 64, 128, 192, 224, 240, 248, 252 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public byte[] getMetaCoding(Coding dflt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        int K = fVlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        int LCoded = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (tokenCoding instanceof Coding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            Coding tc = (Coding) tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (tc.B() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                LCoded = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            } else if (L >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                assert(L == tc.L());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                for (int i = 1; i < LValuesCoded.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    if (LValuesCoded[i] == L) { LCoded = i; break; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        CodingMethod tokenDflt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (LCoded != 0 && tokenCoding == fitTokenCoding(fVlen, L)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            // A simple L value is enough to recover the tokenCoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            tokenDflt = tokenCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        int FDef = (favoredCoding == dflt)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        int UDef = (unfavoredCoding == dflt || unfavoredCoding == null)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        int TDef = (tokenCoding == tokenDflt)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        int TDefL = (TDef == 1) ? LCoded : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        assert(TDef == ((TDefL>0)?1:0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        bytes.write(_meta_pop + FDef + 2*UDef + 4*TDefL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (FDef == 0)  bytes.write(favoredCoding.getMetaCoding(dflt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (TDef == 0)  bytes.write(tokenCoding.getMetaCoding(dflt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            if (UDef == 0)  bytes.write(unfavoredCoding.getMetaCoding(dflt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            throw new RuntimeException(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return bytes.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public static int parseMetaCoding(byte[] bytes, int pos, Coding dflt, CodingMethod res[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        int op = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (op < _meta_pop || op >= _meta_limit)  return pos-1; // backup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        op -= _meta_pop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        int FDef = op % 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        int UDef = (op / 2) % 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        int TDefL = (op / 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        int TDef = (TDefL > 0)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        int L = LValuesCoded[TDefL];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        CodingMethod[] FCode = {dflt}, TCode = {null}, UCode = {dflt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (FDef == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            pos = BandStructure.parseMetaCoding(bytes, pos, dflt, FCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (TDef == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            pos = BandStructure.parseMetaCoding(bytes, pos, dflt, TCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (UDef == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            pos = BandStructure.parseMetaCoding(bytes, pos, dflt, UCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        PopulationCoding pop = new PopulationCoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        pop.L = L;  // might be -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        pop.favoredCoding   = FCode[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        pop.tokenCoding     = TCode[0];  // might be null!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        pop.unfavoredCoding = UCode[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        res[0] = pop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    private String keyString(CodingMethod m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (m instanceof Coding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return ((Coding)m).keyString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        if (m == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            return "none";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return m.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        PropMap p200 = Utils.currentPropMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        boolean verbose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            = (p200 != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
               p200.getBoolean(Utils.COM_PREFIX+"verbose.pop"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        StringBuffer res = new StringBuffer(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        res.append("pop(").append("fVlen=").append(fVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (verbose && fValues != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            res.append(" fV=[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            for (int i = 1; i <= fVlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                res.append(i==1?"":",").append(fValues[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            res.append(";").append(computeSentinelValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            res.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        res.append(" fc=").append(keyString(favoredCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        res.append(" tc=").append(keyString(tokenCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        res.append(" uc=").append(keyString(unfavoredCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        res.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        return res.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
}