Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/hasher/Hasher.java
32287 views
1
/*
2
* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package build.tools.hasher;
27
28
import java.io.*;
29
import java.util.*;
30
31
32
/**
33
* Reads a map in the form of a sequence of key/value-expression pairs from the
34
* standard input, attempts to construct a hash map that fits within the given
35
* table-size and chain-depth constraints, and, if successful, writes source
36
* code to the standard output for a subclass of sun.util.PreHashedMap that
37
* implements the map.
38
*
39
* @see sun.util.PreHashedMap
40
*
41
* @author Mark Reinhold
42
*/
43
44
public class Hasher {
45
46
// This class cannot, sadly, make use of 1.5 features since it must be
47
// compiled and run with the bootstrap JDK, which is 1.4.2.
48
49
static final PrintStream out = System.out;
50
static final PrintStream err = System.err;
51
52
boolean verbose = false;
53
54
List<String> keys = new ArrayList<>(); // Key strings
55
List<String> values = new ArrayList<>(); // Value expressions
56
String pkg = null; // Package prefix for generated class
57
String cln = null; // Name of generated class
58
String vtype = "String"; // Value type
59
int maxBits = 11; // lg table size
60
int maxDepth = 3; // Max chain depth
61
boolean inner = false; // Generating an inner class?
62
boolean empty = false; // Generating an empty table?
63
64
void usage() {
65
err.println("usage: java Hasher [options] [[pkgName.]ClassName]");
66
err.println("options:");
67
err.println(" -e generate empty table (ignores value exprs)");
68
err.println(" -i generate a static inner class");
69
err.println(" -md depth max chain depth (default 3)");
70
err.println(" -mb bits max index bits (lg of table size, default 10)");
71
err.println(" -t type value type (default String)");
72
err.println(" -v verbose");
73
err.println("Key/value-expression pairs are read from standard input");
74
err.println("If class name is given then source is written to standard output");
75
System.exit(1);
76
}
77
78
Hasher(String[] args) {
79
List<String> as = Arrays.asList(args);
80
for (Iterator<String> i = as.iterator(); i.hasNext();) {
81
String a = i.next();
82
if (a.equals("-e")) {
83
empty = true;
84
} else if (a.equals("-i")) {
85
inner = true;
86
} else if (a.equals("-v")) {
87
verbose = true;
88
} else if (a.equals("-md")) {
89
if (!i.hasNext())
90
usage();
91
maxDepth = Integer.parseInt(i.next());
92
} else if (a.equals("-mb")) {
93
if (!i.hasNext())
94
usage();
95
maxBits = Integer.parseInt(i.next());
96
} else if (a.equals("-t")) {
97
if (!i.hasNext())
98
usage();
99
vtype = i.next();
100
} else if (a.startsWith("-")) {
101
usage();
102
} else {
103
int j = a.lastIndexOf('.');
104
if (j >= 0) {
105
pkg = a.substring(0, j);
106
cln = a.substring(j + 1);
107
} else {
108
cln = a;
109
}
110
}
111
}
112
if (verbose)
113
err.println("pkg=" + pkg + ", cln=" + cln);
114
}
115
116
// Read keys and values
117
//
118
Hasher load() throws IOException {
119
BufferedReader br
120
= new BufferedReader(new InputStreamReader(System.in));
121
for (String ln; (ln = br.readLine()) != null;) {
122
String[] ws = ln.split(",?\\s+", 2);
123
String w = ws[0].replaceAll("\"", "");
124
if (keys.contains(w))
125
throw new RuntimeException("Duplicate word in input: " + w);
126
keys.add(w);
127
if (ws.length < 2)
128
throw new RuntimeException("Missing expression for key " + w);
129
values.add(ws[1]);
130
}
131
return this;
132
}
133
134
Object[] ht; // Hash table itself
135
int nb; // Number of bits (lg table size)
136
int md; // Maximum chain depth
137
int mask; // Hash-code mask
138
int shift; // Hash-code shift size
139
140
int hash(String w) {
141
return (w.hashCode() >> shift) & mask;
142
}
143
144
// Build a hash table of size 2^nb, shifting the hash code by s bits
145
//
146
void build(int nb, int s) {
147
148
this.nb = nb;
149
this.shift = s;
150
int n = 1 << nb;
151
this.mask = n - 1;
152
ht = new Object[n];
153
int nw = keys.size();
154
155
for (int i = 0; i < nw; i++) {
156
String w = keys.get(i);
157
String v = values.get(i);
158
int h = hash(w);
159
if (ht[h] == null)
160
ht[h] = new Object[] { w, v };
161
else
162
ht[h] = new Object[] { w, v, ht[h] };
163
}
164
165
this.md = 0;
166
for (int i = 0; i < n; i++) {
167
int d = 1;
168
for (Object[] a = (Object[])ht[i];
169
a != null && a.length > 2;
170
a = (Object[])a[2], d++);
171
this.md = Math.max(md, d);
172
}
173
174
}
175
176
Hasher build() {
177
// Iterate through acceptable table sizes
178
for (int nb = 2; nb < maxBits; nb++) {
179
// Iterate through possible shift sizes
180
for (int s = 0; s < (32 - nb); s++) {
181
build(nb, s);
182
if (verbose)
183
err.println("nb=" + nb + " s=" + s + " md=" + md);
184
if (md <= maxDepth) {
185
// Success
186
out.flush();
187
if (cln != null)
188
err.print(cln + ": ");
189
err.println("Table size " + (1 << nb) + " (" + nb + " bits)"
190
+ ", shift " + shift
191
+ ", max chain depth " + md);
192
return this;
193
}
194
}
195
}
196
throw new RuntimeException("Cannot find a suitable size"
197
+ " within given constraints");
198
}
199
200
// Look for the given key in the hash table
201
//
202
String get(String k) {
203
int h = hash(k);
204
Object[] a = (Object[])ht[h];
205
for (;;) {
206
if (a[0].equals(k))
207
return (String)a[1];
208
if (a.length < 3)
209
return null;
210
a = (Object[])a[2];
211
}
212
}
213
214
// Test that all input keys can be found in the table
215
//
216
Hasher test() {
217
if (verbose)
218
err.println();
219
for (int i = 0, n = keys.size(); i < n; i++) {
220
String w = keys.get(i);
221
String v = get(w);
222
if (verbose)
223
err.println(hash(w) + "\t" + w);
224
if (!v.equals(values.get(i)))
225
throw new Error("Incorrect value: " + w + " --> "
226
+ v + ", should be " + values.get(i));
227
}
228
return this;
229
}
230
231
String ind = ""; // Indent prefix
232
233
// Generate code for a single table entry
234
//
235
void genEntry(Object[] a, int depth, PrintWriter pw) {
236
Object v = empty ? null : a[1];
237
pw.print("new Object[] { \"" + a[0] + "\", " + v);
238
if (a.length < 3) {
239
pw.print(" }");
240
return;
241
}
242
pw.println(",");
243
pw.print(ind + " ");
244
for (int i = 0; i < depth; i++)
245
pw.print(" ");
246
genEntry((Object[])a[2], depth + 1, pw);
247
pw.print(" }");
248
}
249
250
// Generate a PreHashedMap subclass from the computed hash table
251
//
252
Hasher generate() throws IOException {
253
if (cln == null)
254
return this;
255
PrintWriter pw
256
= new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
257
258
if (inner)
259
ind = " ";
260
261
if (!inner && pkg != null) {
262
pw.println();
263
pw.println("package " + pkg + ";");
264
pw.println();
265
}
266
267
if (inner) {
268
pw.println(ind + "private static final class " + cln);
269
} else {
270
pw.println();
271
pw.println("public final class " + cln);
272
}
273
pw.println(ind + " extends sun.util.PreHashedMap<" + vtype +">");
274
pw.println(ind + "{");
275
276
pw.println();
277
pw.println(ind + " private static final int ROWS = "
278
+ ht.length + ";");
279
pw.println(ind + " private static final int SIZE = "
280
+ keys.size() + ";");
281
pw.println(ind + " private static final int SHIFT = "
282
+ shift + ";");
283
pw.println(ind + " private static final int MASK = 0x"
284
+ Integer.toHexString(mask) + ";");
285
pw.println();
286
287
pw.println(ind + " " + (inner ? "private " : "public ")
288
+ cln + "() {");
289
pw.println(ind + " super(ROWS, SIZE, SHIFT, MASK);");
290
pw.println(ind + " }");
291
pw.println();
292
293
pw.println(ind + " protected void init(Object[] ht) {");
294
for (int i = 0; i < ht.length; i++) {
295
if (ht[i] == null)
296
continue;
297
Object[] a = (Object[])ht[i];
298
pw.print(ind + " ht[" + i + "] = ");
299
genEntry(a, 0, pw);
300
pw.println(";");
301
}
302
pw.println(ind + " }");
303
pw.println();
304
305
pw.println(ind + "}");
306
if (inner)
307
pw.println();
308
309
pw.close();
310
return this;
311
}
312
313
public static void main(String[] args) throws IOException {
314
new Hasher(args)
315
.load()
316
.build()
317
.test()
318
.generate();
319
}
320
321
}
322
323