From 23f5ee949cedafa93a56cdacd37e3197c179e732 Mon Sep 17 00:00:00 2001 From: "navid.sassan" Date: Mon, 23 Nov 2020 17:24:48 +0100 Subject: [PATCH] solved s09 ex4 --- 09/src/main/java/ch/zhaw/ads/MyHashtable.java | 73 +++++++++++++++---- 09/src/test/java/ch/zhaw/ads/HashTest.java | 4 +- 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/09/src/main/java/ch/zhaw/ads/MyHashtable.java b/09/src/main/java/ch/zhaw/ads/MyHashtable.java index c5001d6..8f5d820 100644 --- a/09/src/main/java/ch/zhaw/ads/MyHashtable.java +++ b/09/src/main/java/ch/zhaw/ads/MyHashtable.java @@ -1,3 +1,5 @@ +package ch.zhaw.ads; + import java.util.*; public class MyHashtable implements java.util.Map { @@ -5,48 +7,87 @@ public class MyHashtable implements java.util.Map { private V[] values = (V[]) new Object[10]; private int hash(Object k) { - int h = Math.abs(k.hashCode()); - return h % keys.length; + int h = Math.abs(k.hashCode()) % keys.length; + int counter = 0; + + while (keys[h] != null && !keys[h].equals(k)) { + h = (h + 1) % keys.length; + counter++; + if (counter >= keys.length) return -1; + } + return h; } public MyHashtable(int size) { - // to be done + keys = (K[]) new Object[size]; + values = (V[]) new Object[size]; } // Removes all mappings from this map (optional operation). public void clear() { - // to be done - throw new UnsupportedOperationException(); + keys = (K[]) new Object[keys.length]; + values = (V[]) new Object[values.length]; } // 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) { + keys = Arrays.copyOf(keys, keys.length * 2); + values = Arrays.copyOf(values, values.length * 2); + rehash(); + } + + int keyId = hash(key); + if (keyId < 0) return null; + + values[keyId] = value; + keys[keyId] = key; + + return value; } // Returns the value to which this map maps the specified key. public V get(Object key) { - // to be done - throw new UnsupportedOperationException(); + if (key == null) return null; + return values[hash((K) key)]; } // Returns true if this map contains no key-value mappings. public boolean isEmpty() { - // to be done - throw new UnsupportedOperationException(); + return size() == 0; } // 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 keyId = hash((K) key); + if (keyId < 0) return null; + + V removed = values[keyId]; + values[keyId] = null; + keys[keyId] = null; + rehash(); + return removed; } // Returns the number of key-value mappings in this map. public int size() { - // to be done - throw new UnsupportedOperationException(); + int size = 0; + for (int i = 0; i < keys.length; i++) { + if (keys[i] != null) size++; + } + return size; + } + + private void rehash() { + K[] keysClone = keys.clone(); + V[] valuesClone = values.clone(); + clear(); + + for (int i = 0; i < keys.length; i++) { + if (keysClone[i] != null) { + put(keysClone[i], valuesClone[i]); + } + } } // ======================================================================= @@ -85,4 +126,4 @@ public class MyHashtable implements java.util.Map { throw new UnsupportedOperationException(); } -} \ No newline at end of file +} diff --git a/09/src/test/java/ch/zhaw/ads/HashTest.java b/09/src/test/java/ch/zhaw/ads/HashTest.java index e166afe..3189fd4 100644 --- a/09/src/test/java/ch/zhaw/ads/HashTest.java +++ b/09/src/test/java/ch/zhaw/ads/HashTest.java @@ -1,3 +1,5 @@ +package ch.zhaw.ads; + import org.junit.Test; import org.junit.Before; import static org.junit.Assert.*; @@ -176,4 +178,4 @@ public class HashTest { } -} \ No newline at end of file +}