make/jdk/src/classes/build/tools/hasher/Hasher.java
author mbaesken
Thu, 26 Oct 2017 08:52:55 +0200
changeset 47454 7a7bc84f4b6c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8189618: [aix] No jre image is created during images step Reviewed-by: erikj, stuefe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22639
diff changeset
     2
 * Copyright (c) 2004, 2013, 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 build.tools.hasher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Reads a map in the form of a sequence of key/value-expression pairs from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * standard input, attempts to construct a hash map that fits within the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * table-size and chain-depth constraints, and, if successful, writes source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * code to the standard output for a subclass of sun.util.PreHashedMap that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * implements the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see sun.util.PreHashedMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class Hasher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final PrintStream out = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final PrintStream err = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    boolean verbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    51
    List<String> keys = new ArrayList<>();      // Key strings
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    52
    List<String> values = new ArrayList<>();    // Value expressions
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    53
    String pkg = null;                          // Package prefix for generated class
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    54
    String cln = null;                          // Name of generated class
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    55
    String vtype = "String";                    // Value type
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    56
    int maxBits = 11;                           // lg table size
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    57
    int maxDepth = 3;                           // Max chain depth
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    58
    boolean inner = false;                      // Generating an inner class?
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    59
    boolean empty = false;                      // Generating an empty table?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        err.println("usage: java Hasher [options] [[pkgName.]ClassName]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        err.println("options:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        err.println("    -e           generate empty table (ignores value exprs)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        err.println("    -i           generate a static inner class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        err.println("    -md depth    max chain depth (default 3)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        err.println("    -mb bits     max index bits (lg of table size, default 10)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        err.println("    -t type      value type (default String)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        err.println("    -v           verbose");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        err.println("Key/value-expression pairs are read from standard input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        err.println("If class name is given then source is written to standard output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    Hasher(String[] args) {
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    76
        List<String> as = Arrays.asList(args);
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    77
        for (Iterator<String> i = as.iterator(); i.hasNext();) {
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    78
            String a = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (a.equals("-e")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                empty = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            } else if (a.equals("-i")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                inner = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            } else if (a.equals("-v")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            } else if (a.equals("-md")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (!i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    usage();
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    88
                maxDepth = Integer.parseInt(i.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            } else if (a.equals("-mb")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                if (!i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    usage();
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    92
                maxBits = Integer.parseInt(i.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            } else if (a.equals("-t")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                if (!i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    usage();
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    96
                vtype = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            } else if (a.startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                int j = a.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if (j >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    pkg = a.substring(0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    cln = a.substring(j + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    cln = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (verbose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            err.println("pkg=" + pkg + ", cln=" + cln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // Read keys and values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    Hasher load() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        BufferedReader br
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            = new BufferedReader(new InputStreamReader(System.in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (String ln; (ln = br.readLine()) != null;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            String[] ws = ln.split(",?\\s+", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            String w = ws[0].replaceAll("\"", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (keys.contains(w))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                throw new RuntimeException("Duplicate word in input: " + w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            keys.add(w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (ws.length < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                throw new RuntimeException("Missing expression for key " + w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            values.add(ws[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    Object[] ht;                        // Hash table itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    int nb;                             // Number of bits (lg table size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    int md;                             // Maximum chain depth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    int mask;                           // Hash-code mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    int shift;                          // Hash-code shift size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    int hash(String w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return (w.hashCode() >> shift) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // Build a hash table of size 2^nb, shifting the hash code by s bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    void build(int nb, int s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.nb = nb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.shift = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int n = 1 << nb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        this.mask = n - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        ht = new Object[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int nw = keys.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        for (int i = 0; i < nw; i++) {
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   153
            String w = keys.get(i);
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   154
            String v = values.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            int h = hash(w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (ht[h] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                ht[h] = new Object[] { w, v };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                ht[h] = new Object[] { w, v, ht[h] };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.md = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            int d = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            for (Object[] a = (Object[])ht[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                 a != null && a.length > 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                 a = (Object[])a[2], d++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            this.md = Math.max(md, d);
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
    Hasher build() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // Iterate through acceptable table sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        for (int nb = 2; nb < maxBits; nb++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // Iterate through possible shift sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            for (int s = 0; s < (32 - nb); s++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                build(nb, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if (verbose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    err.println("nb=" + nb + " s=" + s + " md=" + md);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (md <= maxDepth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    // Success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    out.flush();
22639
37f4508257fe 8033236: Update GensrcCharsetMapping.gmk to build-infra standards
ihse
parents: 21805
diff changeset
   184
                    if (verbose) {
37f4508257fe 8033236: Update GensrcCharsetMapping.gmk to build-infra standards
ihse
parents: 21805
diff changeset
   185
                        if (cln != null)
37f4508257fe 8033236: Update GensrcCharsetMapping.gmk to build-infra standards
ihse
parents: 21805
diff changeset
   186
                            err.print(cln + ": ");
37f4508257fe 8033236: Update GensrcCharsetMapping.gmk to build-infra standards
ihse
parents: 21805
diff changeset
   187
                        err.println("Table size " + (1 << nb) + " (" + nb + " bits)"
37f4508257fe 8033236: Update GensrcCharsetMapping.gmk to build-infra standards
ihse
parents: 21805
diff changeset
   188
                                    + ", shift " + shift
37f4508257fe 8033236: Update GensrcCharsetMapping.gmk to build-infra standards
ihse
parents: 21805
diff changeset
   189
                                    + ", max chain depth " + md);
37f4508257fe 8033236: Update GensrcCharsetMapping.gmk to build-infra standards
ihse
parents: 21805
diff changeset
   190
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        throw new RuntimeException("Cannot find a suitable size"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                   + " within given constraints");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    // Look for the given key in the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    String get(String k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int h = hash(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Object[] a = (Object[])ht[h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (a[0].equals(k))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                return (String)a[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (a.length < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            a = (Object[])a[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    // Test that all input keys can be found in the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    Hasher test() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (verbose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        for (int i = 0, n = keys.size(); i < n; i++) {
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   219
            String w = keys.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            String v = get(w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (verbose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                err.println(hash(w) + "\t" + w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (!v.equals(values.get(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                throw new Error("Incorrect value: " + w + " --> "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                + v + ", should be " + values.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    String ind = "";                    // Indent prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    // Generate code for a single table entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    void genEntry(Object[] a, int depth, PrintWriter pw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        Object v = empty ? null : a[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        pw.print("new Object[] { \"" + a[0] + "\", " + v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (a.length < 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            pw.print(" }");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        pw.println(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        pw.print(ind + "                     ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        for (int i = 0; i < depth; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            pw.print("    ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        genEntry((Object[])a[2], depth + 1, pw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        pw.print(" }");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    // Generate a PreHashedMap subclass from the computed hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    Hasher generate() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (cln == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        PrintWriter pw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (inner)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            ind = "    ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (!inner && pkg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            pw.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            pw.println("package " + pkg + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            pw.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (inner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            pw.println(ind + "private static final class " + cln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            pw.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            pw.println("public final class " + cln);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        pw.println(ind + "    extends sun.util.PreHashedMap<" + vtype +">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        pw.println(ind + "{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        pw.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        pw.println(ind + "    private static final int ROWS = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                   + ht.length + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        pw.println(ind + "    private static final int SIZE = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                   + keys.size() + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        pw.println(ind + "    private static final int SHIFT = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                   + shift + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        pw.println(ind + "    private static final int MASK = 0x"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                   + Integer.toHexString(mask) + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        pw.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        pw.println(ind + "    " + (inner ? "private " : "public ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                   + cln + "() {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        pw.println(ind + "        super(ROWS, SIZE, SHIFT, MASK);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        pw.println(ind + "    }");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        pw.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        pw.println(ind + "    protected void init(Object[] ht) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        for (int i = 0; i < ht.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (ht[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            Object[] a = (Object[])ht[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            pw.print(ind + "        ht[" + i + "] = ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            genEntry(a, 0, pw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            pw.println(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        pw.println(ind + "    }");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        pw.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        pw.println(ind + "}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (inner)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            pw.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        pw.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public static void main(String[] args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        new Hasher(args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            .load()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            .build()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            .test()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            .generate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
}