-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS_HashTable.h
More file actions
372 lines (331 loc) · 9.27 KB
/
DS_HashTable.h
File metadata and controls
372 lines (331 loc) · 9.27 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#ifndef __HASH_TABLE_H__
#define __HASH_TABLE_H__
#include "DS_HashKeyer.h"
#include <assert.h>
#include <memory.h>
namespace DataStructures
{
template <class KeyType, class DataType>
class HashTable
{
public:
class HashNode
{
public:
KeyType key;
DataType data;
//pointer to the next storage location in the hasnNode.a has node is essentially just a linked list.
HashNode *prev;
HashNode *next;
//returns a newly instantiated object of type HashNode.
HashNode(const KeyType &_key, const DataType &_data, HashNode *_prev = 0, HashNode *_next = 0 );
HashNode() { key = 0; data = DataType(); prev = 0; next = 0; }
};
//java's built-in hash table uses Ko=89 as the default bucket size, as Kn = 2Kn+1 + 1 is a prime number after as many as 5 repetitions.
const static int DEFAULT_BUCKETS = 89;
const static double THRESHOLD;
private:
HashNode **hashNodes;
int numberHashNodes;
int numberHashEntries;
int CalcuHash( const KeyType &key ) const;
void ReHash(int newSize = 0);
//grow() increases the size of the array based on the formula new_size = 2*old_size + 1
void Grow();
int defaultHashSize;
public:
class SimpleIterator
{
public:
SimpleIterator(HashTable *hash)
{
hashTable = hash;
for (currentIndex=0; currentIndex<hash->numberHashNodes; currentIndex++)
{
if (hash->hashNodes[currentIndex])
{
currentNode = hash->hashNodes[currentIndex];
return;
}
}
//currentIndex < 0 and currentNode = 0 means empty hash table or reach the end
currentIndex = -1;
currentNode = 0;
}
SimpleIterator(HashTable *hash, KeyType key)
{
hashTable = hash;
currentNode = hashTable->FindHashNode(key);
if (currentNode)
{
currentIndex = hashTable->CalcuHash(key);
}
else
Reset();
}
void Reset()
{
SimpleIterator::SimpleIterator(hashTable);
}
DataType *GetData()
{
if (currentNode)
return &(currentNode->data);
else
return 0;
}
HashNode *GetNode()
{
if (currentNode)
return currentNode;
else
return 0;
}
void FindNext()
{
if (currentNode)
{
currentNode = currentNode->next;
if (currentNode)
return;
}
if (currentIndex<0)
return;
for (currentIndex=currentIndex+1; currentIndex<hashTable->numberHashNodes; currentIndex++ )
{
currentNode = hashTable->hashNodes[currentIndex];
if (currentNode)
return;
}
currentNode = 0;
currentIndex = -1;
}
private:
HashTable *hashTable;
int currentIndex;
HashNode *currentNode;
};
HashTable(int startingHashNodes = DEFAULT_BUCKETS);
~HashTable();
HashTable &operator = (const HashTable &table);
DataType &operator [] ( const KeyType &key );
bool InHashTable(const KeyType &key);
inline int GetHashTableSize() { return numberHashNodes; }
inline int GetHashEntriesSize() { return numberHashEntries; }
HashNode *Add(const KeyType &key, const DataType &data);
bool Del(const KeyType &key);
HashNode *FindHashNode(const KeyType &key);
bool SetData(const KeyType &key, const DataType &data);
HashTable &Clear();
};
template <class KeyType, class DataType>
const double HashTable<KeyType, DataType>::THRESHOLD = 0.75;
template <class KeyType, class DataType>
HashTable<KeyType, DataType> &HashTable<KeyType, DataType>::operator = (const HashTable<KeyType, DataType> &table)
{
HashNode *currentNode = 0;
defaultHashSize = table.numberHashNodes;
Clear();
//Iterate over all the hashNodes in <table>, copying over each entry.
for ( int nodeIndex = 0; nodeIndex < table.numberHashNodes; nodeIndex++ )
{
currentNode = table.hashNodes[nodeIndex];
while( currentNode != 0 )
{
Add( currentNode->key, currentNode->data );
currentNode = currentNode->next;
}
}
return *this;
}
template <class KeyType, class DataType>
int HashTable<KeyType, DataType>::CalcuHash(const KeyType &key) const
{
HashKeyer<KeyType> calculater(key);
return calculater.Get() % numberHashNodes;
}
template <class KeyType, class DataType>
void HashTable<KeyType, DataType>::Grow()
{
int currentPosition = numberHashNodes;
numberHashNodes = 2*numberHashNodes + 1;
hashNodes = (HashNode **)realloc( hashNodes, (sizeof( HashNode *) * numberHashNodes) );
memset(&hashNodes[currentPosition], 0, sizeof(HashNode *) * (numberHashNodes - currentPosition));
ReHash(numberHashNodes);
}
template <class KeyType, class DataType>
void HashTable<KeyType, DataType>::ReHash(int newSize)
{
//back old table
HashNode **oldNodes = (HashNode **)calloc(sizeof(HashNode *), numberHashNodes);
memcpy(oldNodes, hashNodes, numberHashNodes * sizeof(HashNode *));
int oldSize = numberHashNodes;
//allocate new hash table
int actSize = numberHashNodes;
if (newSize>numberHashNodes)
actSize = newSize;
numberHashNodes = actSize;
numberHashEntries = 0;
free(hashNodes);
hashNodes = (HashNode **)calloc(sizeof(HashNode *), numberHashNodes);
//a while loop to add backed data
for (int i=0; i<oldSize; i++)
{
HashNode *currentNode = oldNodes[i];
while (currentNode)
{
Add( currentNode->key, currentNode->data );
currentNode = currentNode->next;
}
}
//clear backed nodes
for (int i=0; i<oldSize; i++)
{
HashNode *currentNode = oldNodes[i];
while (currentNode)
{
HashNode *nextNode = currentNode->next;
delete currentNode;
currentNode = nextNode;
}
}
//finally, free backed memory
free(oldNodes);
}
template <class KeyType, class DataType>
HashTable<KeyType, DataType>::~HashTable()
{
int nodeIndex = 0;
HashNode *currentNode = 0;
HashNode *nextNode = 0;
for( nodeIndex = 0; nodeIndex < numberHashNodes; nodeIndex++ )
{
currentNode = hashNodes[nodeIndex];
while( currentNode != 0 )
{
nextNode = currentNode->next;
delete currentNode;
currentNode = nextNode;
}
}
free(hashNodes);
}
template <class KeyType, class DataType>
HashTable<KeyType, DataType>::HashTable(int startingHashNodes) :
numberHashNodes(startingHashNodes),
numberHashEntries(0)
{
defaultHashSize = startingHashNodes;
hashNodes = (HashNode **)calloc(sizeof(HashNode *), numberHashNodes);
}
template <class KeyType, class DataType>
typename HashTable<KeyType, DataType>::HashNode *HashTable<KeyType, DataType>::Add( const KeyType &key, const DataType &data )
{
if (InHashTable(key))
return 0;
if (((double)numberHashEntries / numberHashNodes) >= THRESHOLD)
Grow();
int hashValue = CalcuHash(key);
numberHashEntries++;
HashNode *newNode = new HashNode( key, data, 0, 0 );
if( hashNodes[hashValue] != 0 )
{
newNode->prev = hashNodes[hashValue]->prev; //should always be zero, i guess
newNode->next = hashNodes[hashValue];
hashNodes[hashValue]->prev = newNode;
}
hashNodes[hashValue] = newNode;
return newNode;
}
template <class KeyType, class DataType>
bool HashTable<KeyType, DataType>::InHashTable(const KeyType &key)
{
return FindHashNode(key) != 0;
}
template <class KeyType, class DataType>
typename HashTable<KeyType, DataType>::HashNode *HashTable<KeyType, DataType>::FindHashNode(const KeyType &key)
{
int hashValue = CalcuHash(key);
HashNode *currentNode = hashNodes[hashValue];
while( currentNode != 0 )
{
if (currentNode->key == key)
return currentNode;
currentNode = currentNode->next;
}
return 0;
}
template <class KeyType, class DataType>
HashTable<KeyType, DataType> &HashTable<KeyType, DataType>::Clear()
{
int nodeIndex;
for (nodeIndex = 0; nodeIndex < numberHashNodes; nodeIndex++)
{
HashNode *currentNode = hashNodes[nodeIndex];
while( currentNode != 0 )
{
HashNode *nextNode = currentNode->next;
delete currentNode;
currentNode = nextNode;
}
hashNodes[nodeIndex] = 0;
}
numberHashEntries = 0;
numberHashNodes = defaultHashSize;;
hashNodes = (HashNode **)realloc(hashNodes, (sizeof( HashNode *) * numberHashNodes));
return *this;
}
template <class KeyType, class DataType>
bool HashTable<KeyType, DataType>::Del(const KeyType &key)
{
HashNode *node = FindHashNode( key );
if (!node)
return false;
HashNode *prevNode = node->prev;
HashNode *nextNode = node->next;
delete node;
if (prevNode)
prevNode->next = nextNode;
if (nextNode)
nextNode->prev = prevNode;
if (((long)prevNode | (long)nextNode) == 0)
{
int hashValue = CalcuHash(key);
hashNodes[hashValue] = 0;
}
else
{
int hashValue = CalcuHash(key);
if (prevNode)
hashNodes[hashValue] = prevNode;
else
hashNodes[hashValue] = nextNode;
}
numberHashEntries --;
return true;
}
template <class KeyType, class DataType>
DataType &HashTable<KeyType, DataType>::operator[] (const KeyType &key)
{
HashNode *foundNode = FindHashNode( key );
assert(foundNode);
return foundNode->data;
}
template <class KeyType, class DataType>
bool HashTable<KeyType, DataType>::SetData(const KeyType &key, const DataType &data)
{
HashNode *foundNode = FindHashNode( key );
if (!foundNode)
return false;
foundNode->data = data;
return true;
}
template <class KeyType, class DataType>
HashTable<KeyType, DataType>::HashNode::HashNode(const KeyType &_key, const DataType &_data, HashNode *_prev, HashNode *_next ) :
key(_key),
data(_data),
prev(_prev),
next(_next)
{}
} // End namespace
#endif