jdk/src/share/classes/com/sun/java/util/jar/pack/CodingChooser.java
author lana
Tue, 04 Jan 2011 17:05:38 -0800
changeset 7816 55a18147b4bf
parent 7795 98021fc612af
parent 7668 d4a77089c587
permissions -rw-r--r--
Merge
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) 2002, 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.OutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    31
import java.util.ArrayList;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    32
import java.util.Collections;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    33
import java.util.HashSet;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    34
import java.util.Iterator;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    35
import java.util.List;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    36
import java.util.Random;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    37
import java.util.Set;
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    38
import java.util.zip.Deflater;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    39
import java.util.zip.DeflaterOutputStream;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    40
import static com.sun.java.util.jar.pack.Constants.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Heuristic chooser of basic encodings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Runs "zip" to measure the apparent information content after coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
    46
class CodingChooser {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    int effort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    boolean optUseHistogram = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    boolean optUsePopulationCoding = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    boolean optUseAdaptiveCoding = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    boolean disablePopCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    boolean disableRunCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    boolean topLevel = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // Derived from effort; >1 (<1) means try more (less) experiments
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // when looking to beat a best score.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    double fuzz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    Coding[] allCodingChoices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    Choice[] choices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    ByteArrayOutputStream context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    CodingChooser popHelper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    CodingChooser runHelper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    Random stress;  // If not null, stress mode oracle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Element in sorted set of coding choices:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    class Choice {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        final Coding coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        final int index;       // index in choices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        final int[] distance;  // cache of distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Choice(Coding coding, int index, int[] distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            this.coding   = coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            this.index    = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            this.distance = distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // These variables are reset and reused:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int searchOrder; // order in which it is checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int minDistance; // min distance from already-checked choices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int zipSize;     // size of encoding in sample, zipped output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int byteSize;    // size of encoding in sample (debug only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        int histSize;    // size of encoding, according to histogram
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            searchOrder = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            minDistance = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            zipSize = byteSize = histSize = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        boolean isExtra() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return index < 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return stringForDebug();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        private String stringForDebug() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            String s = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (searchOrder < Integer.MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                s += " so: "+searchOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (minDistance < Integer.MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                s += " md: "+minDistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (zipSize > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                s += " zs: "+zipSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (byteSize > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                s += " bs: "+byteSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (histSize > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                s += " hs: "+histSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return "Choice["+index+"] "+s+" "+coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    CodingChooser(int effort, Coding[] allCodingChoices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        PropMap p200 = Utils.currentPropMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (p200 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            this.verbose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                = Math.max(p200.getInteger(Utils.DEBUG_VERBOSE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                           p200.getInteger(Utils.COM_PREFIX+"verbose.coding"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            this.optUseHistogram
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                = !p200.getBoolean(Utils.COM_PREFIX+"no.histogram");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            this.optUsePopulationCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                = !p200.getBoolean(Utils.COM_PREFIX+"no.population.coding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            this.optUseAdaptiveCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                = !p200.getBoolean(Utils.COM_PREFIX+"no.adaptive.coding");
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   128
            int lstress
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                = p200.getInteger(Utils.COM_PREFIX+"stress.coding");
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   130
            if (lstress != 0)
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   131
                this.stress = new Random(lstress);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.effort = effort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // The following line "makes sense" but is too much
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // work for a simple heuristic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        //if (effort > 5)  zipDef.setLevel(effort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.allCodingChoices = allCodingChoices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // If effort = 9, look carefully at any solution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // whose initial metrics are within 1% of the best
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // so far.  If effort = 1, look carefully only at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // solutions whose initial metrics promise a 1% win.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.fuzz = 1 + (0.0025 * (effort-MID_EFFORT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int nc = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        for (int i = 0; i < allCodingChoices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (allCodingChoices[i] == null)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            nc++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        choices = new Choice[nc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        nc = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        for (int i = 0; i < allCodingChoices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (allCodingChoices[i] == null)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            int[] distance = new int[choices.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            choices[nc++] = new Choice(allCodingChoices[i], i, distance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        for (int i = 0; i < choices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            Coding ci = choices[i].coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            assert(ci.distanceFrom(ci) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            for (int j = 0; j < i; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                Coding cj = choices[j].coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                int dij = ci.distanceFrom(cj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                assert(dij > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                assert(dij == cj.distanceFrom(ci));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                choices[i].distance[j] = dij;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                choices[j].distance[i] = dij;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    Choice makeExtraChoice(Coding coding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int[] distance = new int[choices.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        for (int i = 0; i < distance.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            Coding ci = choices[i].coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            int dij = coding.distanceFrom(ci);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            assert(dij > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            assert(dij == ci.distanceFrom(coding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            distance[i] = dij;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        Choice c = new Choice(coding, -1, distance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        c.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    ByteArrayOutputStream getContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (context == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            context = new ByteArrayOutputStream(1 << 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    // These variables are reset and reused:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private int[] values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private int start, end;  // slice of values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private int[] deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private int min, max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private Histogram vHist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private Histogram dHist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private int searchOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private Choice regularChoice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private Choice bestChoice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private CodingMethod bestMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private int bestByteSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private int bestZipSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private int targetSize;   // fuzzed target byte size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private void reset(int[] values, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        this.values = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        this.start = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        this.end = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        this.deltas = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        this.min = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        this.max = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.vHist = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        this.dHist = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        this.searchOrder = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        this.regularChoice = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        this.bestChoice = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this.bestMethod = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        this.bestZipSize = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this.bestByteSize = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this.targetSize = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public static final int MIN_EFFORT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static final int MID_EFFORT = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public static final int MAX_EFFORT = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public static final int POP_EFFORT = MID_EFFORT-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public static final int RUN_EFFORT = MID_EFFORT-2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public static final int BYTE_SIZE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public static final int ZIP_SIZE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    CodingMethod choose(int[] values, int start, int end, Coding regular, int[] sizes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        // Save the value array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        reset(values, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (effort <= MIN_EFFORT || start >= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (sizes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                int[] computed = computeSizePrivate(regular);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                sizes[BYTE_SIZE] = computed[BYTE_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                sizes[ZIP_SIZE]  = computed[ZIP_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return regular;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (optUseHistogram) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            getValueHistogram();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            getDeltaHistogram();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            int val = values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (min > val)  min = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (max < val)  max = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // Find all the preset choices that might be worth looking at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int numChoices = markUsableChoices(regular);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (stress != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // Make a random choice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            int rand = stress.nextInt(numChoices*2 + 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            CodingMethod coding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            for (int i = 0; i < choices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                Choice c = choices[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (c.searchOrder >= 0 && rand-- == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    coding = c.coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (coding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if ((rand & 7) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    coding = regular;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    // Pick a totally random coding 6% of the time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    coding = stressCoding(min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if (!disablePopCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                && optUsePopulationCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                && effort >= POP_EFFORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                coding = stressPopCoding(coding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (!disableRunCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                && optUseAdaptiveCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                && effort >= RUN_EFFORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                coding = stressAdaptiveCoding(coding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        double searchScale = 1.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        for (int x = effort; x < MAX_EFFORT; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            searchScale /= 1.414;  // every 2 effort points doubles work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        int searchOrderLimit = (int)Math.ceil( numChoices * searchScale );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // Start by evaluating the "regular" choice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        bestChoice = regularChoice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        evaluate(regularChoice);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int maxd = updateDistances(regularChoice);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // save these first-cut numbers for later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        int zipSize1 = bestZipSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        int byteSize1 = bestByteSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (regularChoice.coding == regular && topLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            // Give credit for being the default; no band header is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            // Rather than increasing every other size value by the band
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            // header amount, we decrement this one metric, to give it an edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // Decreasing zipSize by a byte length is conservatively correct,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // especially considering that the escape byte is not likely to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // zip well with other bytes in the band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            int X = BandStructure.encodeEscapeValue(_meta_canon_max, regular);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (regular.canRepresentSigned(X)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                int Xlen = regular.getLength(X);  // band coding header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                //regularChoice.histSize -= Xlen; // keep exact byteSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                //regularChoice.byteSize -= Xlen; // keep exact byteSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                regularChoice.zipSize -= Xlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                bestByteSize = regularChoice.byteSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                bestZipSize = regularChoice.zipSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        int dscale = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // Continually select a new choice to evaluate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        while (searchOrder < searchOrderLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            Choice nextChoice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (dscale > maxd)  dscale = 1;  // cycle dscale values!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            int dhi = maxd / dscale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            int dlo = maxd / (dscale *= 2) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            nextChoice = findChoiceNear(bestChoice, dhi, dlo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (nextChoice == null)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            assert(nextChoice.coding.canRepresent(min, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            evaluate(nextChoice);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            int nextMaxd = updateDistances(nextChoice);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (nextChoice == bestChoice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                maxd = nextMaxd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (verbose > 5)  Utils.log.info("maxd = "+maxd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // Record best "plain coding" choice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        Coding plainBest = bestChoice.coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        assert(plainBest == bestMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (verbose > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            Utils.log.info("chooser: plain result="+bestChoice+" after "+bestChoice.searchOrder+" rounds, "+(regularChoice.zipSize-bestZipSize)+" fewer bytes than regular "+regular);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        bestChoice = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (!disablePopCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            && optUsePopulationCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            && effort >= POP_EFFORT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            && bestMethod instanceof Coding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            tryPopulationCoding(plainBest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (!disableRunCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            && optUseAdaptiveCoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            && effort >= RUN_EFFORT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            && bestMethod instanceof Coding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            tryAdaptiveCoding(plainBest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        // Pass back the requested information:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (sizes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            sizes[BYTE_SIZE] = bestByteSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            sizes[ZIP_SIZE]  = bestZipSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (verbose > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            Utils.log.info("chooser: result="+bestMethod+" "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                             (zipSize1-bestZipSize)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                             " fewer bytes than regular "+regular+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                             "; win="+pct(zipSize1-bestZipSize, zipSize1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   380
        CodingMethod lbestMethod = this.bestMethod;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        reset(null, 0, 0);  // for GC
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   382
        return lbestMethod;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    CodingMethod choose(int[] values, int start, int end, Coding regular) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return choose(values, start, end, regular, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    CodingMethod choose(int[] values, Coding regular, int[] sizes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return choose(values, 0, values.length, regular, sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    CodingMethod choose(int[] values, Coding regular) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return choose(values, 0, values.length, regular, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    private int markUsableChoices(Coding regular) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        int numChoices = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        for (int i = 0; i < choices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            Choice c = choices[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            c.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (!c.coding.canRepresent(min, max)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                // Mark as already visited:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                c.searchOrder = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                if (verbose > 1 && c.coding == regular) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    Utils.log.info("regular coding cannot represent ["+min+".."+max+"]: "+regular);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (c.coding == regular)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                regularChoice = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            numChoices++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (regularChoice == null && regular.canRepresent(min, max)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            regularChoice = makeExtraChoice(regular);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (verbose > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                Utils.log.info("*** regular choice is extra: "+regularChoice.coding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (regularChoice == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            for (int i = 0; i < choices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                Choice c = choices[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                if (c.searchOrder != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    regularChoice = c;  // arbitrary pick
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (verbose > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                Utils.log.info("*** regular choice does not apply "+regular);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                Utils.log.info("    using instead "+regularChoice.coding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (verbose > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            Utils.log.info("chooser: #choices="+numChoices+" ["+min+".."+max+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if (verbose > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                for (int i = 0; i < choices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    Choice c = choices[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    if (c.searchOrder >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        Utils.log.info("  "+c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return numChoices;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    // Find an arbitrary choice at least dlo away from a previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    // evaluated choices, and at most dhi.  Try also to regulate its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    // min distance to all previously evaluated choices, in this range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    private Choice findChoiceNear(Choice near, int dhi, int dlo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (verbose > 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            Utils.log.info("findChoice "+dhi+".."+dlo+" near: "+near);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        int[] distance = near.distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        Choice found = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        for (int i = 0; i < choices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            Choice c = choices[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            if (c.searchOrder < searchOrder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                continue;  // already searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            // Distance from "near" guy must be in bounds:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            if (distance[i] >= dlo && distance[i] <= dhi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                // Try also to keep min-distance from other guys in bounds:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if (c.minDistance >= dlo && c.minDistance <= dhi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    if (verbose > 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        Utils.log.info("findChoice => good "+c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                found = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (verbose > 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            Utils.log.info("findChoice => found "+found);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    private void evaluate(Choice c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        assert(c.searchOrder == Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        c.searchOrder = searchOrder++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        boolean mustComputeSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (c == bestChoice || c.isExtra()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            mustComputeSize = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        } else if (optUseHistogram) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            Histogram hist = getHistogram(c.coding.isDelta());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            c.histSize = (int)Math.ceil(hist.getBitLength(c.coding) / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            c.byteSize = c.histSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            mustComputeSize = (c.byteSize <= targetSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            mustComputeSize = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (mustComputeSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            int[] sizes = computeSizePrivate(c.coding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            c.byteSize = sizes[BYTE_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            c.zipSize  = sizes[ZIP_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (noteSizes(c.coding, c.byteSize, c.zipSize))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                bestChoice = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (c.histSize >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            assert(c.byteSize == c.histSize);  // models should agree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (verbose > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            Utils.log.info("evaluated "+c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    private boolean noteSizes(CodingMethod c, int byteSize, int zipSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        assert(zipSize > 0 && byteSize > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        boolean better = (zipSize < bestZipSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (verbose > 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            Utils.log.info("computed size "+c+" "+byteSize+"/zs="+zipSize+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                             ((better && bestMethod != null)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                              (" better by "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                               pct(bestZipSize - zipSize, zipSize)): ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (better) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            bestMethod = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            bestZipSize = zipSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            bestByteSize = byteSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            targetSize = (int)(byteSize * fuzz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private int updateDistances(Choice c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        // update all minDistance values in still unevaluated choices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int[] distance = c.distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        int maxd = 0;  // how far is c from everybody else?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        for (int i = 0; i < choices.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            Choice c2 = choices[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            if (c2.searchOrder < searchOrder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            int d = distance[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (verbose > 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                Utils.log.info("evaluate dist "+d+" to "+c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            int mind = c2.minDistance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            if (mind > d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                c2.minDistance = mind = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if (maxd < d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                maxd = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        // Now maxd has the distance of the farthest outlier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        // from all evaluated choices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (verbose > 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            Utils.log.info("evaluate maxd => "+maxd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        return maxd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    // Compute the coded size of a sequence of values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    // The first int is the size in uncompressed bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    // The second is an estimate of the compressed size of these bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public void computeSize(CodingMethod c, int[] values, int start, int end, int[] sizes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (end <= start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            sizes[BYTE_SIZE] = sizes[ZIP_SIZE] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            resetData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            c.writeArrayTo(byteSizer, values, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            sizes[BYTE_SIZE] = getByteSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            sizes[ZIP_SIZE] = getZipSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            throw new RuntimeException(ee); // cannot happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    public void computeSize(CodingMethod c, int[] values, int[] sizes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        computeSize(c, values, 0, values.length, sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    public int[] computeSize(CodingMethod c, int[] values, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        int[] sizes = { 0, 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        computeSize(c, values, start, end, sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return sizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    public int[] computeSize(CodingMethod c, int[] values) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return computeSize(c, values, 0, values.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    // This version uses the implicit local arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    private int[] computeSizePrivate(CodingMethod c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        int[] sizes = { 0, 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        computeSize(c, values, start, end, sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return sizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    public int computeByteSize(CodingMethod cm, int[] values, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        int len = end-start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if (cm instanceof Coding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            Coding c = (Coding) cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            int size = c.getLength(values, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            int size2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            assert(size == (size2=countBytesToSizer(cm, values, start, end)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                : (cm+" : "+size+" != "+size2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        return countBytesToSizer(cm, values, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    private int countBytesToSizer(CodingMethod cm, int[] values, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            byteOnlySizer.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            cm.writeArrayTo(byteOnlySizer, values, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            return byteOnlySizer.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            throw new RuntimeException(ee); // cannot happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    int[] getDeltas(int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if ((min|max) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            return Coding.makeDeltas(values, start, end, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if (deltas == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            deltas = Coding.makeDeltas(values, start, end, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        return deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    Histogram getValueHistogram() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (vHist == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            vHist = new Histogram(values, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            if (verbose > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                vHist.print("vHist", System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            } else if (verbose > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                vHist.print("vHist", null, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return vHist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    Histogram getDeltaHistogram() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (dHist == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            dHist = new Histogram(getDeltas(0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            if (verbose > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                dHist.print("dHist", System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            } else if (verbose > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                dHist.print("dHist", null, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return dHist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    Histogram getHistogram(boolean isDelta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return isDelta ? getDeltaHistogram(): getValueHistogram();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    private void tryPopulationCoding(Coding plainCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        // assert(plainCoding.canRepresent(min, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        Histogram hist = getValueHistogram();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        // Start with "reasonable" default codings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        final int approxL = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        Coding favoredCoding = plainCoding.getValueCoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        Coding tokenCoding = BandStructure.UNSIGNED5.setL(approxL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        Coding unfavoredCoding = plainCoding.getValueCoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        // There's going to be a band header.  Estimate conservatively large.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        final int BAND_HEADER = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        // Keep a running model of the predicted sizes of the F/T/U sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        int currentFSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        int currentTSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        int currentUSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        // Start by assuming a degenerate favored-value length of 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        // which looks like a bunch of zero tokens followed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        // original sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        // The {F} list ends with a repeated F value; find worst case:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        currentFSize =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            BAND_HEADER + Math.max(favoredCoding.getLength(min),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                   favoredCoding.getLength(max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        // The {T} list starts out a bunch of zeros, each of length 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        final int ZERO_LEN = tokenCoding.getLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        currentTSize = ZERO_LEN * (end-start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        // The {U} list starts out a copy of the plainCoding:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        currentUSize = (int) Math.ceil(hist.getBitLength(unfavoredCoding) / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        int bestPopSize = (currentFSize + currentTSize + currentUSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        int bestPopFVC  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        // Record all the values, in decreasing order of favor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        int[] allFavoredValues = new int[1+hist.getTotalLength()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        //int[] allPopSizes    = new int[1+hist.getTotalLength()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        // What sizes are "interesting"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        int targetLowFVC = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        int targetHighFVC = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        // For each length, adjust the currentXSize model, and look for a win.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        int[][] matrix = hist.getMatrix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        int mrow = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        int mcol = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        int mrowFreq = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        for (int fvcount = 1; fvcount <= hist.getTotalLength(); fvcount++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            // The {F} list gets an additional member.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            // Take it from the end of the current matrix row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            // (It's the end, so that we get larger favored values first.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            if (mcol == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                mrow += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                mrowFreq = matrix[mrow][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                mcol = matrix[mrow].length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            int thisValue = matrix[mrow][--mcol];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            allFavoredValues[fvcount] = thisValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            int thisVLen = favoredCoding.getLength(thisValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            currentFSize += thisVLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            // The token list replaces occurrences of zero with a new token:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            int thisVCount = mrowFreq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            int thisToken = fvcount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            currentTSize += (tokenCoding.getLength(thisToken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                             - ZERO_LEN) * thisVCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            // The unfavored list loses occurrences of the newly favored value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            // (This is the whole point of the exercise!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            currentUSize -= thisVLen * thisVCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            int currentSize = (currentFSize + currentTSize + currentUSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            //allPopSizes[fvcount] = currentSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            if (bestPopSize > currentSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                if (currentSize <= targetSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    targetHighFVC = fvcount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    if (targetLowFVC < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        targetLowFVC = fvcount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    if (verbose > 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        Utils.log.info("better pop-size at fvc="+fvcount+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                                         " by "+pct(bestPopSize-currentSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                                                    bestPopSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                bestPopSize = currentSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                bestPopFVC = fvcount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (targetLowFVC < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (verbose > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                // Complete loss.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                if (verbose > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    Utils.log.info("no good pop-size; best was "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                                     bestPopSize+" at "+bestPopFVC+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                                     " worse by "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                                     pct(bestPopSize-bestByteSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                                         bestByteSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (verbose > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            Utils.log.info("initial best pop-size at fvc="+bestPopFVC+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                             " in ["+targetLowFVC+".."+targetHighFVC+"]"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                             " by "+pct(bestByteSize-bestPopSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                                        bestByteSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        int oldZipSize = bestZipSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        // Now close onto a specific coding, testing more rigorously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        // with the zipSize metric.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        // Questions to decide:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        //   1. How many favored values?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        //   2. What token coding (TC)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        //   3. Sort favored values by value within length brackets?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        //   4. What favored coding?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        //   5. What unfavored coding?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        // Steps 1/2/3 are interdependent, and may be iterated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        // Steps 4 and 5 may be decided independently afterward.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        int[] LValuesCoded = PopulationCoding.LValuesCoded;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   746
        List<Coding> bestFits = new ArrayList<>();
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   747
        List<Coding> fullFits = new ArrayList<>();
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   748
        List<Coding> longFits = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        final int PACK_TO_MAX_S = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        if (bestPopFVC <= 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            bestFits.add(BandStructure.BYTE1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            int bestB = Coding.B_MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            boolean doFullAlso = (effort > POP_EFFORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            if (doFullAlso)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                fullFits.add(BandStructure.BYTE1.setS(PACK_TO_MAX_S));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            for (int i = LValuesCoded.length-1; i >= 1; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                int L = LValuesCoded[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                Coding c0 = PopulationCoding.fitTokenCoding(targetLowFVC,  L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                Coding c1 = PopulationCoding.fitTokenCoding(bestPopFVC,    L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                Coding c3 = PopulationCoding.fitTokenCoding(targetHighFVC, L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                if (c1 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    if (!bestFits.contains(c1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                        bestFits.add(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    if (bestB > c1.B())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                        bestB = c1.B();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                if (doFullAlso) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    if (c3 == null)  c3 = c1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    for (int B = c0.B(); B <= c3.B(); B++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                        if (B == c1.B())  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                        if (B == 1)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                        Coding c2 = c3.setB(B).setS(PACK_TO_MAX_S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                        if (!fullFits.contains(c2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                            fullFits.add(c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            // interleave all B greater than bestB with best and full fits
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   780
            for (Iterator<Coding> i = bestFits.iterator(); i.hasNext(); ) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   781
                Coding c = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                if (c.B() > bestB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    longFits.add(0, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   788
        List<Coding> allFits = new ArrayList<>();
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   789
        for (Iterator<Coding> i = bestFits.iterator(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                      j = fullFits.iterator(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                      k = longFits.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
             i.hasNext() || j.hasNext() || k.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            if (i.hasNext())  allFits.add(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            if (j.hasNext())  allFits.add(j.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            if (k.hasNext())  allFits.add(k.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        bestFits.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        fullFits.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        longFits.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        int maxFits = allFits.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        if (effort == POP_EFFORT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            maxFits = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        else if (maxFits > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            maxFits -= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            maxFits = (maxFits * (effort-POP_EFFORT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                       ) / (MAX_EFFORT-POP_EFFORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            maxFits += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        if (allFits.size() > maxFits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            if (verbose > 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                Utils.log.info("allFits before clip: "+allFits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            allFits.subList(maxFits, allFits.size()).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        if (verbose > 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            Utils.log.info("allFits: "+allFits);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   816
        for (Coding tc : allFits) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            boolean packToMax = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            if (tc.S() == PACK_TO_MAX_S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                // Kludge:  setS(PACK_TO_MAX_S) means packToMax here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                packToMax = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                tc = tc.setS(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            int fVlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            if (!packToMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                fVlen = bestPopFVC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                assert(tc.umax() >= fVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                assert(tc.B() == 1 || tc.setB(tc.B()-1).umax() < fVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                fVlen = Math.min(tc.umax(), targetHighFVC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                if (fVlen < targetLowFVC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                if (fVlen == bestPopFVC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    continue;  // redundant test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            PopulationCoding pop = new PopulationCoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            pop.setHistogram(hist);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            pop.setL(tc.L());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            pop.setFavoredValues(allFavoredValues, fVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            assert(pop.tokenCoding == tc);  // predict correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            pop.resortFavoredValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            int[] tcsizes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                computePopSizePrivate(pop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                                      favoredCoding, unfavoredCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            noteSizes(pop, tcsizes[BYTE_SIZE], BAND_HEADER+tcsizes[ZIP_SIZE]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        if (verbose > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            Utils.log.info("measured best pop, size="+bestByteSize+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                             "/zs="+bestZipSize+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                             " better by "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                             pct(oldZipSize-bestZipSize, oldZipSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            if (bestZipSize < oldZipSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                Utils.log.info(">>> POP WINS BY "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                                 (oldZipSize - bestZipSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    int[] computePopSizePrivate(PopulationCoding pop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                                Coding favoredCoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                                Coding unfavoredCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if (popHelper == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            popHelper = new CodingChooser(effort, allCodingChoices);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            if (stress != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                popHelper.addStressSeed(stress.nextInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            popHelper.topLevel = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            popHelper.verbose -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            popHelper.disablePopCoding = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            popHelper.disableRunCoding = this.disableRunCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            if (effort < MID_EFFORT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                // No nested run codings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                popHelper.disableRunCoding = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        int fVlen = pop.fVlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        if (verbose > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            Utils.log.info("computePopSizePrivate fvlen="+fVlen+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                             " tc="+pop.tokenCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            Utils.log.info("{ //BEGIN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        // Find good coding choices for the token and unfavored sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        int[] favoredValues = pop.fValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        int[][] vals = pop.encodeValues(values, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        int[] tokens = vals[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        int[] unfavoredValues = vals[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            Utils.log.info("-- refine on fv["+fVlen+"] fc="+favoredCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        pop.setFavoredCoding(popHelper.choose(favoredValues, 1, 1+fVlen, favoredCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        if (pop.tokenCoding instanceof Coding &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            (stress == null || stress.nextBoolean())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                Utils.log.info("-- refine on tv["+tokens.length+"] tc="+pop.tokenCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            CodingMethod tc = popHelper.choose(tokens, (Coding) pop.tokenCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            if (tc != pop.tokenCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                    Utils.log.info(">>> refined tc="+tc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                pop.setTokenCoding(tc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        if (unfavoredValues.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            pop.setUnfavoredCoding(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                Utils.log.info("-- refine on uv["+unfavoredValues.length+"] uc="+pop.unfavoredCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            pop.setUnfavoredCoding(popHelper.choose(unfavoredValues, unfavoredCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        if (verbose > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            Utils.log.info("finish computePopSizePrivate fvlen="+fVlen+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                             " fc="+pop.favoredCoding+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                             " tc="+pop.tokenCoding+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                             " uc="+pop.unfavoredCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            //pop.hist.print("pop-hist", null, System.out);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   913
            StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            sb.append("fv = {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            for (int i = 1; i <= fVlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                if ((i % 10) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                    sb.append('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                sb.append(" ").append(favoredValues[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            sb.append('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            sb.append("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            Utils.log.info(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        if (verbose > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            Utils.log.info("} //END");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        if (stress != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            return null;  // do not bother with size computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        int[] sizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            resetData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            // Write the array of favored values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            pop.writeSequencesTo(byteSizer, tokens, unfavoredValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            sizes = new int[] { getByteSize(), getZipSize() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            throw new RuntimeException(ee); // cannot happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        int[] checkSizes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        assert((checkSizes = computeSizePrivate(pop)) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        assert(checkSizes[BYTE_SIZE] == sizes[BYTE_SIZE])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            : (checkSizes[BYTE_SIZE]+" != "+sizes[BYTE_SIZE]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        return sizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    private void tryAdaptiveCoding(Coding plainCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        int oldZipSize = bestZipSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        // Scan the value sequence, determining whether an interesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        // run occupies too much space.  ("Too much" means, say 5% more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        // than the average integer size of the band as a whole.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        // Try to find a better coding for those segments.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   952
        int   lstart  = this.start;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   953
        int   lend    = this.end;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   954
        int[] lvalues = this.values;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   955
        int len = lend-lstart;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        if (plainCoding.isDelta()) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   957
            lvalues = getDeltas(0,0); //%%% not quite right!
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   958
            lstart = 0;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   959
            lend = lvalues.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        int[] sizes = new int[len+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        int fillp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        int totalSize = 0;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   964
        for (int i = lstart; i < lend; i++) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
   965
            int val = lvalues[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            sizes[fillp++] = totalSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            int size = plainCoding.getLength(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            assert(size < Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            //System.out.println("len "+val+" = "+size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            totalSize += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        sizes[fillp++] = totalSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        assert(fillp == sizes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        double avgSize = (double)totalSize / len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        double sizeFuzz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        double sizeFuzz2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        double sizeFuzz3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        if (effort >= MID_EFFORT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            if (effort > MID_EFFORT+1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                sizeFuzz = 1.001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                sizeFuzz = 1.003;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            if (effort > RUN_EFFORT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                sizeFuzz = 1.01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                sizeFuzz = 1.03;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        // for now:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        sizeFuzz *= sizeFuzz; // double the thresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        sizeFuzz2 = (sizeFuzz*sizeFuzz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        sizeFuzz3 = (sizeFuzz*sizeFuzz*sizeFuzz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        // Find some mesh scales we like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        double[] dmeshes = new double[1 + (effort-RUN_EFFORT)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        double logLen = Math.log(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        for (int i = 0; i < dmeshes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            dmeshes[i] = Math.exp(logLen*(i+1)/(dmeshes.length+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        int[] meshes = new int[dmeshes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        int mfillp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        for (int i = 0; i < dmeshes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            int m = (int)Math.round(dmeshes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            m = AdaptiveCoding.getNextK(m-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            if (m <= 0 || m >= len)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            if (mfillp > 0 && m == meshes[mfillp-1])  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            meshes[mfillp++] = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        meshes = BandStructure.realloc(meshes, mfillp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        // There's going to be a band header.  Estimate conservatively large.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        final int BAND_HEADER = 4; // op, KB, A, B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        // Threshold values for a "too big" mesh.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        int[]    threshes = new int[meshes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        double[] fuzzes   = new double[meshes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        for (int i = 0; i < meshes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            int mesh = meshes[i];
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1016
            double lfuzz;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            if (mesh < 10)
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1018
                lfuzz = sizeFuzz3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            else if (mesh < 100)
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1020
                lfuzz = sizeFuzz2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            else
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1022
                lfuzz = sizeFuzz;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1023
            fuzzes[i] = lfuzz;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1024
            threshes[i] = BAND_HEADER + (int)Math.ceil(mesh * avgSize * lfuzz);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        if (verbose > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            System.out.print("tryAdaptiveCoding ["+len+"]"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                             " avgS="+avgSize+" fuzz="+sizeFuzz+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                             " meshes: {");
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1030
            for (int i = 0; i < meshes.length; i++) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1031
                System.out.print(" " + meshes[i] + "(" + threshes[i] + ")");
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1032
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            Utils.log.info(" }");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if (runHelper == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            runHelper = new CodingChooser(effort, allCodingChoices);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            if (stress != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                runHelper.addStressSeed(stress.nextInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            runHelper.topLevel = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            runHelper.verbose -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            runHelper.disableRunCoding = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            runHelper.disablePopCoding = this.disablePopCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            if (effort < MID_EFFORT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                // No nested pop codings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                runHelper.disablePopCoding = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            i = AdaptiveCoding.getNextK(i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            if (i > len)  i = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            for (int j = meshes.length-1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                int mesh   = meshes[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                int thresh = threshes[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                if (i+mesh > len)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                int size = sizes[i+mesh] - sizes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                if (size >= thresh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    // Found a size bulge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    int bend  = i+mesh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    int bsize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    double bigSize = avgSize * fuzzes[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    while (bend < len && (bend-i) <= len/2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                        int bend0 = bend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                        int bsize0 = bsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                        bend += mesh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                        bend = i+AdaptiveCoding.getNextK(bend-i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                        if (bend < 0 || bend > len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                            bend = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                        bsize = sizes[bend]-sizes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                        if (bsize < BAND_HEADER + (bend-i) * bigSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                            bsize = bsize0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                            bend = bend0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    int nexti = bend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    if (verbose > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                        Utils.log.info("bulge at "+i+"["+(bend-i)+"] of "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                                         pct(bsize - avgSize*(bend-i),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                                             avgSize*(bend-i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                        Utils.log.info("{ //BEGIN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                    CodingMethod begcm, midcm, endcm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                    midcm = runHelper.choose(this.values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                                             this.start+i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                                             this.start+bend,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                                             plainCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    if (midcm == plainCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                        // No use working further.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                        begcm = plainCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                        endcm = plainCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                        begcm = runHelper.choose(this.values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                                                 this.start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                                                 this.start+i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                                                 plainCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                        endcm = runHelper.choose(this.values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                                                 this.start+bend,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                                                 this.start+len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                                                 plainCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    if (verbose > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                        Utils.log.info("} //END");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    if (begcm == midcm && i > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                        AdaptiveCoding.isCodableLength(bend)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    if (midcm == endcm && bend < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                        bend = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                    if (begcm != plainCoding ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                        midcm != plainCoding ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                        endcm != plainCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                        CodingMethod chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                        int hlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                        if (bend == len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                            chain = midcm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                            chain = new AdaptiveCoding(bend-i, midcm, endcm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                            hlen += BAND_HEADER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                        if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                            chain = new AdaptiveCoding(i, begcm, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                            hlen += BAND_HEADER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                        int[] chainSize = computeSizePrivate(chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                        noteSizes(chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                                  chainSize[BYTE_SIZE],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                                  chainSize[ZIP_SIZE]+hlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                    i = nexti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        if (verbose > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            if (bestZipSize < oldZipSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                Utils.log.info(">>> RUN WINS BY "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                                 (oldZipSize - bestZipSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    static private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    String pct(double num, double den) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        return (Math.round((num / den)*10000)/100.0)+"%";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    static
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    class Sizer extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        final OutputStream out;  // if non-null, copy output here also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        Sizer(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        Sizer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        private int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            if (out != null)  out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            if (out != null)  out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        public int getSize() { return count; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            String str = super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            // If -ea, print out more informative strings!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            assert((str = stringForDebug()) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        String stringForDebug() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            return "<Sizer "+getSize()+">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    private Sizer zipSizer  = new Sizer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    private Deflater zipDef = new Deflater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    private DeflaterOutputStream zipOut = new DeflaterOutputStream(zipSizer, zipDef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    private Sizer byteSizer = new Sizer(zipOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    private Sizer byteOnlySizer = new Sizer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    private void resetData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        flushData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        zipDef.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        if (context != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            // Prepend given salt to the test output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                context.writeTo(byteSizer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                throw new RuntimeException(ee); // cannot happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        zipSizer.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        byteSizer.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    private void flushData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            zipOut.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            throw new RuntimeException(ee); // cannot happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    private int getByteSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        return byteSizer.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    private int getZipSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        flushData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        return zipSizer.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    /// Stress-test helpers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    void addStressSeed(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        if (stress == null)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        stress.setSeed(x + ((long)stress.nextInt() << 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    // Pick a random pop-coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    private CodingMethod stressPopCoding(CodingMethod coding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        assert(stress != null);  // this method is only for testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        // Don't turn it into a pop coding if it's already something special.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        if (!(coding instanceof Coding))  return coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        Coding valueCoding = ((Coding)coding).getValueCoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        Histogram hist = getValueHistogram();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        int fVlen = stressLen(hist.getTotalLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        if (fVlen == 0)  return coding;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1233
        List<Integer> popvals = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        if (stress.nextBoolean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            // Build the population from the value list.
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1236
            Set<Integer> popset = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            for (int i = start; i < end; i++) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1238
                if (popset.add(values[i]))  popvals.add(values[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            int[][] matrix = hist.getMatrix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            for (int mrow = 0; mrow < matrix.length; mrow++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                int[] row = matrix[mrow];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                for (int mcol = 1; mcol < row.length; mcol++) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1245
                    popvals.add(row[mcol]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        int reorder = stress.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        if ((reorder & 7) <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            // Lose the order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            Collections.shuffle(popvals, stress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            // Keep the order, mostly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            if (((reorder >>>= 3) & 7) <= 2)  Collections.sort(popvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            if (((reorder >>>= 3) & 7) <= 2)  Collections.reverse(popvals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            if (((reorder >>>= 3) & 7) <= 2)  Collections.rotate(popvals, stressLen(popvals.size()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        if (popvals.size() > fVlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            // Cut the list down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            if (((reorder >>>= 3) & 7) <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                // Cut at end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                popvals.subList(fVlen,   popvals.size()).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                // Cut at start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                popvals.subList(0, popvals.size()-fVlen).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        fVlen = popvals.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        int[] fvals = new int[1+fVlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        for (int i = 0; i < fVlen; i++) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1272
            fvals[1+i] = (popvals.get(i)).intValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        PopulationCoding pop = new PopulationCoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        pop.setFavoredValues(fvals, fVlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        int[] lvals = PopulationCoding.LValuesCoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        for (int i = 0; i < lvals.length / 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            int popl = lvals[stress.nextInt(lvals.length)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            if (popl < 0)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            if (PopulationCoding.fitTokenCoding(fVlen, popl) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                pop.setL(popl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        if (pop.tokenCoding == null) {
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1286
            int lmin = fvals[1], lmax = lmin;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            for (int i = 2; i <= fVlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                int val = fvals[i];
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1289
                if (lmin > val)  lmin = val;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1290
                if (lmax < val)  lmax = val;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            }
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1292
            pop.tokenCoding = stressCoding(lmin, lmax);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        computePopSizePrivate(pop, valueCoding, valueCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        return pop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    // Pick a random adaptive coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    private CodingMethod stressAdaptiveCoding(CodingMethod coding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        assert(stress != null);  // this method is only for testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        // Don't turn it into a run coding if it's already something special.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        if (!(coding instanceof Coding))  return coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        Coding plainCoding = (Coding)coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        int len = end-start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        if (len < 2)  return coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        // Decide how many spans we'll create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        int spanlen = stressLen(len-1)+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        if (spanlen == len)  return coding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            assert(!disableRunCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            disableRunCoding = true;  // temporary, while I decide spans
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1313
            int[] allValues = values.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            CodingMethod result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            int scan  = this.end;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1316
            int lstart = this.start;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1317
            for (int split; scan > lstart; scan = split) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                int thisspan;
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1319
                int rand = (scan - lstart < 100)? -1: stress.nextInt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                if ((rand & 7) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                    thisspan = (spanlen==1? spanlen: stressLen(spanlen-1)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                    // Every so often generate a value based on KX/KB format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                    int KX = (rand >>>= 3) & AdaptiveCoding.KX_MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                    int KB = (rand >>>= 3) & AdaptiveCoding.KB_MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                        thisspan = AdaptiveCoding.decodeK(KX, KB);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1328
                        if (thisspan <= scan - lstart)  break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                        // Try smaller and smaller codings:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                        if (KB != AdaptiveCoding.KB_DEFAULT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                            KB = AdaptiveCoding.KB_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                            KX -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                    //System.out.println("KX="+KX+" KB="+KB+" K="+thisspan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                    assert(AdaptiveCoding.isCodableLength(thisspan));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                }
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1338
                if (thisspan > scan - lstart)  thisspan = scan - lstart;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1339
                while (!AdaptiveCoding.isCodableLength(thisspan)) {
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1340
                    --thisspan;
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1341
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                split = scan - thisspan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                assert(split < scan);
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1344
                assert(split >= lstart);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                // Choose a coding for the span [split..scan).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                CodingMethod sc = choose(allValues, split, scan, plainCoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    result = sc;  // the caboose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                    result = new AdaptiveCoding(scan-split, sc, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            disableRunCoding = false; // return to normal value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    // Return a random value in [0..len], gently biased toward extremes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    private Coding stressCoding(int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        assert(stress != null);  // this method is only for testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        for (int i = 0; i < 100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            Coding c = Coding.of(stress.nextInt(Coding.B_MAX)+1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                                 stress.nextInt(Coding.H_MAX)+1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                                 stress.nextInt(Coding.S_MAX+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            if (c.B() == 1)  c = c.setH(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            if (c.H() == 256 && c.B() >= 5)  c = c.setB(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            if (stress.nextBoolean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                Coding dc = c.setD(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                if (dc.canRepresent(min, max))  return dc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            if (c.canRepresent(min, max))  return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        return BandStructure.UNSIGNED5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    // Return a random value in [0..len], gently biased toward extremes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    private int stressLen(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        assert(stress != null);  // this method is only for testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        assert(len >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        int rand = stress.nextInt(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        if (rand < 20)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            return Math.min(len/5, rand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        else if (rand < 40)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            return stress.nextInt(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    // For debug only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    int[] readValuesFrom(InputStream instr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        return readValuesFrom(new InputStreamReader(instr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    int[] readValuesFrom(Reader inrdr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        inrdr = new BufferedReader(inrdr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        final StreamTokenizer in = new StreamTokenizer(inrdr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        final int TT_NOTHING = -99;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        in.commentChar('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        return readValuesFrom(new Iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            int token = TT_NOTHING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            private int getToken() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                if (token == TT_NOTHING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                        token = in.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                        assert(token != TT_NOTHING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                    } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                        throw new RuntimeException(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                return token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                return getToken() != StreamTokenizer.TT_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            public Object next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                int ntok = getToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                token = TT_NOTHING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                switch (ntok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                case StreamTokenizer.TT_EOF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                case StreamTokenizer.TT_NUMBER:
7795
98021fc612af 6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents: 7192
diff changeset
  1425
                    return Integer.valueOf((int) in.nval);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                    assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    int[] readValuesFrom(Iterator iter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        return readValuesFrom(iter, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    int[] readValuesFrom(Iterator iter, int initSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        int[] na = new int[Math.max(10, initSize)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        int np = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            Integer val = (Integer) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            if (np == na.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                na = BandStructure.realloc(na);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            na[np++] = val.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        if (np != na.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            na = BandStructure.realloc(na, np);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        return na;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    void main(String[] av) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        int effort = MID_EFFORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        int ap = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        if (ap < av.length && av[ap].equals("-e")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            ap++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            effort = Integer.parseInt(av[ap++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        int verbose = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        if (ap < av.length && av[ap].equals("-v")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            ap++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            verbose = Integer.parseInt(av[ap++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        Coding[] bcs = BandStructure.getBasicCodings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        CodingChooser cc = new CodingChooser(effort, bcs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        if (ap < av.length && av[ap].equals("-p")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            ap++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            cc.optUsePopulationCoding = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        if (ap < av.length && av[ap].equals("-a")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            ap++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            cc.optUseAdaptiveCoding = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        cc.verbose = verbose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        int[] values = readValuesFrom(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        int[] sizes = {0,0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        CodingMethod cm = cc.choose(values, BandStructure.UNSIGNED5, sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        System.out.println("size: "+sizes[BYTE_SIZE]+"/zs="+sizes[ZIP_SIZE]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        System.out.println(cm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
//*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
}