-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashtable.cpp
262 lines (216 loc) · 8.37 KB
/
Hashtable.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
#include "Hashtable.h"
using namespace std;
typedef pair<int, pair<string, string>> pairOfIntAndStringPair;
Hashtable::Hashtable() {
arrayOfSightings = new Sighting * [200000]{nullptr};
numSightings = 0;
numBuckets = 200000;
}
Hashtable::~Hashtable() {
//delete memory on heap
delete[] arrayOfSightings;
}
void Hashtable::printSighting(Sighting* sighting) {
cout << "Sighting ID: " << sighting->ID << endl;
cout << "Summary: " << sighting->summary << endl;
cout << "Location: " << sighting->city << ", " << sighting->state << endl;
cout << "Date/Time: " << sighting->date_time << endl;
cout << "Shape: " << sighting->shape << endl;
cout << "Duration: " << sighting->duration << endl;
cout << "Coordinates: " << sighting->latitude << " Latitude, " << sighting->longitude << " Longitude" << endl;
cout << endl;
}
//Portions of this hash function are based on the hash function, djb2, developed by Dan Bernstein
int Hashtable::hashFunction(int hashID) {
int hashValue = 5381;
string stringID = to_string(hashID);
char* stringChars = &stringID[0];
int num = *stringChars++;
while (num != 0) {
hashValue = ((hashValue << 5) + hashValue) + num;
num = *stringChars++;
}
if (hashValue - 177621 >= 0) {
hashValue -= 177621;
}
while (hashValue >= numBuckets) {
hashValue = hashValue - 177621;
}
return hashValue;
}
void Hashtable::insertSighting(Sighting* aSighting) {
//Add to the count of number of sightings in the hash table
numSightings++;
//Determine the index for the sighting using the hash function
int index = hashFunction(aSighting->ID);
//Create a variable for tracking if the sighting has been inserted
bool inserted = false;
//Iterate through the table until an empty spot is found
while (!inserted) {
//Check if the spot is empty
if (arrayOfSightings[index] == nullptr) {
arrayOfSightings[index] = aSighting;
inserted = true;
}
else if (index + 1 >= numBuckets) {
index = 0;
}
else {
index++;
}
}
}
void Hashtable::searchByShape(string shapeName) {
//Create a variable for tracking how many instances of the shape there are
int numInstances = 0;
//Iterate through the entire table to find sightings with the specified shape
for (int i = 0; i < numBuckets; i++) {
//If there is a sighting stored in the spot, print the sighting
if (arrayOfSightings[i] != nullptr) {
//If the sighting does contain the shape, print it
if (shapeName.compare(arrayOfSightings[i]->shape) == 0) {
printSighting(arrayOfSightings[i]);
numInstances++;
}
}
}
if (numInstances == 0) {
cout << "The shape, " << shapeName << ", was not reported in any sightings." << endl;
}
else {
cout << "There are " << numInstances << " UFO sightings of " << numSightings << " that describe the UFO shape as a " << shapeName << endl;
cout << "The sightings described as a " << shapeName << "account for " << fixed << setprecision(2) << (numInstances / (float)numSightings) * 100 << '%' << " of the total number of reported sightings." << endl;
}
}
void Hashtable::numSightingsInCity(string cityName, string stateName) {
//Create a variable for tracking the number of sightings in the city
int numCitySightings = 0;
//Iterate through the hash table to find each instance
for (int index = 0; index < numBuckets; index++) {
//Ensure there are sightings stored in the current spot
if (arrayOfSightings[index] != nullptr) {
//If both the city and state match, add it to the count
if (cityName.compare(arrayOfSightings[index]->city) == 0 && stateName.compare(arrayOfSightings[index]->state) == 0) {
numCitySightings++;
printSighting(arrayOfSightings[index]);
}
}
}
//Print the total number of sightings in the city
if (numCitySightings == 0) {
cout << "No sightings were reported in " << cityName << ", " << stateName << "." << endl;
}
else {
cout << "A total of " << numCitySightings << " of " << numSightings << " UFO sightings were reported in " << cityName << ", " << stateName << "." << endl;
cout << "The number of sightings in this city accounts for " << fixed << setprecision(2) << (numCitySightings / (float)numSightings) * 100 << '%' << " of the total number of reported sightings." << endl;
}
}
void Hashtable::mostPopularCities(int N) {
//Create a map to gather the number of sightings in each city
map<pair<string, string>, int> citySightingCounts;
//Iterate through the hash table to tally amounts of sightings for each city
for (int index = 0; index < numBuckets; index++) {
if (arrayOfSightings[index] != nullptr) {
citySightingCounts[make_pair(arrayOfSightings[index]->city, arrayOfSightings[index]->state)] = citySightingCounts[make_pair(arrayOfSightings[index]->city, arrayOfSightings[index]->state)] + 1;
}
}
//Create a minimum priority queue to determine the N most popular cities
priority_queue<pair<int, pair<string, string>>, vector<pair<int, pair<string, string>>>, greater<pair<int, pair<string, string>>> > minQueue;
//Iterate through the map and add the most popular cities to the queue
for (map<pair<string, string>, int>::iterator it = citySightingCounts.begin(); it != citySightingCounts.end(); ++it) {
if (minQueue.size() < N) {
minQueue.push(make_pair(it->second, it->first));
}
else if (it->second > minQueue.top().first) {
minQueue.pop();
minQueue.push(make_pair(it->second, it->first));
}
}
//Add the most popular cities to a vector for printing purposes
vector<pairOfIntAndStringPair> popularCities;
while (!minQueue.empty()) {
popularCities.push_back(minQueue.top());
minQueue.pop();
}
cout << "The " << N << " cities with the most sightings are: " << endl;
for (int i = popularCities.size() - 1; i >= 0; i--) {
cout << N - i << ". " << popularCities[i].second.first << ", " << popularCities[i].second.second << " with " << popularCities[i].first << " sightings, which accounts for " << fixed << setprecision(2) << (popularCities[i].first / (float)numSightings) * 100 << '%' << " of the total number of reported sightings." << endl;
}
}
void Hashtable::searchByKeyword(string keyword) {
//Create a variable for tracking if the keyword was found
bool wasFound = false;
//Create a variable for tracking the number of instances found
int numInstances = 0;
//Iterate through the hash table and search every summary for the keyword
for (int index = 0; index < numBuckets; index++) {
if (arrayOfSightings[index] != nullptr) {
if (arrayOfSightings[index]->summary.find(keyword) != std::string::npos) {
numInstances++;
wasFound = true;
printSighting(arrayOfSightings[index]);
}
}
}
if (wasFound) {
cout << "The keyword, " << keyword << ", was found in " << numInstances << " of " << numSightings << " UFO sighting summaries, which accounts for " << fixed << setprecision(2) << (numInstances / (float)numSightings) * 100 << '%' << " of the total number of reported sightings." << endl;
}
else {
cout << "The keyword, " << keyword << ", was not found in any sighting summaries." << endl;
}
}
void Hashtable::printHashtable() {
//Iterate through the hash table and print each sighting
for (int index = 0; index < numBuckets; index++) {
if (arrayOfSightings[index] != nullptr) {
printSighting(arrayOfSightings[index]);
}
}
}
void Hashtable::findID(int _ID) {
//Determine the hash value for the ID
int IDHash = hashFunction(_ID);
//Create a variable for tracking if the ID was found
bool wasFound = false;
//Create a variable for tracking if the end of the array was reached
bool endReached = false;
//Iterate through the table starting at the hash value
while (IDHash < numBuckets) {
if (arrayOfSightings[IDHash] == nullptr) {
break;
}
else if (arrayOfSightings[IDHash]->ID == _ID) {
printSighting(arrayOfSightings[IDHash]);
wasFound = true;
break;
}
else if (IDHash + 1 == numBuckets && !endReached) {
endReached = true;
IDHash = 0;
}
else {
IDHash++;
}
}
if (!wasFound) {
cout << "Case ID " << _ID << " does not exist." << endl;
}
}
int Hashtable::getLoadFactor() {
return numSightings / numBuckets;
}
void Hashtable::reHash() {
//Determine the new hash table size
int newNumBuckets = numSightings / 0.75;
//Create the expanded hash table
Sighting** tempArrayOfSightings = new Sighting * [newNumBuckets];
for (int i = 0; i < numBuckets; i++) {
tempArrayOfSightings[i] = arrayOfSightings[i];
}
//Reassign variables
arrayOfSightings = tempArrayOfSightings;
numBuckets = newNumBuckets;
}
int Hashtable::getNumSightings() {
return numSightings;
}