-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_202.cpp
More file actions
360 lines (283 loc) · 9.07 KB
/
Copy pathhash_202.cpp
File metadata and controls
360 lines (283 loc) · 9.07 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
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
/* Laura Smith 10/05/2022 11:35 PM hash_202.cpp 360 lines
Lab 5 CS 202 Hash Tables
A hash table is similar to a linked list in that each "node" or this time "probe" has two values.
Hash tables store values at different locations according to a given key which is decifered/ encoded
with a hash function. Once the hash function deciphers the key the location of the value connected to
that key is revealed. This lab helped me learn more about not only hash tables but strings and vectors
as well. I also learned how important error checking is. This file creates a hash table of a given size
and then adds in values with associated keys into the correct spot based on what hash function and
collision strategy the user specified. It can then print the table, find a value at a given key location
and count the total used probes.
Credit: Dorothy Wang for the XOR function idea/help, Katie Moffitt for help debugging in the add function, and Jacob Kennedy for help with the find function.
*/
#include "hash_202.hpp"
#include <cstdio> // printf
#include <iostream> // >>
#include <sstream> // istringstream
using namespace std;
//XOR function that separates each key into groups of 7 and takes the XOR of all of those groups to get the hash
int XOR (string key){
int digit = 0; //used to keep track of what goes in numbers
int hashValue = 0; //final hashValue
int group = key.length() / 7; //used to keep track of groups
string value; //each group of 7
vector <string> Values; //vector of strings which are the groups of 7
vector <int> numbers; //vector of the hex values of Values
//Key gets broken into groups of 7 into Values then turned to hex values in numbers
if (key.length() > 7){
for (int i = 0; i < (group); i++){
value = key.substr((i * 7), 7);
Values.push_back(value);
}
for (int i = 0; i < int(Values.size()); i++){
stringstream s(Values[i]);
s >> hex >> digit;
numbers.push_back(digit);
s.clear();
}
//checks for leftovers and adds them to numbers as a hex value
if ((key.length() % 7) != 0){
key = key.substr(group * 7, 7);
stringstream ss(key);
ss >> hex >> digit;
numbers.push_back(digit);
ss.clear();
}
//for loop to XOR each of the numbers to get the final hash value
hashValue = numbers[0];
for (int i = 1; i < int(numbers.size()); i++){
hashValue = hashValue^numbers[i];
}
return (hashValue);
}
//if the key is shorter than 7 it just needs to be the hex of the key
else {
stringstream sin(key);
sin >> hex >> digit;
sin.clear();
return (digit);
}
}
//Last 7 is a function that just takes the last 7 values of a key and converts them to hex to make the hash
int Last7 (string key){
string value; //string of the last 7 digits of key
int hashValue = 0; //the final hash value
//if else loop where if it's less than 7 value is the whole key, if greater it creates a substring of the last 7 and stores it in value
if (key.length() >= 7){
value = key.substr(key.length() - 7, 7);
}
else {
value = key;
}
//converts the value to hex and returns it
stringstream s(value);
s >> hex >> hashValue;
s.clear();
return (hashValue);
}
//error checks and creates the hash table
string Hash_202::Set_Up(size_t table_size, const string &fxn, const string &collision)
{
//check if table already set up
if (Keys.size() > 0)
return ("Hash table already set up");
//check for bad table size
if (table_size <= 0)
return ("Bad table size");
//check for bad hash function
if (fxn == "XOR")
Fxn = 0;
else if (fxn == "Last7")
Fxn = 1;
else
return ("Bad hash function");
//check for bad collision resolution strategy
if (collision == "Linear")
Coll = 0;
else if (collision == "Double")
Coll = 1;
else
return ("Bad collision resolution strategy");
//resize table (Keys & Vals vector) + initialize Nkeys to 0
Keys.resize(table_size);
Vals.resize(table_size);
Nkeys = 0;
(void) table_size;
(void) fxn;
(void) collision;
return ("");
}
//Add adds the fed in val into the table at the given key location
string Hash_202::Add(const string &key, const string &val)
{
//check if table not set up
if (Keys.size() == 0)
return ("Hash table not set up");
//check if empty string for key
if (key == "")
return ("Empty key");
//check if key not composed of hex digits
for (size_t i = 0; i < key.length(); i++){
if (!isxdigit(key[i]))
return("Bad key (not all hex digits)");
}
//check if empty string for val
if (val.size() == 0)
return ("Empty val");
//check if hash table is full
if (Keys.size() == Nkeys)
return("Hash table full");
//check if key is already in table
if (Find(key) != ""){
return ("Key already in the table");
}
//get index and probe increment
int probeIncrement;
int index;
if (Fxn == 0){ //XOR
probeIncrement = XOR(key);
index = probeIncrement % Keys.size();
}
else if (Fxn == 1){ //Last7
probeIncrement = Last7(key);
index = probeIncrement % Keys.size();
}
//insert into table
if (Keys[index] == ""){
Keys[index] = key;
Vals[index] = val;
}
//handle collisions
else{
if (Coll == 0){ //Linear
int newIndex = 0; //creates the next index to try to store the value at
//for loop goes through the hash table creating a new index to store the value at until it's stored
//If it reaches the end without storing the value, it returns an error that the key can't be inserted
for (size_t i = 1; i < Keys.size(); i++){
newIndex = (probeIncrement + i) % Keys.size();
if (Keys[newIndex] == ""){
index = newIndex;
Keys[index] = key;
Vals[index] = val;
i = Keys.size();
}
else if (i == Keys.size() - 1){
return ("Cannot insert key");
}
}
}
else if (Coll == 1){ //Double
int offset = 0; //what the new index will be with the other hash function
int newIndex = 0; //the new index that the value gets stored to
//if it's XOR then runs Last7 and stores it there, if Last7 it does the opposite. if the offset ends up being 0 it is made to be 1
if (Fxn == 0){
offset = Last7(key) % Keys.size();
if (offset == 0)
offset = 1;
}
else {
offset = XOR(key) % Keys.size();
if (offset == 0)
offset = 1;
}
//for loop to add in the value at the new index if possible/probe is available, if not returns an error message
for (size_t i = 1; i < Keys.size(); i++){
newIndex = (probeIncrement + i * offset) % Keys.size();
if (Keys[newIndex] == ""){
index = newIndex;
Keys[index] = key;
Vals[index] = val;
i = Keys.size();
}
else if (i == Keys.size() - 1){
return ("Cannot insert key");
}
}
}
}
//increment Nkeys
Nkeys++;
(void) key;
(void) val;
return ("");
}
//Find returns the value at the given key location
string Hash_202::Find(const string &key)
{
//check if hash table not set up
if (Keys.size() <= 0 || Vals.size() <= 0)
return ("");
//check if empty key
if (key == "")
return ("");
//check if key is not composed of all hex digits
for (size_t i = 0; i < key.size(); i++){
if (!isxdigit(key[i]))
return("");
}
//get index (same as add)
int index;
if (Fxn == 1){
index = Last7(key) % Keys.size();
}
if (Fxn == 0){
index = XOR(key) % Keys.size();
}
//get probe increment
int probeIncrement;
//if statements to get increments of every hash strategy X collision strategy
if (Coll == 0){
probeIncrement = 1;
}
if (Coll == 1){
if (Fxn == 1){
probeIncrement = XOR(key) % Keys.size();
if (probeIncrement == 0 || probeIncrement % Keys.size() == 0)
probeIncrement = 1;
}
if (Fxn == 0){
probeIncrement = Last7(key) % Keys.size();
if (probeIncrement == 0 || probeIncrement % Keys.size() == 0)
probeIncrement = 1;
}
}
//find val
string val = "";
//goes through and finds val at the key location
for (size_t i = 0; i < Keys.size(); i++){
Nprobes = 0;
if (Keys.at((index + (i * probeIncrement)) % Keys.size()) == key){
val = Vals.at((index + (i * probeIncrement)) % Vals.size());
Nprobes = i;
break;
}
}
(void) key;
return (val); // "" if key not in hash table
}
//Print prints the hash table
void Hash_202::Print() const
{
//if table has been set up
if(Keys.size() != 0) {
for(unsigned long int i = 0; i < Keys.size(); i++) {
//print if not empty
if(Keys.at(i) != "")
printf("%5lu %s %s\n", i, Keys.at(i).c_str(), Vals.at(i).c_str());
}
}
}
//Total Probes counts the number of entries/full spots in the hash table
size_t Hash_202::Total_Probes()
{
size_t sum = 0;
if(Keys.size() != 0) {
for(unsigned long int i = 0; i < Keys.size(); i++) {
if(Keys.at(i) != "") { //don't include empty strings
Find(Keys.at(i));
sum += Nprobes;
}
}
}
return (sum); //error: sum = 0 if table not setup
}