-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensible_Hashing3.cpp
More file actions
222 lines (216 loc) · 5.29 KB
/
Copy pathExtensible_Hashing3.cpp
File metadata and controls
222 lines (216 loc) · 5.29 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
// Arthor - Name - Gaurav Kumar
// - Roll No. - 20074013
// - Program - IDD 5th semester
// -Implementation of extensible Hashing using c++ stl
#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
unordered_map<string,string> dir;
unordered_map<string, vector<int>> bucket;
int Global;
string hashfun(int n, int len = Global)
{
string ans = "";
for (int i = 0; i < len; i++)
{
if (n & 1)
ans += "1";
else
ans += "0";
n >>= 1;
}
reverse(all(ans));
return ans;
}
void intialize(int N, unordered_map<string,string> &temp)
{
Global = N;
for (int i = 0; i < (1 << N); i++)
{
string s = hashfun(i);
temp[s] = s;
}
}
void splitbucket(string s, string h)
{
vector<int> res;
// res.push_back(val);
for (auto it : bucket[h])
res.push_back(it);
// cout<<"Bucket splitting\n";
bucket.erase(h);
string s1 = "0" + h;
string s2 = "1" + h;
int n = h.size() + 1;
for (auto it : res)
bucket[hashfun(it, n)].push_back(it);
for (auto it = dir.begin(); it != dir.end(); it++)
{ ////
if ((it->first).substr(Global - n, n) == s1)
it->second = s1;
if ((it->first).substr(Global - n, n) == s2)
it->second = s2;
}
}
void expansion(string s)
{
Global++;
int n = s.size();
vector<int> res;
// res.push_back(val);
for (auto it : bucket[s])
res.push_back(it);
bucket.erase(s);
unordered_map<string,string> temp;
intialize(Global, temp);
for (auto it : res)
bucket[hashfun(it)].push_back(it);
for (auto it = temp.begin(); it != temp.end(); it++)
{ ////
if ((it->first).substr(Global - n, n) == s)
continue;
it->second = dir[(it->first).substr(Global - n, n)];
}
dir = temp;
}
void shrink()
{
Global--;
unordered_map<string,string> temp;
// intialize(Global,temp);
for (auto it : dir)
temp[it.first.substr(1, Global)] = it.second;
dir = temp;
if(bucket.size()< (1<<(Global-1)))
shrink();
}
bool search(int val)
{
string s = dir[hashfun(val)];
auto v = bucket[s];
for (auto it : v)
if (it == val)
return true;
return false;
}
void drop(string s, string h)
{
string s1 = "0" + h.substr(1, h.size() - 1);
string s2 = "1" + h.substr(1, h.size() - 1);
string temp=h.substr(1, h.size() - 2);
string res = h.substr(1, h.size() - 1);
vector<int> ans;
for (auto it : bucket[s1])
ans.push_back(it);
for (auto it : bucket[s2])
ans.push_back(it);
bucket.erase(s1);
bucket.erase(s2);
for (auto it : ans)
bucket[res].push_back(it);
for (auto it = dir.begin(); it != dir.end(); it++)
if (it->second == s1 || it->second == s2)
it->second = res;
int maxlocal = 0;
for (auto it : dir)
if (it.second.size() > maxlocal)
maxlocal = it.second.size();
unordered_map<string, int> mp;
for (auto it : dir)
if (it.second.size() == maxlocal)
mp[it.second]++;
if (mp.size() == 2 && mp[s1] && mp[s2])
shrink();
if (maxlocal == (Global - 1))
shrink();
if(bucket.find(temp)==bucket.end() && temp.size()>=1)
drop(s,res);
}
void insert(int val)
{
if(search(val)){
cout<<"Element already exists..\n";
}
string s = hashfun(val);
string h = dir[s];
if (bucket[h].size() == 2)
{
if (h.size() == s.size()) // comparing local depth and global depth
expansion(h);
else
splitbucket(s, h);
insert(val);
}
else
{
bucket[h].push_back(val);
// values.push_back(val);
}
}
void remove(int val)
{
if (search(val) == 0)
{
cout << "Element Doesn't Exists...\n";
return;
}
string s = hashfun(val);
string h = dir[s];
if (bucket[h].back() == val)
bucket[h].pop_back();
else
{
int temp = bucket[h].back();
bucket[h].clear();
bucket[h].push_back(temp);
}
if (bucket[h].empty())
{
drop(s, h);
}
}
void showall()
{
cout << "Global Depth = " << Global << "\n";
cout << "Number of Buckets = " << bucket.size() << "\n";
for (auto it : dir)
{
if (bucket.find(it.second) != bucket.end())
{
cout << it.first << " " << it.second << " -> ";
for (auto i : bucket[it.second])
cout << i << " ";
cout << "\n";
}
}
}
signed main()
{
intialize(1, dir);
cout << "Command Line:\n";
cout << "1.Insert Data\n2.Delete Data\n3.Show Data\n0.Exit\n";
int p;
cin >> p;
while (p)
{
if (p == 1)
{
int val;
cin >> val;
insert(val);
}
else if (p == 2)
{
int val;
cin >> val;
remove(val);
}
else if (p == 3)
{
showall();
}
cout << "Next Command:....\n";
cin >> p;
}
return 0;
}