forked from AshveenBansal98/Hashing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearHashing.cpp
More file actions
226 lines (208 loc) · 4.49 KB
/
LinearHashing.cpp
File metadata and controls
226 lines (208 loc) · 4.49 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
#include <bits/stdc++.h>
using namespace std;
class Bucket{
vector <int> values;
Bucket* overflow;
int size;
public:
Bucket(int size){
this->size = size;
overflow = NULL;
}
void insert(int key);
void copy(vector <int> &temp);
int search(int key);
void display();
int del(int key);
bool isEmpty();
bool isOverflow();
};
bool Bucket :: isEmpty(){
return (values.size() == 0);
}
bool Bucket :: isOverflow(){
return (overflow != NULL);
}
int Bucket :: del(int key){
for(int i = 0; i < values.size(); i++){
if (values[i] == key){
values.erase(values.begin() + i);
if (overflow){
values.push_back(overflow->values[0]);
int status = overflow->del(overflow->values[0]);
if (overflow->isEmpty()){
delete(overflow);
overflow = NULL;
}
}
return 1;
}
}
if (overflow){
int status = overflow->del(key);
if (overflow->isEmpty()){
delete(overflow);
overflow = NULL;
}
return status;
}
else
return 0;
}
void Bucket :: display(){
for(int i = 0; i < values.size(); i++)
cout << values[i] << " ";
if (overflow)
overflow->display();
else
cout << endl;
}
void Bucket :: insert(int key){
if (values.size() < size)
values.push_back(key);
else{
if (overflow == NULL)
overflow = new Bucket(size);
overflow->insert(key);
}
}
void Bucket :: copy(vector <int> &temp){
for(int i = 0; i < values.size(); i++)
temp.push_back(values[i]);
values.clear();
if (overflow){
overflow->copy(temp);
delete(overflow);
overflow = NULL;
}
}
int Bucket :: search(int key){
for(int i = 0; i < values.size(); i++)
if (values[i] == key)
return 1;
else if (overflow)
return overflow->search(key);
return 0;
}
class Hashmap{
int numrecords;
int next;
int hash (int n);
void reinsert(int bucket_num);
int numbits;
int bucketsize;
vector <Bucket *> buckets;
public:
Hashmap(int numbits, int bucketsize){
this->numbits = numbits;
this->bucketsize = bucketsize;
next = 0;
numrecords = 1 << numbits;
for(int i = 0; i < numrecords; i++)
buckets.push_back(new Bucket(bucketsize));
}
void insert(int key);
void reinsert(vector <int> &temp);
void del(int key);
void search(int key);
void display();
};
int Hashmap :: hash(int n){
return n&( (1 << numbits) - 1);
}
void Hashmap :: reinsert(vector <int> &temp){
for(int i = 0; i < temp.size(); i++){
int key = temp[i];
int num = hash(key);
buckets[num]->insert(key);
}
}
void Hashmap :: insert(int key){
int num = hash(key);
if (num >= buckets.size()){
num = num - (1 << (numbits - 1));
}
buckets[num]->insert(key);
if (!buckets[num]->isOverflow())
return;
buckets.push_back(new Bucket(bucketsize));
numbits = ceil(log2((double)buckets.size()));
vector <int> temp;
buckets[next]->copy(temp);
reinsert(temp);
next++;
if (next == 1 << (numbits-1)){
next = 0;
}
}
void Hashmap :: display(){
cout << "INDEX OF NEXT BUCKET TO BE SPLIT " << next << endl;
for(int i = 0; i < buckets.size(); i++){
cout << i << " ";
buckets[i]->display();
}
}
void Hashmap :: search(int key){
int num = hash(key);
if (num < buckets.size()){
int status = buckets[num]->search(key);
if (status)
cout << key << " found at bucket index " << num << endl;
else
cout << key << " not found" << endl;
}
else{
num -= 1 << (numbits - 1);
int status = buckets[num]->search(key);
if (status)
cout << key << " found at bucket index " << num << endl;
else
cout << key << " not found" << endl;
}
}
void Hashmap :: del(int key){
int num = hash(key);
if (num < buckets.size()){
int status = buckets[num]->del(key);
if (status)
cout << key << " deleted from bucket index " << num << endl;
else
cout << key << " not found" << endl;
}
else{
num -= 1 << (numbits - 1);
int status = buckets[num]->del(key);
if (status)
cout << key << " deleted from bucket index " << num << endl;
else
cout << key << " not found" << endl;
}
}
int main(){
int numbits, bucket_size;
cin >> numbits >> bucket_size;
Hashmap h(numbits, bucket_size);
int inp;
cin >> inp;
while(inp != -1){
if (inp == 2){
int key;
cin >> key;
h.insert(key);
}
else if (inp == 3){
int key;
cin >> key;
h.search(key);
}
else if (inp == 4){
int key;
cin >> key;
h.del(key);
}
else if (inp == 5){
h.display();
}
cin >> inp;
}
}