-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.cpp
267 lines (231 loc) · 6.1 KB
/
hash.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "hash.h"
#include <ctime>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <iostream>
hash::hash(hashType type, std::vector<int> tableSizes, bool mode){
srand(time(NULL));
fiftyFifty = mode;
for(int i = 0; i < tableSizes.size(); i++){
std::vector<int> row;
for(int j = 0; j < tableSizes[i]; j++){
row.push_back(-1);
}
table.push_back(row);
if(type == hashType::MULT){
parameters.push_back(nextMultParameter());
}
}
this->type = type;
numInserted = 0;
}
int hash::hashFunction0(int key){
if(type == hashType::DIV){
return key%table[0].size();
} else if(type == hashType::MULT){
return floor(table[0].size()*(key*parameters[0] - floor(key*parameters[0])));
}
}
int hash::hashFunction1(int key){
if(type == hashType::DIV){
if(fiftyFifty){
return (int) floor(key/table[1].size()) % table[1].size();
} else{
return key%table[1].size();
}
} else if(type == hashType::MULT){
return floor(table[1].size()*(key*parameters[1] - floor(key*parameters[1])));
}
}
int hash::hashFunction2(int key){
if(type == hashType::DIV){
return key%table[2].size();
} else if(type == hashType::MULT){
return floor(table[2].size()*(key*parameters[2] - floor(key*parameters[2])));
}
}
int hash::hashFunction3(int key){
if(type == hashType::DIV){
return key%table[3].size();
} else if(type == hashType::MULT){
return floor(table[3].size()*(key*parameters[3] - floor(key*parameters[3])));
}
}
int hash::hashFunction4(int key){
if(type == hashType::DIV){
return key%table[4].size();
} else if(type == hashType::MULT){
return floor(table[4].size()*(key*parameters[4] - floor(key*parameters[4])));
}
}
void hash::insert(int key){
int numMoves = 0;
bool placed = false;
if(search(key)){
return; //Does not allow duplicate input
}
while(!placed && numMoves < 50/*<-- Have the user set maxLoop in a function. Default should be 50*/){
int i = numMoves%table.size(); // specifies table column
int index; //specifies a table cell
switch(i){
case 0:
index = hashFunction0(key);
break;
case 1:
index = hashFunction1(key);
break;
case 2:
index = hashFunction2(key);
break;
case 3:
index = hashFunction3(key);
break;
case 4:
index = hashFunction4(key);
break;
}
if(table[i][index] == -1){ //spot
numInserted++;
table[i][index] = key;
placed = true;
}
else{ //spot to put it in has data -> evict a cuckoo
int tempKey = key;
key = table[i][index];
table[i][index] = tempKey;
numMoves++;
}
}
if(!placed){
//numMoves == 50 and we need to rehash table
//need to resize -> could premeditatingly do this
rehash(); //Resize and rehash the table
insert(key); //Try inserting key again
}
}
void hash::remove(int key){
for(int i = 0; i < table.size(); i++){
int index;
switch(i){
case 0:
index = hashFunction0(key);
break;
case 1:
index = hashFunction1(key);
break;
case 2:
index = hashFunction2(key);
break;
case 3:
index = hashFunction3(key);
break;
case 4:
index = hashFunction4(key);
break;
}
if(table[i][index] == key){
numInserted--;
table[i][index] = -1;
}
}
}
bool hash::search(int key){
for(int i = 0; i < table.size(); i++){
int index;
switch(i){
case 0:
index = hashFunction0(key);
break;
case 1:
index = hashFunction1(key);
break;
case 2:
index = hashFunction2(key);
break;
case 3:
index = hashFunction3(key);
break;
case 4:
index = hashFunction4(key);
break;
}
if(table[i][index] == key){
return true;
}
} return false;
}
void hash::rehash(){
std::vector<int> tempVals; //temporary storage for keys so none are lost
int origSize; //Size of original table used for resizing
int tableSize = 0;
for(int i = 0; i < table.size(); i++){
tableSize += table[i].size();
}
float loadFactor = (float)numInserted/(float)tableSize;
printf("load factor at rehash = %f\n", loadFactor);
numInserted = 0; //this function will call insert over again for each element so need to reset
/*Save values*/
for(int x = 0; x < table.size(); x++){
for(int y = 0; y < table[x].size(); y++){
if(table[x][y] != -1){
tempVals.push_back(table[x][y]);
}
}
}
/*Resize tables and generate different hash functions*/
for(int i = 0; i < table.size(); i++){
origSize = table[i].size(); //Save original table size
if(type == hashType::DIV){ //DIV - clear/resize table and re-insert values
table[i].clear(); //clear the table for rehashing
//Double size
for(int n = 0; n < origSize * 2; n++){
table[i].push_back(-1); //Mark every cell as vacant with -1
}
} else if(type == hashType::MULT){
/* h(k) = floor(m(kA - floor(k*A)))...
...so we have a few options for changing MULT hash function:
a. pick a different constant A' where A'!=A and 0 < A' < 1
b. resize the table, thus changing the m we multiply by
c. both of the above
HIJACKED COMMENT BELOW
1)
for creation of tables with MULT hashing the constructor
needs 5 floats too so if you used the function below
'nextMultParameter' to pick a float then both should work
2)
the float vector to fill is called parameters, its size is the
same size as the table
*/
table[i].clear(); //clear the table for rehashing
//Double size
for(int n = 0; n < origSize * 2; n++){
table[i].push_back(-1); //Mark every cell as vacant with -1
}
//Repick each table's A
for(int k = 0; k < table.size(); k++){
parameters[k] = nextMultParameter();
}
}
}
/*Insert old values into new tables*/
for(int j = 0; j < tempVals.size(); j++){
insert(tempVals[j]); //reinsert all of the values originally in the table
}
}
float hash::nextMultParameter(){
float inRange = static_cast<float>(rand()) / static_cast<float>((RAND_MAX));
while(inRange == 0.0f || inRange == 1.0f){
inRange = static_cast<float>(rand()) / static_cast<float>((RAND_MAX));
}
return inRange;
}
//will be ugly with numbers with 5+ decimal places
void hash::print(){
for(int i = 0; i < table.size(); i++){
for(int j = 0; j < table[i].size(); j++){
printf("%5d",table[i][j]);
}
printf("\n");
}
}