-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleHashing.java
46 lines (41 loc) · 1.42 KB
/
DoubleHashing.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
/**
* File: DoubleHashing.java
* @author Mario Torres
* Date: October 25, 2021
* Description: Creates the hash function for double hashing for a hash table
* @param <T> Generic Type
*/
public class DoubleHashing<T> extends HashTable<T> {
/**
* Hash table created using double hashing
*
* @param tableSize
*/
public DoubleHashing(int tableSize) {
super(tableSize);
}
/**
* primaryHash - Returns the primary hash of the double hashing hash function
*
* @param hashObject - Object used to retrieve hashCode used
* @return int location of the primary hash
*/
private int primaryHash(HashObject<T> hashObject) {
return positiveMod(hashObject.getKey().hashCode(), getTableSize());
}
/**
* secondaryHash - Returns the secondary hash of the double hashing hash function
*
* @param hashObject - Object used to retrieve hashCode used
* @return int location of the secondary hash
*/
private int secondaryHash(HashObject<T> hashObject) {
return (1 + positiveMod(hashObject.getKey().hashCode(), getTableSize() - 2));
}
/**
* Returns the index position of our table after implementing a double hashing hash function. Uses both primaryHash and secondaryHash along with given index
*/
protected int hashFunction(HashObject<T> hashObject, int index) {
return positiveMod(primaryHash(hashObject) + (index * secondaryHash(hashObject)), getTableSize());
}
}