-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
593 lines (518 loc) · 17 KB
/
Copy pathHashTable.cpp
File metadata and controls
593 lines (518 loc) · 17 KB
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include <iostream>
#include <cstddef>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
template <typename KEY, typename VALUE>
class Entry
{
template<typename K, typename V> friend class HashTable;
private:
KEY key;
VALUE value;
Entry * next_entry;
public:
/*** Constructors ***/
Entry(void) : key(KEY()), value(VALUE()), next_entry(NULL) {}
//If you would like you can convert constructors to have this look
Entry(KEY key, VALUE value)
{
this->template key = key;
this->template value = value;
this->template next_entry = NULL;
}
Entry(KEY key, VALUE value, Entry * next) : key(key), value(value), next_entry(next) {}
//accessors
KEY get_key() { return key; }
VALUE get_value() { return value; }
Entry * get_next_entry() { return next_entry; }
//mutators
void set_key(KEY key) { this->key = key; }
void set_value(VALUE value) { this->value = value; }
void set_next_entry(Entry * entry) { this->next_entry = entry; }
};
template<typename K, typename V>
class HashTable
{
private:
int N;
Entry<K,V> **hash_table; //Make sure you understand this line
bool isResizing; //Used in insert, when resizing
int timesSinceCalled; //Also used in insert, when resizing
public:
HashTable()
{
this->N = 10000;
hash_table = new Entry<K, V> * [this->N];
for (int i = 0; i < this->N; i++)
hash_table[i] = NULL;
isResizing = false;
timesSinceCalled = 0;
}
HashTable(int N)
{
this->N = N;
hash_table = new Entry<K, V> * [this->N];
for (int i = 0; i < this->N; i++)
hash_table[i] = NULL;
isResizing = false;
timesSinceCalled = 0;
}
/* hashcose string to int */
int hashcode(string s) {
int hash_value = 0; //Value you are returning
for (int i = 0; i < s.length(); i++) {
hash_value = (127 * hash_value + s.at(i)) % 16908799; //Charat or at?
}
return hash_value;
}
//hashcode int to int
int hashcode(int i) {
int hash_value = i;
//Algorithms to create hash codes for ints
//Modding by same prime number as was used to make hash codes from strings
if (i % 2 == 0) {
hash_value = (1011 * (hash_value / 5) + (hash_value * 879)+1983) % 16908799;
}
if (i % 3 == 0) {
hash_value = (1031 * (hash_value / 7) + (hash_value * 597)+3124) % 16908799;
}
if (i % 6 == 0) {
hash_value = (1001 * (hash_value / 9) + (hash_value * 231)+1329) % 16908799;
}
else {
hash_value = (1007 * (hash_value / 3) + (hash_value * 1071)+2451) % 16908799;
}
if (hash_value < 0) {
hash_value = abs(hash_value);
}
return hash_value;
}
// hascode char to int
int hashcode(char c) {
//Algorithms to create hash codes for strings
//Again modding by same prime number as was used to make hash codes from strings
//Initially modifying keys
int hash_value = c;
if (c % 2 == 0) {
hash_value *= 135;
}
if (c % 3 == 0) {
hash_value += 137;
}
if (c % 5 == 0) {
hash_value += 6;
}
if (c % 6 == 0) {
hash_value *= 79;
}
if (c % 7 == 0) {
c += 183;
}
//Modifying all keys
hash_value *= 19;
hash_value + 531;
//Using now previously modifed keys to make hash codes
//Using same algorithms as ints
if (c % 2 == 0) {
hash_value = (1011 * (hash_value / 5) + (hash_value * 879) + 1983) % 16908799;
}
if (c % 3 == 0) {
hash_value = (1031 * (hash_value / 7) + (hash_value * 597) + 3124) % 16908799;
}
if (c % 6 == 0) {
hash_value = (1001 * (hash_value / 9) + (hash_value * 231) + 1329) % 16908799;
}
else {
hash_value = (1007 * (hash_value / 3) + (hash_value * 1071) + 2451) % 16908799;
}
if (hash_value < 0) {
hash_value = abs(hash_value);
}
return hash_value;
}
// hascode long to int
int hashcode(unsigned long ul) {
int hash_value = ul;
//Algorithms to create hash codes for unsigned longs
//Again modding by same prime number as was used to make hash codes from strings
//Making values larger because unsigned longs can hold larger values than ints
if (ul % 2 == 0) {
hash_value = (21908 * (hash_value / 5) + (hash_value * 879) + 124536) % 16908799;
}
if (ul % 3 == 0) {
hash_value = (98746 * (hash_value / 7) + (hash_value * 597) + 7382646) % 16908799;
}
if (ul % 6 == 0) {
hash_value = (1789 * (hash_value / 9) + (hash_value * 231) + 172) % 16908799;
}
else {
hash_value = (24371 * (hash_value / 3) + (hash_value * 1071) + 7) % 16908799;
}
if (hash_value < 0) {
hash_value = abs(hash_value);
}
return hash_value;
}
// basic compression function
int compression(int hc) {
return ((5 * hc + 11) % 16908799) % N;
}
// insert e into hash table
void insert(Entry<K, V> * e) {
//Entry<K, V>* entry1;
//Finding the hashcode of the entry (e)'s key
//Compressing/hashing that hashcode
//Storing this value into bucket (the number stored is the bucket the netry is being stored in
int bucket = compression(hashcode(e->get_key()));
//If the bucket is empty, put the entry into the bucket
if (hash_table[bucket] == NULL){
hash_table[bucket] = e;
}
//Else, if the bucket is not empty, put the entry into a chain off of the bucket
else {
e->set_next_entry(hash_table[bucket]);
hash_table[bucket] = e;
/* entry1 = hash_table[bucket];
while (entry1->get_next_entry() != NULL) {
entry1 = entry1->get_next_entry();
}
entry1->set_next_entry(e); */
}
//Calling resize function
double loadFactor;
if (timesSinceCalled > (N/2)) {
if (isResizing == false) {
//Ideal Load Factor is 0.8-1.2
//Growing in load factor is greater than 1.5 becuase load factor has a linear time complexity
//Due to how costly the resize operation is, there is a small gap between the ideal load factor
//and the load factor it is being checked at
loadFactor = compute_load_factor();
if (loadFactor > 1.5) {
resize(1);
timesSinceCalled = 0;
}
}
}
timesSinceCalled++;
}
// replace e1 with e2
void replace(Entry<K, V> * e1, Entry<K, V> * e2) {
Entry<K, V>* temp = find(e1); //Find (e1) returns the address of e1
Entry<K, V>* previousToTemp = find_previous(temp);
Entry<K, V>* tempNextEntry = temp->get_next_entry();
int bucket = -1;
//If the entry is not in the list, return
if (temp == NULL) {
cout << "ERROR: Entry 1 is not in the function. Therefore, entry 1 was not replaced with entry 2." << endl;
return;
}
else {
//If temp is pointing to the last element in the list, but is not the head
if (tempNextEntry == NULL && previousToTemp != NULL) {
previousToTemp->set_next_entry(NULL);
}
//If temp is pointing to an element in the middle of the list
//(not the head or the last element in the list)
else if (tempNextEntry != NULL && previousToTemp != NULL) {
previousToTemp->set_next_entry(tempNextEntry);
}
//If temp is pointing tot he head of the list, and there ARE other elements in the list
else if (tempNextEntry != NULL && previousToTemp == NULL) {
//Finding hashcode of e1's key and compressing it
bucket = compression(hashcode(e1->get_key()));
hash_table[bucket] = tempNextEntry;
}
//If temp is pointing to the head of the list, and there are no other elements in the list,
//No extra steps are required
delete temp; //Deletes what temp is pointing to
temp = e2; //Temp is now pointing to e2
insert(temp); //Inserts temp into hash table (temp is pointing to the address of e2,
//so e2 is inserted into the hash table)
}
}
//find e and return
Entry<K, V> * find(Entry<K, V> * e) {
if (e == NULL) {
return NULL;
}
//Finding hashcode of e's key and compressing it
int bucket = compression(hashcode(e->get_key()));
//Creating an Entry pointer to use as a cursor
//Entry pointer initially points to head of list at bucket passed in
Entry<K, V>* entry1 = hash_table[bucket];
//If the entry is not found, return NULL
if (entry1 == NULL)
{
return NULL;
}
//If the entry is the "head" bucket return that entry
else if ((entry1->get_key() == e->get_key()) && (entry1->get_value() == e->get_value())) {
return hash_table[bucket];
}
//else loop through list extending from that bucket until the entry is found, then return the entry
else {
entry1 = entry1->get_next_entry();
while (entry1 != NULL) {
//Return entry if found
if ((entry1->get_key() == e->get_key()) && (entry1->get_value() == e->get_value())) {
return entry1;
}
//else set entry1 to next entry in the list
else {
entry1 = entry1->get_next_entry();
}
}
}
//If the entry was not found in any of the buckets, return NULL
return NULL;
}
// remove e from the table
void remove(Entry<K, V> * e) {
//(Using pieces from find function)
//Finding hashcode of e's key and compressing it
int bucket = compression(hashcode(e->get_key()));
//Creating an Entry pointer to use as a cursor
//Entry pointer initially points to head of list at bucket passed in
Entry<K, V>* entry1 = hash_table[bucket];
Entry<K, V>* entry2 = NULL;
//If there is nothing in the bucket, return
if (entry1 == NULL) {
cout << "This entry does not exist. " << endl;
return;
}
//If the entry is the "head" bucket, make the next entry the "head" of the list
//Then delete entry1 (the element that matches what was passed into the function
else if ((entry1->get_key() == e->get_key()) && (entry1->get_value() == e->get_value())) {
//Entry1 is already equal to hash_table[bucket] (acts as head of the list)
hash_table[bucket] = entry1->get_next_entry();
delete entry1;
return;
}
//else loop through list extending from that bucket until the entry is found
else {
entry1 = entry1->get_next_entry();
while (entry1 != NULL) {
//If entry is found, set entry1's previous entry equal to (pointing to) entry1's next entry
//Then delete entry1
if ((entry1->get_key() == e->get_key()) && (entry1->get_value() == e->get_value())) {
entry2=find_previous(entry1);
entry2->set_next_entry(entry1->get_next_entry()); //Check?
delete entry1;
}
//else set entry1 to next entry in the list
else {
entry1 = entry1->get_next_entry();
}
}
}
//Calling resize function
double loadFactor;
if (timesSinceCalled > (N / 2)) {
if (isResizing == false) {
//Ideal Load Factor is 0.8-1.2
//Growing in load factor is greater than 1.5 becuase load factor has a linear time complexity
//Due to how costly the resize operation is, there is a small gap between the ideal load factor
//and the load factor it is being checked at
loadFactor = compute_load_factor();
if (loadFactor < 0.5) {
resize(0);
timesSinceCalled = 0;
}
}
}
timesSinceCalled++;
}
// if grow = 1 increase the table
// otherwise decrease the table
//
void resize(bool grow) {
//Creating a flag to check if the table is still resizing (declared as member variable)
isResizing = true;
///Growing
Entry<K, V>** temp = NULL;
Entry<K, V>** temp2 = NULL;
Entry<K, V>*e1 = NULL;
Entry<K, V>*next = NULL;
int oldN = N;
if (grow == 1) {
N = 2 * N; //Doubling the size of the list to grow
}
else {
if (N <= 1) {
cout << "There is only one element in the hash table. Cannot shrink. " << endl;
return;
}
N = N / 2; //Halfing the size of the list to shrink
}
temp = new Entry<K, V>*[N]; //Creates a new dynamic array
temp2 = hash_table; //temp2 now points to the original hash_table, which has the elements you want to move into the new array
hash_table = temp; //hash_table now points to temp (the currently empty array that is double the size of hash_table when this function was called
for (int i = 0; i < oldN ; i++) {
e1 = temp2[i];
while (e1 != NULL) {
next = e1->get_next_entry();
e1->set_next_entry(NULL);
insert(e1);
e1 = next;
}
temp2[i] = NULL;
}
isResizing = false;
}
double compute_load_factor() {
//Load factor is n/N
//N is already computed
//Need to find n
Entry<K, V>* entry1 = NULL; //Creating a pointer to an entry
double n = 0; //Counts number of elements in the hash table (n)
double load_factor;
if (N == 0) {
//If the denominator = 0, the quotient would be undefined
cout << "ERROR: There were 0 buckets. Therefore, the load factor would compute an undefined number.";
cout << "-1 has been returned." << endl;
return -1;
}
//Calculating n
for (int i = 0; i < N; i++) {
//Setting entry1 to the head of the linked list of bucket i
entry1 = hash_table[i];
//Moving to next element in the list and keeping track of n
while (entry1!=NULL) {
n++;
entry1 = entry1->get_next_entry();
}
}
load_factor = n / N;
return load_factor;
}
int longest_chain_length() {
Entry<K, V>* entry1 = NULL; //Creating a pointer to an entry
int chainLengthCounter = 0;
int maxChain = 0;
for (int i = 0; i < N; i++) {
//Setting entry1 to the head of the linked list of bucket i
entry1 = hash_table[i];
//Moving to next element in the list
while (entry1 != NULL) {
chainLengthCounter++;
entry1=entry1->get_next_entry();
}
if (chainLengthCounter > maxChain) {
maxChain = chainLengthCounter;
}
chainLengthCounter = 0;
}
return maxChain;
}
//FINDING PREVIOUS
Entry<K, V>* find_previous(Entry<K, V>* e) {
//Finding hashcode of e's key and compressing it
int bucket = compression(hashcode(e->get_key()));
//Creating an Entry pointer to use as a cursor
//Entry pointer initially points to head of list at bucket passed in
Entry<K, V>* entry1 = hash_table[bucket];
//If there is not a previous entry (if e is the head of the list), return NULL
if ((entry1->get_key() == e->get_key()) && (entry1->get_value() == e->get_value())) {
return NULL;
}
else {
entry1 = entry1->get_next_entry();
while (entry1 != NULL) {
//Return entry if found
if ((entry1->get_key() == e->get_key()) && (entry1->get_value() == e->get_value())) {
}
//else set entry1 to next entry in the list
else {
while (entry1->get_next_entry()->get_key() != e->get_key() && entry1->get_next_entry()->get_value() != e->get_value()) {
entry1 = entry1->get_next_entry();
}
//Returns entry1, which is the previous entry to e
return entry1;
}
}
}
}
//Deconstructor
~HashTable() {
Entry<K, V>* e1;
Entry<K, V>* prev = NULL;
for (int i = 0; i < N; i++) {
e1 = hash_table[i];
while (e1 != NULL) {
prev = e1;
e1 = e1->get_next_entry();
delete prev;
}
}
delete hash_table;
}
//DEBUG FUNCTIONS
void debug1() {
int bucket = compression(hashcode(static_cast <unsigned long> (34)));
Entry<K, V>* current;
current = hash_table[bucket];
while (current!=NULL){
cout << "The memory address of current is " << current << endl;
current = current->get_next_entry(); //Check later!!!! (get/set)
}
}
//Good to use to check resize
//Similar to longestchain
//Run before and after resize is called
void debug2() {
Entry<K, V>* entry1 = NULL; //Creating a pointer to an entry
int chainLengthCounter = 0;
int maxChain = 0;
for (int i = 0; i < N; i++) {
//Setting entry1 to the head of the linked list of bucket i
entry1 = hash_table[i];
//Moving to next element in the list
while (entry1 != NULL) {
chainLengthCounter++;
entry1 = entry1->get_next_entry();
}
if (chainLengthCounter > maxChain) {
maxChain = chainLengthCounter;
}
cout << "The bucket " << i << " has " << chainLengthCounter << " entries. " << endl;
chainLengthCounter = 0;
}
}
};
/* I recommend you make a temp main for testing all of your boundary
* cases. I reserve the right to change the main function. I promise
* it will only call the function prototypes provided; which means, you
* cannot change the prototypes.
*/
//Prof's main method
int main()
{
//seed the random number generator
srand(42);
HashTable<int, string> table_01(10000);
int key;
string value;
Entry<int, string> * e;
// Fill the table with random entries
for (int i = 0; i < 100000; i++)
{
/* create a random entry */
//key = (sizeof(int) < sizeof(long)) ? (static_cast<int>(((unsigned long)rand()) << (sizeof(int) * 8)) | rand()) : rand();
value = "";
key = rand() % 119984135204;
for (int j = 0; j < (rand() % 45 + 1); j++)
value += 'a' + rand() % 26;
//key = value;
e = new Entry<int, string>(key, value);
if (i % 10000 == 0) {
cout << "Currently at element " << i << endl;
}
table_01.insert(e);
}
cout << "Longest Chain: " << table_01.longest_chain_length() << endl;
cout << "Load Factor: " << table_01.compute_load_factor() << endl;
return 0;
}