1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
– |
import java.util.*; |
6 |
|
import java.io.*; |
7 |
+ |
import java.util.*; |
8 |
|
|
9 |
|
public class MapWordLoops { |
10 |
|
|
25 |
|
static final int numTests = 3; |
26 |
|
|
27 |
|
public static void main(String[] args) { |
28 |
< |
Class mapClass = null; |
28 |
> |
Class<?> mapClass = null; |
29 |
|
try { |
30 |
|
mapClass = Class.forName(args[0]); |
31 |
< |
} catch(ClassNotFoundException e) { |
31 |
> |
} catch (ClassNotFoundException e) { |
32 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
33 |
|
} |
34 |
|
|
39 |
|
|
40 |
|
for (int s = WORDS_FILES.length-1; s >= 0; --s) |
41 |
|
tests(mapClass, numTests, s); |
42 |
– |
|
42 |
|
} |
43 |
|
|
44 |
< |
static void tests(Class mapClass, int numTests, int sizeIndex) { |
44 |
> |
static void tests(Class<?> mapClass, int numTests, int sizeIndex) { |
45 |
|
try { |
46 |
|
String[] key = readWords(sizeIndex); |
47 |
|
int size = key.length; |
64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
< |
|
69 |
< |
static Map<String,String> newMap(Class cl) { |
67 |
> |
static Map<String,String> newMap(Class<?> cl) { |
68 |
|
try { |
69 |
< |
Map m = (Map<String,String>)cl.newInstance(); |
70 |
< |
return m; |
73 |
< |
} catch(Exception e) { |
69 |
> |
return (Map<String,String>) cl.getConstructor().newInstance(); |
70 |
> |
} catch (Exception e) { |
71 |
|
throw new RuntimeException("Can't instantiate " + cl + ": " + e); |
72 |
|
} |
73 |
|
} |
74 |
|
|
75 |
|
static void pause() { |
76 |
< |
try { Thread.sleep(100); } catch(InterruptedException ie) { return; } |
76 |
> |
try { Thread.sleep(100); } |
77 |
> |
catch (InterruptedException ie) { return; } |
78 |
|
} |
79 |
|
|
80 |
|
static String[] readWords(int sizeIndex) throws IOException { |
113 |
|
long startTime = System.currentTimeMillis(); |
114 |
|
runner.run(); |
115 |
|
long afterRun = System.currentTimeMillis(); |
116 |
< |
long runTime = (afterRun - startTime); |
116 |
> |
long runTime = afterRun - startTime; |
117 |
|
int np = runner.total; |
118 |
|
if (runner.total == runner.hashCode()) |
119 |
|
System.out.println("Useless Number" + runner.total); |
124 |
|
return runTime; |
125 |
|
} |
126 |
|
|
129 |
– |
|
127 |
|
static class Runner implements Runnable { |
128 |
|
final Map<String,String> map; |
129 |
|
final String[] key; |
144 |
|
rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L); |
145 |
|
} |
146 |
|
|
150 |
– |
|
147 |
|
int oneStep(int j) { |
148 |
|
int n = key.length; |
149 |
|
int r = rng.next() & 0x7FFFFFFF; |
172 |
|
if (r < pctrem) { |
173 |
|
map.remove(k); |
174 |
|
++nremoves; |
175 |
< |
j += ((r >>> 8) & 7) + n / 2; |
175 |
> |
j += ((r >>> 8) & 7) + n / 2; |
176 |
|
if (j >= n) j -= n; |
177 |
|
} |
178 |
|
} |