forked from client69/Open
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearProbingHashTable.java
More file actions
87 lines (70 loc) · 2.74 KB
/
Copy pathLinearProbingHashTable.java
File metadata and controls
87 lines (70 loc) · 2.74 KB
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
84
85
86
87
public class LinearProbingHashTable<TKey, TValue> {
private int size; //# of elements
private TKey[] keys; //The hash table: stored in parallel arrays
private TValue[] values;
//Initialize hash table
public LinearProbingHashTable(int initialCapacity) {
keys = (TKey[]) new Object[initialCapacity];
values = (TValue[]) new Object[initialCapacity];
size = 0;
}
//Add element to hash table
public void put(TKey key, TValue value) {
//If # of elements becomes 1/2 size of array, double it
if (size + 1 > keys.length / 2)
resize(keys.length * 2);
//Calculate hash & convert it to index of bucket: chops hash code in half
//(since no negatives) and uses modulo to scale to 0 - buckets.length - 1
int index = Math.abs(key.hashCode()) % keys.length;
//Starting at hash index, advance until an empty bucket is found
while (keys[index] != null) {
index++;
if (index >= keys.length)
index = 0;
}
//Place element
keys[index] = key;
values[index] = value;
size++;
}
//Get value of element in hash table, given key
public TValue get(TKey key) {
//Calculate hash & convert it to index of bucket
int index = Math.abs(key.hashCode()) % keys.length;
//Starting at hash index, advance until the key or an
//empty element is found (empty element means that
//the loop has looked at all keys that collide to the
//same hash and didn't find your key)
while (keys[index] != null && !keys[index].equals(key)) {
index++;
if (index >= keys.length)
index = 0;
}
//Either returns the key or null if the key wasn't found
return (TValue)values[index];
}
//Gets the # of elements in the hash table
public int size() {
return size;
}
//Is the hash table empty?
public boolean isEmpty() {
return size == 0;
}
//Change size of hash table, reassign all elements to new buckets
private void resize(int newCapacity) {
//Save old hash table
TKey[] oldKeys = keys;
TValue[] oldValues = values;
//Reset hash table to the new size
keys = (TKey[]) new Object[newCapacity];
values = (TValue[]) new Object[newCapacity];
size = 0;
//Go through old hash table's buckets and rehash filled ones
for (int i = 0; i < oldKeys.length; i++) {
if (oldKeys[i] != null) {
put(oldKeys[i], oldValues[i]);
}
}
}
}