Skip to content

Commit 271bcd0

Browse files
committed
Fix universal hashing, implement ohter haching functions
1 parent af2f539 commit 271bcd0

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

HashTable/HashTableInterface.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
public interface HashTableInterface<K, V> {
77

8+
public static enum HASH_FUNCTION {
9+
10+
UNIVERSAL, MODULO, MULTIPLICATIF
11+
};
812
public static final int INITIAL_TABLE_SIZE = 10;
913
public static final double LOAD_FACTOR = 0.5;
1014
public static final int BIGNUMBER = 9161;
@@ -13,7 +17,7 @@ public interface HashTableInterface<K, V> {
1317

1418
/* get the position of key */
1519
int indexOf(K key);
16-
20+
1721
/* check whether the specified key exist in the hashtable */
1822
boolean contains(K key);
1923

@@ -43,7 +47,7 @@ public interface HashTableInterface<K, V> {
4347

4448
/* get the number of resizes */
4549
int getNumResizes();
46-
50+
4751
/* get the current load factor */
4852
default float loadFactor() {
4953
return getSize() / (float) getTableSize();
@@ -65,13 +69,26 @@ default void printStatus() {
6569
}
6670

6771
/* return a random universal hashing function */
68-
default Function<K, Integer> getHashingFunction() {
69-
int a, b, p;
70-
a = r.nextInt();
71-
b = r.nextInt();
72-
p = HashTableInterface.nextPrime(BIGNUMBER);
73-
74-
return (x) -> Math.abs(((a * x.hashCode() + b * x.hashCode()) * p) % getTableSize());
72+
default Function<K, Integer> getHashingFunction(){
73+
return getHashingFunction(HASH_FUNCTION.UNIVERSAL);
74+
}
75+
default Function<K, Integer> getHashingFunction(HASH_FUNCTION type) {
76+
Function<K, Integer> f;
77+
if (type == HASH_FUNCTION.MODULO) {
78+
f = x -> Math.abs(x.hashCode()) % getTableSize();
79+
} else if (type == HASH_FUNCTION.UNIVERSAL) {
80+
int a, b, p;
81+
a = r.nextInt();
82+
b = r.nextInt();
83+
p = HashTableInterface.nextPrime(BIGNUMBER);
84+
f = x -> Math.abs(((a * x.hashCode() + b) % p) % getTableSize());
85+
} else {
86+
f = x -> {
87+
double c = 0.618;
88+
return (int) Math.floor(((Math.abs(x.hashCode()) * c) % 1) * getTableSize());
89+
};
90+
}
91+
return f;
7592
}
7693

7794
/* get the next prime number after a*/

HashTable/SeparateChaining/SCHashTable.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public SCHashTable() {
1717
foh = getHashingFunction();
1818
}
1919

20-
public SCHashTable(Function<K, Integer> f) {
20+
public SCHashTable(HASH_FUNCTION type) {
2121
array = new Node[tableSize];
22-
foh = f;
22+
foh = getHashingFunction(type);
2323
}
2424

2525
@Override
@@ -175,6 +175,9 @@ public static void main(String[] args) {
175175
ht.put("walter", "white");
176176
ht.put("arys", "startk");
177177
ht.put("dextr", "red");
178+
ht.put("benaich", "white");
179+
ht.put("med", "startk");
180+
ht.put("real", "red");
178181

179182
ht.print();
180183

@@ -188,11 +191,20 @@ public static void main(String[] args) {
188191
ht.printGraph();
189192
ht.printStatus();
190193

191-
// Testing
192-
SCHashTable<String, String> table = new SCHashTable<>();
193-
for (int i = 0; i < 100000; i++) {
194+
// Testing Modulo
195+
testing(HASH_FUNCTION.UNIVERSAL);
196+
testing(HASH_FUNCTION.MULTIPLICATIF);
197+
testing(HASH_FUNCTION.MODULO);
198+
}
199+
200+
private static void testing(HASH_FUNCTION type) {
201+
System.out.println("============ "+type+" ============");
202+
SCHashTable<String, String> table = new SCHashTable<>(type);
203+
for (int i = 0; i < 100; i++) {
194204
table.put(HashTableInterface.randomString(), HashTableInterface.randomString());
195205
}
196206
table.printStatus();
207+
table.printGraph();
197208
}
209+
198210
}

0 commit comments

Comments
 (0)