-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash-table.cc
339 lines (278 loc) · 7.06 KB
/
hash-table.cc
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
/*Name: Hash table
* Skills:
typename list<Node>::iterator it; // because 'it' depends on the template parameter 'Node', 'it' is a dependent name which cannot be looked-up immediately
In Delete, user list<Node>& ls = ... // use &
* version 1.0:
hash table: array (vector)
hash function: division, multiplication, djb2, sdbm, lose base
collision: chaining (list)
performance:
insert: worst case: O (N) // all nodes has the same index so that
you need to search all the N node in the list to insert
best case: O (1) // empty index
delete: worst case: O (N), best case: O (1)
search: worst case: O (N), best case: O (1)
version 2.0
hash table: self-balance binary search tree
hash function: division
collision: self-balance binary search tree
performance:
insert, delete and search: O(logN)
*/
#include <iostream>
#include <vector>
#include <list>
#include <cstdint> // c++ 11 support: g++ -std=c++11 -o hash-table hash-table.cc // must update gcc/g++ to 4.8 version
//#include <stdint.h>
#include <string>
#include <cmath>
using namespace std;
// T: mapped data type
// the type of the Key is uint32_t
template<typename T>
class HashTable
{
public:
// store the (key, data)
class Node
{
public:
Node ()
{
key = 0;
}
virtual ~Node ()
{
}
public:
uint32_t key;
T data;
};
HashTable (uint32_t size)
: m_size (size)
{
m_htable.resize (m_size, list<Node>(0) ); // or list<Node>(), create m_size items in vector, each vector has 0 node in list
}
virtual ~HashTable ()
{
}
/*APIs*/
bool Insert (uint32_t key, T data);
bool Delete (uint32_t key);
T Search (uint32_t key);
void Print ();
// hash function
uint32_t HashDivision (uint32_t key); // map Key to the index of vector
uint32_t HashMultiplication (uint32_t key);
uint64_t HashDjb2 (string &str);
uint64_t HashSdbm (string &str);
uint64_t HashLoselose (string &str);
private:
uint32_t m_size; // the maximum length of hash table
vector<list<Node> > m_htable; // hash table
};
template<typename T>
uint32_t HashTable<T>::HashDivision (uint32_t key)
{
return key % m_size;// division
}
template<typename T>
uint32_t HashTable<T>::HashMultiplication (uint32_t key)
{
uint32_t hash = 0;
uint32_t m= 8;
float a = 0.618;
hash = floor (m * (key*a - floor (key*a) ) );
return hash;
}
template<typename T>
uint64_t HashTable<T>::HashDjb2 (string &str)
{
uint64_t hash = 5381;
int c;
uint32_t i = 0;
while ( i < str.size ())
{
c = static_cast<int>(str.at (i));
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
i++;
}
return hash;
}
template<typename T>
uint64_t HashTable<T>::HashSdbm (string &str)
{
uint64_t hash = 0;
int c;
uint32_t i = 0;
while ( i < str.size ())
{
c = static_cast<int>(str.at (i));
hash = c + (hash << 6) + (hash << 16) - hash;
i++;
}
return hash;
}
template<typename T>
uint64_t HashTable<T>::HashLoselose (string &str)
{
uint64_t hash = 0;
int c;
uint32_t i = 0;
while ( i < str.size ())
{
c = static_cast<int>(str.at (i));
hash += c;
i++;
}
return hash;
}
template<typename T>
bool HashTable<T>::Insert (uint32_t key, T data)
{
bool rt = false;
Node node;
node.key = key;
node.data = data;
uint32_t index = HashDivision (key);
// find the index-th item in vector
list<Node> ls = m_htable[index];
if (ls.size () == 0)
{
// new item in vector
ls.push_back (node);
m_htable[index] = ls;
rt = true;
}
else
{
// collision in vector
// find node in the list of m_htable[index]
typename list<Node>::iterator it;
for (it = ls.begin (); it != ls.end (); it++)
{
if (it->key == key)
{
// update the existing node
it->data = data;
rt = true;
break;
}
}
if (rt == false)
{
// new item in list (collision in vector)
m_htable[index].push_back (node);
rt = true;
}
}
return rt;
}
template<typename T>
void HashTable<T>::Print ()
{
cout << "Print: " << endl;
for (uint32_t i = 0; i < m_htable.size (); i++)
{
list<Node> ls = m_htable[i];
if (ls.size () == 0)
{
cout << endl;
continue;
}
typename list<Node>::iterator it;
for (it = ls.begin (); it != ls.end (); it++)
{
cout << "(" << it->key << ", " << it->data << ") ";
}
cout << endl;
}
}
template<typename T>
T HashTable<T>::Search (uint32_t key)
{
bool found = false;
uint32_t index = HashDivision (key);
// find the index-th item in vector
list<Node> ls = m_htable[index];
if (ls.size () == 0)
{
// no result
found = false;
}
else
{
typename list<Node>::iterator it;
for (it = ls.begin (); it != ls.end (); it++)
{
if (it->key == key)
{
return it->data;
}
}
}
if (!found)
{
// Do not search, create an empty an return it;
Insert (key, "");
return Search (key);
}
}
template<typename T>
bool HashTable<T>::Delete (uint32_t key)
{
uint32_t index = HashDivision (key);
// find the index-th item in vector
list<Node>& ls = m_htable[index]; // note: we must use &, otherwise, we cannot erase node in m_htable
if (ls.size () == 0)
{
// no result
cout << " cannot find the key number " << key << endl;
return false;
}
else
{
typename list<Node>::iterator it;
for (it = ls.begin (); it != ls.end (); it++)
{
if (it->key == key)
{
ls.erase(it);
return true;
}
}
}
}
int main (int argc, char *argv[])
{
HashTable<string> table(5);
cout << "Insert: " << endl;
table.Insert (0, "zero");
table.Insert (1, "one");
table.Insert (2, "second");
table.Insert (3, "three");
table.Insert (4, "four");
table.Insert (5, "five");
table.Insert (6, "six");
table.Insert (7, "seven");
table.Insert (8, "eight");
table.Insert (9, "nigh");
table.Insert (5, "five");
table.Print ();
uint32_t key = 5;
cout << " search: " << key << " ";
cout << table.Search (key) << endl;
cout << " search: " << key + 8 << " ";
cout << table.Search (key + 8) << endl;
table.Delete (key);
table.Print ();
cout << " search: " << key << " ";
cout << table.Search (key) << endl;
string str("two");
cout << " djb2 " << table.HashDjb2 (str) << endl;
cout << " sdbm " << table.HashSdbm (str) << endl;
cout << " lose " << table.HashLoselose (str) << endl;
for (uint32_t i = 0; i < 10; i++)
cout << " Multiplication " << " key " << i << " hash " << table.HashMultiplication (i) << endl;
return 0;
}