-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.cpp
518 lines (406 loc) · 11 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include "hash.h"
hash::hash()
{
hash_table = new std::list<unsigned long long int>[20];
this->table_size = 20;
this->items = 0;
}
hash::hash(unsigned long long int size)
{
hash_table = new std::list<unsigned long long int>[size];
this->table_size = size;
this->items = 0;
}
void hash::insert_hash(unsigned long long int k)
{
if (table_size + 1 == items) double_size();
insert(k);
}
void hash::insert(unsigned long long int k)// hash key
{
unsigned long long int index = hash_function(k, table_size);
hash_table[index].push_back(k);
this->items++;
}
void hash::delete_hash(unsigned long long int k)
{
if (table_size - 1 == (items / 4)) halve_size();
// get the hash index of key
unsigned long long int index = hash_function(k, table_size);
// find the key in (inex)th std::list
std::list <unsigned long long int> ::iterator i;
for (i = hash_table[index].begin();
i != hash_table[index].end(); i++) {
if (*i == k)
{
break;
}
}
// if key is found in hash table, remove it
if (i != hash_table[index].end())
{
hash_table[index].erase(i);
this->items--;
}
}
unsigned long long int hash::search_hash(unsigned long long int k)
{
unsigned long long int index = hash_function(k, table_size);
// find the key in (inex)th std::list
std::list <unsigned long long int> ::iterator i;
for (i = hash_table[index].begin();
i != hash_table[index].end(); i++) {
if (*i == k)
break;
}
if (i == hash_table[index].end())
return 666;
else
return hash_function(*i,this->table_size);
}
void hash::show_hash()
{
for (int i = 0; i < table_size; i++) {
std::cout << i;
for (auto x : hash_table[i])
std::cout << " --> " << x;
std::cout << std::endl;
}
}
unsigned long long int hash::hash_function(unsigned long long int k, unsigned long long int ts)
{
unsigned long long int p = get_next_prime(table_size);
unsigned long long int r = (double)rand() / RAND_MAX;
unsigned long long int a = (p - 1) * r + 1;
unsigned long long int b = (p - 1) * r;
std::string a1 = std::to_string(a);
std::string k1 = std::to_string(k);
std::string ans = multiply(a1, k1);
std::string::size_type sz = 0; // alias of size_t
unsigned long long y;
while (!ans.empty()) {
y = std::stoull(ans, &sz, 0);
ans = ans.substr(sz);
}
//unsigned long long int y = std::stoll(ans);
//unsigned long long int y = a * k;
return ((y + b) % p) % ts; //here, m is the table size
}
void hash::double_size()
{
std::list<unsigned long long int>* temp = new std::list<unsigned long long int>[table_size];
unsigned long long int gt = table_size;
for (int i = 0; i < table_size; i++)
{
for (auto x : hash_table[i])
{
temp[i].push_back(x);
}
}
delete[] hash_table;
this->table_size *= 2;
hash_table = new std::list<unsigned long long int>[table_size];
// this is rehashing on the hash_table.
for (int i = 0; i < gt; i++)
{
for (auto x : temp[i])
{
insert(x);
}
}
delete[] temp; // need to delete the chains.
}
void hash::halve_size()
{
std::list<unsigned long long int>* temp = new std::list<unsigned long long int>[table_size/2];
for (int i = 0; i < table_size; i++)
{
for (auto x : hash_table[i])
{
unsigned long long int k = hash_function(x, table_size/2);
temp[i].push_back(k);
}
}
delete[] hash_table;
hash_table = new std::list<unsigned long long int>[table_size / 2];
this->table_size /= 2;
for (int i = 0; i < table_size; i++)
{
for (auto x : temp[i])
{
insert(x);
}
}
delete[] temp; // need to delete the chained portion.
}
void hash::displayHash() {
for (int i = 0; i < this->table_size; i++) {
std::cout <<"|| " << i << " ||";
for (auto x : hash_table[i])
{
std::cout << " --> " << x;
}
std::cout << std::endl;
}
}
int hash::count_collisons()
{
int collisons = 0;
for (int i = 0; i < table_size; i++)
{
if (hash_table[i].size() > 1)
{
collisons += hash_table[i].size() -1;
}
}
return collisons;
}
bool hash::is_prime(int p)
{
int i;
bool isPrime = true;
for (i = 2; i <= p / 2; ++i)
{
if (p % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
return 1;
else
return 0;
}
int hash::get_next_prime(unsigned long long int t)
{
for (int i = t; i < (2*t);i++)
{
if (is_prime(i)) return i;
}
return -1;
}
int hash::get_next_prime2(unsigned long long int t)
{
for (int i = (2*t) -1; i >= 0;i--)
{
if (is_prime(i)) return i;
}
}
std::string hash::multiply(std::string num1, std::string num2)
{
int len1 = num1.size();
int len2 = num2.size();
if (len1 == 0 || len2 == 0)
return "0";
// will keep the result number in std::vector
// in reverse order
std::vector<int> result(len1 + len2, 0);
// Below two indexes are used to find positions
// in result.
int i_n1 = 0;
int i_n2 = 0;
// Go from right to left in num1
for (int i = len1 - 1; i >= 0; i--)
{
int carry = 0;
int n1 = num1[i] - '0';
// To shift position to left after every
// multiplication of a digit in num2
i_n2 = 0;
// Go from right to left in num2
for (int j = len2 - 1; j >= 0; j--)
{
// Take current digit of second number
int n2 = num2[j] - '0';
// Multiply with current digit of first number
// and add result to previously stored result
// at current position.
int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
// Carry for next iteration
carry = sum / 10;
// Store result
result[i_n1 + i_n2] = sum % 10;
i_n2++;
}
// store carry in next cell
if (carry > 0)
result[i_n1 + i_n2] += carry;
// To shift position to left after every
// multiplication of a digit in num1.
i_n1++;
}
// ignore '0's from the right
int i = result.size() - 1;
while (i >= 0 && result[i] == 0)
i--;
// If all were '0's - means either both or
// one of num1 or num2 were '0'
if (i == -1)
return "0";
// generate the result std::string
std::string s = "";
while (i >= 0)
s += std::to_string(result[i--]);
return s;
}
unsigned long long int hash::hash_function2(unsigned long long int k, unsigned long long int ts)
{
unsigned long long int p = get_next_prime2(table_size);
unsigned long long int r = (double)rand() / RAND_MAX;
unsigned long long int a = (p - 1) * r + 1;
unsigned long long int b = (p - 1) * r;
std::string a1 = std::to_string(a);
std::string k1 = std::to_string(k);
std::string ans = multiply(a1, k1);
std::string::size_type sz = 0; // alias of size_t
unsigned long long y;
while (!ans.empty()) {
y = std::stoull(ans, &sz, 0);
ans = ans.substr(sz);
}
//unsigned long long int y = std::stoll(ans);
//unsigned long long int y = a * k;
return ((y + b) % p) % ts; //here, m is the table size
}
void hash::insert_double_hashing(unsigned long long int k, int total_keys)
{
unsigned long long int index1 = hash_function(k, table_size);
if (items == table_size) double_size2(); //function for if table is full.
if (hash_table[index1].empty())
{
this->items++;
hash_table[index1].push_back(k);
}
else
{
unsigned long long int index2 = hash_function2(k, table_size);
unsigned long long int index3;
int i;
for (i = 1; i < total_keys; i++)
{
index3 = (index1 + (i * index2)) % table_size;
if (hash_table[index3].empty())
{
this->items++;
hash_table[index3].push_back(k);
break;
}
}
}
}
void hash::double_size2()
{
std::list<unsigned long long int>* temp = new std::list<unsigned long long int>[table_size];
unsigned long long int gt = table_size;
for (int i = 0; i < table_size; i++)
{
for (auto x : hash_table[i])
{
temp[i].push_back(x);
}
}
delete[] hash_table;
int y = table_size;
this->table_size *= 2;
hash_table = new std::list<unsigned long long int>[table_size];
// this is rehashing on the hash_table.
for (int i = 0; i < y; i++)
{
for (auto x : temp[i])
{
hash_table[i].push_back(x);
}
}
delete[] temp; // need to delete the chains.
}
void hash::build_perfect_hashing()
{
unsigned long long int index, index1;
p_hash_table = new unsigned long long int* [table_size] {0};
for (int i = 0; i < table_size; i++)
{
if (hash_table[i].size() == 1)
{
index = hash_function(hash_table[i].front(), table_size);
p_hash_table[index] = new unsigned long long int[2]{ 0 };
p_hash_table[index][0] = 2;
p_hash_table[index][1] = hash_table[i].front();
}
else if (hash_table[i].size() > 1)
{
index = hash_function(hash_table[i].front(), table_size);
p_hash_table[index] = new unsigned long long int[1 + square(hash_table[i].size()) + 1]{0}; // square of the keys.
p_hash_table[index][0] = hash_table[i].size() + 2;
for (auto j : hash_table[i])
{
index1 = hash_function3(j, square(hash_table[i].size())) + 1;
if (p_hash_table[index][index1] != 0) std::cout << "collision" << std::endl;
p_hash_table[index][index1] = j;
}
}
else if (hash_table[i].empty())
{
p_hash_table[i] = new unsigned long long int[1]{ 0 };
}
}
}
void hash::search_perfect_hashing(unsigned long long int k)
{
unsigned long long int index2;
unsigned long long int index1 = hash_function(k, table_size);
if (p_hash_table[index1][0] == 1)
{
index2 = 1;
std::cout << "this key is located at perfect_hash_table[" << index1 << "]" << index2 << std::endl;
return;
}
else if (p_hash_table[index1][0] == 0)
{
std::cout << "key not present" << std::endl;
return;
}
else
{
index2 = hash_function2(k, square(p_hash_table[index1][0]) ) + 1;
std::cout << "this key is located at index: table[" << index1 << "][" << index2 << "]"<< std::endl;
return;
}
std::cout << "key not present" << std::endl;
return;
}
unsigned long long int hash::square(unsigned long long int s)
{
return s * s;
}
void hash::display_perfect_hashing()
{
for (int i = 0; i < table_size; i++)
{
std::cout << "|| " << i << " || --> ";
for (int j = 1; j < p_hash_table[i][0]; j++)
{
std::cout << "| " << p_hash_table[i][j];
}
std::cout << " |" << std::endl;
}
}
unsigned long long int hash::hash_function3(unsigned long long int k, unsigned long long int ts)
{
unsigned long long int p = get_next_prime2(table_size*6757 + 31);
unsigned long long int r = (double)rand() / RAND_MAX;
unsigned long long int a = (p - 1) * r + 1;
unsigned long long int b = (p - 1) * r;
std::string a1 = std::to_string(a);
std::string k1 = std::to_string(k);
std::string ans = multiply(a1, k1);
std::string::size_type sz = 0; // alias of size_t
unsigned long long y;
while (!ans.empty()) {
y = std::stoull(ans, &sz, 0);
ans = ans.substr(sz);
}
//unsigned long long int y = std::stoll(ans);
//unsigned long long int y = a * k;
return ((y + b) % p) % ts; //here, m is the table size
}