-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleHashing.java
164 lines (154 loc) · 3.91 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Implementation of Double Hashing in Java
* @author Sai Krishna Reddy Syamala (sxs169430)
* @author Mihir Hindocha (mxh170027)
* Ver 1.0: 10/21/2018
*/
package sxs169430;
public class DoubleHashing<T> {
private T[] table; // The array of elements
private int size; // Current size
private int PRIME;
private static final int DEFAULT_SIZE = 1000; // default array size if not provided
/* Constructor */
public DoubleHashing() {
this(DEFAULT_SIZE);
}
public DoubleHashing(int ts) {
formArray(ts);
PRIME = getPrime(ts);
clear();
}
/**
* creates a new array with the specified size
* @param arraySize
*/
@SuppressWarnings("unchecked")
private void formArray(int arraySize) {
table = (T[]) new Object[getPrime(arraySize)];
}
/* Function to get prime number less than table size for myhash2 function */
public int getPrime(int n) {
while (!isPrime(--n));
return n;//Returns a prime number less than the table size
}
/* Check if the number is a prime number */
protected static boolean isPrime(int m) {
int n = m;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
/* Function to get number of elements in the table */
public int size() {
return size;
}
// returns true if the table is empty
public boolean isEmpty() {
return size == 0;
}
/**
* returns a hashValue for the element to be inserted
* @param x - element to be placed in the table
* @return hashValue
*/
private int myhash1(T x) {
int hashVal = Math.abs(x.hashCode());
hashVal %= table.length;
if (hashVal < 0)
hashVal += table.length;
return hashVal;
}
/**
* second hash function to reduce the collision
* @param x - element to be inserted
* @return hashValue
*/
private int myhash2(T x) {
int hashVal = Math.abs(x.hashCode());
hashVal %= PRIME;
if(hashVal < 0)
hashVal += table.length;
return PRIME - hashVal%PRIME;
}
/* Function to clear hash table */
public void clear() {
size = 0;
for (int i = 0; i < table.length; i++)
table[i] = null;
}
/**
* returns a slot in the table for the given element
* @param key - element to be inserted
* @return - index in the table
*/
public int find(T key) {
// returns the entry for the key
int hash1 = myhash1(key);
int hash2 = myhash2(key);
while (table[hash1] != null && !table[hash1].equals(key)) {
hash1 += hash2;
if (hash1 >= table.length)
hash1 -= table.length;
}
return hash1;
}
/**
* function to add an element to the table
* @param x - element to be added
* @return - true if element is inserted, false if element already exists
*/
public boolean add(T x) {
// Insert x
int hash1 = find(x);
if (contains(x))
return false; // if the element already exists in the table
table[hash1] = x;
size++;
// Rehash if the table is half full
if (size > table.length / 2)
rehash();
return true;
}
/**
* function to expand the array and rehash all the old values.
*/
private void rehash() {
T[] oldArray = table;
// Create a new double-sized, empty table
formArray(2 * oldArray.length);
size = 0;
// Copy the old array
for (T item : oldArray)
if (item != null)
add(item);
}
/**
* function to check if the table contains the given element
* @param key - element to be checked
* @return - true if the element exists in the table.
*/
public boolean contains(T key) {
int hash1 = find(key);
if (table[hash1] != null)
return true;
return false;
}
/**
* Function to remove a key
* @param key - element to be removed from the table
* @return - true if element is removed
*/
public boolean remove(T key) {
int hash1 = find(key);
if (table[hash1] != null) {
table[hash1] = null;
size--;
return true;
} else
return false;
}
}