-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjb2HashTable.cpp
117 lines (98 loc) · 3.25 KB
/
djb2HashTable.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
/*******************************************************
* CS 3100-01 Data Structures and Algorithms *
* Tyler Cosculluela *
* Professor Liu *
* December 5, 2018 *
* *
* Project 4 - Hashmaps *
******************************************************/
#include "djb2HashTable.h"
djb2HashTable::djb2HashTable() {
for (int i=0; i< 88801; ++i){
table[i] = new LList(); // "Perfectly balanced,
}
}
djb2HashTable::~djb2HashTable() {
for (int i = 0; i < 88801; ++i) {
delete table[i]; // as all things should be" -Thanos
}
}
//www.cse.yorku.ca/%7eoz/hash.html
unsigned long djb2HashTable::hash(const char *str) { //modified to work on c_strings
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash % 88801;
}
//Returns true if the search key is located in the table
bool djb2HashTable::search(string sKey) {
unsigned long hashCode = hash(sKey.c_str());
LList* bucket = table[hashCode];
if (bucket->locate(sKey)) {
cout << sKey << " found in bucket #" << hashCode << endl;
return true;
} else return false;
}
//Returns true if the insertion key is unique, and adds the key to the table
bool djb2HashTable::insert(string iKey) {
unsigned long hashCode = hash(iKey.c_str()); //.c_str() converts to a const char array,
LList* bucket = table[hashCode];
//Don't insert the key if it's already in the table
if (!search(iKey)){
bucket->insertEnd(iKey);
cout << "Inserted " << iKey << " into bucket #" << hashCode << endl;
return true;
} else return false;
}
//Returns true if the key is found, and removes it from the table
bool djb2HashTable::remove(string sKey) {
unsigned long hashCode = hash(sKey.c_str());
if (!search(sKey)) return false; //Can't remove a key if it doesn't exist
LList *bucket = table[hashCode];
bucket->removeNode(sKey);
cout << "Removed " << sKey << " from bucket #" << hashCode << endl;
return true;
}
//Print the table to console
void djb2HashTable::print() {
for (int i =0; i<88801; ++i) {
cout << "Printing bucket - " << i << endl;
cout << *table[i];
}
cout << "\n--There are " << num << " items in the table" << endl;
}
//Print the table to a text file
void djb2HashTable::log(ostream& outs) {
for (int i =0; i<88801; ++i) {
outs << "Bucket - " << i << endl;
outs << *table[i];
}
}
//Loads the table from an input file
bool djb2HashTable::loadFile(const string &filename) {
ifstream ins(filename.c_str());
if (!ins){
cout << "Error: unable to open file" << endl;
return false;
}
string last;
int tempNum;
getline(ins, last);
insert(last);
num = 1;
while (getline(ins, last)){
insert(last);
++num;
}
ins.close();
return true;
}
//Returns the number of occupied buckets
int djb2HashTable::getOccupied() {
int count = 0;
for (int i = 0; i < 88801; ++i) {
if (!table[i]->is_empty()) ++count;
}
return count;
}