Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package searching;
/**
* With the given partial implementation of LinearProbingHashST, we ask you to
* implement the resize, get and put methods
* You are not allowed to use already existing classes and methods from Java
*/
public class LinearProbingHashST<Key, Value> {
private static final int INIT_CAPACITY = 4;
// Please do not add/remove variables/modify their visibility.
protected int n; // number of key-value pairs in the symbol table
protected int m; // size of linear probing table
protected Key[] keys; // the keys
protected Value[] vals; // the values
/**
* Initializes an empty symbol table.
*/
public LinearProbingHashST() {
this(INIT_CAPACITY);
}
/**
* Initializes an empty symbol table with the specified initial capacity.
*
* @param capacity the initial capacity
*/
public LinearProbingHashST(int capacity) {
m = capacity;
n = 0;
keys = (Key[]) new Object[m];
vals = (Value[]) new Object[m];
}
/**
* Returns the number of key-value pairs in this symbol table.
*
* @return the number of key-value pairs in this symbol table
*/
public int size() {
return n;
}
/**
* Returns the current capacity of the table
*
* @return the current capacity of the table
*/
public int capacity() {
return m;
}
/**
* Returns true if this symbol table is empty.
*
* @return {@code true} if this symbol table is empty;
* {@code false} otherwise
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* Returns true if this symbol table contains the specified key.
*
* @param key the key
* @return {@code true} if this symbol table contains {@code key};
* {@code false} otherwise
* @throws IllegalArgumentException if {@code key} is {@code null}
*/
public boolean contains(Key key) {
if (key == null) throw new IllegalArgumentException("argument to contains() is null");
return get(key) != null;
}
// hash function for keys - returns value between 0 and M-1
protected int hash(Key key) {
return (key.hashCode() & 0x7fffffff) % m;
}
/**
* Resizes the hash table to the given capacity by re-hashing all of the keys
*
* @param capacity the capacity
*/
protected void resize(int capacity) {
LinearProbingHashST<Key, Value> t = new LinearProbingHashST<>(capacity);
for (int i = 0; i < capacity(); i ++) {
if (keys[i] != null)
t.put(keys[i], vals[i]);
keys = t.keys;
vals = t.vals;
m = t.capacity();
* Inserts the specified key-value pair into the symbol table, overwriting the old
* value with the new value if the symbol table already contains the specified key.
* The load factor should never exceed 50% so make sure to resize correctly
*
* @param key the key
* @param val the value
* @throws IllegalArgumentException if {@code key} is {@code null}
*/
public void put(Key key, Value val) {
if (key == null) throw new IllegalArgumentException("BOUUUH");
if (n + 1 > m/2)
resize(2*m);
int i;
for (i = hash(key); keys[i] != null; i = (i+ 1)%m) {
if (keys[i].equals(key)) {
vals[i] = val;
return;
}
}
keys[i] = key;
vals[i] = val;
n++;
}
/**
* TODO
* Returns the value associated with the specified key.
* @param key the key
* @return the value associated with {@code key};
* {@code null} if no such value
* @throws IllegalArgumentException if {@code key} is {@code null}
*/
public Value get(Key key) {
if (key == null) throw new IllegalArgumentException("BOUUUH");
for (int i = hash(key); keys[i] != null; i = (i+ 1)%m) {
if (keys[i].equals(key))
return vals[i];
}
return null;
}
/**
* Returns all keys in this symbol table
*/
public Object[] keys() {
Object[] exportKeys = new Object[n];
int j = 0;
for (int i = 0; i < m; i++)
if (keys[i] != null) exportKeys[j++] = keys[i];
return exportKeys;
}
}