-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path41 HashTable_OpenAddressing.cpp
351 lines (291 loc) · 7.16 KB
/
41 HashTable_OpenAddressing.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
/*
*
* Hash Table : Open Addressing Method
*
* - Md. Zarif Ul Alam
* Staring at : 18 November 2020 2:35pm
*
* */
/* Which of the favors of your Lord will you deny ? */
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define F first
#define S second
#define ALL(x) (x).begin(), (x).end()
#define READ freopen("alu.txt", "r", stdin)
#define WRITE freopen("vorta.txt", "w", stdout)
#ifndef ONLINE_JUDGE
#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
#else
#define DBG(x)
#define endl "\n"
#endif
template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p);
template <class T>
ostream &operator <<(ostream &os, vector<T>&v);
template <class T>
ostream &operator <<(ostream &os, set<T>&v);
struct Node
{
string key;
int val;
bool isDeletedBefore; /*marker*/
Node(){ this->isDeletedBefore = true; }
Node(string key,int val,bool isDeletedBefore = false)
{
this->key = key;
this->val = val;
this->isDeletedBefore = isDeletedBefore;
}
};
/*
* N > M to handle collisions
* */
struct HashTable_OpenAddressing
{
int N; /* initial number of assumed data */
int M; /* initial hast table size */
vector<Node*>ht;
function<int(string)> hashValue;
function<int(string)> auxhashValue;
function<int(int,int)> probeFunction;
template<typename T1,typename T2>
HashTable_OpenAddressing(T1 func1,T1 func2,T2 func3)
{
N = 10000;
// M = (1<<15); /*N > M*/
M = (1<<3); /*N > M*/
ht = vector<Node*>(M,NULL);
hashValue = func1;
auxhashValue = func2;
probeFunction = func3;
}
int hash(string key)
{
int ret = (hashValue(key)&(0x7fffffff)) % M;
return ret;
}
int auxhash(string key)
{
int ret = (auxhashValue(key)&(0x7fffffff)) % M;
return ret == 0 ? 1 : ret;
}
int normalize(int val)
{
return (val & 0x7fffffff)%M;
}
int probe(string key,int x)
{
int aux = auxhash(key);
return probeFunction(x,aux);
}
int search(string key)
{
int idx = hash(key);
int init_offset = idx;
int firstDeletedIdx = -1;
int x = 1; /*probe parameter*/
while(true)
{
if(ht[idx] != NULL && ht[idx]->isDeletedBefore == true)
{
if(firstDeletedIdx == -1) firstDeletedIdx = idx;
}
else if(ht[idx] != NULL)
{
if(ht[idx]->key == key)
{
int ret = ht[idx]->val;
if(firstDeletedIdx != -1)
{
ht[firstDeletedIdx] = ht[idx];
ht[idx] = new Node("marker",INT_MIN,true); /*rip marker*/
}
return ret;
}
}
else if(ht[idx] == NULL) return INT_MIN;
idx = normalize(init_offset + probe(key,x));
x++;
}
}
bool insert(string key,int val)
{
int idx = hash(key);
int init_offset = idx;
int firstDeletedIdx = -1;
int x = 1; /*probe parameter*/
while(true)
{
if(ht[idx] != NULL && ht[idx]->isDeletedBefore == true)
{
if(firstDeletedIdx == -1) firstDeletedIdx = idx;
}
else if(ht[idx] != NULL)
{
if(ht[idx]->key == key)
{
if(firstDeletedIdx != -1)
{
ht[firstDeletedIdx] = ht[idx];
ht[idx] = new Node("marker",INT_MIN,true); /*rip marker*/
}
}
return false;
}
else if(ht[idx] == NULL)
{
if(firstDeletedIdx != -1)
{
ht[firstDeletedIdx] = new Node(key,val);
}
else
{
ht[idx] = new Node(key,val);
}
return true;
}
idx = normalize(init_offset + probe(key,x));
x++;
}
}
int erase(string key)
{
int idx = hash(key);
int init_offset = idx;
int firstDeletedIdx = -1;
int x = 1; /*probe parameter*/
while(true)
{
if(ht[idx] != NULL && ht[idx]->isDeletedBefore == true)
{
continue;
}
else if(ht[idx] != NULL)
{
if(ht[idx]->key == key)
{
int ret = ht[idx]->val;
ht[idx] = ht[idx] = new Node("marker",INT_MIN,true); /*rip marker*/
return ret;
}
}
else if(ht[idx] == NULL) return INT_MIN; /*item not found*/
idx = normalize(init_offset + probe(key,x));
x++;
}
}
void print()
{
int cnt = 0;
for(int i=0;i<M;i++) cnt += (ht[i]==NULL);
// DBG(cnt);
}
};
int hashF1(string s)
{
int h = 0 , p = 31;
for(int i=0;i<(int)s.size();i++)
{
h = (h * p + s[i]);
}
return h;
}
int hashF2(string s)
{
int h = 0 , prime = 31 , p = 1 , mod = 1e9+7;
for(int i=0;i<(int)s.size();i++)
{
h = ((h%mod) + (p * s[i])%mod) % mod;
p = ((p%mod) * (prime%mod))%mod;
}
return h;
}
int auxhashF(string s)
{
int h = 7 , p = 31;
for(int i=0;i<(int)s.size();i++)
{
h = (h * p + s[i]);
}
return h;
}
int doubleHashingProbe(int x,int aux)
{
return x*aux;
}
int customHashingProbe(int x,int aux)
{
return (x*aux + x*x)>>1;
}
int32_t main()
{
HashTable_OpenAddressing h(hashF1,auxhashF,doubleHashingProbe);
// HashTable_OpenAddressing h(hashF2,auxhashF,doubleHashingProbe);
//
// HashTable_OpenAddressing h(hashF1,auxhashF,customHashingProbe);
// HashTable_OpenAddressing h(hashF2,auxhashF,customHashingProbe);
h.print();
int q;
cin>>q;
int val = 1;
while(q--)
{
string type;
cin>>type;
string s;
cin>>s;
if(type=="I")
{
if(h.insert(s,val)) val++;
}
else if(type=="F")
{
int ret = h.search(s);
if(ret != INT_MIN) cout<<"Value : "<<ret<<endl;
else cout<<"Not Present"<<endl;
}
else if(type=="E")
{
int ret = h.erase(s);
if(ret != INT_MIN) cout<<"Deleted Value : "<<ret<<endl;
else cout<<"Not Present"<<endl;
}
// h.print();
}
return 0;
}
/**
**/
template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p)
{
os<<"{"<<p.first<<", "<<p.second<<"} ";
return os;
}
template <class T>
ostream &operator <<(ostream &os, vector<T>&v)
{
os<<"[ ";
for(T i:v)
{
os<<i<<" " ;
}
os<<" ]";
return os;
}
template <class T>
ostream &operator <<(ostream &os, set<T>&v)
{
os<<"[ ";
for(T i:v)
{
os<<i<<" ";
}
os<<" ]";
return os;
}