solved Task 3
This commit is contained in:
parent
15e14023db
commit
d2d55109a8
|
@ -1,13 +1,11 @@
|
||||||
package ch.zhaw.ads;
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public class MyHashtable<K, V> implements Map<K, V> {
|
public class MyHashtable<K, V> implements Map<K, V> {
|
||||||
private K[] keys = (K[]) new Object[10];
|
private K[] keys;// = (K[]) new Object[10];
|
||||||
private V[] values = (V[]) new Object[10];
|
private V[] values;// = (V[]) new Object[10];
|
||||||
|
|
||||||
private int hash(Object k) {
|
private int hash(Object k) {
|
||||||
int h = Math.abs(k.hashCode());
|
int h = Math.abs(k.hashCode());
|
||||||
|
@ -16,36 +14,97 @@ public class MyHashtable<K, V> implements Map<K, V> {
|
||||||
|
|
||||||
public MyHashtable(int size) {
|
public MyHashtable(int size) {
|
||||||
// to be done
|
// to be done
|
||||||
|
keys = (K[]) new Object[size];
|
||||||
|
values = (V[]) new Object[size];
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyHashtable() {
|
||||||
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes all mappings from this map (optional operation).
|
// Removes all mappings from this map (optional operation).
|
||||||
public void clear() {
|
public void clear() {
|
||||||
// to be done
|
// 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).
|
// Associates the specified value with the specified key in this map (optional operation).
|
||||||
public V put(K key, V value) {
|
public V put(K key, V value) {
|
||||||
// to be done
|
// 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.
|
// Returns the value to which this map maps the specified key.
|
||||||
public V get(Object key) {
|
public V get(Object key) {
|
||||||
// to be done
|
// 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).
|
// Removes the mapping for this key from this map if present (optional operation).
|
||||||
public V remove(Object key) {
|
public V remove(Object key) {
|
||||||
// to be done (Aufgabe 3)
|
// 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<K> keysToRehash = new ArrayList<>();
|
||||||
|
List<V> 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.
|
// Returns the number of key-value mappings in this map.
|
||||||
public int size() {
|
public int size() {
|
||||||
// to be done
|
// to be done
|
||||||
throw new UnsupportedOperationException();
|
int counter = 0;
|
||||||
|
for(K key : keys) {
|
||||||
|
if(key != null){
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnsupportedOperationException ===================================================================
|
// UnsupportedOperationException ===================================================================
|
||||||
|
|
Loading…
Reference in New Issue