/* The Great Win32 Language Shootout http://dada.perl.it/shootout/
contributed by Isaac Gouy (Nice novice)
To compile:
nicec --sourcepath=.. -d=. -a hash2.jar hash2
To run:
java -jar hash2.jar 150
*/
import ackermann; // reuse toSingleInt
void main(String[] args){
var n = toSingleInt(args);
let nKeys = 10000;
HashMap table1 = new HashMap(nKeys);
HashMap table2 = new HashMap();
for (var i = 0; i <= nKeys; i++)
table1.put( "foo_" + toString(i), new Cell(value: i) );
String key;
int v1;
?Cell c2; // c2 = table2.get(key) can be null
while (n-- > 0) {
Iterator> item = table1.entrySet().iterator();
while(item.hasNext()) {
Map.Entry e = item.next();
key = e.getKey();
v1 = e.getValue().value;
if ( (c2 = table2.get(key)) != null)
c2.value += v1;
else
table2.put(key, new Cell(value: v1) );
}
}
print( toString( table1.get("foo_1") )); print(" ");
print( toString( table1.get("foo_9999") )); print(" ");
print( toString( table2.get("foo_1") )); print(" ");
println( toString( table2.get("foo_9999") ));
}
class Cell { int value; }
String toString(?Cell c) {
if (c==null) return "null"; else return toString(c.value);
}