-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextendible-hashing.cpp
411 lines (331 loc) · 10.7 KB
/
extendible-hashing.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <bits/stdc++.h>
using namespace std;
// function to get the last k bits of a number n
string getLastKBits(int n, int k) {
string bits;
for (int i = 0; i < k; i++) {
if (n & (1 << i))
bits.push_back('1');
else
bits.push_back('0');
}
reverse(bits.begin(), bits.end());
return bits;
}
// function to convert a binary string to its corresponding decimal value
int getDecimal(string binary) {
int n = binary.size();
reverse(binary.begin(), binary.end());
int value = 0;
for (int i = 0; i < n; i++) {
if (binary[i] == '1') value += (1 << i);
}
return value;
}
class Bucket {
private:
int capacity;
int localDepth;
public:
vector<int> data;
int getLocalDepth();
void increaseLocalDepth();
void decreaseLocalDepth();
bool isFull();
bool isEmpty();
// initializes a bucket with given capacity and local depth
Bucket(int cap, int depth) {
capacity = cap;
localDepth = depth;
}
};
class Directory {
private:
int globalDepth;
int bucketCapacity;
public:
vector<Bucket*> pointers;
int getGlobalDepth();
void expand();
void setPointers(string lastK, Bucket* &newBucket);
void split(int bucketNo);
void insert(int value);
int search(int value);
void shrink();
void merge(int bucketNo);
void deleteValue(int value);
void show();
// Initializes a directory with global depth = 0 and bucket of given capacity
Directory(int bucketCap) {
bucketCapacity = bucketCap;
globalDepth = 0;
pointers.resize(1);
pointers[0] = new Bucket(bucketCap, 0);
}
};
// Bucket Functions
// function to get the current local depth of the bucket
int Bucket::getLocalDepth() {
return localDepth;
}
// function to increment the current local depth of the bucket
void Bucket::increaseLocalDepth() {
localDepth = localDepth + 1;
}
// function to decrement the current local depth of the bucket
void Bucket::decreaseLocalDepth() {
localDepth = localDepth - 1;
}
// function to check if the bucket is full
bool Bucket::isFull() {
return data.size() == capacity;
}
// function to check if the bucket is empty
bool Bucket::isEmpty() {
return data.size() == 0;
}
// Directory Functions
// function to get the current global depth of the directory
int Directory::getGlobalDepth() {
return globalDepth;
}
// function to expand the directory to double its entries
void Directory::expand() {
int currDepth = globalDepth;
int noOfEntries = (1 << currDepth);
for (int i = 0; i < noOfEntries; i++)
pointers.push_back(pointers[i]);
globalDepth = currDepth + 1;
}
// function to set the pointers to newBucket after splitting
void Directory::setPointers(string lastK, Bucket* &newBucket) {
int k = lastK.size();
int noOfEntries = (1 << globalDepth);
for (int i = 0; i < noOfEntries; i++) {
if (getLastKBits(i, k) == lastK) {
pointers[i] = newBucket;
}
}
}
// function to split a bucket
void Directory::split(int bucketNo) {
int noOfEntries = (1 << globalDepth);
Bucket* currBucket = pointers[bucketNo];
vector<int> values = currBucket->data;
currBucket->data = {};
int currLocalDepth = currBucket->getLocalDepth();
string lastK = getLastKBits(bucketNo, currLocalDepth);
lastK = "1" + lastK;
Bucket* newBucket = new Bucket(bucketCapacity, currLocalDepth + 1);
currBucket->increaseLocalDepth();
setPointers(lastK, newBucket);
for (auto val : values) {
string lastK = getLastKBits(val, globalDepth);
int bucketNo = getDecimal(lastK);
(pointers[bucketNo]->data).push_back(val);
}
}
// function to insert a value in the directory
void Directory::insert(int value) {
string lastK = getLastKBits(value, globalDepth);
int bucketNo = getDecimal(lastK);
Bucket* bucket = pointers[bucketNo];
if (!bucket->isFull())
(bucket->data).push_back(value);
else {
if (bucket->getLocalDepth() < globalDepth) {
split(bucketNo);
insert(value);
}
else {
expand();
insert(value);
}
}
}
// function to search the bucket number of a particular value
// returns -1 if the value is not present in the directory
int Directory::search(int value) {
string lastK = getLastKBits(value, globalDepth);
int bucketNo = getDecimal(lastK);
for (auto &val : pointers[bucketNo]->data) {
if (val == value)
return bucketNo;
}
return -1;
}
// function to shrink the directory to half its size
void Directory::shrink() {
int noOfEntries = (1 << globalDepth);
for (int i = noOfEntries-1; i >= noOfEntries/2; i--)
pointers.pop_back();
globalDepth = globalDepth - 1;
}
// function to merge to buckets after deletion
void Directory::merge(int bucketNo) {
vector<int> counterParts;
int noOfEntries = (1 << globalDepth);
int currLocalDepth = pointers[bucketNo]->getLocalDepth();
for (int i = 0; i < noOfEntries; i++) {
if (pointers[i] == pointers[bucketNo]) {
int counterPart = (i ^ (1 << (currLocalDepth-1)));
counterParts.push_back(counterPart);
}
}
bool sameBucket = true;
for (int i = 0; i < counterParts.size(); i++) {
if (pointers[counterParts[i]] != pointers[counterParts[0]]) {
sameBucket = false;
break;
}
}
// merging the buckets if all the counterparts point to the same bucket
// and the sum of their sizes is less than the bucket capacity
if (sameBucket) {
Bucket* bucket1 = pointers[bucketNo];
Bucket* bucket2 = pointers[counterParts[0]];
if (bucket1->data.size() + bucket2->data.size() <= bucketCapacity) {
vector<int> values1 = bucket1->data;
vector<int> values2 = bucket2->data;
bucket1->data.clear(), bucket2->data.clear();
for (auto counterPart : counterParts)
pointers[counterPart] = bucket1;
for (auto val : values1)
bucket1->data.push_back(val);
for (auto val : values2)
bucket1->data.push_back(val);
pointers[bucketNo]->decreaseLocalDepth();
bool toShrink = true;
for (int i = 0; i < noOfEntries; i++) {
if (pointers[i]->getLocalDepth() == globalDepth) {
toShrink = false;
break;
}
}
if (toShrink)
shrink();
if(globalDepth > 0)
merge(bucketNo);
}
}
}
// function to delete a value from the directory
void Directory::deleteValue(int value) {
int bucketNo = search(value);
if (bucketNo == -1) {
cout << "Value not present in the directory!\n\n";
return;
}
// erasing the value from the bucket
auto it = find(pointers[bucketNo]->data.begin(), pointers[bucketNo]->data.end(), value);
pointers[bucketNo]->data.erase(it);
merge(bucketNo);
}
// function to show the contents of the directory
void Directory::show() {
cout << "\n\n";
cout << "Global Depth = " << globalDepth << endl << endl;
map<Bucket*, vector<string>> mp;
int noOfEntries = (1 << globalDepth);
for (int i = 0; i < noOfEntries; i++) {
string lastK = getLastKBits(i, globalDepth);
Bucket* bucket = pointers[i];
mp[bucket].push_back(lastK);
}
int maxSpace = 0;
int maxSpace2 = 0;
for (auto &val : mp) {
int n = val.second.size();
int g = globalDepth;
int space = g*n + 2*n + 5;
int space2 = 0;
if ((val.first)->data.empty()) {
maxSpace2 = max(maxSpace2, 10);
}
else {
for (auto &no : (val.first)->data) {
string tmp = to_string(no);
space2 += tmp.size();
space2 += 2;
}
}
maxSpace = max(maxSpace, space);
maxSpace2 = max(maxSpace2, space2);
}
for (auto &val : mp) {
for (int i = 0; i < maxSpace + 12 && i < 60; i++) cout << "--";
cout << endl;
int n = val.second.size();
int g = globalDepth;
int space1 = g*n + 2*n + 2;
int space2 = 0;
if ((val.first)->data.empty()) {
space2 = 10;
}
else {
for (auto &no : (val.first)->data) {
string tmp = to_string(no);
space2 += tmp.size();
space2 += 2;
}
}
for (int i = 0; i < val.second.size(); i++) {
cout << val.second[i] << " ,"[i != val.second.size() - 1] << " ";
}
int noOfSpaces = maxSpace - space1;
for (int i = 0; i < noOfSpaces && i < 50; i++) cout << " ";
cout << "--> ";
int noOfSpaces2 = maxSpace2 - space2;
for (int i = 0; i < noOfSpaces2 && i < 50; i++) cout << " ";
if ((val.first)->isEmpty()) {
cout << "No values";
}
else {
for (int i = 0; i < (val.first)->data.size(); i++) {
cout << (val.first)->data[i] << " ,"[i != (val.first)->data.size() - 1] << " ";
}
}
cout << " (Local Depth = " << (val.first)->getLocalDepth() << ")" << endl;
}
for (int i = 0; i < maxSpace + 12 && i < 60; i++) cout << "--";
cout << endl;
cout << "\n\n";
}
int main() {
int bucketCapacity;
cout << "Enter bucket capacity: ";
cin >> bucketCapacity;
Directory directory(bucketCapacity);
while (1) {
int option;
cout << "Choose the operation you want to perform:" << endl
<< "1. Show directory" << endl
<< "2. Insert a value" << endl
<< "3. Delete a value" << endl
<< "4. Quit" << endl;
cin >> option;
switch (option) {
case 1:
directory.show();
break;
case 2:
int value;
cout << "Enter the value you want to insert: ";
cin >> value;
directory.insert(value);
break;
case 3:
cout << "Enter the value you want to delete: ";
cin >> value;
directory.deleteValue(value);
break;
case 4:
goto end;
break;
default:
cout << "Invalid Input\n" << endl;
}
}
end: {}
return 0;
}