-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
218 lines (182 loc) · 5.38 KB
/
hash.cpp
File metadata and controls
218 lines (182 loc) · 5.38 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// Created by willworthington on 12/4/2018.
//
#include "hash.h"
#include <vector>
#include <list>
#include <iterator>
#include <string>
#include <cstring>
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include "dirent.h"
//#includ <dirent.h>
using namespace std;
Hash::Hash(int n){
keyLength=n;
for(int i=0; i<HASH_TABLE_SIZE; i++){
hashTable[i] = new list<int>;
}
}
//all functions used in filling the hash table
int Hash::hashFunction(){
unsigned int hashAddress=0;
int dif;
int mult=1;
for(int i=0; i<key.size(); i++){
dif = keyLength - i;
if (i>0){
mult*=PRIME;
}
hashAddress += stringToInt(key[i])*dif*mult;
}
hashAddress = hashAddress%HASH_TABLE_SIZE;
return hashAddress;
}
unsigned int Hash::stringToInt(const string s){
unsigned int val=1;
for (int i=0; i<s.size(); i++){
val+=s[i];
}
return val;
}
void Hash::scanFile(const string dName, const string fName, const int fileIndex){
key.clear();
ifstream myfile(dName+"/"+fName,std::iostream::binary);
int tableIndex;
if(myfile){
string text;
while(!myfile.eof()){
myfile >> text;
//this seems redundant with the while loop condition, but
//it corrects the issue I was having with the last word being
//read twice before eof() was true
if (!myfile.eof()){
text = cleanText(text);
if (text.size()>0) key.push_back(text);
if(key.size()>keyLength){
key.erase(key.begin());
}
if(key.size()==keyLength){
tableIndex = hashFunction();
hashTable[tableIndex]->push_front(fileIndex);
}
}
}
}
myfile.close();
}
void Hash::scanDirectory(const string dName){
//Open Directory
DIR *dir;
struct dirent *entry;
dir = opendir(dName.c_str());
int count=0;
//Read files from directory
if(dir){
while ((entry=readdir(dir))){
string fName = string(entry->d_name);
if ((fName!=".")&&(fName!="..")){
fileNames.push_back(fName);
scanFile(dName,fName,count);
count++;
}
}
}
else{
cout << "Directory not Found!" << endl;
}
//Close Directory
closedir(dir);
}
void Hash::findCollisions(){
//initializes collisionTable to null grid of size (numberOfFiles)x(numberOfFiles)
collisionTable.resize(sizeof(int)*numberOfFiles());
for(int i=0; i<numberOfFiles(); i++){
collisionTable[i].resize(sizeof(int)*numberOfFiles(),0);
}
int a, b;
for(int i=0; i<HASH_TABLE_SIZE; i++){
list<int> ::iterator it;
for(it=hashTable[i]->begin(); it!=hashTable[i]->end(); it++){
a=*it;
list<int> ::iterator it2;
for(it2=next(it); it2!=hashTable[i]->end(); it2++){
b=*it2;
if (a>b){
collisionTable[a][b]++;
}
else if (a<b){
collisionTable[b][a]++;
}
}
}
}
}
void Hash::identifyCollisions(const int bound){
struct collisionNode{
int value;
int col;
int row;
};
vector<collisionNode> collisionList;
int collisions;
for(int i=1; i<numberOfFiles(); i++){
for(int j=0; j<i; j++){
collisions=collisionTable[i][j];
if(collisions>bound){
collisionNode a {collisions, j, i};
collisionList.push_back(a);
//cout << a.value << " collisions: " << fileNames[a.col] << " & " << fileNames[a.row] << endl;
//a.value = collisions;
//a.col = j;
//a.row = i;
//cout << collisions << " collisions: " << fileNames[j] << " & " << fileNames[i] << endl;
}
}
}
//sort the list of collisions
int minIndex;
for(int i=0; i<collisionList.size()-1; i++){
minIndex=i;
for(int j=i+1; j<collisionList.size(); j++){
if(collisionList[j].value<collisionList[minIndex].value){
minIndex=j;
}
}
if(i!=minIndex) {
collisionNode temp = collisionList[minIndex];
collisionList[minIndex] = collisionList[i];
collisionList[i] = temp;
}
}
//Print the now sorted list
for(int i=collisionList.size()-1; i>=0; i--){
cout << collisionList[i].value << " collisions: " << fileNames[collisionList[i].col] << " & " << fileNames[collisionList[i].row] << endl;
//cout << a.value << " collisions: " << fileNames[a.col] << " & " << fileNames[a.row] << endl;
}
}
string Hash::cleanText(const string dirtyWord){
string cleanWord;
char letter;
bool upperCase;
bool lowerCase;
bool number;
for(int i=0; i<dirtyWord.size(); i++){
letter = dirtyWord[i];
upperCase=(letter>='A')&&(letter<='Z');
lowerCase=(letter>='a')&&(letter<='z');
number=(letter>='0')&&(letter<='9');
if(upperCase||lowerCase||number){
if(upperCase){
letter+=32;
}
cleanWord += letter;
}
}
return cleanWord;
}
int Hash::numberOfFiles(){
return fileNames.size();
}