forked from AshveenBansal98/Hashing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendibleHashing.cpp
More file actions
212 lines (194 loc) · 4.63 KB
/
ExtendibleHashing.cpp
File metadata and controls
212 lines (194 loc) · 4.63 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
#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;
}
bool isFull();
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++;
}
bool Bucket :: isFull(){
if (m.size() == size)
return true;
else
return false;
}
int Bucket :: insert(int key){
if (isFull())
return -1;
for(int i = 0; i < m.size(); i++)
if (m[i] == key)
return 0;
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 globaldepth;
int bucketsize;
vector <Bucket *> buckets;
int hash (int n);
void split(int bucket_num);
void reinsert(int bucket_num);
void Doubledirectory();
public:
Directory(int globaldepth, int bucket_size){
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);
if (flag == -1){
split(bucket_num);
buckets[bucket_num]->insert(key);
}
}
}
void Directory :: split(int bucket_num){
int localdepth = buckets[bucket_num]->getdepth();
//cout << globaldepth << " " << localdepth << endl;
if (globaldepth == localdepth)
Doubledirectory();
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);
}
void Directory :: insert(int key){
int bucket_num = hash(key);
int flag = buckets[bucket_num]->insert(key);
if (flag == -1){
cout << "splitting bucketnum " << bucket_num << endl;
split(bucket_num);
insert(key);
}
else if (flag == 0){
cout << "Key already exists" << endl;
}
else{
cout << key << " inserted in bucket having number " << bucket_num << endl;
}
}
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(){
set <Bucket*> s;
for(int i = 0; i < (1 << globaldepth); i++){
if (s.find(buckets[i]) != s.end())
continue;
cout << i << ": ";
buckets[i]->display();
s.insert(buckets[i]);
}
}
int main(){
int globaldepth, bucket_size;
cin >> globaldepth >> bucket_size;
Directory d(globaldepth, bucket_size);
int inp;
cin >> inp;
while(inp != -1){
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;
}
}