diff --git a/src/ch/zhaw/ads/MyHashtable.java b/src/ch/zhaw/ads/MyHashtable.java index 740d37a..bff0186 100644 --- a/src/ch/zhaw/ads/MyHashtable.java +++ b/src/ch/zhaw/ads/MyHashtable.java @@ -1,13 +1,11 @@ package ch.zhaw.ads; -import java.util.Collection; -import java.util.Map; -import java.util.Set; +import java.util.*; @SuppressWarnings("unchecked") public class MyHashtable implements Map { - private K[] keys = (K[]) new Object[10]; - private V[] values = (V[]) new Object[10]; + private K[] keys;// = (K[]) new Object[10]; + private V[] values;// = (V[]) new Object[10]; private int hash(Object k) { int h = Math.abs(k.hashCode()); @@ -16,36 +14,97 @@ public class MyHashtable implements Map { public MyHashtable(int size) { // to be done + keys = (K[]) new Object[size]; + values = (V[]) new Object[size]; + } + + public MyHashtable() { + clear(); } // Removes all mappings from this map (optional operation). public void clear() { // to be done - throw new UnsupportedOperationException(); + keys = (K[]) new Object[1000]; + values = (V[]) new Object[1000]; } // 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(); + if(size() >= keys.length) { + throw new IllegalStateException("Overflow"); + } else { + int i = -1; + int hashCode = hash(key); + int index; + do { + i++; + index = (hashCode + i) % keys.length; + } while (keys[index] != null && !keys[index].equals(key)); + 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(); + int i = -1; + int hashCode = hash(key); + int index; + do { + i++; + index = (hashCode + i) % keys.length; + } while (keys[index] != null && !(keys[index].equals(key))); + return values[index]; } // 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(); + int i = -1; + int hashCode = hash(key); + int index; + do { + i++; + if(i >= keys.length){ + return null; + } + index = (hashCode + i) % keys.length; + } while (keys[index] != null && !(keys[index].equals(key))); + V toReturn = values[index]; + keys[index] = null; + values[index] = null; + List keysToRehash = new ArrayList<>(); + List valuesToRehash = new ArrayList<>(); + i++; + index = (hashCode + i) % keys.length; + while(keys[index] != null) { + keysToRehash.add(keys[index]); + valuesToRehash.add(values[index]); + keys[index] = null; + values[index] = null; + i++; + index = (hashCode + i) % keys.length; + } + for(int indexToRehash = 0; indexToRehash < keysToRehash.size(); indexToRehash++){ + put(keysToRehash.get(indexToRehash), valuesToRehash.get(indexToRehash)); + } + return toReturn; } // Returns the number of key-value mappings in this map. public int size() { // to be done - throw new UnsupportedOperationException(); + int counter = 0; + for(K key : keys) { + if(key != null){ + counter++; + } + } + return counter; } // UnsupportedOperationException ===================================================================