-
Notifications
You must be signed in to change notification settings - Fork 0
/
McCuckoo.cpp
382 lines (361 loc) · 11.9 KB
/
McCuckoo.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
381
382
#include <iostream>
#include <cmath>
#include <iomanip>
#include <limits>
using namespace std;
const int M = 5; // table size
const int S = 3; // stash size
const char separator = ' ';
const int width = 8;
class MultiCopyCuckoo
{
public:
// initializing all tables with 0 as it is unoccupied
short onChipCtr1[M] = {0};
short onChipCtr2[M] = {0};
int offChipTable1[M] = {0};
int offChipTable2[M] = {0};
int Stash[S] = {0};
short FlagTable1[M] = {0};
short FlagTable2[M] = {0};
int hashFunction1(int key)
{
return (key + 1) % M;
}
int hashFunction2(int key)
{
return (key * 7) % M;
}
int hashFunctionStash(int key)
{
return key % S;
}
bool Insert_in_Table(int key)
{
if (Lookup(key)) // if key is already present in the table
{
cout << "Key is already present in the table" << endl;
return false;
}
int pos1 = hashFunction1(key);
int pos2 = hashFunction2(key);
int counter1_value = onChipCtr1[pos1];
int counter2_value = onChipCtr2[pos2];
if (counter1_value == 1 && counter2_value == 1)
{
bool insert = Insert_in_Stash(key, pos1, pos2);
return insert;
}
int key1_removed, key2_removed;
if (counter1_value == 2)
{
key1_removed = offChipTable1[pos1];
}
if (counter2_value == 2)
{
key2_removed = offChipTable2[pos2];
}
if (counter1_value == 2 && counter2_value == 2 && key1_removed == key2_removed) // if 2 keys match the same positions in both tables
{
offChipTable1[pos1] = key;
onChipCtr1[pos1] = 1;
onChipCtr2[pos2] = 1;
return true;
}
if (counter1_value == 2 && counter2_value == 2) // if both positions are occupied by keys with equal redundancy
{
offChipTable1[pos1] = key;
offChipTable2[pos2] = key;
int pos_removed2 = hashFunction2(key1_removed);
int pos_removed1 = hashFunction1(key2_removed);
onChipCtr1[pos_removed1] = 1;
onChipCtr2[pos_removed2] = 1;
onChipCtr1[pos1] = 1;
onChipCtr2[pos2] = 1;
return true;
}
if (counter1_value == 1 && counter2_value == 2) // keys in position have 2 copies and 1 copy
{
offChipTable2[pos2] = key;
onChipCtr2[pos2] = 1;
int pos_removed = hashFunction1(key2_removed);
onChipCtr1[pos_removed] = 1;
return true;
}
if (counter1_value == 2 && counter2_value == 1) // same as above case
{
offChipTable1[pos1] = key;
onChipCtr1[pos1] = 1;
int pos_removed = hashFunction2(key1_removed);
onChipCtr2[pos_removed] = 1;
return true;
}
if (counter1_value == 0 && counter2_value == 2) // redundancy of inserted element must be greater than that of key already present in table
{
offChipTable1[pos1] = key;
offChipTable2[pos2] = key;
int pos_removed = hashFunction1(key2_removed);
onChipCtr1[pos_removed] = 1;
onChipCtr1[pos1] = 2;
onChipCtr2[pos2] = 2;
return true;
}
if (counter1_value == 2 && counter2_value == 0)
{
offChipTable1[pos1] = key;
offChipTable2[pos2] = key;
int pos_removed = hashFunction2(key1_removed);
onChipCtr2[pos_removed] = 1;
onChipCtr1[pos1] = 2;
onChipCtr2[pos2] = 2;
return true;
}
if (counter1_value == 1 && counter2_value == 0) // only 1 position where the element can be inserted
{
offChipTable2[pos2] = key;
onChipCtr2[pos2] = 1;
return true;
}
if (counter1_value == 0 && counter2_value == 1)
{
offChipTable1[pos1] = key;
onChipCtr1[pos1] = 1;
return true;
}
if (counter1_value == 0 && counter2_value == 0) // both positions are empty
{
offChipTable1[pos1] = key;
offChipTable2[pos2] = key;
onChipCtr1[pos1] = 2;
onChipCtr2[pos2] = 2;
return true;
}
return false;
}
bool Insert_in_Stash(int key, int pos1, int pos2) // called if insertion in main table fails
{
int counter = 0;
int LinearProbeIndex = hashFunctionStash(key);
while (Stash[LinearProbeIndex] != 0)
{
if (Stash[LinearProbeIndex] == -1)
{
break;
}
if (counter == S) // if all positions in stash are occupied
{
return false;
}
LinearProbeIndex = (LinearProbeIndex + 1) % S;
counter++;
}
Stash[LinearProbeIndex] = key;
FlagTable1[pos1]++;
FlagTable2[pos2]++;
return true;
}
bool Lookup_only_in_table(int key)
{
int CandidateBucket1 = hashFunction1(key);
int CandidateBucket2 = hashFunction2(key);
if (onChipCtr1[CandidateBucket1] == 0 || onChipCtr2[CandidateBucket2] == 0) // if on chip counters are 0 no need to look at table
{
return false;
}
else if (onChipCtr1[CandidateBucket1] >= 1 || onChipCtr2[CandidateBucket2] >= 1)
{
if ((offChipTable1[CandidateBucket1] == key) || (offChipTable2[CandidateBucket2] == key))
{
return true;
}
}
return false;
}
int Lookup_in_Stash(int key) // called when lookup in table fails
{
int LinearProbeIndex = hashFunctionStash(key);
int ctr = 0;
while (ctr < S)
{
if (Stash[LinearProbeIndex] == key)
{
return LinearProbeIndex;
}
else if (Stash[LinearProbeIndex] == 0)
{
return -1;
}
else
{
LinearProbeIndex = (LinearProbeIndex + 1) % S;
}
ctr++;
}
return -1;
}
bool Lookup(int key) // main lookup function
{
if (Lookup_only_in_table(key))
{
return true;
}
else if (Lookup_in_Stash(key) != -1)
{
return true;
}
else
{
return false;
}
}
bool Delete(int key)
{
int pos1 = hashFunction1(key);
int pos2 = hashFunction2(key);
if (Lookup_only_in_table(key)) // check if key is present in table
{
if (offChipTable1[pos1] == key)
onChipCtr1[pos1] = 0;
if (offChipTable2[pos2] == key)
onChipCtr2[pos2] = 0;
return true;
}
else
{
if (FlagTable1[pos1] == 0 || FlagTable2[pos2] == 0) // check if lookup in stash is necessary
{
return false;
}
int pos = Lookup_in_Stash(key);
Stash[pos] = -1;
FlagTable1[pos1]--;
FlagTable2[pos2]--;
return true;
}
}
void printElement(int n)
{
cout << left << setw(width) << setfill(separator) << n;
}
void display()
{
cout << "|" << setw(width * 6.70) << setfill('-') << "-"
<< "|" << endl;
cout << "|" << left << setw(width) << setfill(separator) << "ctr1"
<< "|" << left << setw(width) << setfill(separator) << "ctr2"
<< "|" << left << setw(width) << setfill(separator) << "flg1"
<< "|" << left << setw(width) << setfill(separator) << "tbl1"
<< "|" << left << setw(width) << setfill(separator) << "flg2"
<< "|" << left << setw(width) << setfill(separator) << "tbl2"
<< "|" << endl;
cout << "|" << setw(width * 6.70) << setfill('-') << "-"
<< "|" << endl;
for (int i = 0; i < M; i++)
{
cout << "|" << left << setw(width) << setfill(separator) << onChipCtr1[i]
<< "|" << left << setw(width) << setfill(separator) << onChipCtr2[i]
<< "|" << left << setw(width) << setfill(separator) << FlagTable1[i]
<< "|" << left << setw(width) << setfill(separator) << offChipTable1[i]
<< "|" << left << setw(width) << setfill(separator) << FlagTable2[i]
<< "|" << left << setw(width) << setfill(separator) << offChipTable2[i] << "|" << endl;
}
cout << "|" << setw(width * 6.70) << setfill('-') << "-"
<< "|" << endl;
cout << endl
<< "STASH MEMORY\n";
for (int i = 0; i < S; i++)
{
cout << Stash[i] << " ";
}
cout << endl
<< endl;
}
};
int main()
{
MultiCopyCuckoo mccuckoo;
cout << "Welcome\n";
cout << "*******************************************\n";
while (1)
{
bool ans = false;
int key;
int choice;
cout << "1.Insert 2.Delete 3.Lookup 4.Display 5.Exit\n";
cin >> choice;
cout << endl;
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Error: Input must be an integer. Please try again.\n\n";
continue;
}
switch (choice)
{
case 1:
cout << "Enter the key to be inserted\n";
cin >> key;
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Error: Input must be an integer. Please try again.\n\n";
continue;
}
ans = mccuckoo.Insert_in_Table(key);
if (ans == true)
cout << "Insertion successful\n"
<< endl;
else
cout << "Insertion failed either due to duplicate key or table being full. Rehashing may be required\n"
<< endl;
break;
case 2:
cout << "Enter the key to be deleted\n";
cin >> key;
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Error: Input must be an integer. Please try again.\n\n";
continue;
}
ans = mccuckoo.Delete(key);
if (ans == true)
cout << "Deletion successful\n"
<< endl;
else
cout << "Key not present\n"
<< endl;
break;
case 3:
cout << "Enter the key to be looked up\n";
cin >> key;
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Error: Input must be an integer. Please try again.\n\n";
continue;
}
ans = mccuckoo.Lookup(key);
if (ans == true)
cout << "Key found\n"
<< endl;
else
cout << "Key not found\n"
<< endl;
break;
case 4:
mccuckoo.display();
break;
case 5:
cout << "Thank You\n";
exit(0);
default:
cout << "Invalid choice\n";
}
}
return 0;
}