-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
202 lines (133 loc) · 3.36 KB
/
hash.cpp
File metadata and controls
202 lines (133 loc) · 3.36 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* @file Hash.cpp The hash tables member function for the required program
*
* @brief
* Processes files and creates a hash table from data and outputs
* the hash table to a file. Keeps track of some list statistics too.
*
* @author Clint Bettiga
* @date 11/08/13
*/
#include "hash.h"
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
/**
* remove
*
* Removes a word from the hash table. And recalculates
* the average at each remove.
*
* @param word String from the parsed file.
*/
void Hash::remove(string word){
int hashLocation = hf(word);
if(!hashTable[hashLocation].empty() && hashLocation <= HASH_TABLE_SIZE){
if(search(word)){
hashTable[hashLocation].remove(word);
arr[hf(word)]--;
calcAvg();
}
}
}
/**
* print
*
* Prints the hash table listing in the required format.
*
*/
void Hash::print(){
for(int i=0;i<HASH_TABLE_SIZE;i++){
cout<<i<<":\t";
for (list<string>::iterator it=hashTable[i].begin(); it != hashTable[i].end(); ++it)
cout << *it << ", ";
cout<<endl;
}
}
/**
* processFile
*
* Opens a file and adds data to hash table. Keeps track of the collisions,
* longestlist, and the average list size by using an array.
*
* @param filename Passes the filename to be opened for processing
*/
void Hash::processFile(string filename){
ifstream file(filename.c_str());
string word;
for (int i = 0; i < HASH_TABLE_SIZE; i++)
arr[i] = 0;
while (getline(file, word)){
int hashLocation = hf(word);
if(!hashTable[hashLocation].empty())
collisions++;
hashTable[hashLocation].push_back(word);
arr[hashLocation]++;
calcAvg();
}
longestList = *max_element(arr, arr + HASH_TABLE_SIZE);
}
/**
* search
*
* Searches the hash table to see if a word exists in the list.
* Returns true if found. If not returns false.
*
* @param word String from the parsed file.
*/
bool Hash::search(string word){
int hashLocation = hf(word);
if(!hashTable[hashLocation].empty()){
for (list<string>::iterator it=hashTable[hashLocation].begin(); it != hashTable[hashLocation].end(); ++it)
if(*it == word)
return 1;
}
else
return 0;
return 0;
}
/**
* output
*
* Writes the required output to file
*
* @param filename The name of the output file.
*/
void Hash::output(string filename){
ofstream myfile (filename.c_str());
if (myfile.is_open()){
for(int i=0;i<HASH_TABLE_SIZE;i++){
myfile<<i<<":\t";
for (list<string>::iterator it=hashTable[i].begin(); it != hashTable[i].end(); ++it)
myfile << *it << ", ";
myfile<<endl;
}
myfile.close();
}
}
/**
* printStats
*
* Prints the hash table required calculations
*
*/
void Hash::printStats (){
cout<<"Total collisions = "<<collisions<<endl;
cout<<"Longest list = "<<longestList<<endl;
cout<<"Average list length = "<<avgLength<<endl;
}
/**
* calcAvg
*
* Calculates the average size of collision lists
* in the hash table.
*
*/
void Hash::calcAvg(){
double sum = 0.0;
for (int i = 0; i < HASH_TABLE_SIZE; i++)
sum += arr[i];
newAvgListLen = sum/HASH_TABLE_SIZE;
avgLength = (newAvgListLen + avgLength) / 2.0;
}