-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
HashTableDoubleHashing.java
51 lines (41 loc) · 1.23 KB
/
HashTableDoubleHashing.java
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
/**
* An implementation of a hashtable using double hashing as a collision resolution technique.
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.datastructures.hashtable;
import java.math.BigInteger;
public class HashTableDoubleHashing<K extends SecondaryHash, V>
extends HashTableOpenAddressingBase<K, V> {
private int hash;
public HashTableDoubleHashing() {
super();
}
public HashTableDoubleHashing(int capacity) {
super(capacity);
}
// Designated constructor
public HashTableDoubleHashing(int capacity, double loadFactor) {
super(capacity, loadFactor);
}
@Override
protected void setupProbing(K key) {
// Cache second hash value.
hash = normalizeIndex(key.hashCode2());
// Fail safe to avoid infinite loop.
if (hash == 0) hash = 1;
}
@Override
protected int probe(int x) {
return x * hash;
}
// Adjust the capacity until it is a prime number. The reason for
// doing this is to help ensure that the GCD(hash, capacity) = 1 when
// probing so that all the cells can be reached.
@Override
protected void adjustCapacity() {
while (!(BigInteger.valueOf(capacity).isProbablePrime(20))) {
capacity++;
}
}
}