Nice TWiki > Doc > HashBeginnerExample (r1.1 vs. r1.6) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
 <<O>>  Difference Topic HashBeginnerExample (r1.6 - 10 Jan 2004 - IsaacGouy)
Changed:
<
<

table1.put("foo_" + i, new Cell(value: i) );

>
>

table1["foo_" + i] = new Cell(value: i);

Changed:
<
<

?Cell c2; // c2 = table2.get(key) can be null

>
>

?Cell c2; // c2 = table2[key] can be null

Changed:
<
<

key = each.getKey(); v1 = each.getValue().value;

>
>

key = each.getKey; v1 = each.getValue.value;

Changed:
<
<

table2.put(key, new Cell(value: v1) );

>
>

table2[key] = new Cell(value: v1);


 <<O>>  Difference Topic HashBeginnerExample (r1.5 - 10 Jan 2004 - IsaacGouy)
Changed:
<
<

nicec --sourcepath=.. -a hash2.jar hash2

>
>

nicec --sourcepath=.. -a hash.jar hash

Changed:
<
<

java -jar hash2.jar 150

>
>

java -jar hash.jar 150

Changed:
<
<

key = entry.getKey(); v1 = entry.getValue().value;

>
>

key = each.getKey(); v1 = each.getValue().value;

Changed:
<
<

toString(c@Cell) = c.value.toString()

>
>

toString(Cell c) = c.value.toString();

Changed:
<
<

toString(c@Cell) = c.value.toString()

>
>

toString(Cell c) = c.value.toString()


 <<O>>  Difference Topic HashBeginnerExample (r1.4 - 02 Sep 2003 - IsaacGouy)
Changed:
<
<

while (n-- > 0) { for (Map.Entry entry : table1.entrySet()) {

>
>

while (n-- > 0) for (each : table1.entrySet()) {

Changed:
<
<

}

>
>


 <<O>>  Difference Topic HashBeginnerExample (r1.3 - 28 Aug 2003 - ArjanB)
Changed:
<
<

for (var i = 0; i <= nKeys; i++) table1.put( "foo_" + toString(i), new Cell(value: i) );

>
>

for (int i = 0; i <= nKeys; i++) table1.put("foo_" + i, new Cell(value: i) );

Changed:
<
<

Iterator<Map.Entry<String, Cell>> 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)

>
>

for (Map.Entry entry : table1.entrySet()) { key = entry.getKey(); v1 = entry.getValue().value; if ( (c2 = table2[key]) != null)

Changed:
<
<

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") ));

>
>

print("" + table1["foo_1"]); print(" " + table1["foo_9999"]); print(" " + table2["foo_1"]); println(" " + table2["foo_9999"]);

Changed:
<
<

String toString(?Cell c) { if (c==null) return "null"; else return toString(c.value); }

>
>

toString(c@Cell) = c.value.toString()

Added:
>
>

for (Map.Entry entry : table1.entrySet()) { Using the enhanced for loop to iterate over the map entries.

table2[key] This is equivalent to table2.get(key)

Changed:
<
<

String toString(?Cell c) { Define a package function

>
>

toString(c@Cell) = c.value.toString() Implement the toString method for the class Cell using the short return syntax. Note that the method is implemted outside the class.


 <<O>>  Difference Topic HashBeginnerExample (r1.2 - 27 Aug 2003 - IsaacGouy)
Changed:
<
<

/* Please try the BeginnerExamples? before the NoviceExamples?

>
>

/* Please try the NoviceExamples? before the BeginnerExamples?

Added:
>
>

%META:TOPICMOVED{by="IsaacGouy" date="1062001690" from="Doc.HashNoviceExample" to="Doc.HashBeginnerExample"}%


 <<O>>  Difference Topic HashBeginnerExample (r1.1 - 27 Aug 2003 - IsaacGouy)
Added:
>
>

%META:TOPICINFO{author="IsaacGouy" date="1061942495" format="1.0" version="1.1"}% %META:TOPICPARENT{name="CodeExamples"}%

/* Please try the BeginnerExamples before the NoviceExamples
To compile:
   nicec --sourcepath=.. -a hash2.jar hash2
To run:
   java -jar hash2.jar 150
*/

void main(String[] args){
   var n = toSingleInt(args);
   let nKeys = 10000;

   HashMap<String,Cell> table1 = new HashMap(nKeys);
   HashMap<String,Cell> 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<Map.Entry<String, Cell>> item = table1.entrySet().iterator();
      while(item.hasNext()) {
         Map.Entry<String, Cell> 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);
}


int toSingleInt(String[] s){
   try { return Integer.parseInt(s[0]); }
   catch (Exception e){ return 1; } }



/* Notes - language
Compare with Java at the "Win32 Language Shootout"

   var n = toSingleInt(args);
Declare variable n, the compiler will infer the variable type
from the return type of the package function toSingleInt(String[] s)

   let nKeys = 10000;
Declare constant nKeys, the compiler will infer the constant type
from the literal value.

   HashMap<String,Cell> table1 = new HashMap(nKeys);
Declare and initialize table1 with type parameter <String,Cell>
HashMap<String,Cell> keys are type String and values are type Cell

   class Cell { int value; }
Define a new class Cell, with a single field. Using a Cell
in the HashMap allows entries to be updated without using
put(key,value).

   String key;
Declare variable key with type String (that excludes null values)

   ?Cell c2;
Declare variable c2 as an Option Type with type Cell or null.

   String toString(?Cell c) {
Define a package function

   int toSingleInt(String[] s){
Define a package function

*/

-- IsaacGouy? - 26 Aug 2003


Topic HashBeginnerExample . { View | Diffs | r1.6 | > | r1.5 | > | r1.4 | More }
Revision r1.1 - 27 Aug 2003 - 00:01 GMT - IsaacGouy
Revision r1.6 - 10 Jan 2004 - 19:03 GMT - IsaacGouy
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.