55
66public 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*/
0 commit comments