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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
|
/** @file
Parser: hash class decl.
Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
*/
#ifndef PA_HASH_H
#define PA_HASH_H
#define IDENT_PA_HASH_H "$Id: pa_hash.h,v 1.105 2025/01/06 19:47:18 moko Exp $"
#include "pa_memory.h"
#include "pa_types.h"
#include "pa_string.h"
const int HASH_ALLOCATES_COUNT=29;
// nearest primes to 5 * 2^n
static uint Hash_allocates[HASH_ALLOCATES_COUNT]={
5, 11, 19, 41, 79, 163, 317, 641, 1279, 2557, 5119,
10243, 20479, 40961, 81919, 163841, 327673, 655357,
1310719, 2621447, 5242883, 10485767, 20971529, 41943049,
83886091, 167772161, 335544323, 671088637, 1342177283};
/// useful generic hash function
inline void generic_hash_code(uint& result, char c) {
result=(result<<4)+c;
if(uint g=(result&0xF0000000)) {
result=result^(g>>24);
result=result^g;
}
}
/// useful generic hash function
inline void generic_hash_code(uint& result, const char* s) {
while(char c=*s++) {
result=(result<<4)+c;
if(uint g=(result&0xF0000000)) {
result=result^(g>>24);
result=result^g;
}
}
}
/// useful generic hash function
inline void generic_hash_code(uint& result, const char* buf, size_t size) {
const char* end=buf+size;
while(buf<end) {
result=(result<<4)+*buf++;
if(uint g=(result&0xF0000000)) {
result=result^(g>>24);
result=result^g;
}
}
}
/// simple hash code of int. used by EXIF mapping
inline uint hash_code(int self) {
uint result=0;
generic_hash_code(result, (const char*)&self, sizeof(self));
return result;
}
#endif // PA_HASH_H
#ifndef PA_HASH_CLASS
#define PA_HASH_CLASS
/**
Simple hash.
Automatically rehashed when almost is_full.
Contains no 0 values.
get returning 0 means there were no such.
"put value 0" means "remove"
*/
#ifdef HASH_ORDER
#undef HASH
#undef HASH_STRING
#undef HASH_NEW_PAIR
#undef HASH_ORDER_CLEAR
#undef HASH_FOR_EACH
#define HASH OrderedHash
#define HASH_STRING OrderedHashString
#define HASH_NEW_PAIR(code, key, value) *ref=new Pair(code, key, value, *ref, this->last); this->last=&((*ref)->next)
#define HASH_ORDER_CLEAR() first=0; last=&first
#define HASH_FOR_EACH \
for(Pair *pair=this->first; pair; pair=pair->next)
#else
#define HASH Hash
#define HASH_STRING HashString
#define HASH_NEW_PAIR(code, key, value) *ref=new Pair(code, key, value, *ref)
#define HASH_ORDER_CLEAR()
#define HASH_FOR_EACH \
Pair **ref=this->refs; \
for(int index=0; index<this->allocated; index++) \
for(Pair *pair=*ref++; pair; pair=pair->link)
#endif
template<typename K, typename V> class HASH: public PA_Object {
protected:
class Pair;
public:
typedef K key_type;
typedef V value_type;
HASH() {
allocated=Hash_allocates[allocates_index=0];
fpairs_count=fused_refs=0;
refs=new(PointerGC) Pair*[allocated];
HASH_ORDER_CLEAR();
}
HASH(const HASH& source) {
allocates_index=source.allocates_index;
allocated=source.allocated;
fused_refs=source.fused_refs;
fpairs_count=source.fpairs_count;
refs=new(PointerGC) Pair*[allocated];
// clone & rehash
#ifdef HASH_ORDER
HASH_ORDER_CLEAR();
for(Pair *pair=source.first; pair; pair=pair->next)
{
uint index=pair->code%allocated;
Pair **ref=&refs[index];
HASH_NEW_PAIR(pair->code, pair->key, pair->value);
}
#else
for(int i=0; i<source.allocated; i++)
for(Pair *pair=source.refs[i]; pair; pair=pair->link)
{
Pair **ref=&refs[i];
HASH_NEW_PAIR(pair->code, pair->key, pair->value);
}
#endif
}
#ifdef USE_DESTRUCTORS
~HASH() {
Pair **ref=refs;
for(int index=0; index<allocated; index++)
for(Pair *pair=*ref++; pair;){
Pair *next=pair->link;
delete pair;
pair=next;
}
operator delete[](refs);
}
#endif
/// put a [value] under the [key] @returns existed or not
bool put(K key, V value) {
if(!value) {
remove(key);
return false;
}
if(is_full())
expand();
uint code=hash_code(key);
uint index=code%allocated;
Pair **ref=&refs[index];
for(Pair *pair=*ref; pair; pair=pair->link)
if(pair->code==code && pair->key==key) {
// found a pair with the same key
pair->value=value;
return true;
}
// proper pair not found -- create&link_in new pair
if(!*ref) // root cell were fused_refs?
fused_refs++; // not, we'll use it and record the fact
HASH_NEW_PAIR(code, key, value);
fpairs_count++;
return false;
}
/// remove the [key] @returns existed or not
bool remove(K key) {
uint code=hash_code(key);
uint index=code%allocated;
for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link){
Pair *pair=*ref;
if(pair->code==code && pair->key==key) {
// found a pair with the same key
Pair *next=pair->link;
#ifdef HASH_ORDER
*(pair->prev)=pair->next;
if(pair->next)
pair->next->prev=pair->prev;
else
last=pair->prev;
#endif
*ref=next;
--fpairs_count;
return true;
}
}
return false;
}
/// return true if key exists
bool contains(K key){
uint code=hash_code(key);
uint index=code%allocated;
for(Pair *pair=refs[index]; pair; pair=pair->link){
if(pair->code==code && pair->key==key)
return true;
}
return false;
}
/// get associated [value] by the [key]
V get(K key) const {
uint code=hash_code(key);
uint index=code%allocated;
for(Pair *pair=refs[index]; pair; pair=pair->link)
if(pair->code==code && pair->key==key)
return pair->value;
return V(0);
}
#ifdef HASH_ORDER
String::Body first_key() const {
#ifdef HASH_CODE_CACHING
return (first) ? String::Body(first->key, first->code) : String::Body();
#else
return (first) ? first->key : String::Body();
#endif
}
V first_value() const {
return (first) ? first->value : V(0);
}
inline Pair* last_pair() const {
return (fpairs_count) ? (Pair*)((char *)last - offsetof(Pair, next)) : NULL;
}
String::Body last_key() const {
if(Pair* pair = last_pair()) {
#ifdef HASH_CODE_CACHING
return String::Body(pair->key, pair->code);
#else
return pair->key;
#endif
} else {
return String::Body();
}
}
V last_value() const {
if(Pair* pair = last_pair())
return pair->value;
return NULL;
}
void order_clear() {
HASH_ORDER_CLEAR();
}
void order_next(Pair* pair) {
pair->prev=last;
pair->next=0;
*last=pair;
last=&(pair->next);
}
#endif //HASH_ORDER
/// put a [value] under the [key] if that [key] existed @returns existed or not
bool put_replaced(K key, V value) {
if(!value) {
remove(key);
return false;
}
uint code=hash_code(key);
uint index=code%allocated;
for(Pair *pair=refs[index]; pair; pair=pair->link)
if(pair->code==code && pair->key==key) {
// found a pair with the same key, replacing
pair->value=value;
return true;
}
// proper pair not found
return false;
}
/// put a [value] under the [key] if that [key] NOT existed @returns existed or not
bool put_dont_replace(K key, V value) {
if(!value) {
remove(key);
return false;
}
if(is_full())
expand();
uint code=hash_code(key);
uint index=code%allocated;
Pair **ref=&refs[index];
for(Pair *pair=*ref; pair; pair=pair->link)
if(pair->code==code && pair->key==key) {
// found a pair with the same key, NOT replacing
return true;
}
// proper pair not found -- create&link_in new pair
if(!*ref) // root cell were fused_refs?
fused_refs++; // not, we'll use it and record the fact
HASH_NEW_PAIR(code, key, value);
fpairs_count++;
return false;
}
/// put all 'src' values if NO with same key existed
void merge_dont_replace(const HASH& src) {
#ifdef HASH_ORDER
for(Pair *pair=src.first; pair; pair=pair->next)
#else
for(int i=0; i<src.allocated; i++)
for(Pair *pair=src.refs[i]; pair; pair=pair->link)
#endif
put_dont_replace(pair->key, pair->value);
}
/// number of elements in hash
int count() const { return fpairs_count; }
/// iterate over all pairs
template<typename I> void for_each(void callback(K, V, I), I info) const {
HASH_FOR_EACH
callback(pair->key, pair->value, info);
}
/// iterate over all pairs
template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
HASH_FOR_EACH
callback(pair->key, pair->value, info);
}
/// iterate over all pairs until condition becomes true, return that element
template<typename I> V first_that(bool callback(K, V, I), I info) const {
HASH_FOR_EACH
if(callback(pair->key, pair->value, info))
return pair->value;
return V(0);
}
/// remove all elements
void clear() {
memset(refs, 0, sizeof(*refs)*allocated);
fpairs_count=fused_refs=0;
HASH_ORDER_CLEAR();
}
protected:
/// the index of [allocated] in [Hash_allocates]
int allocates_index;
/// number of allocated pairs
int allocated;
/// used pairs
int fused_refs;
/// stored pairs total (including those by links)
int fpairs_count;
/// pair storage
class Pair: public PA_Allocated {
public:
uint code;
K key;
V value;
Pair *link;
#ifdef HASH_ORDER
Pair **prev;
Pair *next;
Pair(uint acode, K akey, V avalue, Pair *alink, Pair **aprev) : code(acode), key(akey), value(avalue), link(alink),
prev(aprev), next(0) { *aprev=this; }
#else
Pair(uint acode, K akey, V avalue, Pair *alink) : code(acode), key(akey), value(avalue), link(alink) {}
#endif
} **refs;
#ifdef HASH_ORDER
Pair *first;
Pair **last;
#endif
/// filled to threshold (THRESHOLD_PERCENT=75), needs expanding
bool is_full() { return fused_refs + allocated/4 >= allocated; }
/// allocate larger buffer & rehash
void expand() {
int old_allocated=allocated;
Pair **old_refs=refs;
if (allocates_index<HASH_ALLOCATES_COUNT-1) allocates_index++;
// allocated bigger refs array
allocated=Hash_allocates[allocates_index];
refs=new(PointerGC) Pair*[allocated];
// rehash
Pair **old_ref=old_refs;
for(int old_index=0; old_index<old_allocated; old_index++)
for(Pair *pair=*old_ref++; pair; ) {
Pair *next=pair->link;
uint new_index=pair->code%allocated;
Pair **new_ref=&refs[new_index];
pair->link=*new_ref;
*new_ref=pair;
pair=next;
}
operator delete[](old_refs);
}
private: //disabled
HASH& operator = (const HASH&) { return *this; }
};
/**
Simple String::body hash.
Allows hash code caching
*/
#ifdef HASH_CODE_CACHING
template<typename V> class HASH_STRING: public HASH<const CORD,V> {
public:
typedef typename HASH<const CORD,V>::Pair Pair;
typedef const String::Body &K;
typedef K key_type;
/// put a [value] under the [key] @returns existed or not
bool put(K str, V value) {
if(!value) {
remove(str);
return false;
}
if(this->is_full())
this->expand();
CORD key=str.get_cord();
uint code=str.get_hash_code();
uint index=code % this->allocated;
Pair **ref=&this->refs[index];
for(Pair *pair=*ref; pair; pair=pair->link)
if(pair->code==code && CORD_cmp(pair->key,key)==0) {
// found a pair with the same key
pair->value=value;
return true;
}
// proper pair not found -- create&link_in new pair
if(!*ref) // root cell were fused_refs?
this->fused_refs++; // not, we'll use it and record the fact
HASH_NEW_PAIR(code, key, value);
this->fpairs_count++;
return false;
}
/// remove the [key] @returns existed or not
bool remove(K str) {
CORD key=str.get_cord();
uint code=str.get_hash_code();
uint index=code % this->allocated;
for(Pair **ref=&this->refs[index]; *ref; ref=&(*ref)->link){
Pair *pair=*ref;
if(pair->code==code && CORD_cmp(pair->key,key)==0) {
// found a pair with the same key
Pair *next=pair->link;
#ifdef HASH_ORDER
*(pair->prev)=pair->next;
if(pair->next)
pair->next->prev=pair->prev;
else
this->last=pair->prev;
#endif
*ref=next;
--this->fpairs_count;
return true;
}
}
return false;
}
/// return true if key exists
bool contains(K str){
CORD key=str.get_cord();
uint code=str.get_hash_code();
uint index=code % this->allocated;
for(Pair *pair=this->refs[index]; pair; pair=pair->link){
if(pair->code==code && CORD_cmp(pair->key,key)==0)
return true;
}
return false;
}
/// get associated [value] by the [key]
V get(K str) const {
CORD key=str.get_cord();
uint code=str.get_hash_code();
uint index=code % this->allocated;
for(Pair *pair=this->refs[index]; pair; pair=pair->link)
if(pair->code==code && CORD_cmp(pair->key,key)==0)
return pair->value;
return V(0);
}
/// get associated [value] by the [key], optimized
V get(const char *key) const {
uint code=0;
if(key && *key){
generic_hash_code(code, key);
} else {
key=0;
}
uint index=code % this->allocated;
for(Pair *pair=this->refs[index]; pair; pair=pair->link)
if(pair->code==code && CORD_cmp(pair->key,(CORD)key)==0)
return pair->value;
return V((const char *)0);
}
/// put a [value] under the [key] if that [key] existed @returns existed or not
bool put_replaced(K str, V value) {
if(!value) {
remove(str);
return false;
}
CORD key=str.get_cord();
uint code=str.get_hash_code();
uint index=code % this->allocated;
for(Pair *pair=this->refs[index]; pair; pair=pair->link)
if(pair->code==code && CORD_cmp(pair->key,key)==0) {
// found a pair with the same key, replacing
pair->value=value;
return true;
}
// proper pair not found
return false;
}
/// put a [value] under the [key] if that [key] NOT existed @returns existed or not
bool put_dont_replace(K str, V value) {
if(!value) {
remove(str);
return false;
}
if(this->is_full())
this->expand();
CORD key=str.get_cord();
uint code=str.get_hash_code();
uint index=code % this->allocated;
Pair **ref=&this->refs[index];
for(Pair *pair=*ref; pair; pair=pair->link)
if(pair->code==code && CORD_cmp(pair->key,key)==0) {
// found a pair with the same key, NOT replacing
return true;
}
// proper pair not found -- create&link_in new pair
if(!*ref) // root cell were fused_refs?
this->fused_refs++; // not, we'll use it and record the fact
HASH_NEW_PAIR(code, key, value);
this->fpairs_count++;
return false;
}
/// rename $.from[] to $.to[]
void rename(K from, K to) {
CORD key_from=from.get_cord();
uint code_from=from.get_hash_code();
uint index_from=code_from % this->allocated;
CORD key_to=to.get_cord();
uint code_to=to.get_hash_code();
uint index_to=code_to % this->allocated;
if(code_from == code_to && CORD_cmp(key_from, key_to)==0)
return;
for(Pair **ref=&this->refs[index_from]; *ref; ref=&(*ref)->link){
Pair *pair=*ref;
if(pair->code==code_from && CORD_cmp(pair->key,key_from)==0) {
// found a pair with the required key
// remove it from the from key first
Pair *next=pair->link;
*ref=next;
// to simplify code
remove(to);
// change pair key
pair->code=code_to;
(CORD &)(pair->key)=key_to;
// link to the to key, hash order left intact
if(!(pair->link=this->refs[index_to])) // root was unused?
this->fused_refs++; // we've used it and record the fact
this->refs[index_to]=pair;
return;
}
}
}
/// put all 'src' values if NO with same key existed
void merge_dont_replace(const HASH_STRING& src) {
#ifdef HASH_ORDER
for(Pair *pair=src.first; pair; pair=pair->next)
#else
for(int i=0; i<src.allocated; i++)
for(Pair *pair=src.refs[i]; pair; pair=pair->link)
#endif
put_dont_replace(String::Body(pair->key, pair->code), pair->value);
}
/// iterate over all pairs
template<typename I> void for_each(void callback(K, V, I), I info) const {
HASH_FOR_EACH
callback(String::Body(pair->key, pair->code), pair->value, info);
}
/// iterate over all pairs
template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
HASH_FOR_EACH
callback(String::Body(pair->key, pair->code), pair->value, info);
}
/// iterate over all pairs until condition becomes true, return that element
template<typename I> V first_that(bool callback(K, V, I), I info) const {
HASH_FOR_EACH
if(callback(String::Body(pair->key, pair->code), pair->value, info))
return pair->value;
return V(0);
}
#else //HASH_CODE_CACHING
template<typename V> class HASH_STRING: public HASH<const String::Body,V>{
public:
typedef typename HASH<const String::Body,V>::Pair Pair;
#endif //HASH_CODE_CACHING
/// simple hash iterator
class Iterator {
const HASH_STRING<V>& fhash;
Pair *fcurrent;
#ifndef HASH_ORDER
int i;
#endif
public:
#ifdef HASH_ORDER
Iterator(const HASH_STRING<V>& ahash): fhash(ahash) {
fcurrent=fhash.first;
}
void next() {
fcurrent=fcurrent->next;
}
#else
Iterator(const HASH_STRING<V>& ahash): fhash(ahash) {
fcurrent=0;
for(i=0; i<fhash.allocated; i++)
if (fcurrent=fhash.refs[i])
break;
}
void next() {
if(fcurrent=fcurrent->link)
return;
for(i++; i<fhash.allocated; i++)
if (fcurrent=fhash.refs[i])
break;
}
#endif
operator bool () {
return fcurrent != NULL;
}
String::Body key(){
#ifdef HASH_CODE_CACHING
return String::Body(fcurrent->key, fcurrent->code);
#else
return fcurrent->key;
#endif
}
V value(){
return fcurrent->value;
}
Pair *pair(){
return fcurrent;
}
};
#ifdef HASH_ORDER
/// simple reverse hash iterator
class ReverseIterator {
const HASH_STRING<V>& fhash;
Pair *fcurrent;
public:
ReverseIterator(const HASH_STRING<V>& ahash): fhash(ahash) {
fcurrent=fhash.last_pair();
}
void prev() {
fcurrent=(fcurrent->prev == &fhash.first) ? NULL : (Pair*)((char *)fcurrent->prev - offsetof(Pair, next));
}
operator bool () {
return fcurrent != NULL;
}
String::Body key(){
#ifdef HASH_CODE_CACHING
return String::Body(fcurrent->key, fcurrent->code);
#else
return fcurrent->key;
#endif
}
V value(){
return fcurrent->value;
}
Pair *pair(){
return fcurrent;
}
};
#endif
};
#ifndef HASH_ORDER
/// Auto-object used to temporarily substituting/removing string hash values
template <typename H, typename V>
class Temp_hash_value {
H *fhash;
String::Body fname;
V saved_value;
public:
Temp_hash_value(H *ahash, String::Body aname, V avalue) : fhash(ahash), fname(aname) {
if(fhash){
saved_value=fhash->get(aname);
fhash->put(aname, avalue);
}
}
~Temp_hash_value() {
if(fhash)
fhash->put(fname, saved_value);
}
};
#endif
#endif //PA_HASH_CLASS
|