HashMap
authorbdemsky <bdemsky>
Fri, 3 Nov 2006 02:20:50 +0000 (02:20 +0000)
committerbdemsky <bdemsky>
Fri, 3 Nov 2006 02:20:50 +0000 (02:20 +0000)
Robust/src/ClassLibrary/HashMap.java
Robust/src/ClassLibrary/HashMapIterator.java [new file with mode: 0644]
Robust/src/Main/Main.java

index c814f6b5fb6a93baab36e095e5bb6939c0f6e67d..beb895f62803d4b02cc07be850be48f5920d898a 100644 (file)
@@ -45,6 +45,11 @@ public class HashMap {
        return numItems;
     }
 
+    /* 0=keys, 1=values */
+    public HashMapIterator iterator(int type) {
+       return new HashMapIterator(this, type);
+    }
+
     Object remove(Object key) {
        int bin=key.hashCode()%table.length;
        HashEntry ptr=table[bin];
diff --git a/Robust/src/ClassLibrary/HashMapIterator.java b/Robust/src/ClassLibrary/HashMapIterator.java
new file mode 100644 (file)
index 0000000..c0f0eb4
--- /dev/null
@@ -0,0 +1,46 @@
+class HashMapIterator {
+    HashMap map;
+    int type;
+    int bin;
+    HashEntry he;
+
+    public HashMapIterator(HashMap map, int type) {
+       this.map=map;
+       this.type=type;
+       this.bin=0;
+       this.he=null;
+    }
+
+    public boolean hasNext() {
+       if (he.next!=null)
+           return true;
+       int i=bin;
+       while(map.table[i]==null&&(i<map.table.length))
+           i++;
+       return (i<map.table.length);
+    }
+
+    public Object next() {
+       if (he.next!=null) {
+           he=he.next;
+           Object o;
+           if (type==0)
+               o=he.key;
+           else
+               o=he.value;
+           return o;
+       }
+       while((map.table[bin]==null)&&(bin<map.table.length))
+           bin++;
+       if (bin<map.table.length) {
+           he=map.table[bin];
+           Object o;
+           if (type==0)
+               o=he.key;
+           else
+               o=he.value;
+           return o;
+       } else System.error();
+    }
+
+}
index ec7687ade18d1774370b6dd9f8cb612a4353a848..b0e2aa495e86c2189ab4f316b647dc8a7c60f323 100644 (file)
@@ -56,6 +56,7 @@ public class Main {
       readSourceFile(state, ClassLibraryPrefix+"System.java");
       readSourceFile(state, ClassLibraryPrefix+"String.java");
       readSourceFile(state, ClassLibraryPrefix+"HashMap.java");
+      readSourceFile(state, ClassLibraryPrefix+"HashMapIterator.java");
       readSourceFile(state, ClassLibraryPrefix+"HashEntry.java");
       readSourceFile(state, ClassLibraryPrefix+"Integer.java");
       readSourceFile(state, ClassLibraryPrefix+"StringBuffer.java");