-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtendibleHashing.cpp
396 lines (334 loc) · 9.91 KB
/
ExtendibleHashing.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
#include <cassert>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
#define BIGPRIME 99991
#define DEBUG 1
int call_depth = 0;
void dbg_print(string cls, string func, string msg = "") {
if (!DEBUG)
return;
for (int i = 0; i < call_depth - 1; i++) {
cerr << " ";
}
if (call_depth - 1)
cerr << "└─";
cerr << cls << '\t' << func;
if (msg != "")
cerr << " : " << msg;
cerr << '\n';
}
class Bucket {
const string cls = "Bucket";
const long unsigned int size;
int depth;
list<pair<const int, string>> values;
bool updateKey(const int key, const string value);
string getValueFromKey(const int key);
public:
Bucket(const int depth, const int size);
bool insert(const int key, const string value);
int getDepth() { return this->depth; };
bool mergeFromBucket(Bucket *b) {
// DEBUG
call_depth++;
dbg_print(this->cls, "mergeFromBucket");
// DEBUG
bool ret = true;
for (pair<int, string> p : b->getValues()) {
if (!this->insert(p.first, p.second))
ret = false;
}
// reduce depth
this->depth--;
call_depth--;
return ret;
};
list<pair<const int, string>> getValues() { return this->values; };
int findKey(const int key);
bool remove(const int key);
void print() {
cout << " ";
for (pair<int, string> p : this->values) {
cout << "(" << p.first << ", " << p.second << ") ";
}
cout << '\n';
}
};
Bucket::Bucket(const int depth, const int size) : size(size), depth(depth) {
assert(this->depth > 0 && this->size > 0);
}
int Bucket::findKey(const int key) {
int counter = 0;
for (pair<const int, const string> p : this->values) {
if (p.first == key)
return counter;
counter++;
}
return -1;
}
string Bucket::getValueFromKey(const int key) {
for (pair<const int, const string> p : this->values) {
if (p.first == key)
return p.second;
}
return "\0";
}
bool Bucket::updateKey(const int key, const string value) {
for (pair<const int, string> &p : this->values) {
if (p.first == key) {
p.second = value;
return true;
}
}
return false;
}
bool Bucket::insert(const int key, const string value) {
// DEBUG
call_depth++;
const string msg = "key = " + to_string(key) + ", value = " + value;
dbg_print(this->cls, "insert", msg);
// DEBUG
bool ret = true;
if (findKey(key) >= 0) {
this->updateKey(key, value);
ret = true;
} else if (this->values.size() < size) {
this->values.emplace_back(make_pair(key, value));
ret = true;
} else
ret = false;
call_depth--;
return ret;
}
bool Bucket::remove(const int key) {
// DEBUG
call_depth++;
const string msg = "key = " + to_string(key);
dbg_print(this->cls, "remove", msg);
// DEBUG
bool ret = true;
if (findKey(key) >= 0) {
this->values.remove({key, getValueFromKey(key)});
ret = true;
} else {
ret = false;
}
call_depth--;
return ret;
}
class Directory {
const string cls = "Directory";
const long unsigned int bucket_size;
int global_depth;
vector<Bucket *> buckets;
int hashing_func(const int n) { return n % BIGPRIME; };
void grow();
void split(int bucket_index);
int getBucketCount() {
assert((1 << this->global_depth) == (int)this->buckets.size());
return 1 << this->global_depth;
};
int getPosFromKey(const int key) {
return this->hashing_func(key) % this->getBucketCount();
}
void join_buckets();
void shrink();
public:
Directory(const int depth, const int bucket_size);
void insert(const int key, const string value);
bool remove(const int key);
void print() {
int count = 0;
cout << "Buckets :\n";
for (Bucket *b : this->buckets) {
cout << count++ << '\t';
b->print();
}
};
};
Directory::Directory(const int depth, const int bucket_size)
: bucket_size(bucket_size), global_depth(depth) {
assert(this->global_depth > 0 && this->bucket_size > 0);
for (int i = 0; i < (1 << this->global_depth); i++) {
this->buckets.push_back(new Bucket(depth, bucket_size));
}
}
void Directory::shrink() {
// DEBUG
call_depth++;
dbg_print(this->cls, "shrink");
// DEBUG
// check if the local depth of all buckets is less global depth
bool can_shrink = true;
for (Bucket *b : this->buckets) {
if (b->getDepth() >= this->global_depth) {
can_shrink = false;
break;
}
}
if (can_shrink) {
const int half_count = this->getBucketCount() / 2;
this->buckets.erase(this->buckets.begin() + half_count,
this->buckets.end());
this->global_depth--;
}
assert(this->getBucketCount() == (int)this->buckets.size());
call_depth--;
}
void Directory::join_buckets() {
// DEBUG
call_depth++;
dbg_print(this->cls, "join_buckets");
// DEBUG
// try to join all buckets
const int half_count = this->getBucketCount() / 2;
for (int i = 0; i < half_count; i++) {
// check if two buckets can be merged
if (this->buckets[i] != this->buckets[i + half_count] &&
(this->buckets[i]->getValues().size() +
this->buckets[i + half_count]->getValues().size() <=
this->bucket_size)) {
// DEBUG
call_depth++;
const string msg =
"merge " + to_string(i + half_count) + " to " + to_string(i);
dbg_print(this->cls, "join", msg);
call_depth--;
// DEBUG
// merge the second bucket into first
this->buckets[i]->mergeFromBucket(this->buckets[i + half_count]);
delete this->buckets[i + half_count];
this->buckets[i + half_count] = this->buckets[i];
}
}
call_depth--;
}
void Directory::split(const int bucket_index) {
// DEBUG
call_depth++;
dbg_print(this->cls, "split");
// DEBUG
// bucket index provides oooxxx
// where xxx is determined parts of hash
// ooo is undetermined
// count(x) = local depth
// count(o) = globalDepth - localDepth
// after splitting -> ooXxxx point to different buckets
if (this->global_depth <= this->buckets[bucket_index]->getDepth()) {
cerr << "Err: split attempted on a bucket whose dept is equal to "
"global depth";
exit(-1);
}
const int local_depth = this->buckets[bucket_index]->getDepth();
// get 000xxx
const int b_least = bucket_index % (1 << local_depth);
assert(this->buckets[b_least] == this->buckets[bucket_index]);
const int increment = (1 << local_depth);
assert(increment < this->getBucketCount());
Bucket *b1 = new Bucket(local_depth + 1, this->bucket_size);
Bucket *b2 = new Bucket(local_depth + 1, this->bucket_size);
Bucket *b = this->buckets[b_least];
for (int index = b_least, i = 0; index < this->getBucketCount();
index += increment, i++) {
if (i % 2 == 0)
this->buckets[index] = b1;
else
this->buckets[index] = b2;
}
for (pair<const int, const string> p : b->getValues()) {
this->insert(p.first, p.second);
}
delete b;
call_depth--;
}
void Directory::grow() {
// DEBUG
call_depth++;
dbg_print(this->cls, "grow");
// DEBUG
const vector<Bucket *> temp = this->buckets;
this->buckets.insert(buckets.end(), temp.begin(), temp.end());
this->global_depth++;
call_depth--;
}
void Directory::insert(const int key, const string value) {
// DEBUG
call_depth++;
const string msg = "key = " + to_string(key) + ", bucket index = " +
to_string(this->getPosFromKey(key));
dbg_print(this->cls, "insert", msg);
// DEBUG
const int pos = this->getPosFromKey(key);
if (!this->buckets[pos]->insert(key, value)) {
// split the bucket or grow the directory
if (this->buckets[pos]->getDepth() < this->global_depth) {
// split the bucket
this->split(pos);
} else {
// grow
this->grow();
}
this->insert(key, value);
}
call_depth--;
}
bool Directory::remove(const int key) {
// DEBUG
call_depth++;
const string msg = "key = " + to_string(key);
dbg_print(this->cls, "remove", msg);
// DEBUG
bool ret = false;
const int pos = this->getPosFromKey(key);
if (this->buckets[pos]->remove(key)) {
ret = true;
}
this->join_buckets();
this->shrink();
call_depth--;
return ret;
}
void menu() {
cout << "Command \tDescription\n"
<< "---------------------------\n"
<< "i <key, value>\tinsert\n"
<< "d <key> \tdelete\n"
<< "p \tprint\n"
<< "q \tquit\n";
}
int main() {
cout << "Extendible Hashing\n";
int depth_temp = 1, size_temp = 2;
cout << "Enter directory depth : (for example 1) ";
cin >> depth_temp;
cout << "Enter bucket size : (for example 2) ";
cin >> size_temp;
Directory d(depth_temp, size_temp);
menu();
do {
string choice;
int key;
string value;
cout << "> ";
cin >> choice;
if (choice == "i") {
cin >> key >> value;
d.insert(key, value);
} else if (choice == "p") {
d.print();
} else if (choice == "d") {
cin >> key;
d.remove(key);
} else if (choice != "q") {
cout << "\nInvalid option '" << choice << "'\n\n";
menu();
} else {
break;
}
} while (true);
cout << endl;
return 0;
}