forked from jfmrod/MAPseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ekmerhashmap.h
604 lines (489 loc) · 15 KB
/
ekmerhashmap.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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#ifndef EKMERHASHMAP_H
#define EKMERHASHMAP_H
#include "ekmerhashmap_dec.h"
#include <eutils/logger.h>
const size_t CSTRHASH_INIT_MASK=0xFF;
template <int KSIZE,class T,size_t (*hashfunc)(const char*,int)>
ekmerhashmap<KSIZE,T,hashfunc>::iter::iter(): hashmap(0x00), hitem(0x00), bucket(0) {}
template <int KSIZE,class T,size_t (*hashfunc)(const char*,int)>
typename ekmerhashmap<KSIZE,T,hashfunc>::iter& ekmerhashmap<KSIZE,T,hashfunc>::iter::operator++()
{
hitem=hitem->next;
if (hitem) return(*this);
++bucket;
while (hashmap->_hashitems[bucket]==0x00 && bucket < hashmap->_hashmask+1) ++bucket;
if (bucket < hashmap->_hashmask+1)
hitem=hashmap->_hashitems[bucket];
return(*this);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
const char *ekmerhashmap<KSIZE,T,hashfunc>::iter::key() const
{
lddieif(hitem==0x00,"trying to access end iterator");
return(hitem->key);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
size_t ekmerhashmap<KSIZE,T,hashfunc>::iter::hash() const
{
lddieif(hitem==0x00,"trying to access end iterator");
return(hitem->hash);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
T& ekmerhashmap<KSIZE,T,hashfunc>::iter::value() const
{
lddieif(hitem==0x00,"trying to access end iterator");
return(*hitem->value);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
T& ekmerhashmap<KSIZE,T,hashfunc>::iter::operator*() const
{
lddieif(hitem==0x00,"trying to access end iterator");
return(*hitem->value);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
T* ekmerhashmap<KSIZE,T,hashfunc>::iter::operator->() const
{
lddieif(hitem==0x00,"trying to access end iterator");
return(hitem->value);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
bool ekmerhashmap<KSIZE,T,hashfunc>::iter::operator==(const ekmerhashmap<KSIZE,T,hashfunc>::iter& i) const
{
return(hashmap==i.hashmap && hitem==i.hitem);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
bool ekmerhashmap<KSIZE,T,hashfunc>::iter::operator!=(const ekmerhashmap<KSIZE,T,hashfunc>::iter& i) const
{
return(hashmap!=i.hashmap || hitem!=i.hitem);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
typename ekmerhashmap<KSIZE,T,hashfunc>::iter& ekmerhashmap<KSIZE,T,hashfunc>::iter::operator=(const ekmerhashmap<KSIZE,T,hashfunc>::iter& i)
{
hashmap=i.hashmap;
bucket=i.bucket;
hitem=i.hitem;
return(*this);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
ekmerhashmap<KSIZE,T,hashfunc>::ekmerhashmap(): count(0)
{
_hashmask = CSTRHASH_INIT_MASK;
_hashitems=new ekmerhashitem<KSIZE,T>*[_hashmask+1];
size_t i;
for (i=0; i<_hashmask+1; ++i)
_hashitems[i]=0x00;
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
ekmerhashmap<KSIZE,T,hashfunc>::ekmerhashmap(const ekmerhashmap<KSIZE,T,hashfunc>& oldhm): count(oldhm.count)
{
_hashmask = oldhm._hashmask;
_hashitems=new ekmerhashitem<KSIZE,T>*[_hashmask+1];
ekmerhashitem<KSIZE,T> *oldhmitem;
size_t i;
for (i=0; i<_hashmask+1; ++i){
_hashitems[i]=0x00;
for (oldhmitem=oldhm._hashitems[i]; oldhmitem!=0x00; oldhmitem=oldhmitem->next)
_hashitems[i]=new ekmerhashitem<KSIZE,T>(oldhmitem->key,oldhmitem->value,_hashitems[i],oldhmitem->hash);
}
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
ekmerhashmap<KSIZE,T,hashfunc>::~ekmerhashmap()
{
clear();
delete[] _hashitems;
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
typename ekmerhashmap<KSIZE,T,hashfunc>::iter ekmerhashmap<KSIZE,T,hashfunc>::get(const char * key) const
{
typename ekmerhashmap<KSIZE,T,hashfunc>::iter it;
it.hashmap=this;
it.bucket=hash(key) & _hashmask;
it.hitem=_hashitems[it.bucket];
while (it.hitem!=0x00){
if (memcmp(key,it.hitem->key,KSIZE)==0) // there is no collision
return(it);
it.hitem=it.hitem->next;
}
return(end());
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
typename ekmerhashmap<KSIZE,T,hashfunc>::iter ekmerhashmap<KSIZE,T,hashfunc>::gethash(size_t hash,const char * key) const
{
typename ekmerhashmap<KSIZE,T,hashfunc>::iter it;
it.hashmap=this;
it.bucket=hash & _hashmask;
it.hitem=_hashitems[it.bucket];
while (it.hitem!=0x00){
if (memcmp(key,it.hitem->key,KSIZE)==0) // there is no collision
return(it);
it.hitem=it.hitem->next;
}
return(end());
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
typename ekmerhashmap<KSIZE,T,hashfunc>::iter ekmerhashmap<KSIZE,T,hashfunc>::addhash(size_t hash,const char * key,const T& value)
{
typename ekmerhashmap<KSIZE,T,hashfunc>::iter it;
it.hashmap=this;
it.bucket=hash & _hashmask;
it.hitem=_hashitems[it.bucket];
while (it.hitem!=0x00){
if (memcmp(key,it.hitem->key,KSIZE)==0){
it.hitem->value=new T(value);
return(it);
}
it.hitem=it.hitem->next;
}
if (size() > (3u*(_hashmask+1))>>2u) resizehash();
it.bucket=hash & _hashmask;
// non existent value
++count;
_hashitems[it.bucket]=new ekmerhashitem<KSIZE,T>(key,new T(value),_hashitems[it.bucket],hash);
it.hitem=_hashitems[it.bucket];
return(it);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
typename ekmerhashmap<KSIZE,T,hashfunc>::iter ekmerhashmap<KSIZE,T,hashfunc>::begin() const
{
typename ekmerhashmap<KSIZE,T,hashfunc>::iter i;
i.hashmap=this;
i.bucket=0;
i.hitem=0x00;
while (_hashitems[i.bucket]==0x00 && i.bucket<_hashmask+1) ++i.bucket;
if (i.bucket==_hashmask+1)
return(end());
i.hitem=_hashitems[i.bucket];
return(i);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
typename ekmerhashmap<KSIZE,T,hashfunc>::iter ekmerhashmap<KSIZE,T,hashfunc>::end() const
{
typename ekmerhashmap<KSIZE,T,hashfunc>::iter i;
i.hashmap=this;
i.bucket=0;
i.hitem=0x00;
return(i);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
void ekmerhashmap<KSIZE,T,hashfunc>::reserve(size_t i)
{
size_t a;
size_t c=1;
a=0x01;
while (i>0){
i=i>>1;
a=(a<<1)|0x01;
++c;
}
if (c>=sizeof(size_t)*8){
lwarn("reached limit of hash table index size");
a=0x8000000000000000u-0x01u;
}
resizehash(a);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
void ekmerhashmap<KSIZE,T,hashfunc>::resizehash(size_t newmask) const
{
size_t thashmask;
ekmerhashitem<KSIZE,T> **thashitems;
if (newmask < _hashmask) return;
ldinfo("resizing hash table");
if (newmask==0)
thashmask = (_hashmask << 1)|0x01;
else
thashmask = newmask;
thashitems=new ekmerhashitem<KSIZE,T>*[thashmask+1u];
ldieif(thashitems==0x00,"unable to allocate memory for hashmap");
size_t i;
for (i=0; i<thashmask+1u; ++i)
thashitems[i]=0x00;
size_t khash;
ekmerhashitem<KSIZE,T>* hitem;
typename ekmerhashmap<KSIZE,T,hashfunc>::iter it;
for (it=begin(); it!=end(); ++it){
khash = it.hash() & thashmask;
hitem = it.hitem; //gethashitem(khash & _hashmask,it.key());
if (hitem->prev)
hitem->prev->next=hitem->next;
else
_hashitems[khash&_hashmask]=hitem->next;
if (hitem->next)
hitem->next->prev=hitem->prev;
hitem->prev=0x00;
hitem->next=thashitems[khash];
thashitems[khash]=hitem;
if (hitem->next)
hitem->next->prev=hitem;
}
delete[] _hashitems;
_hashmask=thashmask;
_hashitems=thashitems;
ldinfo("finished resize");
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
ekmerhashitem<KSIZE,T>* ekmerhashmap<KSIZE,T,hashfunc>::gethashitem(size_t khash,const char *key) const
{
ekmerhashitem<KSIZE,T>* hitem;
hitem = _hashitems[khash];
while (hitem != 0x00){
if (memcmp(hitem->key,key,KSIZE)==0)
return(hitem);
hitem=hitem->next;
}
lderror("ekmerhashmap: did not find key");
return(0x00);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
void ekmerhashmap<KSIZE,T,hashfunc>::clear()
{
size_t i;
ekmerhashitem<KSIZE,T> *hitem;
for (i=0; i<_hashmask+1; ++i){
while (_hashitems[i]){
hitem=_hashitems[i];
_hashitems[i]=hitem->next;
delete hitem->value;
delete hitem;
}
}
count=0;
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
bool ekmerhashmap<KSIZE,T,hashfunc>::exists(const char * key) const
{
size_t i;
ekmerhashitem<KSIZE,T>* hitem;
size_t um_hash=hash(key);
i=um_hash & _hashmask;
hitem=_hashitems[i];
while (hitem!=0x00){
if (memcmp(key,hitem->key,KSIZE)==0)
return(true);
hitem=hitem->next;
}
// non existent value
return(false);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
void ekmerhashmap<KSIZE,T,hashfunc>::erase(const typename ekmerhashmap<KSIZE,T,hashfunc>::iter& it)
{
ldieif(it.hitem==0x00,"trying to delete empty iterator");
if (it.hitem->prev) it.hitem->prev->next=it.hitem->next;
else _hashitems[it.bucket]=it.hitem->next;
if (it.hitem->next) it.hitem->next->prev=it.hitem->prev;
delete it.hitem->value;
delete it.hitem;
// it.hitem=0x00;
--count;
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
void ekmerhashmap<KSIZE,T,hashfunc>::erase(const char * key)
{
size_t i;
ekmerhashitem<KSIZE,T>* hitem;
size_t um_hash=hash(key);
i=um_hash & _hashmask;
hitem=_hashitems[i];
size_t j;
while (hitem!=0x00){
if (memcmp(key,hitem->key,KSIZE)==0){
if (hitem->prev) hitem->prev->next=hitem->next;
else _hashitems[i]=hitem->next;
if (hitem->next) hitem->next->prev=hitem->prev;
delete hitem->value;
delete hitem;
--count;
return;
}
hitem=hitem->next;
}
lddie("tried to delete key from hashmap that does not exist");
// non existent value
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
ekmerhashmap<KSIZE,T,hashfunc>& ekmerhashmap<KSIZE,T,hashfunc>::operator+=(const ekmerhashmap<KSIZE,T,hashfunc>& hm)
{
size_t i;
for (i=0; i<hm.size(); ++i)
add(hm.keys(i),hm.values(hm.keys(i)));
return(*this);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
T& ekmerhashmap<KSIZE,T,hashfunc>::addref(const char * key,T* value)
{
size_t i;
ekmerhashitem<KSIZE,T>* hitem;
if (size() > (3u*(_hashmask+1))>>2u) resizehash();
size_t um_hash=hash(key);
i=um_hash & _hashmask;
hitem=_hashitems[i];
while (hitem!=0x00){
if (memcmp(key,hitem->key,KSIZE)==0){ // there is no collision
hitem->value = value;
return(*hitem->value);
}
hitem=hitem->next;
}
// non existent value
++count;
_hashitems[i]=new ekmerhashitem<KSIZE,T>(key,value,_hashitems[i],um_hash);
return(*_hashitems[i]->value);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
T& ekmerhashmap<KSIZE,T,hashfunc>::add(const char * key,const T& value)
{
size_t i;
ekmerhashitem<KSIZE,T>* hitem;
if (size() > (3u*(_hashmask+1))>>2u) resizehash();
size_t um_hash=hash(key);
i=um_hash & _hashmask;
hitem=_hashitems[i];
while (hitem!=0x00){
if (memcmp(key,hitem->key,KSIZE)==0){ // there is no collision
hitem->value=new T(value);
return(*hitem->value);
}
hitem=hitem->next;
}
// non existent value
++count;
_hashitems[i]=new ekmerhashitem<KSIZE,T>(key,new T(value),_hashitems[i],um_hash);
return(*_hashitems[i]->value);
/*
operator[](key) = value;
return(operator[](key));
*/
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
const T& ekmerhashmap<KSIZE,T,hashfunc>::operator[](const char * key) const
{
size_t i;
ekmerhashitem<KSIZE,T>* hitem;
if (size() > (3u*(_hashmask+1))>>2u) resizehash();
size_t um_hash=hash(key);
i=um_hash & _hashmask;
hitem=_hashitems[i];
while (hitem!=0x00){
if (memcmp(key,hitem->key,KSIZE)==0) // there is no collision
return(*hitem->value);
hitem=hitem->next;
}
// non existent value
// ++count;
_hashitems[i]=new ekmerhashitem<KSIZE,T>(key,new T,_hashitems[i],um_hash);
return(*_hashitems[i]->value);
}
/*
template <int KSIZE,class T>
const T& ekmerhashmap<KSIZE,T,hashfunc>::operator[](int ind) const
{
int i;
ehashitem<T>* hitem;
lddieif(ind > _keys.size(),"ekmerhashmap: index out of bounds");
i=hash(_keys.at(ind));
hitem=_hashitems[i];
while (hitem!=0x00){
if (_keys.at(ind) == hitem->key) // there is no collision
return(*hitem->value);
hitem=hitem->next;
}
ldie("ekmerhashmap: index out of bounds: "+estr(ind));
// return(*(T*)0x00);
}
*/
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
T& ekmerhashmap<KSIZE,T,hashfunc>::operator[](const char * key)
{
size_t i;
ekmerhashitem<KSIZE,T>* hitem;
if (size() > (3u*(_hashmask+1))>>2u) resizehash();
size_t um_hash=hash(key);
i=um_hash & _hashmask;
hitem=_hashitems[i];
while (hitem!=0x00){
if (memcmp(key,hitem->key,KSIZE)==0) // there is no collision
return(*hitem->value);
hitem=hitem->next;
}
// lerror("key not found");
// throw "key not found";
// non existent value
++count;
_hashitems[i]=new ekmerhashitem<KSIZE,T>(key,new T,_hashitems[i],um_hash);
return(*_hashitems[i]->value);
}
//#pragma GCC diagnostic ignored "-Wreturn-type"
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
const T& ekmerhashmap<KSIZE,T,hashfunc>::values(const char * key) const
{
size_t i;
ekmerhashitem<KSIZE,T>* hitem;
if (size() > (3u*(_hashmask+1))>>2u) resizehash();
size_t um_hash=hash(key);
i=um_hash & _hashmask;
hitem=_hashitems[i];
while (hitem!=0x00){
if (memcmp(key,hitem->key,KSIZE)==0) // there is no collision
return(*hitem->value);
hitem=hitem->next;
}
lerror("ekmerhashmap: key not found");
throw "ekmerhashmap: key not found";
// return(*(T*)0x00);
}
template <int KSIZE,class T,size_t (*hashfunc)(const char *,int)>
T& ekmerhashmap<KSIZE,T,hashfunc>::values(const char * key)
{
size_t i;
ekmerhashitem<KSIZE,T>* hitem;
if (size() > (3u*(_hashmask+1))>>2u) resizehash();
size_t um_hash=hash(key);
i=um_hash & _hashmask;
hitem=_hashitems[i];
while (hitem!=0x00){
if (memcmp(key,hitem->key,KSIZE)==0) // there is no collision
return(*hitem->value);
hitem=hitem->next;
}
lerror("ekmerhashmap: key not found");
throw "ekmerhashmap: key not found";
// return(*(T*)0x00);
}
/*
template <int KSIZE,class T>
T& ekmerhashmap<KSIZE,T,hashfunc>::operator[](int ind)
{
int i;
ehashitem<T>* hitem;
i=hash(_keys.at(ind));
hitem=_hashitems[i];
while (hitem!=0x00){
if (_keys.at(ind) == hitem->key) // there is no collision
return(*hitem->value);
hitem=hitem->next;
}
ldie("ekmerhashmap: index out of bounds: "+estr(ind));
return(*(T*)0x00);
}
//#pragma GCC diagnostic warning "-Wreturn-type"
*/
/*
template <unsigned int (*hashfunc)(const evar&)>
void ekmerhashmap<evar,evar,hashfunc>::addvar(evar& key,evar& var)
{
add(key,var);
}
template <unsigned int (*hashfunc)(const evar&)>
evar ekmerhashmap<evar,evar,hashfunc>::getvar(int i) const
{
return(evar());
// return(values(i));
}
template <unsigned int (*hashfunc)(const evar&)>
evar ekmerhashmap<evar,evar,hashfunc>::getvarkey(int i) const
{
return(evar());
// return(keys(i));
}
*/
#endif