-
Notifications
You must be signed in to change notification settings - Fork 13
/
GHashMap.hh
482 lines (424 loc) · 13.9 KB
/
GHashMap.hh
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/********************************************************************************
* Hash map class templates
*********************************************************************************/
#ifndef GHashMap_HH
#define GHashMap_HH
#include "GBase.h"
#include "khashl.hh"
#include <type_traits>
#include <typeinfo>
#define XXH_INLINE_ALL 1
#include "xxhash.h"
#include "wyhash.h"
template <typename K> struct GHashKey_xxHash32 { //K generic (class, primitive, pointer except const char* )
//template <typename T=K> inline typename std::enable_if< std::is_trivial<T>::value, uint32_t>::type
uint32_t operator()(const K& s) const { //only works for trivial types!
static_assert(std::is_trivial<K>::value, "Error: cannot use this for non-trivial types!\n");
return XXH32((const void *) &s, sizeof(K), 0);
}
};
template <> struct GHashKey_xxHash32<const char*> {
inline uint32_t operator()(const char* s) const {
return XXH32(s, strlen(s), 0);
}
};
template <typename K> struct GHashKey_xxHash { //K generic (class, primitive, pointer except const char* )
//template <typename T=K> inline typename std::enable_if< std::is_trivial<T>::value, uint32_t>::type
uint64_t operator()(const K& s) const { //only works for trivial types!
static_assert(std::is_trivial<K>::value, "Error: cannot use this for non-trivial types!\n");
return XXH64((const void *) &s, sizeof(K), 0);
}
};
template <> struct GHashKey_xxHash<const char*> {
inline uint32_t operator()(const char* s) const {
return XXH64(s, strlen(s), 0);
}
};
template <typename K> struct GHashKey_wyHash { //K generic (class, primitive, pointer except const char* )
//template <typename T=K> inline typename std::enable_if< std::is_trivial<T>::value, uint32_t>::type
uint64_t operator()(const K& s) const { //only works for trivial types!
static_assert(std::is_trivial<K>::value, "Error: cannot use this for non-trivial types!\n");
return wyhash((const void *) &s, sizeof(K), 0, _wyp);
}
};
template <> struct GHashKey_wyHash<const char*> {
inline uint32_t operator()(const char* s) const {
return wyhash(s, strlen(s), 0, _wyp);
}
};
template <typename K> struct GHashKey_Eq { //K is a type having the == operator defined
inline bool operator()(const K& x, const K& y) const {
return (x == y); //requires == operator to be defined for K
}
};
template <> struct GHashKey_Eq<const char*> {
inline bool operator()(const char* x, const char* y) const {
return (strcmp(x, y) == 0);
}
};
// GHashSet<KType> never makes a deep copy of a char* key, it only stores the pointer
// - for pointer keys like char*, key allocation must be managed separately (and should always survive the GHashSet)
template <typename K=const char*, class Hash=GHashKey_wyHash<K>, class Eq=GHashKey_Eq<K>, typename khInt_t=uint64_t >
class GHashSet: public std::conditional< is_char_ptr<K>::value,
klib::KHashSetCached< K, Hash, Eq, khInt_t >,
klib::KHashSet< K, Hash, Eq, khInt_t > >::type {
protected:
khInt_t i_iter=0;
public:
inline khInt_t Add(const K ky) { // return -1 if the key already exists
int absent=-1;
khInt_t i=this->put(ky, &absent);
if (absent==1) //key was actually added
return i;
return -1;
}
inline khInt_t Remove(K ky) { //return index being removed, or -1 if no such key exists
khInt_t i=this->get(ky);
if (i!=this->end()) {
this->del(i);
return i;
}
return -1;
}
inline void Clear() {
this->clear(); //does not shrink
}
inline void Reset() {
this->clear();
GFREE(this->used); GFREE(this->keys);
this->bits=0; this->count=0;
}
~GHashSet() {
this->Reset();
}
inline bool operator[](K ky) { //RH only (read-only), cannot assign (use Add instead)
return (this->get(ky)!=this->end());
}
inline bool hasKey(K ky) {
return (this->get(ky)!=this->end());
}
int Find(K ky) {//return internal slot location if found,
// or -1 if not found
khInt_t r=this->get(ky);
if (r==this->end()) return -1;
return (int)r;
}
void startIterate() { //iterator-like initialization
i_iter=0;
}
K* Next() {
//returns a pointer to next valid key in the table (NULL if no more)
if (this->count==0) return NULL;
uint32_t nb=this->n_buckets();
while (i_iter<nb && !this->_used(i_iter)) i_iter++;
if (i_iter==nb) return NULL;
K* k=&(this->key(i_iter-1));
++i_iter;
return k;
}
inline uint32_t Count() { return this->count; }
};
// GStrSet always allocates a new copy of each added string;
// if you don't want that, just use GHashSet<const char*> instead and manage the key allocation separately
template <class Hash=GHashKey_wyHash<const char*>, class Eq=GHashKey_Eq<const char*>, typename khInt_t=uint64_t>
class GStrSet: public GHashSet<const char*, Hash, Eq, khInt_t> {
protected:
const char* lastKey=NULL;
public:
inline int Add(const char* ky) { // return -1 if the key already exists
int absent=-1;
khInt_t i=this->put(ky, &absent);
if (absent==1) {//key was actually added
const char* s=Gstrdup(ky);
this->key(i)=s; //store a copy of the key string
lastKey=s;
return i;
}
//key was already there
return -1;
}
inline const char* getLastKey() { return lastKey; }
int Remove(const char* ky) { //return index being removed, or -1 if no such key exists
khInt_t i=this->get(ky);
if (i!=this->end()) {
const char* s=this->key(i);
if (s==lastKey) lastKey=NULL;
GFREE(s); //free string copy
this->del(i);
return i;
}
return -1;
}
inline void Clear() {
khInt_t nb=this->n_buckets();
for (khInt_t i = 0; i != nb; ++i) {
if (!this->_used(i)) continue;
//deallocate string copy
GFREE(this->key(i));
}
lastKey=NULL;
this->clear(); //does not shrink !
}
inline void Reset() {
this->Clear();
GFREE(this->used); GFREE(this->keys);
lastKey=NULL;
this->bits=0; this->count=0;
}
~GStrSet() {
this->Reset();
}
};
// Generic hash map where keys and values can be of any type
// Note: keys are always copied (shared) as simple value, there is no deep copy/allocation for pointers
// so pointer keys must me managed separately
// Note: pointer values are automatically deallocated on container destruction by default,
// use GHashMap(false) to disable that when V is a pointer
template <class K, class V, class Hash=GHashKey_wyHash<K>, class Eq=GHashKey_Eq<K>, typename khInt_t=uint64_t>
class GHashMap:public std::conditional< is_char_ptr<K>::value,
klib::KHashMapCached< K, V, Hash, Eq, khInt_t>,
klib::KHashMap< K, V, Hash, Eq, khInt_t> >::type {
protected:
khInt_t i_iter=0;
bool freeItems=false;
public:
//---- these should be reimplemented for GHash
inline int Add(const K ky, const V val) { // if a key does not exist allocate a copy of the key
// return -1 if the key already exists
int absent=-1;
khInt_t i=this->put(ky, &absent);
if (absent==1) { //key was actually added
this->value(i)=val; //value is always copied
return i;
}
return -1;
}
template <typename T=V> inline
typename std::enable_if< std::is_pointer<T>::value, int>::type
Remove(K ky) { //return index being removed
khInt_t i=this->get(ky);
if (i!=this->end()) {
if (freeItems) delete this->value(i);
this->del(i);
return i;
}
return -1;
}
template <typename T=V> inline
typename std::enable_if< !std::is_pointer<T>::value, int>::type
Remove(K ky) { //return index being removed
khInt_t i=this->get(ky);
if (i!=this->end()) {
this->del(i);
return i;
}
return -1;
}
template <typename T=V> inline
typename std::enable_if< std::is_pointer<T>::value, void>::type
Clear() {
if (!freeItems) {
this->clear(); //does not shrink !
return;
}
khInt_t nb=this->n_buckets();
for (khInt_t i = 0; i != nb; ++i) {
if (!this->_used(i)) continue;
if (freeItems) delete this->value(i);
}
this->clear();
}
template <typename T=V> inline
typename std::enable_if< !std::is_pointer<T>::value, void>::type
Clear() { this->clear(); }
inline void Reset() {
this->Clear();
GFREE(this->used); GFREE(this->keys);
this->bits=0; this->count=0;
}
~GHashMap() {
this->Reset();
}
// -- these can be shared with GHash:
GHashMap(bool doFree=std::is_pointer<V>::value):freeItems(doFree) {
static_assert(std::is_trivial<K>::value,
"Error: cannot use this for non-trivial types!\n");
if (!std::is_pointer<V>::value) doFree=false;
};
//return pointer to stored value if found, NULL otherwise
// if the stored value is a pointer, it's going to be a pointer to that
template <typename T=V> inline
typename std::enable_if< std::is_pointer<T>::value, T>::type
Find(const K ky) {
khInt_t r=this->get(ky);
if (r==this->end()) return NULL;
return this->value(r);
}
template <typename T=V> inline
typename std::enable_if< !std::is_pointer<T>::value, T*>::type
Find(const K ky) {
khInt_t r=this->get(ky);
if (r==this->end()) return NULL;
return &(this->value(r));
}
//-- operator[] should be defined just like Find?
template <typename T=V> inline
typename std::enable_if< std::is_pointer<T>::value, T>::type
operator[](const K ky) {
khInt_t r=this->get(ky);
if (r==this->end()) return NULL;
return this->value(r);
}
template <typename T=V> inline
typename std::enable_if< !std::is_pointer<T>::value, T*>::type
operator[](const K ky) {
khInt_t r=this->get(ky);
if (r==this->end()) return NULL;
return &(this->value(r));
}
inline bool hasKey(K ky) {
return (this->get(ky)!=this->end());
}
inline void startIterate() { //iterator-like initialization
i_iter=0;
}
template <typename T=K> inline
typename std::enable_if< !std::is_pointer<T>::value, T*>::type
Next (V& val) {
//returns a pointer to next key entry in the table (NULL if no more)
if (this->count==0) return NULL;
khInt_t nb=this->n_buckets();
while (i_iter<nb && !this->_used(i_iter)) i_iter++;
if (i_iter==nb) return NULL;
val=this->value(i_iter);
K* k=&(this->key(i_iter));
++i_iter;
return k;
}
template <typename T=K> inline
typename std::enable_if< std::is_pointer<T>::value, T>::type
Next (V& val) {
//returns a pointer to next key entry in the table (NULL if no more)
if (this->count==0) return NULL;
khInt_t nb=this->n_buckets();
while (i_iter<nb && !this->_used(i_iter)) i_iter++;
if (i_iter==nb) return NULL;
val=this->value(i_iter);
K k = this->key(i_iter);
++i_iter;
return k;
}
template <typename T=V> inline
typename std::enable_if< !std::is_pointer<T>::value, T*>::type
NextData () {
//returns a pointer to next key entry in the table (NULL if no more)
if (this->count==0) return NULL;
khInt_t nb=this->n_buckets();
while (i_iter<nb && !this->_used(i_iter)) i_iter++;
if (i_iter==nb) return NULL;
T* val=&(this->value(i_iter));
++i_iter;
return val;
}
template <typename T=V> inline
typename std::enable_if< std::is_pointer<T>::value, T>::type
NextData () {
//returns a pointer to next key entry in the table (NULL if no more)
if (this->count==0) return NULL;
khInt_t nb=this->n_buckets();
while (i_iter<nb && !this->_used(i_iter)) i_iter++;
if (i_iter==nb) return NULL;
T val=this->value(i_iter);
++i_iter;
return val;
}
inline uint32_t Count() { return this->count; }
};
// GHash<VType>(doFree=true) -- basic string hashmap
// Note: this hash map always makes a copy of the string key which can be costly
// use GHashMap<const char*, VTYPE> for a faster alternative
template <class V, class Hash=GHashKey_wyHash<const char*>, class Eq=GHashKey_Eq<const char*>, typename khInt_t=uint64_t >
class GHash:public GHashMap<const char*, V, Hash, Eq, khInt_t> {
protected:
const char* lastKey=NULL;
public:
GHash(bool doFree=true) {
this->freeItems=doFree;
};
//---- these should be now reimplemented
inline int Add(const char* ky, const V val) { // if a key does not exist allocate a copy of the key
// return -1 if the key already exists
int absent=-1;
khInt_t i=this->put(ky, &absent);
if (absent==1) { //key was actually added
const char* s=Gstrdup(ky);
this->key(i)=s; //store a copy of the key string
lastKey=s;
this->value(i)=val; //value is always copied
return i;
}
return -1;
}
inline const char* getLastKey() { return lastKey; }
template <typename T=V> inline
typename std::enable_if< std::is_pointer<T>::value, int>::type
Remove(const char* ky) { //return index being removed
khInt_t i=this->get(ky);
if (i!=this->end()) {
const char* s=this->key(i);
if (s==lastKey) lastKey=NULL;
GFREE(s); //free string copy
if (this->freeItems) delete this->value(i);
this->del(i);
return i;
}
return -1;
}
template <typename T=V> inline
typename std::enable_if< !std::is_pointer<T>::value, int>::type
Remove(const char* ky) { //return index being removed
khInt_t i=this->get(ky);
if (i!=this->end()) {
const char* s=this->key(i);
if (s==lastKey) lastKey=NULL;
GFREE(s); //free string copy
this->del(i);
return i;
}
return -1;
}
template <typename T=V> inline
typename std::enable_if< std::is_pointer<T>::value, void>::type
Clear() {
khInt_t nb=this->n_buckets();
for (khInt_t i = 0; i != nb; ++i) {
if (!this->_used(i)) continue;
if (this->freeItems) delete this->value(i);
GFREE(this->key(i));
}
lastKey=NULL;
this->clear();
}
template <typename T=V> inline
typename std::enable_if< !std::is_pointer<T>::value, void>::type
Clear() {
khInt_t nb=this->n_buckets();
for (khInt_t i = 0; i != nb; ++i) {
if (! this->_used(i) ) continue;
GFREE(this->key(i));
}
lastKey=NULL;
this->clear();
}
inline void Reset() {
this->Clear();
GFREE(this->used); GFREE(this->keys);
this->bits=0; this->count=0;
}
~GHash() {
this->Reset();
}
};
template<typename T>
using GIntHash = GHashMap<int, T, GHashKey_xxHash32<int>, GHashKey_Eq<int>, uint32_t>;
#endif