diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/MyHashtable.java b/app/src/main/java/ch/nuth/zhaw/exbox/MyHashtable.java index 4db3d6d..904fab8 100644 --- a/app/src/main/java/ch/nuth/zhaw/exbox/MyHashtable.java +++ b/app/src/main/java/ch/nuth/zhaw/exbox/MyHashtable.java @@ -6,37 +6,69 @@ import java.util.Set; @SuppressWarnings("unchecked") public class MyHashtable implements Map { + private int capacity; + private int size; private K[] keys = (K[]) new Object[10]; private V[] values = (V[]) new Object[10]; + public MyHashtable(int size) { + capacity = size; + clear(); + } + + private boolean hashEquals(Object x, Object y) { + if (x == null || y == null) { + return x == y; + } else { + return x.hashCode() == y.hashCode(); + } + } + private int hash(Object k) { int h = Math.abs(k.hashCode()); return h % keys.length; } - public MyHashtable(int size) { - // to be done - } - // Removes all mappings from this map (optional operation). public void clear() { - // to be done - throw new UnsupportedOperationException(); + keys = (K[]) new Object[capacity]; + values = (V[]) new Object[capacity]; } - // Associates the specified value with the specified key in this map (optional operation). + // Associates the specified value with the specified key in this map (optional + // operation). public V put(K key, V value) { - // to be done - throw new UnsupportedOperationException(); + int hashCode = hash(key); + int index = hashCode; + + for (int i = 0; keys[index] != null && !hashEquals(keys[index], key); i++) { + index = hash(hashCode + (int) Math.pow(i, 2)); + } + + if (keys[index] == null) { + size++; + } + + keys[index] = key; + values[index] = value; + + return value; } // Returns the value to which this map maps the specified key. public V get(Object key) { // to be done - throw new UnsupportedOperationException(); + for (int i = 0; i < keys.length; i++) { + if (hashEquals(key, keys[i])) { + return values[i]; + } + } + + return null; } - // Removes the mapping for this key from this map if present (optional operation). + // Removes the mapping for this key from this map if present (optional + // operation). public V remove(Object key) { // to be done (Aufgabe 3) throw new UnsupportedOperationException(); @@ -44,11 +76,11 @@ public class MyHashtable implements Map { // Returns the number of key-value mappings in this map. public int size() { - // to be done - throw new UnsupportedOperationException(); + return size; } - // UnsupportedOperationException =================================================================== + // UnsupportedOperationException + // =================================================================== // Returns a collection view of the values contained in this map. public Collection values() { throw new UnsupportedOperationException(); @@ -74,7 +106,8 @@ public class MyHashtable implements Map { throw new UnsupportedOperationException(); } - // Copies all of the mappings from the specified map to this map (optional operation). + // Copies all of the mappings from the specified map to this map (optional + // operation). public void putAll(Map t) { throw new UnsupportedOperationException(); }