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