solved s09 ex4
This commit is contained in:
parent
b4d1461048
commit
23f5ee949c
@ -1,3 +1,5 @@
|
||||
package ch.zhaw.ads;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class MyHashtable<K,V> implements java.util.Map<K,V> {
|
||||
@ -5,48 +7,87 @@ public class MyHashtable<K,V> implements java.util.Map<K,V> {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =======================================================================
|
||||
|
@ -1,3 +1,5 @@
|
||||
package ch.zhaw.ads;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.Before;
|
||||
import static org.junit.Assert.*;
|
||||
|
Loading…
x
Reference in New Issue
Block a user