-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.cpp
381 lines (335 loc) · 8.82 KB
/
HashTable.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
* CS 3345 HON
* Project 2, HASH TABLE
* Used compiler g++ 4.8.5 - 11 with compile command : g++ -std=c++11 - o hash HashTable.cpp
* @author CMLQ
* date 10 / 27 / 20
*/
#include <iostream>
#include <fstream>
#include <string>
// Entry class
class Entry
{
public:
std::string Key; //from 1-20 upper-case alphabetic chars
std::string Value; //from 1-20 chars
int avail; //unused = 0, free = 1, occupied = 2
//constructor for entry
Entry(std::string Key, std::string Value, int avail);
};
// constructor for entry, sets the Key, Value and avial
Entry::Entry(std::string Key, std::string Value, int avail)
{
this->Key = Key;
this->Value = Value;
this->avail = avail;
}
// MyHashTable Class
class MyHashTable
{
public:
// the table itself
Entry** table;
// the table size;
int size;
// the prime number associated with the table;
int prime;
// number of probes for unsuccessful searches
int unsuccessfulSearchProbes;
// number of probes for successful searches
int successfulSearchProbes;
// number of probes for successful inserts
int successfulInsertProbes;
//constructur that takes the table size and prime
MyHashTable(int, int);
// insert an entry based off a key and string, returns true if successfully inserted, false if otherwise
bool insert(std::string, std::string);
// find an entry based off a key, returns the value of the key if present, an empty string otherwise
std::string find(std::string);
// remove an entry based off a key, returns true if successfully removed, false if otherwise
bool remove(std::string);
// lists the amount of nonempty entries in the table
int membership();
// lists all entries in key value pairs along with position
void listAll();
//uses horner method to simplify the hash function
unsigned int horner_hash(std::string, int);
//first hash function
int hash(std::string, int);
// second hash function
int hash2(std::string, int);
~MyHashTable();
};
//empties the table
MyHashTable::~MyHashTable()
{
for (int i = 0; i < size; i++)
{
table[i]->Value = "";
table[i]->avail = 0;
table[i]->Key = "";
}
}
//function that simplifies the hash function with Horners method
unsigned int MyHashTable::horner_hash(std::string key, int x)
{
unsigned int result = key[0];
for (unsigned int i = 1; i < key.length(); i++)
{
result = (unsigned int)(result * x + key[i]);
}
return result;
}
// hash function, using the horner_hash function
int MyHashTable::hash(std::string key, int tablesize)
{
return horner_hash(key, 31) % tablesize;
}
// second hash function
int MyHashTable::hash2(std::string key, int tablesize)
{
int x = prime - (hash(key, tablesize) % prime);
return x;
}
// creates an empty hash table based off a size and prime number
MyHashTable::MyHashTable(int sz, int R)
{
this->size = sz;
this->prime = R;
table = new Entry * [sz];
for (int i = 0; i < sz; i++)
{
table[i] = new Entry("", "", 0);
}
unsuccessfulSearchProbes = 0;
successfulSearchProbes = 0;
successfulInsertProbes = 0;
}
// insert an entry based off a key and string, returns true if successfully inserted, false if otherwise
bool MyHashTable::insert(std::string key, std::string value)
{
bool inserted = false;
int originalSSearchProbe = successfulSearchProbes;
int originalUSearchProbe = unsuccessfulSearchProbes;
std::string found = find(key);
successfulSearchProbes = originalSSearchProbe;
unsuccessfulSearchProbes = originalUSearchProbe;
if (found != "")
{
return inserted;
}
int index1 = hash(key, size);
int index2 = hash2(key, size);
int i = 0;
int index3 = index1;
while (table[index3]->avail != 0 || table[index3]->avail != 1)
{
index3 = (index1 + i * index2);
// if index3 becomes bigger than the size of the table, wrap the index to the front (beginning) of the table
while (index3 >= size)
{
index3 = index3 - size;
}
successfulInsertProbes++;
if ((table[index3]->avail == 0 || table[index3]->avail == 1))
{
table[index3]->Value = value;
table[index3]->Key = key;
table[index3]->avail = 2;
inserted = true;
break;
}
i++;
}
return inserted;
}
// find an entry based off a key, returns the value of the key if present, an empty string otherwise
std::string MyHashTable::find(std::string key)
{
std::string found = "";
int index1 = hash(key, size);
int index2 = hash2(key, size);
int i = 0;
int numProbes = 1;
//index 3 will serve as our probing sequence
int index3 = index1;
while (table[index3]->avail != 0 && table[index3]->Key != key)
{
i++;
numProbes++;
//probing function
index3 = index1 + i * index2;
// if index becomes bigger than the size of the table, wrap the index to the front (beginning) of the table
while (index3 >= size)
{
index3 = index3 - size;
}
}
found = table[index3]->Value;
// an unsuccessful search, increase the number of unsuccessfulSearchProbes
if (found == "")
{
unsuccessfulSearchProbes += numProbes;
}
else // a succesful search, increase the number of successfulSearchProbes
{
successfulSearchProbes += numProbes;
}
return found;
}
// remove an entry based off a key, returns true if successfully removed, false if otherwise
bool MyHashTable::remove(std::string key)
{
bool removed = false;
int index1 = hash(key, size);
int index2 = hash2(key, size);
//index 3 will serve as a probing sequence
int index3 = index1;
int i = 0;
//while the avail at updated index is not equal to empty
while (table[index3]->avail != 0)
{
index3 = (index1 + i * index2);
// if index3 becomes bigger than the size of the table, wrap the index to the front (beginning) of the table
while (index3 >= size)
{
index3 = index3 - size;
}
//key has been found, make it empty
if (table[(index3)]->avail == 2 && table[(index3)]->Key == key) {
table[index3]->avail = 1;
table[index3]->Value = "";
table[index3]->Key = "";
removed = true;
break;
}
i++;
}
return removed;
}
//outputs number of nonempty members
int MyHashTable::membership()
{
int memcount = 0;
for (int i = 0; i < size; i++)
{
if (table[i]->avail == 2)
{
memcount++;
}
}
return memcount;
}
// outputs all nonempty entries with its position, key and value
void MyHashTable::listAll()
{
for (int i = 0; i < size; i++)
{
if (table[i]->avail == 2)
std::cout << i << " " << table[i]->Key << ":" << table[i]->Value << std::endl;
}
}
int main()
{
// file to read
std::ifstream myfile;
myfile.open("HashTableText.txt");
//variables necessary for commands and inputs
std::string key, newKey, delimiter = ":", val;
bool insert, remove;
char command, test;
int originalSSearchProbe, originalUSearchProbe;
//table size, prime number
int sz, R;
//successful insert count, successful search count, unsuccessful search count
int si = 0, ss = 0,us = 0, delim = 0;
std::string find;
MyHashTable ta(0, 0);
while (myfile >> command)
{
switch (command)
{
case 'D':
myfile >> sz >> R;
(ta) = MyHashTable(sz, R);
break;
case 'C':
//clear
ta.~MyHashTable();
si = 0;
ss = 0;
us = 0;
ta.successfulInsertProbes = 0;
ta.successfulSearchProbes = 0;
ta.unsuccessfulSearchProbes = 0;
break;
case 'H':
myfile >> key;
std::cout << key << " " << ta.hash(key, 17) << std::endl;
break;
case 'A':
//insert
myfile >> key;
newKey = key.substr(0, key.find(delimiter));
val = key.substr(key.find(delimiter) + 1, key.size() - 1);
insert = (ta).insert(newKey, val);
if (insert == true)
{
std::cout << "Key " << newKey << " inserted" << std::endl;
si++;
}
else
{
std::cout << "Key " << newKey << " already exists" << std::endl;
}
break;
case 'R':
//delete
myfile >> key;
remove = (ta).remove(key);
if (remove == true)
{
std::cout << "Key " << key << " deleted" << std::endl;
}
else
{
std::cout << "Key " << key << " not found" << std::endl;
}
break;
case 'S':
//search
myfile >> key;
find = (ta).find(key);
if (find == "")
{
std::cout << "Key " << key << " not found" << std::endl;
us++;
}
else
{
std::cout << "Key " << key << ":" << find << std::endl;
ss++;
}
break;
case 'M':
std::cout << "Membership is " << (ta).membership() << "\n";
break;
case 'P':
ta.listAll();
break;
case 'T':
std::cout << "Total Number of Successful Inserts = " << si << std::endl;
std::cout << "Total Number of Probes on Successful Inserts = " << ta.successfulInsertProbes << std::endl;
std::cout << "Total Number of Successful Searches = " << ss << std::endl;
std::cout << "Total Number of Probes on Successful Searches = " << ta.successfulSearchProbes << std::endl;
std::cout << "Total Number of Unsuccessful Searches = " << us << std::endl;
std::cout << "Total Number of Probes on Unsuccessful Searches = " << ta.unsuccessfulSearchProbes << std::endl;
break;
case 'E':
exit(0);
}
}
myfile.close();
return 0;
}