HashSet
authorbdemsky <bdemsky>
Fri, 3 Nov 2006 02:49:52 +0000 (02:49 +0000)
committerbdemsky <bdemsky>
Fri, 3 Nov 2006 02:49:52 +0000 (02:49 +0000)
Robust/src/ClassLibrary/HashMap.java
Robust/src/ClassLibrary/HashSet.java [new file with mode: 0644]
Robust/src/ClassLibrary/Integer.java
Robust/src/ClassLibrary/Object.java
Robust/src/Main/Main.java

index beb895f62803d4b02cc07be850be48f5920d898a..2d6201de66faa7f9937e4c82a5d922f8ba50e649 100644 (file)
@@ -53,13 +53,13 @@ public class HashMap {
     Object remove(Object key) {
        int bin=key.hashCode()%table.length;
        HashEntry ptr=table[bin];
-       if (ptr.key==key) {
+       if (ptr.key.equals(key)) {
            table[bin]=ptr.next;
            numItems--;
            return ptr.value;
        }
        while(ptr.next!=null) {
-           if (ptr.next.key==key) {
+           if (ptr.next.key.equals(key)) {
                Object oldvalue=ptr.value;
                ptr.next=ptr.next.next;
                numItems--;
@@ -73,7 +73,7 @@ public class HashMap {
        int bin=key.hashCode()%table.length;
        HashEntry ptr=table[bin];
        while(ptr!=null) {
-           if (ptr.key==key) {
+           if (ptr.key.equals(key)) {
                return ptr.value;
            }
        }
@@ -84,7 +84,7 @@ public class HashMap {
        int bin=key.hashCode()%table.length;
        HashEntry ptr=table[bin];
        while(ptr!=null) {
-           if (ptr.key==key) {
+           if (ptr.key.equals(key)) {
                return true;
            }
        }
@@ -100,7 +100,7 @@ public class HashMap {
        int bin=key.hashCode()%table.length;
        HashEntry ptr=table[bin];
        while(ptr!=null) {
-           if (ptr.key==key) {
+           if (ptr.key.equals(key)) {
                Object oldvalue=ptr.value;
                ptr.value=value;
                return oldvalue;
diff --git a/Robust/src/ClassLibrary/HashSet.java b/Robust/src/ClassLibrary/HashSet.java
new file mode 100644 (file)
index 0000000..4393879
--- /dev/null
@@ -0,0 +1,28 @@
+public class HashSet {
+    HashMap map;
+    HashSet() {
+       map=new HashMap();
+    }
+    HashSet(int initialCapacity) {
+       map=new HashMap(initialCapacity);
+    }
+    HashSet(int initialCapacity, float loadFactor) {
+       map=new HashMap(initialCapacity, loadFactor);
+    }
+    public boolean add(Object o) {
+       return (map.put(o, this)==null);
+    }
+    public boolean remove(Object o) {
+       return (map.remove(o)!=null);
+    }
+    public boolean isEmpty() {
+       return map.isEmpty();
+    }
+
+    public int size() {
+       return map.size();
+    }
+    public HashMapIterator iterator() {
+       return map.iterator(0);
+    }
+}
index 650474b805475f0577bef9d5c53405fc1a0e81e8..4ce665f94193147b144248dd0cdf76994760cb6e 100644 (file)
@@ -47,8 +47,4 @@ public class Integer {
     public String toString() {
        return String.valueOf(value);
     }
-
-    public int hashCode() {
-       return value;
-    }
 }
index 0b0a4346103973636129deb96ce3c52184337bad..d36f07aa37cbba7bc75258ada86c1912bec8cff6 100644 (file)
@@ -5,7 +5,6 @@ public class Object {
        return String.valueOf(this);
     }
 
-
     public boolean equals(Object o) {
        if (o==this)
            return true;
index b0e2aa495e86c2189ab4f316b647dc8a7c60f323..9478237483155ae4b209f7ca8098fb915ab24a3b 100644 (file)
@@ -55,6 +55,7 @@ public class Main {
       readSourceFile(state, ClassLibraryPrefix+"Object.java");
       readSourceFile(state, ClassLibraryPrefix+"System.java");
       readSourceFile(state, ClassLibraryPrefix+"String.java");
+      readSourceFile(state, ClassLibraryPrefix+"HashSet.java");
       readSourceFile(state, ClassLibraryPrefix+"HashMap.java");
       readSourceFile(state, ClassLibraryPrefix+"HashMapIterator.java");
       readSourceFile(state, ClassLibraryPrefix+"HashEntry.java");