-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriver.cpp
114 lines (88 loc) · 3.1 KB
/
Driver.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
/**
Lab 3 - Linear Probing, Quadratic Probing, and Double Hashing
CSC 255 Objects and Algorithms (Fall 2020)
Oakton Community College
Professor: Kamilla Murashkina
@file Driver.cpp
@author Russell Taylor
@date 9/9/20
*/
#include <iostream>
#include "Driver.hpp"
/**
Opens file and calls methods to test hashing
@param ifname the input filename
@param ofname the output filename
*/
void Driver::testFile(const string& ifname, const string& ofname) {
cout << "Input file " << ifname << ", output file " << ofname << "\n";
readData(ifname);
fout.open(ofname);
testData("Random Order");
sort(data.begin(), data.end());
testData("Ascending Order");
sort(data.begin(), data.end(), greater<int>());
testData("Descending Order");
data.clear();
fout.close();
cout << "Done\n";
}
/**
Reads data from input file
@param ifname the input file name
*/
void Driver::readData(const string& ifname) {
ifstream fin(ifname);
int key;
while (fin >> key)
data.push_back(key);
fin.close();
}
/**
Tests the hash table methods
@param description a description of the sort order of the data
*/
void Driver::testData(const string& description) {
fout << "*** " << description << " Start *** " << "\n\n";
LinearProbing<int, int> lp(tableSize);
QuadraticProbing<int, int> qp(tableSize);
DoubleHashing<int, int> dh(tableSize, doubleFactor);
for (auto& key : data)
testInputKey(key, lp, qp, dh);
fout << "Linear " << lp.getCollisions() << " collisions\n";
fout << "Quadratic " << qp.getCollisions() << " collisions\n";
fout << "Double " << dh.getCollisions() << " collisions\n";
fout << "\n*** " << description << " End *** " << "\n\n";
}
/**
Inputs a key into the hash table
@param key the key
@param lp a linear probing object
@param qp a quadratic probing object
@param dh a double hashing object
*/
void Driver::testInputKey(const int key, Hash<int, int>& lp, Hash<int, int>& qp, Hash<int, int>& dh) {
const int value = key * 2;
testKeyValue("Linear ", lp, key, value);
testKeyValue("Quadratic", qp, key, value);
testKeyValue("Double ", dh, key, value);
fout << "\n";
}
/**
Test the key value
@param description a description of the collision resolution strategy
@param hashTable a hash table object
@param key the key
@param value the value
*/
void Driver::testKeyValue(string description, Hash<int, int>& hashTable, const int key, const int value) {
const int previousCollisions = hashTable.getCollisions();
hashTable.put(key, value);
const int* retrievedValue = hashTable.get(key);
const string& retrievedText = retrievedValue != nullptr ? to_string(*retrievedValue) : "null";
fout << key << " : " << value << " -> " << retrievedText << ", collisions " << (hashTable.getCollisions() - previousCollisions) << "\n";
if (retrievedValue == nullptr || *retrievedValue != value) {
fout << "Retrieved value " << retrievedText << " does not match stored value " << value << " for key " << key << "\n";
throw runtime_error("value mismatch");
}
}