solved s09 ex4
This commit is contained in:
parent
b4d1461048
commit
23f5ee949c
@ -1,3 +1,5 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class MyHashtable<K,V> implements java.util.Map<K,V> {
|
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 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()) % keys.length;
|
||||||
return h % 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) {
|
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).
|
// Removes all mappings from this map (optional operation).
|
||||||
public void clear() {
|
public void clear() {
|
||||||
// to be done
|
keys = (K[]) new Object[keys.length];
|
||||||
throw new UnsupportedOperationException();
|
values = (V[]) new Object[values.length];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
if (size() >= keys.length) {
|
||||||
throw new UnsupportedOperationException();
|
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.
|
// Returns the value to which this map maps the specified key.
|
||||||
public V get(Object key) {
|
public V get(Object key) {
|
||||||
// to be done
|
if (key == null) return null;
|
||||||
throw new UnsupportedOperationException();
|
return values[hash((K) key)];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if this map contains no key-value mappings.
|
// Returns true if this map contains no key-value mappings.
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
// to be done
|
return size() == 0;
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
int keyId = hash((K) key);
|
||||||
throw new UnsupportedOperationException();
|
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.
|
// Returns the number of key-value mappings in this map.
|
||||||
public int size() {
|
public int size() {
|
||||||
// to be done
|
int size = 0;
|
||||||
throw new UnsupportedOperationException();
|
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.Test;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user