From 2b8a4d3fd1e63f22e9465d978113c160312dd14b Mon Sep 17 00:00:00 2001 From: bdemsky Date: Fri, 3 Nov 2006 02:49:52 +0000 Subject: [PATCH] HashSet --- Robust/src/ClassLibrary/HashMap.java | 10 +++++----- Robust/src/ClassLibrary/HashSet.java | 28 ++++++++++++++++++++++++++++ Robust/src/ClassLibrary/Integer.java | 4 ---- Robust/src/ClassLibrary/Object.java | 1 - Robust/src/Main/Main.java | 1 + 5 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 Robust/src/ClassLibrary/HashSet.java diff --git a/Robust/src/ClassLibrary/HashMap.java b/Robust/src/ClassLibrary/HashMap.java index beb895f6..2d6201de 100644 --- a/Robust/src/ClassLibrary/HashMap.java +++ b/Robust/src/ClassLibrary/HashMap.java @@ -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 index 00000000..43938799 --- /dev/null +++ b/Robust/src/ClassLibrary/HashSet.java @@ -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); + } +} diff --git a/Robust/src/ClassLibrary/Integer.java b/Robust/src/ClassLibrary/Integer.java index 650474b8..4ce665f9 100644 --- a/Robust/src/ClassLibrary/Integer.java +++ b/Robust/src/ClassLibrary/Integer.java @@ -47,8 +47,4 @@ public class Integer { public String toString() { return String.valueOf(value); } - - public int hashCode() { - return value; - } } diff --git a/Robust/src/ClassLibrary/Object.java b/Robust/src/ClassLibrary/Object.java index 0b0a4346..d36f07aa 100644 --- a/Robust/src/ClassLibrary/Object.java +++ b/Robust/src/ClassLibrary/Object.java @@ -5,7 +5,6 @@ public class Object { return String.valueOf(this); } - public boolean equals(Object o) { if (o==this) return true; diff --git a/Robust/src/Main/Main.java b/Robust/src/Main/Main.java index b0e2aa49..94782374 100644 --- a/Robust/src/Main/Main.java +++ b/Robust/src/Main/Main.java @@ -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"); -- 2.34.1