-
Notifications
You must be signed in to change notification settings - Fork 0
/
40 HashTable_SeparateChaining.cpp
289 lines (225 loc) · 4.95 KB
/
40 HashTable_SeparateChaining.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
/*
*
* Hash Table : Separate Chaining Method
*
* - Md. Zarif Ul Alam
* Staring at : 16 November 2020 6:10pm
*
* */
/* 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;
Node* nxt;
Node(){}
Node(string key,int val)
{
this->key = key;
this->val = val;
this->nxt = NULL;
}
Node(string key,int val,Node* nxt)
{
this->key = key;
this->val = val;
this->nxt = nxt;
}
};
struct HashTable_SeparateChaining
{
int N; /* initial number of assumed data */
int M; /* initial hast table size */
int collisionCnt = 0 , probecnt = 0;
vector<Node*>ht;
function<int(string)> hashValue;
template<typename T>
HashTable_SeparateChaining(T func)
{
N = 10000;
// M = 1009; /*prime*/
M = 10007; /*prime*/
// M = 25; /*prime*/
ht = vector<Node*>(M);
hashValue = func;
}
int hash(string key)
{
int ret = (hashValue(key)&(0x7fffffff)) % M;
return ret;
}
int search(string key)
{
int idx = hash(key);
Node* now = ht[idx];
while(now != NULL)
{
probecnt++;
if(now->key == key)
return now->val;
now = now->nxt;
}
return INT_MIN; /*not found*/
}
bool insert(string key,int val)
{
int idx = hash(key);
assert(idx<M);
Node* now = ht[idx];
while(now != NULL)
{
if(now->key == key)
return false; /*already exists*/
now = now->nxt;
}
if(ht[idx] != NULL) collisionCnt++;
ht[idx] = new Node(key,val,ht[idx]);
return true;
}
int erase(string key)
{
int idx = hash(key);
Node* prv = NULL;
Node* now = ht[idx];
while(now != NULL)
{
if(now->key == key)
{
int ret = now->val;
if(prv) prv->nxt = now->nxt;
else ht[idx] = now->nxt;
delete (now);
return ret;
}
prv = now;
now = now->nxt;
}
return INT_MIN;
}
void print()
{
for(int i=0;i<M;i++)
{
cout<<"Chain "<<i<<" : ";
Node* now = ht[i];
while(now != NULL) cout<< now->key<<" " , now = now->nxt;
cout<<endl;
}
}
void printChainLengths()
{
for(int i=0;i<M;i++)
{
int cnt = 0;
Node* now = ht[i];
while(now != NULL) now = now->nxt , cnt++;
cout<<endl;
cout<<"Chain "<<i<<" : "<<cnt<<endl;
}
}
};
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;
}
int32_t main()
{
HashTable_SeparateChaining h(hashF1);
// cout<<0x7fffffff<<endl;
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();
}
// h.printChainLengths();
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;
}