-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleHash.h
194 lines (174 loc) · 3.4 KB
/
SimpleHash.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
#ifndef SIMPLE_HASH_H
#define SIMPLE_HASH_H
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <new>
#define USE_MEMORY_MAP 0
#if USE_MEMORY_MAP
#include "MemoryMap.h"
#endif
template < class Key,
uint32_t hashTableSize = 8192, // *MUST* be a power of 2!
uint32_t hashTableEntries = 2048 >
class SimpleHash
{
public:
class HashEntry
{
public:
HashEntry(void)
{
mNext = NULL;
}
Key mKey;
HashEntry* mNext;
};
inline bool empty(void) const
{
return mHashTableCount ? true : false;
}
SimpleHash(void)
{
mMemoryMapFileName = NULL;
#if USE_MEMORY_MAP
mMemoryMap = NULL;
#endif
mEntries = NULL;
mHashTableCount = 0;
for (uint32_t i = 0; i < hashTableSize; i++)
{
mHashTable[i] = NULL;
}
}
inline void init(void)
{
assert( mMemoryMapFileName );
if ( mEntries == NULL )
{
#if USE_MEMORY_MAP
uint64_t size = sizeof(HashEntry)*hashTableEntries;
mMemoryMap = createMemoryMap(mMemoryMapFileName,size,true);
assert( mMemoryMap );
if ( mMemoryMap )
{
mEntries = (HashEntry *)mMemoryMap->getBaseAddress();
}
else
{
printf("Failed to allocate memory for hashmap %s. Exiting\r\n", mMemoryMapFileName);
exit(1);
}
#else
uint64_t size = sizeof(HashEntry)*hashTableEntries;
uint64_t mb = size / (1024*1024);
printf("Allocating %d MB of memory for %s\r\n", (uint32_t)mb, mMemoryMapFileName );
mEntries = (HashEntry *)malloc(size);
if ( mEntries == NULL )
{
printf("Failed to allocate memory for hashmap %s. Exiting\r\n", mMemoryMapFileName);
exit(1);
}
#endif
}
}
~SimpleHash(void)
{
#if USE_MEMORY_MAP
if ( mMemoryMap )
{
mMemoryMap->release();
}
#else
free(mEntries);
#endif
}
inline uint32_t getIndex(const Key *k) const
{
assert(k);
HashEntry *h = (HashEntry *)k;
return (uint32_t)(h-mEntries);
}
inline Key * getKey(uint32_t i) const
{
Key *ret = NULL;
assert( i < mHashTableCount );
if ( i < mHashTableCount )
{
ret = &mEntries[i].mKey;
}
return ret;
}
inline Key* find(const Key& key) const
{
Key* ret = NULL;
uint32_t hash = getHash(key);
HashEntry* h = mHashTable[hash];
while (h)
{
if (h->mKey == key)
{
ret = &h->mKey;
break;
}
h = h->mNext;
}
return ret;
}
// Inserts are not thread safe; use a mutex
inline Key * insert(const Key& key)
{
Key *ret = NULL;
init(); // allocate the entries table
if (mHashTableCount < hashTableEntries)
{
HashEntry* h = &mEntries[mHashTableCount];
new ( h ) HashEntry;
h->mKey = key;
ret = &h->mKey;
mHashTableCount++;
uint32_t hash = getHash(key);
if (mHashTable[hash])
{
HashEntry* next = mHashTable[hash];
mHashTable[hash] = h;
h->mNext = next;
}
else
{
mHashTable[hash] = h;
}
}
else
{
assert(0); // we should never run out of hash entries
printf("Overflowed hash table for hashmap %s.\n", mMemoryMapFileName);
exit(1);
}
return ret;
}
inline uint32_t size(void) const
{
return mHashTableCount;
}
void setMemoryMapFileName(const char *f)
{
mMemoryMapFileName = f;
}
private:
inline uint32_t getHash(const Key& key) const
{
return key.getHash() & (hashTableSize - 1);
}
HashEntry *mHashTable[hashTableSize];
unsigned int mHashTableCount;
HashEntry *mEntries;
const char *mMemoryMapFileName;
#if USE_MEMORY_MAP
MemoryMap *mMemoryMap;
#endif
};
#endif