-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.cpp
218 lines (203 loc) · 5.26 KB
/
hash.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
#include "cppWebSearch.h"
/*
* Constructor to create base hash table
*/
HashTable::HashTable() {
tableSize = 500;
baseArray = new WordStruct[tableSize]; // array of structs to hold words
// initialize parameters
for(int i=0; i<tableSize; i++) {
baseArray[i].hasValue = false;
baseArray[i].next = nullptr;
}
}
/*
* Destructor, frees all memory that was allocated in relation to hash table
*/
HashTable::~HashTable(){
// delete all linked list word structs and all top url structs in vector
for(int i=0; i<tableSize; i++) {
if(baseArray[i].next != nullptr) {
WordStruct *temp = baseArray[i].next;
WordStruct *prev;
while(temp->next != nullptr) {
prev = temp;
temp = temp->next;
prev->wOccr.clear();
delete prev;
}
}
}
// delete original array
delete [] baseArray;
}
/*
* Calculates the hash table location for a processed word. If the word has already been found
* within a website the count for that word is incremented. If the word is found in a new website
* that website url is stored.
*/
void HashTable::Insert(std::string in_word, std::string url){
int location = HashSum(in_word);
// location has no values yet
if(baseArray[location].hasValue == false) {
baseArray[location].word = in_word;
// TopURL struct stores each website and the number of occurences of a word in that site
TopURL *cU = new TopURL;
cU->count = 1;
cU->url = url;
baseArray[location].wOccr.push_back(cU);
baseArray[location].hasValue = true;
}
// location already has at least one word stored
else {
WordStruct *temp = &baseArray[location];
// if word already added increment count
if(temp->word == in_word) {
for(int i=0; i<(int)temp->wOccr.size(); i++) {
// same word found within the same website
if(temp->wOccr[i]->url == url) {
temp->wOccr[i]->count++;
return;
}
}
// same word found within a different website
TopURL *cU = new TopURL;
cU->count = 1;
cU->url = url;
temp->wOccr.push_back(cU);
//temp->count++;
return;
}
// find next available location to insert word (no order)
while(temp->next != nullptr) {
temp = temp->next;
// if word already added just increment count
if(temp->word == in_word) {
for(int i=0; i<(int)temp->wOccr.size(); i++) {
// same word found within the same website
if(temp->wOccr[i]->url == url) {
temp->wOccr[i]->count++;
return;
}
}
// same word found within a different website
TopURL *cU = new TopURL;
cU->count = 1;
cU->url = url;
temp->wOccr.push_back(cU);
//temp->count++;
return;
}
}
// create new struct and initialize parameters
WordStruct *toInsert = new WordStruct;
toInsert->word = in_word;
TopURL *cU = new TopURL;
cU->count = 1;
cU->url = url;
toInsert->wOccr.push_back(cU);
toInsert->hasValue = true;
toInsert->next = nullptr;
temp->next = toInsert;
}
}
/*
* Searches for the website that has the highest occurence of entered word
*/
void HashTable::Find(std::string in_word){
int location = HashSum(in_word);
if(baseArray[location].hasValue == false) {
std::cout<<"Word not found\n";
return;
}
if(baseArray[location].word == in_word) {
int max = 0;
std::string bestURL;
WordStruct *temp = &baseArray[location];
for(int i=0; i<(int)temp->wOccr.size(); i++) {
if(temp->wOccr[i]->count > max) {
max = temp->wOccr[i]->count;
bestURL = temp->wOccr[i]->url;
}
}
std::cout<<"Best website is: "<<bestURL<<"\n"<<in_word<<" count = "<< max << std::endl;
return;
}
else {
int max = 0;
std::string bestURL;
WordStruct *temp = &baseArray[location];
while(temp->next != nullptr) {
temp = temp->next;
if(temp->word == in_word)
break;
}
if(temp->word != in_word) {
std::cout << "Word not found\n";
return;
}
for(int i=0; i<(int)temp->wOccr.size(); i++) {
if(temp->wOccr[i]->count > max) {
max = temp->wOccr[i]->count;
bestURL = temp->wOccr[i]->url;
}
}
std::cout<<"Best website is: "<<bestURL<<"\n"<<in_word<<" count = "<< max << std::endl;
}
}
/*
* Prints all words stored in the hash table
*/
void HashTable::Print(){
int i;
int empty = 0;
for(i=0; i<tableSize; i++)
if(baseArray[i].hasValue == false)
empty++;
if(empty == tableSize)
std::cout<<"There are no stored words.\n";
else {
for(i=0; i<tableSize; i++) {
if(baseArray[i].hasValue == true) {
std::cout<<baseArray[i].word<<std::endl;
if(baseArray[i].next != nullptr) {
WordStruct *temp = baseArray[i].next;
while(temp != nullptr) {
std::cout<<temp->word<<std::endl;
temp = temp->next;
}
}
}
}
}
}
/*
* Calculates the hash sum needed to acces correct element of array
*/
int HashTable::HashSum(std::string word){
unsigned int location = 0;
for(unsigned long i=0; i<word.length(); i++) {
location += word[i]; // sum ascii values of all characters in string
}
location = location % tableSize;
return location;
}
/*
* Clears all values stored in hash table
*/
void HashTable::Clear() {
for(int i=0; i<tableSize; i++) {
if(baseArray[i].next != nullptr) {
WordStruct *temp = baseArray[i].next;
baseArray[i].next = nullptr;
WordStruct *prev;
while(temp->next != nullptr) {
prev = temp;
temp = temp->next;
prev->wOccr.clear();
delete prev;
}
}
baseArray[i].hasValue = false;
}
}