-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJMain.cpp
More file actions
186 lines (156 loc) · 4.39 KB
/
JMain.cpp
File metadata and controls
186 lines (156 loc) · 4.39 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
/*
* Author: Jaden <lastname>
* Date: 07/15/2020
* Contributers:
* Ctl-F (Spencer Brough)
*/
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <chrono>
#include <unordered_map>
#include "FormulaHasher.h"
#include <time.h>
#include <unordered_set>
#define CLOCK_DELAY_MS 2500
using namespace std::chrono;
inline long currentTime(){
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}
using namespace std;
using namespace ctl_f;
// Class: FormulaHasher
// string hash(string);
/**
* Populates the given vector to max byte size of hash. Then populates it's vector's vecor to max byte size of hash.
* Then populates it's vector and so on and so forth till the 3d vector is size maxbyte size
*
* input:
* hashTable: A vector of vectors of vectors of strings, to be populated to max byte size
* return:
* a populated hash tables to max size
* */
vector<vector<vector<string>>> populateHashtable(vector<vector<vector<string>>> &hashTable, int maxSize)
{
for(int i= 0; i <= maxSize; i++)
{
vector<vector<string>> _2ndTable;
for(int j=0; j <= maxSize; j++)
{
vector<string> _3rdTable;
_2ndTable.push_back(_3rdTable);
}
hashTable.push_back(_2ndTable);
}
return hashTable;
}
/**
*
* generates string baased on the maximum worldlength
*
* input:
* maxWordLength: the largest possible word
* return:
* the random string
* */
string generateRandomStringInt(int maxWordLength)
{
int wordLength = rand() % maxWordLength + 1;
string randString = "";
string alphabet =
"0123456789"
"QWERTYUIOPASDFGHJKLZXCVBNM"
"qwertyuiopasdfghjklzxcvbnm,";
for(int i = 0; i < wordLength; i++)
{
randString += alphabet[rand() % alphabet.length()];
}
return randString;
}
/**
* generates a test run of the hash algorithm and reports all collisions, will tell you the total amount of collisions at the end
*
* input:
* hashTable: a vector of vectore of vector of strings, to store the hashcodes in a table
* values: the total number of values to be used during this test run
* maxWordLength: maximum length of words to be used in this test run
*
* */
void testHashtable(vector<vector<vector<string>>> &hashTable, int values, int maxWordLength, int maxSize)
{
populateHashtable(hashTable, maxSize);
unordered_set<string> testedInputs;
unordered_map<string, string> hashMap;
FormulaHasher hasher;
int collisionCounter = 0;
ofstream collisionReport;
collisionReport.open("HashCollisionReport.txt");
if(!collisionReport.is_open()){
cout << "Cannot create report file, aborting\n";
return;
}
long timeStamp = 0;
for(int i = 0; i < values; i++)
{
string word = generateRandomStringInt(maxWordLength);
while(testedInputs.find(word) != testedInputs.end()){
word = generateRandomStringInt(maxWordLength);
}
testedInputs.insert(word);
string hash = hasher.hash(word);
int indexOne = 0;
int indexTwo = 0;
if(currentTime() - timeStamp > CLOCK_DELAY_MS){
cout << "> " << i << "/" << values << " [" << ((double)i / (double)values * 100.0) << "%]\n";
timeStamp = currentTime();
}
//gets proper indexs from the hash
indexOne += hash[0];
indexTwo += hash[1];
bool add = true;
//collision check
for(int j = 0; j < hashTable[indexOne][indexTwo].size(); j++)
{
if(hashTable[indexOne][indexTwo][j] == hash)
{
//cout << "There is a collision between, " << word
// << " and " << hashMap[hash] << endl;
collisionReport << word << "\t" << hashMap[hash] << "\n";
collisionCounter++;
add = false;
}
}
if(add == true)
{
hashMap[hash] = word;
hashTable[indexOne][indexTwo].push_back(hash);
}
}
collisionReport.close();
cout << "\n";
cout << "Testing Report: " << endl;
cout << "Total collisions: " << collisionCounter << " out of " << values << " total tests" << endl;
}
int main()
{
srand(time(NULL));
int values; // = 500000; //999999;
cout << "How many tests do you want to run: ";
cin >> values;
cout << "\n";
if(values < 1){
cout << "Zero Tests Ran\n";
return 0;
}
int maxWordLength = 50;
int maxSize = 255;
vector<vector<vector<string>>> hashTable;
cout << "Starting Test:\n";
long startTime = currentTime();
testHashtable(hashTable, values, maxWordLength, maxSize);
long endTime = currentTime();
cout << "Tested in ~" << ((endTime-startTime) / 1000) << "(s) (" << (endTime-startTime) << "ms)\n";
return 0; //#
}