corba/src/jdk.rmic/share/classes/sun/rmi/rmic/iiop/StaticStringsHash.java
author duke
Thu, 24 Aug 2017 16:26:58 +0200
changeset 45797 d9f6bc6ba599
parent 25862 a5e25d68f971
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
/*
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
 * Licensed Materials - Property of IBM
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
 * RMI-IIOP v1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
 * Copyright IBM Corp. 1998 1999  All Rights Reserved
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
package sun.rmi.rmic.iiop;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
 * StaticStringsHash takes an array of constant strings and
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
 * uses several different hash methods to try to find the
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
 * 'best' one for that set. The set of methods is currently
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
 * fixed, but with a little work could be made extensible thru
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
 * subclassing.
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
 * The current set of methods is:
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
 * <ol>
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
 * <li> length() - works well when all strings are different length.</li>
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
 * <li> charAt(n) - works well when one offset into all strings is different.</li>
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
 * <li> hashCode() - works well with larger arrays.</li>
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
 * </ol>
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
 * After constructing an instance over the set of strings, the
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
 * <code>getKey(String)</code> method can be used to use the selected hash
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
 * method to produce a key.  The <code>method</code> string will contain
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
 * "length()", "charAt(n)", or "hashCode()", and is intended for use by
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
 * code generators.
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
 * The <code>keys</code> array will contain the full set of unique keys.
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
 * <p>
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
 * The <code>buckets</code> array will contain a set of arrays, one for
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
 * each key in the <code>keys</code>, where <code>buckets[x][y]</code>
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
 * is an index into the <code>strings</code> array.
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
 * @author      Bryan Atsatt
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
public class StaticStringsHash {
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
    /** The set of strings upon which the hash info is created */
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
    public String[] strings = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
    /** Unique hash keys */
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
    public int[] keys = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
    /** Buckets for each key, where buckets[x][y] is an index
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
     * into the strings[] array. */
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
    public int[][] buckets = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
    /** The method to invoke on String to produce the hash key */
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
    public String method = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
    /** Get a key for the given string using the
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
     * selected hash method.
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
     * @param str the string to return a key for.
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
     * @return the key.
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
    public int getKey(String str) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
        switch (keyKind) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
        case LENGTH: return str.length();
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
        case CHAR_AT: return str.charAt(charAt);
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
        case HASH_CODE: return str.hashCode();
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
        throw new Error("Bad keyKind");
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
    /** Constructor
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
     * @param strings the set of strings upon which to
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
     * find an optimal hash method. Must not contain
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
     * duplicates.
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
    public StaticStringsHash(String[] strings) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
        this.strings = strings;
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
        length = strings.length;
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
        tempKeys = new int[length];
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
        bucketSizes = new int[length];
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
        setMinStringLength();
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
        // Decide on the best algorithm based on
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
        // which one has the smallest maximum
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
        // bucket depth. First, try length()...
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
        int currentMaxDepth = getKeys(LENGTH);
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
        int useCharAt = -1;
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
        boolean useHashCode = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
        if (currentMaxDepth > 1) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
            // At least one bucket had more than one
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
            // entry, so try charAt(i).  If there
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
            // are a lot of strings in the array,
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
            // and minStringLength is large, limit
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
            // the search to a smaller number of
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
            // characters to avoid spending a lot
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
            // of time here that is most likely to
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
            // be pointless...
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
            int minLength = minStringLength;
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
            if (length > CHAR_AT_MAX_LINES &&
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
                length * minLength > CHAR_AT_MAX_CHARS) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
                minLength = length/CHAR_AT_MAX_CHARS;
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
            charAt = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
            for (int i = 0; i < minLength; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
                int charAtDepth = getKeys(CHAR_AT);
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
                if (charAtDepth < currentMaxDepth) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
                    currentMaxDepth = charAtDepth;
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
                    useCharAt = i;
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
                    if (currentMaxDepth == 1) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
                        break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
                charAt++;
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
            charAt = useCharAt;
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
            if (currentMaxDepth > 1) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
                // At least one bucket had more than one
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
                // entry, try hashCode().
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
                //
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
                // Since the cost of computing a full hashCode
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
                // (for the runtime target string) is much higher
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
                // than the previous methods, use it only if it is
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
                // substantially better. The definition of 'substantial'
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
                // here is not very well founded, and could be improved
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
                // with some further analysis ;^)
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
                int hashCodeDepth = getKeys(HASH_CODE);
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
                if (hashCodeDepth < currentMaxDepth-3) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
                    // Using the full hashCode results in at least
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
                    // 3 fewer entries in the worst bucket, so will
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
                    // therefore avoid at least 3 calls to equals()
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
                    // in the worst case.
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
                    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
                    // Note that using a number smaller than 3 could
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
                    // result in using a hashCode when there are only
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
                    // 2 strings in the array, and that would surely
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
                    // be a poor performance choice.
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
                    useHashCode = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
            // Reset keys if needed...
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
            if (!useHashCode) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
                if (useCharAt >= 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
                    // Use the charAt(i) method...
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
                    getKeys(CHAR_AT);
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
                } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
                    // Use length method...
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
                    getKeys(LENGTH);
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
        // Now allocate and fill our real hashKeys array...
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
        keys = new int[bucketCount];
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
        System.arraycopy(tempKeys,0,keys,0,bucketCount);
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
        // Sort keys and bucketSizes arrays...
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
        boolean didSwap;
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
        do {
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
            didSwap = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
            for (int i = 0; i < bucketCount - 1; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
                if (keys[i] > keys[i+1]) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
                    int temp = keys[i];
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
                    keys[i] = keys[i+1];
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
                    keys[i+1] = temp;
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
                    temp = bucketSizes[i];
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
                    bucketSizes[i] = bucketSizes[i+1];
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
                    bucketSizes[i+1] = temp;
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
                    didSwap = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
        while (didSwap == true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
        // Allocate our buckets array. Fill the string
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
        // index slot with an unused key so we can
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
        // determine which are free...
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
        int unused = findUnusedKey();
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
        buckets = new int[bucketCount][];
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
        for (int i = 0; i < bucketCount; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
            buckets[i] = new int[bucketSizes[i]];
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
            for (int j = 0; j < bucketSizes[i]; j++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
                buckets[i][j] = unused;
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
        // And fill it in...
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
        for(int i = 0; i < strings.length; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
            int key = getKey(strings[i]);
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
            for (int j = 0; j < bucketCount; j++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
                if (keys[j] == key) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
                    int k = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
                    while (buckets[j][k] != unused) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
                        k++;
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
                    buckets[j][k] = i;
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
                    break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
    /** Print an optimized 'contains' method for the
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
     * argument strings
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
    public static void main (String[] args) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
        StaticStringsHash hash = new StaticStringsHash(args);
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
        System.out.println();
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
        System.out.println("    public boolean contains(String key) {");
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
        System.out.println("        switch (key."+hash.method+") {");
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
        for (int i = 0; i < hash.buckets.length; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
            System.out.println("            case "+hash.keys[i]+": ");
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
            for (int j = 0; j < hash.buckets[i].length; j++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
                if (j > 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
                    System.out.print("                } else ");
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
                } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
                    System.out.print("                ");
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
                System.out.println("if (key.equals(\""+ hash.strings[hash.buckets[i][j]] +"\")) {");
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
                System.out.println("                    return true;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
            System.out.println("                }");
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
        System.out.println("        }");
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
        System.out.println("        return false;");
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
        System.out.println("    }");
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
    private int length;
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
    private int[] tempKeys;
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
    private int[] bucketSizes;
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
    private int bucketCount;
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
    private int maxDepth;
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
    private int minStringLength = Integer.MAX_VALUE;
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
    private int keyKind;
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
    private int charAt;
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
    private static final int LENGTH = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
    private static final int CHAR_AT = 1;
02bb8761fcce Initial load
duke
parents:
diff changeset
   279
    private static final int HASH_CODE = 2;
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
02bb8761fcce Initial load
duke
parents:
diff changeset
   281
    /* Determines the maximum number of charAt(i)
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
     * tests that will be done. The search is
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
     * limited because if the number of characters
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
     * is large enough, the likelyhood of finding
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
     * a good hash key  based on this method is
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
     * low. The CHAR_AT_MAX_CHARS limit only
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
     * applies f there are more strings than
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
     * CHAR_AT_MAX_LINES.
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
    private static final int CHAR_AT_MAX_LINES = 50;
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
    private static final int CHAR_AT_MAX_CHARS = 1000;
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
    private void resetKeys(int keyKind) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
        this.keyKind = keyKind;
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
        switch (keyKind) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
        case LENGTH: method = "length()"; break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
        case CHAR_AT: method = "charAt("+charAt+")"; break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
        case HASH_CODE: method = "hashCode()"; break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
        maxDepth = 1;
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
        bucketCount = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
        for (int i = 0; i < length; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
            tempKeys[i] = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
            bucketSizes[i] = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
    private void setMinStringLength() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
        for (int i = 0; i < length; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
            if (strings[i].length() < minStringLength) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
                minStringLength = strings[i].length();
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
    private int findUnusedKey() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
        int unused = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
        int keysLength = keys.length;
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
        // Note that we just assume that resource
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
        // exhaustion will occur rather than an
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
        // infinite loop here if the set of keys
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
        // is very large.
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
        while (true) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
            boolean match = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
            for (int i = 0; i < keysLength; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
                if (keys[i] == unused) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
                    match = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
                    break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
            if (match) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
                unused--;
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
                break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
        return unused;
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
    private int getKeys(int methodKind) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
        resetKeys(methodKind);
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
        for(int i = 0; i < strings.length; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
            addKey(getKey(strings[i]));
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
        return maxDepth;
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
    private void addKey(int key) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
        // Have we seen this one before?
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
        boolean addIt = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   355
        for (int j = 0; j < bucketCount; j++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
            if (tempKeys[j] == key) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
                addIt = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
                bucketSizes[j]++;
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
                if (bucketSizes[j] > maxDepth) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
                    maxDepth = bucketSizes[j];
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
                break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   363
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   364
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   365
02bb8761fcce Initial load
duke
parents:
diff changeset
   366
        if (addIt) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   367
            tempKeys[bucketCount] = key;
02bb8761fcce Initial load
duke
parents:
diff changeset
   368
            bucketSizes[bucketCount] = 1;
02bb8761fcce Initial load
duke
parents:
diff changeset
   369
            bucketCount++;
02bb8761fcce Initial load
duke
parents:
diff changeset
   370
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   371
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   372
}