-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.h
267 lines (215 loc) · 5.83 KB
/
hashtable.h
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
// Hash Table
#ifndef HASHTABLE_H
#define HASHTABLE_H
#ifdef _WIN32
#pragma once
#endif
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <vector>
typedef int HashTableIterator_t;
//-----------------------------------------------------------------------------
// Hash Table: key (only integer type) - value (any data)
//-----------------------------------------------------------------------------
template <typename KeyT, class ValueT>
class CHashTable
{
public:
typedef struct HashPair_s
{
KeyT key;
ValueT value;
} HashPair_t;
public:
CHashTable(int iBucketsCount);
~CHashTable();
ValueT *Find( const KeyT key ) const;
bool Insert( const KeyT key, const ValueT &value, void (*pfnOnInsertFailed)(ValueT *pFoundValue, ValueT *pInsertValue) = NULL );
bool Remove( const KeyT key, bool (*pfnOnRemove)(ValueT *pRemoveValue, ValueT *pUserValue) = NULL, ValueT *pUserValue = NULL );
void RemoveAll();
void Clear();
void Purge();
void IterateEntries( void (*pfnCallback)(const KeyT key, ValueT &value) );
HashTableIterator_t First( int ndxBucket );
HashTableIterator_t Next( int ndxBucket, HashTableIterator_t iterator );
KeyT &KeyAt( int ndxBucket, HashTableIterator_t iterator );
ValueT &ValueAt( int ndxBucket, HashTableIterator_t iterator );
bool IsValidIterator( HashTableIterator_t iterator );
int Count() const { return m_Size; }
int Size() const { return m_Size; }
private:
unsigned int HashKey(const KeyT key) const;
protected:
typedef std::vector<HashPair_t> HashTableBucketList_t;
std::vector<HashTableBucketList_t> m_Buckets;
int m_Size = 0;
};
template <typename KeyT, class ValueT>
CHashTable<KeyT, ValueT>::CHashTable(int iBucketsCount)
{
if ((iBucketsCount & (iBucketsCount - 1)) == 0) // power of two
m_Size = iBucketsCount - 1;
else
m_Size = iBucketsCount;
m_Buckets.reserve(m_Size);
m_Buckets.resize(m_Size);
for (int i = 0; i < m_Size; i++)
{
m_Buckets[i].clear();
}
}
template <typename KeyT, class ValueT>
CHashTable<KeyT, ValueT>::~CHashTable()
{
Purge();
}
template <typename KeyT, class ValueT>
inline ValueT *CHashTable<KeyT, ValueT>::Find(const KeyT key) const
{
unsigned int hash = HashKey(key);
int index = hash % m_Size;
for (size_t i = 0; i < m_Buckets[index].size(); i++)
{
if ( m_Buckets[index][i].key == key )
{
return const_cast<ValueT *>(&m_Buckets[index][i].value);
}
}
return NULL;
}
template <typename KeyT, class ValueT>
inline bool CHashTable<KeyT, ValueT>::Insert(const KeyT key, const ValueT &value, void (*pfnOnInsertFailed)(ValueT *pFoundValue, ValueT *pInsertValue) /* = NULL */)
{
unsigned int hash = HashKey(key);
int index = hash % m_Size;
for (size_t i = 0; i < m_Buckets[index].size(); i++)
{
if ( m_Buckets[index][i].key == key )
{
if (pfnOnInsertFailed)
pfnOnInsertFailed(const_cast<ValueT *>(&m_Buckets[index][i].value), const_cast<ValueT *>(&value));
return false;
}
}
m_Buckets[index].push_back({ key, value });
return true;
}
template <typename KeyT, class ValueT>
inline bool CHashTable<KeyT, ValueT>::Remove(const KeyT key, bool (*pfnOnRemove)(ValueT *pRemoveValue, ValueT *pUserValue) /* = NULL */, ValueT *pUserValue /* = NULL */)
{
unsigned int hash = HashKey(key);
int index = hash % m_Size;
for (size_t i = 0; i < m_Buckets[index].size(); i++)
{
if ( m_Buckets[index][i].key == key )
{
if ( pfnOnRemove && !pfnOnRemove(&m_Buckets[index][i].value, pUserValue) )
return false;
m_Buckets[index].erase( m_Buckets[index].begin() + i );
return true;
}
}
return false;
}
template <typename KeyT, class ValueT>
inline void CHashTable<KeyT, ValueT>::RemoveAll()
{
Clear();
}
template <typename KeyT, class ValueT>
inline void CHashTable<KeyT, ValueT>::Purge()
{
if ( !m_Buckets.empty() )
{
for (int i = 0; i < m_Size; i++)
{
m_Buckets[i].clear();
}
m_Buckets.clear();
}
}
template <typename KeyT, class ValueT>
inline void CHashTable<KeyT, ValueT>::Clear()
{
if ( !m_Buckets.empty() )
{
for (int i = 0; i < m_Size; i++)
{
m_Buckets[i].clear();
}
}
}
template <typename KeyT, class ValueT>
inline void CHashTable<KeyT, ValueT>::IterateEntries(void (*pfnCallback)(const KeyT key, ValueT &value))
{
if ( !m_Buckets.empty() )
{
for (int i = 0; i < m_Size; i++)
{
for (size_t j = 0; j < m_Buckets[i].size(); j++)
{
pfnCallback( m_Buckets[i][j].key, m_Buckets[i][j].value );
}
}
}
}
template <typename KeyT, class ValueT>
HashTableIterator_t CHashTable<KeyT, ValueT>::First(int ndxBucket)
{
if (ndxBucket >= 0 && ndxBucket < m_Size)
{
if ( !m_Buckets[ndxBucket].empty() )
{
return 0;
}
}
return -1;
}
template <typename KeyT, class ValueT>
HashTableIterator_t CHashTable<KeyT, ValueT>::Next(int ndxBucket, HashTableIterator_t iterator)
{
if (ndxBucket >= 0 && ndxBucket < m_Size)
{
if ( !m_Buckets[ndxBucket].empty() && ++iterator < (int)m_Buckets[ndxBucket].size() )
{
return iterator;
}
}
return -1;
}
template <typename KeyT, class ValueT>
KeyT &CHashTable<KeyT, ValueT>::KeyAt(int ndxBucket, HashTableIterator_t iterator)
{
return m_Buckets[ndxBucket][iterator].key;
}
template <typename KeyT, class ValueT>
ValueT &CHashTable<KeyT, ValueT>::ValueAt(int ndxBucket, HashTableIterator_t iterator)
{
return m_Buckets[ndxBucket][iterator].value;
}
template <typename KeyT, class ValueT>
bool CHashTable<KeyT, ValueT>::IsValidIterator(HashTableIterator_t iterator)
{
return (iterator != (HashTableIterator_t)-1);
}
template <typename KeyT, class ValueT>
__forceinline unsigned int CHashTable<KeyT, ValueT>::HashKey(const KeyT key) const
{
unsigned char *pKey = (unsigned char *)&key;
// Jenkins hash function
unsigned int i = 0;
unsigned int hash = 0;
while ( i != sizeof(KeyT) )
{
hash += pKey[i++];
hash += hash << 10;
hash ^= hash >> 6;
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
#endif // HASHTABLE_H