-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
393 lines (340 loc) · 13 KB
/
main.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
#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <ctime>
#include "Video_Game.h"
#include "SeperateChaining.h"
#include "Probing.h"
#include "Node.h"
using namespace std;
unsigned long hornerHash(string key, unsigned long tableSize) {
unsigned long hashVal = 0;
for (char letter : key) {
hashVal = hashVal * 37 + letter;
}
return hashVal % tableSize;
}
//sdbm taken from https://stackoverflow.com/questions/14409466/simple-hash-functions
unsigned long sdbmHash(string key, unsigned long tableSize) {
unsigned long hashVal = 0;
for (char letter : key){
hashVal = letter + (hashVal << 6) + (hashVal << 16) - hashVal;
}
return hashVal % tableSize;
}
string videoGameKey(Video_Game vg) {
return vg.getName();
}
string otherVideoGameKey(Video_Game vg) {
return vg.getGenre();
}
int main() {
//Establish header class and vector
Video_Game project;
vector<Video_Game> video_games;
//Read csv file into vector
project.readGamesFromFile("vgsales.csv",video_games);
//TIME VARS
clock_t start;
double duration;
//PARTS A & B
/*
* SEPERATE CHAINING
*/
//Establish Hash Algorithms
int size1 = 503;
int size2 = 1009;
int size3 = 10007;
int size4 = 16573; //first prime number before video_games.size()
int size5 = 46601; //first prime number after video_games.size()
//Establish Hash Tables
SeparateChaining<Video_Game> vgSepChain1(size1,videoGameKey);
SeparateChaining<Video_Game> vgSepChain2(size2,videoGameKey);
SeparateChaining<Video_Game> vgSepChain3(size3,videoGameKey);
SeparateChaining<Video_Game> vgSepChain4(size4,videoGameKey);
SeparateChaining<Video_Game> vgSepChain5(size5,videoGameKey);
//Establish Alternate Hash Tables
SeparateChaining<Video_Game> vgSepChainAltHash(size5,videoGameKey);
SeparateChaining<Video_Game> vgSepChainAltKey(size5,otherVideoGameKey);
SeparateChaining<Video_Game> vgSepChainAltHashAltKey(size5,otherVideoGameKey);
//Establish Probing Tables
Probing<Video_Game> vgProbing1(size1,videoGameKey);
Probing<Video_Game> vgProbing2(size2,videoGameKey);
Probing<Video_Game> vgProbing3(size3,videoGameKey);
Probing<Video_Game> vgProbing4(size4,videoGameKey);
Probing<Video_Game> vgProbing5(size5,videoGameKey);
//Establish Alternate Probing Tables
Probing<Video_Game> vgProbingAltHash(size5,videoGameKey);
Probing<Video_Game> vgProbingAltKey(size5,otherVideoGameKey);
Probing<Video_Game> vgProbingAltHashAltKey(size5,otherVideoGameKey);
//Establish counters
int numSepChain1Reads = 0;
int numSepChain2Reads = 0;
int numSepChain3Reads = 0;
int numSepChain4Reads = 0;
int numSepChain5Reads = 0;
int numSepChain6Reads = 0;
int numSepChain7Reads = 0;
int numSepChain8Reads = 0;
vector<int> sepChainReadsVector;
// ************* 1 ************* //
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgSepChain1.insert(video_games[i], numSepChain1Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size1 << " Seperate Chaining Inserts is: " << numSepChain1Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
sepChainReadsVector.push_back(numSepChain1Reads);
// ************* 2 ************* //
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgSepChain2.insert(video_games[i], numSepChain2Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size2 << " 1009 Seperate Chaining Inserts is: " << numSepChain2Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
sepChainReadsVector.push_back(numSepChain2Reads);
// ************* 3 ************* //
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgSepChain3.insert(video_games[i], numSepChain3Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size3 << " 10007 Seperate Chaining Inserts is: " << numSepChain3Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
sepChainReadsVector.push_back(numSepChain3Reads);
// ************* 4 ************* //
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgSepChain4.insert(video_games[i], numSepChain4Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size4 << " Seperate Chaining Inserts is: " << numSepChain4Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
sepChainReadsVector.push_back(numSepChain4Reads);
// ************* 5 ************* //
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgSepChain5.insert(video_games[i], numSepChain5Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size5 << " 16603 Seperate Chaining Inserts is: " << numSepChain5Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
sepChainReadsVector.push_back(numSepChain5Reads);
//SEPARATE CHAINING, DIFFERENT HASHING FUNCTION
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgSepChainAltHash.altInsert(video_games[i], numSepChain6Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size5 << " sbdm Hash Seperate Chaining Inserts is: " << numSepChain6Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
sepChainReadsVector.push_back(numSepChain6Reads);
//SEPARATE CHAINING, DIFFERENT KEY
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgSepChainAltKey.insert(video_games[i], numSepChain7Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size5 << " alt key Seperate Chaining Inserts is: " << numSepChain7Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
sepChainReadsVector.push_back(numSepChain7Reads);
//SEPARATE CHAINING, DIFFERENT HASHING FUNCTION, DIFFERENT KEY
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size5 << " sbdm Hash w/ alt key Seperate Chaining Inserts is: " << numSepChain8Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
sepChainReadsVector.push_back(numSepChain8Reads);
/*
* QUADRATIC PROBING
*/
//Establish counters
int numProbing1Reads = 0;
int numProbing2Reads = 0;
int numProbing3Reads = 0;
int numProbing4Reads = 0;
int numProbing5Reads = 0;
int numProbing6Reads = 0;
int numProbing7Reads = 0;
int numProbing8Reads = 0;
vector<int> probingReadsVector;
// ************* 1 ************* //
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgProbing1.insert(video_games[i], numProbing1Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size1 << " Probing Inserts is: " << numProbing1Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
probingReadsVector.push_back(numProbing1Reads);
// ************* 2 ************* //
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgProbing2.insert(video_games[i], numProbing2Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size2 << " Probing Inserts is: " << numProbing2Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
probingReadsVector.push_back(numProbing2Reads);
// ************* 3 ************* //
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgProbing3.insert(video_games[i], numProbing3Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size3 << " Probing Inserts is: " << numProbing3Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
probingReadsVector.push_back(numProbing3Reads);
// ************* 4 ************* //
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgProbing4.insert(video_games[i], numProbing4Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size4 << " Probing Inserts is: " << numProbing4Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
probingReadsVector.push_back(numProbing4Reads);
// ************* 5 ************* //
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgProbing5.insert(video_games[i], numProbing5Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size5 << " Probing Inserts is: " << numProbing5Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
probingReadsVector.push_back(numProbing5Reads);
//PROBING, DIFFERENT HASHING FUNCTION
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgProbingAltHash.altInsert(video_games[i], numProbing6Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size5 << " sbdm Hash Probing Inserts is: " << numProbing6Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
probingReadsVector.push_back(numProbing6Reads);
//PROBING, DIFFERENT KEY
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgProbingAltKey.insert(video_games[i], numProbing7Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size5 << " alt key Probing Inserts is: " << numProbing7Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
probingReadsVector.push_back(numProbing7Reads);
//PROBING, DIFFERENT HASHING FUNCTION, DIFFERENT KEY
//Start Timer
start = clock();
//Insert all the things
for (int i=0; i < video_games.size(); ++i) {
vgProbingAltHashAltKey.altInsert(video_games[i], numProbing8Reads);
}
//PRINT TIME
duration = 100*(( clock() - start ) / (double) CLOCKS_PER_SEC);
cout << " The # of reads w/ " << size5 << " sbdm Hash w/ alt key Probing Inserts is: " << numProbing8Reads
<< ". DURATION: " << duration << "ms" << endl;
//Push back into vector
probingReadsVector.push_back(numProbing8Reads);
/*
* READS -> CSV FILES
*/
ofstream readsFile;
readsFile.open("../reads.csv");
//Header
readsFile << ",";
readsFile << size1;
readsFile << ",";
readsFile << size2;
readsFile << ",";
readsFile << size3;
readsFile << ",";
readsFile << size4;
readsFile << ",";
readsFile << size5;
readsFile << ",";
readsFile << size5 << " w/ sdbm Hash,";
readsFile << size5 << " w/ Alt Key,";
readsFile << size5 << " w/ sdbm Hash and Alt Key";
readsFile << endl; //end line
//EACH ROW AFTER
readsFile << "Separate Chaining" << ","; //Row Name
for (int i=0; i < sepChainReadsVector.size(); ++i) {
readsFile << sepChainReadsVector[i]; // add each entry
if (i < sepChainReadsVector.size() - 1) {
readsFile << ","; //add comma after each entry but the last
}
}
readsFile << endl; //end line
readsFile << "Probing" << ","; //Row Name
for (int i=0; i < probingReadsVector.size(); ++i) {
readsFile << probingReadsVector[i]; // add each entry
if (i < probingReadsVector.size() - 1) {
readsFile << ","; //add comma after each entry but the last
}
}
readsFile << endl; //end line
return 0;
}