forked from AshveenBansal98/Hashing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendibleHashingDoubleBuffering.cpp
More file actions
233 lines (213 loc) · 5.2 KB
/
Copy pathExtendibleHashingDoubleBuffering.cpp
File metadata and controls
233 lines (213 loc) · 5.2 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
#include <bits/stdc++.h>
using namespace std;
class Bucket{
vector <int> m;
int localdepth;
int size;
public:
Bucket(int depth, int size){
this->size = size;
this->localdepth = depth;
}
int getsize();
bool isEmpty();
int insert(int key);
int search(int key);
void copy(vector <int> &temp);
int getdepth();
void display();
void increasedepth();
void del(int key);
};
void Bucket :: del(int key){
for(int i = 0; i < m.size(); i++)
if (m[i] == key){
m.erase(m.begin() + i);
return;
}
cout << "Key not found" << endl;
}
void Bucket :: display(){
//cout << this->localdepth << " ";
for(int i = 0; i < m.size(); i++)
cout << m[i] << " ";
cout << endl;
}
int Bucket :: getdepth(){
return this->localdepth;
}
void Bucket :: increasedepth(){
this->localdepth++;
}
int Bucket :: getsize(){
return m.size();
}
int Bucket :: insert(int key){
m.push_back(key);
return 1;
}
int Bucket :: search(int key){
for(int i = 0; i < m.size(); i++)
if (m[i] == key)
return 1;
return 0;
}
void Bucket :: copy(vector <int> &temp){
for(int i = 0; i < m.size(); i++)
temp.push_back(m[i]);
m.clear();
}
class Directory{
int count;
int globaldepth;
int bucketsize;
vector <Bucket *> buckets;
int hash (int n);
int split(int bucket_num);
void reinsert(int bucket_num);
void Doubledirectory();
public:
Directory(int globaldepth, int bucket_size){
count = 0;
this->globaldepth = globaldepth;
this->bucketsize = bucket_size;
for(int i = 0; i < (1 << globaldepth); i++)
buckets.push_back(new Bucket(globaldepth, bucket_size));
}
void insert(int key);
void del(int key);
void search(int key);
void display();
};
void Directory :: del(int key){
int bucket_num = hash(key);
buckets[bucket_num]->del(key);
}
int Directory :: hash (int n){
return n&( (1 << globaldepth) - 1);
}
void Directory :: Doubledirectory(){
for(int i = 0; i < (1 << globaldepth); i++)
buckets.push_back(buckets[i]);
this->globaldepth++;
//cout << "line 94 " << this->globaldepth << endl;;
}
void Directory :: reinsert(int bucket_num){
vector <int> temp;
buckets[bucket_num]->copy(temp);
for(int i = 0; i < temp.size(); i++){
int key = temp[i];
int bucket_num = hash(key);
//cout << "new bucket_num" << bucket_num << endl;
int flag = buckets[bucket_num]->insert(key);
}
}
int Directory :: split(int bucket_num){
int localdepth = buckets[bucket_num]->getdepth();
//cout << globaldepth << " " << localdepth << endl;
int mirrorindex = bucket_num ^ (1 << localdepth);
buckets[bucket_num]->increasedepth();
localdepth++;
//cout << buckets[bucket_num]->getdepth() << endl;
buckets[mirrorindex] = new Bucket(localdepth, bucketsize);
int num = 1 << localdepth;
//for(int i = mirrorindex + num; i < (1 << globaldepth); i += num)
// buckets[i] = buckets[mirrorindex];
//for(int i = mirrorindex - num; i >=0 ; i -= num)
// buckets[i] = buckets[mirrorindex];
reinsert(bucket_num);
int over = 0;
if (buckets[bucket_num]->getsize() > bucketsize)
over++;
if (buckets[mirrorindex]->getsize() > bucketsize)
over++;
return over;
}
void Directory :: insert(int key){
int bucket_num = hash(key);
if (buckets[bucket_num]->search(key) == 1){
cout << "Key already exists" << endl;
return;
}
int temp = 0;
if(buckets[bucket_num]->getsize() != bucketsize){
int flag = buckets[bucket_num]->insert(key);
return;
}
/*while(buckets[bucket_num]->getsize() == bucketsize && buckets[bucket_num]->getdepth() < globaldepth){
temp = split(bucket_num);
}
if(buckets[bucket_num]->getsize() != bucketsize){
int flag = buckets[bucket_num]->insert(key);
return;
}*/
count++;
if (count != buckets.size()){
int flag = buckets[bucket_num]->insert(key);
}
else{
int flag = buckets[bucket_num]->insert(key);
Doubledirectory();
count = 0;
//set <Bucket *> s;
for(int i = 0; i < 1 << (globaldepth - 1); i++){
/*if (s.find(buckets[i]) != s.end())
continue;*/
count += split(i);
//s.insert(buckets[i]);
}
}
}
void Directory :: search (int key){
int bucket_num = hash(key);
int flag = buckets[bucket_num]->search(key);
if (flag == 1){
cout << key << " exists in bucket number " << bucket_num << endl;
}
else{
cout << key << " doesnot exist " << endl;
}
}
void Directory :: display(){
cout << endl;
//set <Bucket*> s;
for(int i = 0; i < (1 << globaldepth); i++){
//if (s.find(buckets[i]) != s.end())
// continue;
cout <<"Bucket " << i << ": ";
buckets[i]->display();
//s.insert(buckets[i]);
}
cout << endl;
}
int main(){
Directory* d = NULL;
int inp;
cin >> inp;
while(inp != -1){
if (inp == 1){
int globaldepth, bucket_size;
cin >> globaldepth >> bucket_size;
d = new Directory(globaldepth, bucket_size);
}
if (inp == 2){
int key;
cin >> key;
d->insert(key);
}
else if (inp == 3){
int key;
cin >> key;
d->search(key);
}
else if (inp == 3){
int key;
cin >> key;
d->del(key);
}
else if (inp == 5){
d->display();
}
cin >> inp;
}
}