-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cpp
111 lines (93 loc) · 3.46 KB
/
Program.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
#include "Program.h"
Program::Program() {
hashTableTrivial = nullptr;
hashTableFNV = nullptr;
iFileName = "empty_I_FILE_NAME";
}
Program::~Program() {
delete hashTableTrivial;
delete hashTableFNV;
}
void Program::run() {
//createIntegerFile(); //Uncomment if you want to test the algorithms with a different
readControlFile(); //integer set of the same size and distribution.
testAlgorithms();
}
void Program::createIntegerFile() {
///Setup
ofstream file("intList.txt");
int first_lo = 100000, first_hi = 199999; /* 1/2 chance to choose a number in [first_lo, first_hi] */
int second_lo = 200000, second_hi = 499999; /* 1/6 chance to choose a number in [second_lo, second_hi] */
int third_lo = 500000, third_hi = 599999; /* 1/3 chance to choose a number in [third_lo, third_hi] */
srandom(time(nullptr));
///Generating and outputting integers to file
for (int i = 0; i < NUM_INTEGERS; i++) {
//Possible numbers that "temp" is: 0,1,2,3,4,5 (six possible outcomes)
long temp = random() % 6;
if (temp <= 2) /* 0,1,2 = 3/6 = 1/2 probability */
file << random() % (first_hi - first_lo + 1) + first_lo << endl;
else if (temp <= 3) /* 3 = 1/6 probability */
file << random() % (second_hi - second_lo + 1) + second_lo << endl;
else /* 4,5 = 2/6 = 1/3 probability */
file << random() % (third_hi - third_lo + 1) + third_lo << endl;
}
///Close output file
file.close();
}
void Program::readControlFile() {
///Open input file
ifstream iFile("ControlFile.txt");
if (!iFile.is_open()) {
cout << "Could not open control file." << endl;
exit(200);
}
///Reads line by line, running code based on each provided command in the file
string str;
while (iFile >> str)
if (str == "or") {
iFile >> str;
iFileName = str;
readIntFile();
}
///Close input file
iFile.close();
}
void Program::readIntFile() {
///Open input file
ifstream iFile(iFileName);
if (!iFile.is_open()) {
cout << "Could not open integer file." << endl;
exit(200);
}
///Reads the integer, then inserts it into vector.
string str = "empty_TEMP_STRING";
while (getline(iFile, str)) {
data.push_back(stoull(str));
}
///Close input file
iFile.close();
}
void Program::testAlgorithms() {
///Setup
hashTableTrivial = new TrivialHashTable(data.size());
hashTableFNV = new FNVHashTable();
const int oneMillion = 1000000;
time_point start;
time_point end;
///Test Trivial
cout << "Inserting into trivial hash table..." << endl;
start = steady_clock::now();
for (auto & num : data) hashTableTrivial->insert(num);
end = steady_clock::now();
cout << "Inserted all data into trivial hash table in "
<< duration_cast<microseconds>(end - start).count() / static_cast<double>(oneMillion)
<< " seconds with " << hashTableTrivial->getNumCollisions() << " collisions." << endl;
///Test FNV
cout << "Inserting into FNV hash table..." << endl;
start = steady_clock::now();
for (auto & num : data) hashTableFNV->insert(num);
end = steady_clock::now();
cout << "Inserted all data into FNV hash table in "
<< duration_cast<microseconds>(end - start).count() / static_cast<double>(oneMillion)
<< " seconds with " << hashTableFNV->getNumCollisions() << " collisions." << endl;
}