-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
272 lines (215 loc) · 10 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
/*
* File: main.cpp
* Author: Jason Nawrocki
*
* Created on April 28, 2015, 8:10 PM
*/
/*
* File: main.cpp
* Author: smajerci
*
* Created on November 18, 2014, 7:22 PM
*/
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "Entry.h"
#include "GameBoard.h"
#include "HashTable.h"
#include "main.h"
using namespace std;
int main(int argc, char** argv) {
int numBoards = 10000;
int numSearches = 2500000;
cout << numBoards << " GameBoards inserted; "
<< numSearches << " searches" << endl << endl;
// OUTPUT: PART 1
// ------
//
// *** create required table of statistics with varying load factors
int numBuckets;
//print the data of all the following hash tables:
numBuckets = 2000;
printData(numBuckets, numBoards, numSearches);
numBuckets = 2222;
printData(numBuckets, numBoards, numSearches);
numBuckets = 2500;
printData(numBuckets, numBoards, numSearches);
numBuckets = 2857;
printData(numBuckets, numBoards, numSearches);
numBuckets = 3333;
printData(numBuckets, numBoards, numSearches);
numBuckets = 4000;
printData(numBuckets, numBoards, numSearches);
numBuckets = 5000;
printData(numBuckets, numBoards, numSearches);
numBuckets = 6666;
printData(numBuckets, numBoards, numSearches);
numBuckets = 10000;
printData(numBuckets, numBoards, numSearches);
numBuckets = 20000;
printData(numBuckets, numBoards, numSearches);
// OUTPUT: PART 2
// ------
// NOTE: set DIMENSION in GameBoard.h to 4 for this section
// *** make a hash table of size 100
HashTable table = HashTable(100);
// make an array of 4 GameBoards, all of which are different
GameBoard boards[4];
for (int i = 0; i < 4; ++i) {
boards[i] = randomBoard();
}
while (allDiff(boards) == false) {
for (int i = 0; i < 4; ++i) {
boards[i] = randomBoard();
}
}
// *** insert the first two boards into the hash table with arbitrary values
table.insert(boards[0], 0);
table.insert(boards[1], 1);
cout << "———————————————————------------------------————————" << endl << endl;
cout << "ADDED BOARDS 1 AND 2 TO THE HASH TABLE" << endl << endl;
cout << "———————————————————------------------------————————" << endl << endl;
cout << "SEARCHES AFTER INSERTIONS BUT BEFORE REMOVALS" << endl << endl;
cout << "———————————————————------------------------————————" << endl;
cout << "Searching for Board 1 (which is in the hash table). Board 1:" << endl << endl;
boards[0].print();
Entry e = table.find(boards[0]); // *** try to find boards[0] in the hash table
cout << "Here’s what was returned from the find:" << endl << endl;
e.getBoard().print();
cout << "———————————————————------------------------————————" << endl << endl;
cout << "Searching for Board 2 (which is in the hash table). Board 2:" << endl << endl;
boards[1].print();
e = table.find(boards[1]);// *** try to find boards[1] in the hash table
cout << "Here’s what was returned from the find:" << endl << endl;
e.getBoard().print();
cout << "———————————————————------------------------————————" << endl << endl;
cout << "Searching for Board 3 (which is NOT in the hash table). Board 3:" << endl << endl;
boards[2].print();
e = table.find(boards[2]);// *** try to find boards[2] in the hash table
cout << "Here’s what was returned from the find:" << endl << endl;
e.getBoard().print();
cout << "———————————————————------------------------————————" << endl << endl;
cout << "Searching for Board 4 (which is NOT in the hash table). Board 4:" << endl << endl;
boards[3].print();
e = table.find(boards[3]);// *** try to find boards[3] in the hash table
cout << "Here’s what was returned from the find:" << endl << endl;
e.getBoard().print();
cout << "———————————————————------------------------————————" << endl << endl;
cout << "REMOVALS" << endl << endl;
cout << "———————————————————------------------------————————" << endl;
cout << "Removing Board 1 (which is in the hash table):" << endl << endl;
boards[0].print();
e = table.remove(boards[0]); // *** remove boards[0]
cout << "Here’s what was returned from the remove:" << endl << endl;
e.getBoard().print();
cout << "———————————————————------------------------————————" << endl << endl;
cout << "Removing Board 2 (which is in the hash table):" << endl << endl;
boards[1].print();
e = table.remove(boards[1]); // *** remove boards[1]
cout << "Here’s what was returned from the remove:" << endl << endl;
e.getBoard().print();
cout << "———————————————————------------------------————————" << endl << endl;
cout << "Removing Board 3 (which is NOT in the hash table):" << endl << endl;
boards[2].print();
e = table.remove(boards[2]); // *** remove boards[2]
cout << "Here’s what was returned from the remove:" << endl << endl;
e.getBoard().print();
cout << "———————————————————------------------------————————" << endl << endl;
cout << "Removing Board 4 (which is NOT in the hash table):" << endl << endl;
boards[3].print();
e = table.remove(boards[3]); // *** remove boards[3]
cout << "Here’s what was returned from the remove:" << endl << endl;
e.getBoard().print();
cout << "———————————————————------------------------————————" << endl << endl;
cout << "SEARCHES AFTER REMOVALS" << endl << endl;
cout << "———————————————————------------------------————————" << endl;
cout << "Searching for Board 1 (no longer in the hash table). Board 1:" << endl << endl;
boards[0].print();
e = table.find(boards[0]); // *** try to find boards[0] in the hash table
cout << "Here’s what was returned from the find:" << endl << endl;
e.getBoard().print();
cout << "———————————————————------------------------————————" << endl << endl;
cout << "Searching for Board 2 (no longer in the hash table). Board 2:" << endl << endl;
boards[1].print();
e = table.find(boards[1]); // *** try to find boards[1] in the hash table
cout << "Here’s what was returned from the find:" << endl << endl;
e.getBoard().print();
cout << "———————————————————------------------------————————" << endl << endl;
cout << "Call to isEmpty function returns: " << table.isEmpty() << endl << endl;
cout << "———————————————————------------------------————————" << endl << endl;
return EXIT_SUCCESS;
}
//
// Inputs: None.
//
// Output: A random gameboard.
//
// Creates and returns a GameBoard containing random
// integers
//
GameBoard randomBoard() {
GameBoard board;
for (int y = 0; y < board.size(); y++) {
for (int x = 0; x < board.size(); x++) {
double fval = rand() * 17;
int value = (int) fval;
board.setElementAt(x, y, value);
}
}
return board;
}
//
// Inputs: A HashTable and an integer specifiying the
// number of random Gameboards to be inserted
// into the table.
//
// Output: None.
//
// The specified number of GameBoards is created and
// inserted into the HashTable. The value associated
// with each board is just the value of the for-loop
// control variable, since, for this lab, this value
// does not matter.
//
void initTable(HashTable &table, int numBoards) {
for (int i = 0; i < numBoards; i++) {
table.insert(randomBoard(), i);
}
}
//
// Inputs: An array of 4 GameBoards.
//
// Output: A Boolean value:
// - true if all 4 boards are different
// - false, otherwise
//
bool allDiff(GameBoard boards[4]) {
return
!boards[0].equals(boards[1]) &&
!boards[0].equals(boards[2]) &&
!boards[0].equals(boards[3]) &&
!boards[1].equals(boards[2]) &&
!boards[1].equals(boards[3]) &&
!boards[2].equals(boards[3]);
}
//This function prints the number of buckets, load factor, variance, and time
//taken to do 25000000 searches in a given hash table.
//The print is made with specific formatting
void printData(int numBuckets, int numBoards, int numSearches) {
double start, end, seconds;
HashTable table2 = HashTable(numBuckets);
initTable(table2, numBoards);
start = clock();
for (int i = 0; i < numSearches; ++i) {
table2.find(randomBoard());
}
end = clock();
seconds = (end - start) / CLOCKS_PER_SEC;
//table1.printListSizes();
cout << "number buckets: " << setw(5) << numBuckets << " load factor: "
<< setw(5) << setprecision(2) << setiosflags(ios::fixed)
<< table2.getLoadFactor(numBoards) << " variance: "
<< setw(5) << setprecision(3) << table2.calcVariance() << " seconds: "
<< setw(10) << setprecision(8) << seconds << " seconds" << endl;
}