/** A hash table with linear probing and the MAD hash function */
public class HashTable implements Map {
protected static class HashEntry implements Entry {
Object key, value;
HashEntry () { /* default constructor */ }
HashEntry(Object k, Object v) { key = k; value = v; }
public Object key() { return key; }
public Object value() { return value; }
protected Object setValue(Object v) { // set a new value, returning old
Object temp = value;
value = v;
return temp; // return old value
}
}
/** Nested class for a default equality tester */
protected static class DefaultEqualityTester implements EqualityTester {
DefaultEqualityTester() { /* default constructor */ }
/** Returns whether the two objects are equal. */
public boolean isEqualTo(Object a, Object b) { return a.equals(b); }
}
protected static Entry AVAILABLE = new HashEntry(null, null); // empty marker
protected int n = 0; // number of entries in the dictionary
protected int N; // capacity of the bucket array
protected Entry[] A; // bucket array
protected EqualityTester T; // the equality tester
protected int scale, shift; // the shift and scaling factors
/** Creates a hash table with initial capacity 1023. */
public HashTable() {
N = 1023; // default capacity
A = new Entry[N];
T = new DefaultEqualityTester(); // use the default equality tester
java.util.Random rand = new java.util.Random();
scale = rand.nextInt(N-1) + 1;
shift = rand.nextInt(N);
}
/** Creates a hash table with the given capacity and equality tester. */
public HashTable(int bN, EqualityTester tester) {
N = bN;
A = new Entry[N];
T = tester;
java.util.Random rand = new java.util.Random();
scale = rand.nextInt(N-1) + 1;
shift = rand.nextInt(N);
}