Annotation of parser3/src/include/pa_hash.h, revision 1.75
1.28 paf 1: /** @file
1.29 paf 2: Parser: hash class decl.
3:
1.74 misha 4: Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)
1.29 paf 5:
1.54 paf 6: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 7: */
8:
1.59 paf 9: /*
10: The prime numbers used from zend_hash.c,
11: the part of Zend scripting engine library,
12: Copyrighted (C) 1999-2000 Zend Technologies Ltd.
13: http://www.zend.com/license/0_92.txt
14: For more information about Zend please visit http://www.zend.com/
15: */
16:
1.1 paf 17: #ifndef PA_HASH_H
18: #define PA_HASH_H
1.56 paf 19:
1.75 ! misha 20: static const char * const IDENT_HASH_H="$Date: 2009-05-14 11:27:23 $";
1.1 paf 21:
1.59 paf 22: #include "pa_memory.h"
1.1 paf 23: #include "pa_types.h"
1.74 misha 24: #include "pa_string.h"
1.59 paf 25:
26: const int HASH_ALLOCATES_COUNT=29;
1.1 paf 27:
1.61 paf 28: /** Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster
29:
30: paf: HPUX ld could not handle static member: unsatisfied symbols
31: */
32: static uint Hash_allocates[HASH_ALLOCATES_COUNT]={
33: 5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963,
34: 16229, 32531, 65407, 130987, 262237, 524521, 1048793,
35: 2097397, 4194103, 8388857, 16777447, 33554201, 67108961,
36: 134217487, 268435697, 536870683, 1073741621, 2147483399};
37:
1.68 misha 38: /// useful generic hash function
39: inline void generic_hash_code(uint& result, char c) {
40: result=(result<<4)+c;
41: if(uint g=(result&0xF0000000)) {
42: result=result^(g>>24);
43: result=result^g;
44: }
45: }
46: /// useful generic hash function
47: inline void generic_hash_code(uint& result, const char* s) {
48: while(char c=*s++) {
49: result=(result<<4)+c;
50: if(uint g=(result&0xF0000000)) {
51: result=result^(g>>24);
52: result=result^g;
53: }
54: }
55: }
56:
57: /// useful generic hash function
58: inline void generic_hash_code(uint& result, const char* buf, size_t size) {
59: const char* end=buf+size;
60: while(buf<end) {
61: result=(result<<4)+*buf++;
62: if(uint g=(result&0xF0000000)) {
63: result=result^(g>>24);
64: result=result^g;
65: }
66: }
67: }
68:
69: /// simple hash code of int. used by EXIF mapping
70: inline uint hash_code(int self) {
71: uint result=0;
72: generic_hash_code(result, (const char*)&self, sizeof(self));
73: return result;
74: }
75:
1.75 ! misha 76: #endif // PA_HASH_H
! 77:
! 78: #ifndef PA_HASH_CLASS
! 79: #define PA_HASH_CLASS
1.29 paf 80: /**
1.59 paf 81: Simple hash.
1.29 paf 82:
1.59 paf 83: Automatically rehashed when almost is_full.
1.51 paf 84: Contains no 0 values.
85: get returning 0 means there were no such.
86: "put value 0" means "remove"
1.29 paf 87: */
1.75 ! misha 88: #ifdef HASH_ORDER
! 89:
! 90: #undef HASH
! 91: #undef HASH_STRING
! 92: #undef NEW_PAIR
! 93:
! 94: #define HASH OrderedHash
! 95: #define HASH_STRING OrderedHashString
! 96: #define NEW_PAIR(code, key, value) *ref=new Pair(code, key, value, *ref, this->last); this->last=&((*ref)->next)
! 97:
! 98: #else
! 99:
! 100: #define HASH Hash
! 101: #define HASH_STRING HashString
! 102: #define NEW_PAIR(code, key, value) *ref=new Pair(code, key, value, *ref)
! 103:
! 104: #endif
! 105:
! 106: template<typename K, typename V> class HASH: public PA_Object {
1.1 paf 107: public:
108:
1.59 paf 109: typedef K key_type;
110: typedef V value_type;
1.3 paf 111:
1.75 ! misha 112: HASH() {
1.61 paf 113: allocated=Hash_allocates[allocates_index=0];
1.59 paf 114: threshold=allocated*THRESHOLD_PERCENT/100;
115: fpairs_count=fused_refs=0;
116: refs=new(UseGC) Pair*[allocated];
1.75 ! misha 117: #ifdef HASH_ORDER
! 118: first=0;
! 119: last=&first;
! 120: #endif
1.59 paf 121: }
1.25 paf 122:
1.75 ! misha 123: HASH(const HASH& source) {
1.59 paf 124: allocates_index=source.allocates_index;
125: allocated=source.allocated;
126: threshold=source.threshold;
127: fused_refs=source.fused_refs;
128: fpairs_count=source.fpairs_count;
129: refs=new(UseGC) Pair*[allocated];
1.75 ! misha 130: #ifdef HASH_ORDER
! 131: first=0;
! 132: last=&first;
! 133: #endif
1.59 paf 134: // clone & rehash
135: Pair **old_ref=source.refs;
136: for(int index=0; index<allocated; index++)
137: for(Pair *pair=*old_ref++; pair; ) {
138: Pair *next=pair->link;
1.45 paf 139:
1.75 ! misha 140: Pair **ref=&refs[index];
! 141: NEW_PAIR(pair->code, pair->key, pair->value);
1.38 paf 142:
1.59 paf 143: pair=next;
144: }
1.43 parser 145: }
146:
1.73 misha 147: #ifdef USE_DESTRUCTORS
1.75 ! misha 148: ~HASH() {
1.72 misha 149: Pair **ref=refs;
150: for(int index=0; index<allocated; index++)
151: for(Pair *pair=*ref++; pair;){
152: Pair *next=pair->link;
153: delete pair;
154: pair=next;
155: }
1.71 misha 156: delete[] refs;
157: }
1.73 misha 158: #endif
1.71 misha 159:
1.59 paf 160: /// put a [value] under the [key] @returns existed or not
161: bool put(K key, V value) {
162: if(!value) {
163: remove(key);
164: return false;
165: }
166: if(is_full())
167: expand();
168:
169: uint code=hash_code(key);
170: uint index=code%allocated;
171: Pair **ref=&refs[index];
172: for(Pair *pair=*ref; pair; pair=pair->link)
173: if(pair->code==code && pair->key==key) {
174: // found a pair with the same key
175: pair->value=value;
176: return true;
177: }
178:
179: // proper pair not found -- create&link_in new pair
180: if(!*ref) // root cell were fused_refs?
181: fused_refs++; // not, we'll use it and record the fact
1.75 ! misha 182: NEW_PAIR(code, key, value);
1.59 paf 183: fpairs_count++;
184: return false;
1.24 paf 185: }
1.10 paf 186:
1.63 paf 187: /// put a [value] under the [key] @returns existed or not
1.65 paf 188: template<typename R, typename F, typename I> R replace_maybe_append(K key, V value, F prevent, I info) {
189: if(!value) {
190: // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel)
191: remove(key);
192: // this has nothing to do with properties, doing no special property handling here
193: return 0;
194: }
1.64 paf 195:
1.63 paf 196: if(is_full())
197: expand();
198:
199: uint code=hash_code(key);
200: uint index=code%allocated;
201: Pair **ref=&refs[index];
202: for(Pair *pair=*ref; pair; pair=pair->link)
203: if(pair->code==code && pair->key==key) {
204: // found a pair with the same key
1.65 paf 205: pair->value=value;
206: return reinterpret_cast<R>(1);
207: }
208:
209: // proper pair not found
210: // prevent-function intercepted append?
211: if(R result=prevent(value, info))
212: return result;
213:
214: //create&link_in new pair
215: if(!*ref) // root cell were fused_refs?
216: fused_refs++; // not, we'll use it and record the fact
1.75 ! misha 217: NEW_PAIR(code, key, value);
1.65 paf 218: fpairs_count++;
219: return 0;
220: }
1.63 paf 221:
1.65 paf 222: /// put a [value] under the [key] @returns existed or not
223: template<typename R, typename F1, typename F2, typename I>
224: R maybe_replace_maybe_append(K key, V value, F1 prevent_replace, F2 prevent_append, I info)
225: {
226: if(!value) {
227: // they can come here from Temp_value_element::dctor to restore some empty value
228: remove(key);
229: // this has nothing to do with properties, doing no special property handling here
230: return 0;
231: }
232:
233: if(is_full())
234: expand();
235:
236: uint code=hash_code(key);
237: uint index=code%allocated;
238: Pair **ref=&refs[index];
239: for(Pair *pair=*ref; pair; pair=pair->link)
240: if(pair->code==code && pair->key==key) {
241: // found a pair with the same key
242:
243: // prevent-function intercepted replace?
244: if(R result=prevent_replace(pair->value, info))
1.63 paf 245: return result;
246:
247: pair->value=value;
248: return reinterpret_cast<R>(1);
249: }
250:
1.65 paf 251: // proper pair not found
252: // prevent-function intercepted append?
253: if(R result=prevent_append(value, info))
254: return result;
255:
256: //create&link_in new pair
1.63 paf 257: if(!*ref) // root cell were fused_refs?
258: fused_refs++; // not, we'll use it and record the fact
1.75 ! misha 259: NEW_PAIR(code, key, value);
1.63 paf 260: fpairs_count++;
261: return 0;
262: }
263:
1.65 paf 264: /// put a [value] under the [key] @returns existed or not
265: template<typename R, typename F1, typename I>
266: R maybe_replace_never_append(K key, V value, F1 prevent_replace, I info)
267: {
268: if(!value) {
269: // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel)
270: remove(key);
271: // this has nothing to do with properties, doing no special property handling here
272: return 0;
273: }
274:
275: if(is_full())
276: expand();
277:
278: uint code=hash_code(key);
279: uint index=code%allocated;
280: Pair **ref=&refs[index];
281: for(Pair *pair=*ref; pair; pair=pair->link)
282: if(pair->code==code && pair->key==key) {
283: // found a pair with the same key
284:
285: // prevent-function intercepted replace?
286: if(R result=prevent_replace(pair->value, info))
287: return result;
288:
289: pair->value=value;
290: return reinterpret_cast<R>(1);
291: }
292:
293: return 0;
294: }
295:
1.59 paf 296: /// remove the [key] @returns existed or not
297: bool remove(K key) {
298: uint code=hash_code(key);
299: uint index=code%allocated;
1.75 ! misha 300: for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link){
! 301: Pair *pair=*ref;
! 302: if(pair->code==code && pair->key==key) {
1.59 paf 303: // found a pair with the same key
1.75 ! misha 304: Pair *next=pair->link;
! 305: #ifdef HASH_ORDER
! 306: *(pair->prev)=pair->next;
! 307: if(pair->next)
! 308: pair->next->prev=pair->prev;
! 309: else
! 310: last=pair->prev;
! 311: #endif
! 312: delete pair;
1.59 paf 313: *ref=next;
314: --fpairs_count;
315: return true;
316: }
1.75 ! misha 317: }
1.8 paf 318:
1.59 paf 319: return false;
320: }
1.48 paf 321:
1.70 misha 322: /// return true if key exists
1.69 misha 323: bool contains(K key){
1.67 misha 324: uint code=hash_code(key);
325: uint index=code%allocated;
1.70 misha 326: for(Pair *pair=refs[index]; pair; pair=pair->link){
327: if(pair->code==code && pair->key==key)
1.67 misha 328: return true;
329: }
330:
331: return false;
332: }
333:
1.59 paf 334: /// get associated [value] by the [key]
335: V get(K key) const {
336: uint code=hash_code(key);
337: uint index=code%allocated;
338: for(Pair *pair=refs[index]; pair; pair=pair->link)
339: if(pair->code==code && pair->key==key)
340: return pair->value;
341:
342: return V(0);
1.33 paf 343: }
1.70 misha 344:
1.51 paf 345: /// put a [value] under the [key] if that [key] existed @returns existed or not
1.63 paf 346: bool put_replaced(K key, V value) {
1.59 paf 347: if(!value) {
348: remove(key);
349: return false;
350: }
351: uint code=hash_code(key);
352: uint index=code%allocated;
353: for(Pair *pair=refs[index]; pair; pair=pair->link)
354: if(pair->code==code && pair->key==key) {
355: // found a pair with the same key, replacing
356: pair->value=value;
357: return true;
358: }
359:
360: // proper pair not found
361: return false;
1.64 paf 362: }
363:
364: /// put a [value] under the [key] if that [key] existed @returns existed or not
365: template<typename R, typename F> R maybe_put_replaced(K key, V value, F prevent) {
1.65 paf 366: if(!value) {
367: // they can come here from Temp_value_element::dctor to restore some empty value
368: remove(key);
369: // this has nothing to do with properties, doing no special property handling here
370: return 0;
371: }
1.64 paf 372:
373: uint code=hash_code(key);
374: uint index=code%allocated;
375: for(Pair *pair=refs[index]; pair; pair=pair->link)
376: if(pair->code==code && pair->key==key) {
377: // found a pair with the same key, replacing
378: // prevent-function intercepted put?
379: if(R result=prevent(pair->value))
380: return result;
381:
382: pair->value=value;
383: return reinterpret_cast<R>(1);
384: }
385:
386: // proper pair not found
387: return 0;
1.59 paf 388: }
1.18 paf 389:
1.51 paf 390: /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
1.59 paf 391: bool put_dont_replace(K key, V value) {
392: if(!value) {
393: remove(key);
394: return false;
395: }
396: if(is_full())
397: expand();
398:
399: uint code=hash_code(key);
400: uint index=code%allocated;
401: Pair **ref=&refs[index];
402: for(Pair *pair=*ref; pair; pair=pair->link)
403: if(pair->code==code && pair->key==key) {
404: // found a pair with the same key, NOT replacing
405: return true;
406: }
407:
408: // proper pair not found -- create&link_in new pair
409: if(!*ref) // root cell were fused_refs?
410: fused_refs++; // not, we'll use it and record the fact
1.75 ! misha 411: NEW_PAIR(code, key, value);
1.59 paf 412: fpairs_count++;
413: return false;
414: }
1.18 paf 415:
1.59 paf 416: /** put all 'src' values if NO with same key existed
417: @todo optimize this.allocated==src.allocated case
418: */
1.75 ! misha 419: void merge_dont_replace(const HASH& src) {
1.59 paf 420: for(int i=0; i<src.allocated; i++)
421: for(Pair *pair=src.refs[i]; pair; pair=pair->link)
422: put_dont_replace(pair->key, pair->value);
1.36 paf 423: }
1.11 paf 424:
1.29 paf 425: /// number of elements in hash
1.59 paf 426: int count() const { return fpairs_count; }
1.25 paf 427:
1.59 paf 428: /// iterate over all pairs
429: template<typename I> void for_each(void callback(K, V, I), I info) const {
430: Pair **ref=refs;
431: for(int index=0; index<allocated; index++)
432: for(Pair *pair=*ref++; pair; pair=pair->link)
433: callback(pair->key, pair->value, info);
434: }
1.45 paf 435:
1.59 paf 436: /// iterate over all pairs
437: template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
438: Pair **ref=refs;
439: for(int index=0; index<allocated; index++)
440: for(Pair *pair=*ref++; pair; pair=pair->link)
441: callback(pair->key, pair->value, info);
442: }
1.38 paf 443:
1.59 paf 444: /// iterate over all pairs until condition becomes true, return that element
445: template<typename I> V first_that(bool callback(K, V, I), I info) const {
1.75 ! misha 446: #ifdef HASH_ORDER
! 447: for(Pair *pair=first; pair; pair=pair->next)
! 448: if(callback(pair->key, pair->value, info))
! 449: return pair->value;
! 450: #else
1.59 paf 451: Pair **ref=refs;
452: for(int index=0; index<allocated; index++)
453: for(Pair *pair=*ref++; pair; pair=pair->link)
454: if(callback(pair->key, pair->value, info))
455: return pair->value;
1.75 ! misha 456: #endif
1.59 paf 457: return V(0);
458: }
1.27 paf 459:
1.29 paf 460: /// remove all elements
1.59 paf 461: void clear() {
462: memset(refs, 0, sizeof(*refs)*allocated);
463: fpairs_count=fused_refs=0;
1.75 ! misha 464: #ifdef HASH_ORDER
! 465: first=0;
! 466: last=&first;
! 467: #endif
1.59 paf 468: }
1.15 paf 469:
1.74 misha 470: protected:
1.1 paf 471:
1.39 paf 472: /// expand when these %% of allocated exausted
1.1 paf 473: enum {
474: THRESHOLD_PERCENT=75
475: };
1.9 paf 476:
1.61 paf 477: /// the index of [allocated] in [Hash_allocates]
1.19 paf 478: int allocates_index;
1.1 paf 479:
1.39 paf 480: /// number of allocated pairs
1.19 paf 481: int allocated;
1.1 paf 482:
1.59 paf 483: /// helper: expanding when fused_refs == threshold
1.1 paf 484: int threshold;
485:
1.39 paf 486: /// used pairs
1.59 paf 487: int fused_refs;
1.44 parser 488:
489: /// stored pairs total (including those by links)
1.59 paf 490: int fpairs_count;
1.1 paf 491:
1.39 paf 492: /// pair storage
1.59 paf 493: class Pair: public PA_Allocated {
494: public:
1.1 paf 495: uint code;
1.59 paf 496: K key;
497: V value;
1.1 paf 498: Pair *link;
1.75 ! misha 499: #ifdef HASH_ORDER
! 500: Pair **prev;
! 501: Pair *next;
! 502:
! 503: Pair(uint acode, K akey, V avalue, Pair *alink, Pair **aprev) : code(acode), key(akey), value(avalue), link(alink),
! 504: prev(aprev), next(0) { *aprev=this; }
! 505: #else
! 506: Pair(uint acode, K akey, V avalue, Pair *alink) : code(acode), key(akey), value(avalue), link(alink) {}
! 507: #endif
1.2 paf 508: } **refs;
1.1 paf 509:
1.75 ! misha 510: #ifdef HASH_ORDER
! 511: Pair *first;
! 512: Pair **last;
! 513: #endif
! 514:
1.39 paf 515: /// filled to threshold: needs expanding
1.59 paf 516: bool is_full() { return fused_refs==threshold; }
1.5 paf 517:
1.39 paf 518: /// allocate larger buffer & rehash
1.59 paf 519: void expand() {
520: int old_allocated=allocated;
521: Pair **old_refs=refs;
522:
523: allocates_index=allocates_index+1<HASH_ALLOCATES_COUNT?allocates_index+1:HASH_ALLOCATES_COUNT-1;
524: // allocated bigger refs array
1.61 paf 525: allocated=Hash_allocates[allocates_index];
1.59 paf 526: threshold=allocated*THRESHOLD_PERCENT/100;
527: refs=new(UseGC) Pair*[allocated];
528:
529: // rehash
530: Pair **old_ref=old_refs;
531: for(int old_index=0; old_index<old_allocated; old_index++)
532: for(Pair *pair=*old_ref++; pair; ) {
533: Pair *next=pair->link;
534:
535: uint new_index=pair->code%allocated;
536: Pair **new_ref=&refs[new_index];
537: pair->link=*new_ref;
538: *new_ref=pair;
539:
540: pair=next;
541: }
542:
543: delete[] old_refs;
544: }
1.4 paf 545:
546: private: //disabled
547:
1.75 ! misha 548: HASH& operator = (const HASH&) { return *this; }
1.1 paf 549: };
1.59 paf 550:
1.74 misha 551: /**
1.75 ! misha 552: Simple String::body hash.
! 553: Allows hash code caching
1.74 misha 554: */
555:
556: #ifdef HASH_CODE_CACHING
557:
1.75 ! misha 558: template<typename V> class HASH_STRING: public HASH<const CORD,V> {
1.74 misha 559: public:
560:
1.75 ! misha 561: typedef typename HASH<const CORD,V>::Pair Pair;
1.74 misha 562: typedef const String::Body &K;
563:
564: typedef K key_type;
565:
566: /// put a [value] under the [key] @returns existed or not
567: bool put(K str, V value) {
568: if(!value) {
569: remove(str);
570: return false;
571: }
572: if(this->is_full())
573: this->expand();
574:
575: CORD key=str.get_cord();
576:
577: uint code=str.get_hash_code();
578: uint index=code%this->allocated;
579: Pair **ref=&this->refs[index];
580: for(Pair *pair=*ref; pair; pair=pair->link)
581: if(pair->code==code && CORD_cmp(pair->key,key)==0) {
582: // found a pair with the same key
583: pair->value=value;
584: return true;
585: }
586:
587: // proper pair not found -- create&link_in new pair
588: if(!*ref) // root cell were fused_refs?
589: this->fused_refs++; // not, we'll use it and record the fact
1.75 ! misha 590: NEW_PAIR(code, key, value);
1.74 misha 591: this->fpairs_count++;
592: return false;
593: }
594:
595: /// put a [value] under the [key] @returns existed or not
596: template<typename R, typename F, typename I> R replace_maybe_append(K str, V value, F prevent, I info) {
597: if(!value) {
598: // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel)
599: remove(str);
600: // this has nothing to do with properties, doing no special property handling here
601: return 0;
602: }
603:
604: if(this->is_full())
605: this->expand();
606:
607: CORD key=str.get_cord();
608:
609: uint code=str.get_hash_code();
610: uint index=code%this->allocated;
611: Pair **ref=&this->refs[index];
612: for(Pair *pair=*ref; pair; pair=pair->link)
613: if(pair->code==code && CORD_cmp(pair->key,key)==0) {
614: // found a pair with the same key
615: pair->value=value;
616: return reinterpret_cast<R>(1);
617: }
618:
619: // proper pair not found
620: // prevent-function intercepted append?
621: if(R result=prevent(value, info))
622: return result;
623:
624: //create&link_in new pair
625: if(!*ref) // root cell were fused_refs?
626: this->fused_refs++; // not, we'll use it and record the fact
1.75 ! misha 627: NEW_PAIR(code, key, value);
1.74 misha 628: this->fpairs_count++;
629: return 0;
630: }
631:
632: /// put a [value] under the [key] @returns existed or not
633: template<typename R, typename F1, typename F2, typename I>
634: R maybe_replace_maybe_append(K str, V value, F1 prevent_replace, F2 prevent_append, I info) {
635: if(!value) {
636: // they can come here from Temp_value_element::dctor to restore some empty value
637: remove(str);
638: // this has nothing to do with properties, doing no special property handling here
639: return 0;
640: }
641:
642: if(this->is_full())
643: this->expand();
644:
645: CORD key=str.get_cord();
646:
647: uint code=str.get_hash_code();
648: uint index=code%this->allocated;
649: Pair **ref=&this->refs[index];
650: for(Pair *pair=*ref; pair; pair=pair->link)
651: if(pair->code==code && CORD_cmp(pair->key,key)==0) {
652: // found a pair with the same key
653:
654: // prevent-function intercepted replace?
655: if(R result=prevent_replace(pair->value, info))
656: return result;
657:
658: pair->value=value;
659: return reinterpret_cast<R>(1);
660: }
661:
662: // proper pair not found
663: // prevent-function intercepted append?
664: if(R result=prevent_append(value, info))
665: return result;
666:
667: //create&link_in new pair
668: if(!*ref) // root cell were fused_refs?
669: this->fused_refs++; // not, we'll use it and record the fact
1.75 ! misha 670: NEW_PAIR(code, key, value);
1.74 misha 671: this->fpairs_count++;
672: return 0;
673: }
674:
675: /// put a [value] under the [key] @returns existed or not
676: template<typename R, typename F1, typename I>
677: R maybe_replace_never_append(K str, V value, F1 prevent_replace, I info) {
678: if(!value) {
679: // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel)
680: remove(str);
681: // this has nothing to do with properties, doing no special property handling here
682: return 0;
683: }
684:
685: if(this->is_full())
686: this->expand();
687:
688: CORD key=str.get_cord();
689:
690: uint code=str.get_hash_code();
691: uint index=code%this->allocated;
692: Pair **ref=&this->refs[index];
693: for(Pair *pair=*ref; pair; pair=pair->link)
694: if(pair->code==code && CORD_cmp(pair->key,key)==0) {
695: // found a pair with the same key
696:
697: // prevent-function intercepted replace?
698: if(R result=prevent_replace(pair->value, info))
699: return result;
700:
701: pair->value=value;
702: return reinterpret_cast<R>(1);
703: }
704:
705: return 0;
706:
707: }
708:
709: /// remove the [key] @returns existed or not
710: bool remove(K str) {
711: CORD key=str.get_cord();
712: uint code=str.get_hash_code();
713: uint index=code%this->allocated;
1.75 ! misha 714: for(Pair **ref=&this->refs[index]; *ref; ref=&(*ref)->link){
! 715: Pair *pair=*ref;
! 716: if(pair->code==code && CORD_cmp(pair->key,key)==0) {
1.74 misha 717: // found a pair with the same key
1.75 ! misha 718: Pair *next=pair->link;
! 719: #ifdef HASH_ORDER
! 720: *(pair->prev)=pair->next;
! 721: if(pair->next)
! 722: pair->next->prev=pair->prev;
! 723: else
! 724: this->last=pair->prev;
! 725: #endif
! 726: delete pair;
1.74 misha 727: *ref=next;
728: --this->fpairs_count;
729: return true;
730: }
1.75 ! misha 731: }
1.74 misha 732:
733: return false;
734: }
735:
736: /// return true if key exists
737: bool contains(K str){
738: CORD key=str.get_cord();
739: uint code=str.get_hash_code();
740: uint index=code%this->allocated;
741: for(Pair *pair=this->refs[index]; pair; pair=pair->link){
742: if(pair->code==code && CORD_cmp(pair->key,key)==0)
743: return true;
744: }
745:
746: return false;
747: }
748:
749: /// get associated [value] by the [key]
750: V get(K str) const {
751: CORD key=str.get_cord();
752: uint code=str.get_hash_code();
753: uint index=code%this->allocated;
754: for(Pair *pair=this->refs[index]; pair; pair=pair->link)
755: if(pair->code==code && CORD_cmp(pair->key,key)==0)
756: return pair->value;
757:
758: return V(0);
759: }
760:
761: /// put a [value] under the [key] if that [key] existed @returns existed or not
762: bool put_replaced(K str, V value) {
763: if(!value) {
764: remove(str);
765: return false;
766: }
767:
768: CORD key=str.get_cord();
769: uint code=str.get_hash_code();
770: uint index=code%this->allocated;
771: for(Pair *pair=this->refs[index]; pair; pair=pair->link)
772: if(pair->code==code && CORD_cmp(pair->key,key)==0) {
773: // found a pair with the same key, replacing
774: pair->value=value;
775: return true;
776: }
777:
778: // proper pair not found
779: return false;
780: }
781:
782: /// put a [value] under the [key] if that [key] existed @returns existed or not
783: template<typename R, typename F> R maybe_put_replaced(K str, V value, F prevent) {
784: if(!value) {
785: // they can come here from Temp_value_element::dctor to restore some empty value
786: remove(str);
787: // this has nothing to do with properties, doing no special property handling here
788: return 0;
789: }
790:
791: CORD key=str.get_cord();
792: uint code=str.get_hash_code();
793: uint index=code%this->allocated;
794: for(Pair *pair=this->refs[index]; pair; pair=pair->link)
795: if(pair->code==code && CORD_cmp(pair->key,key)==0) {
796: // found a pair with the same key, replacing
797: // prevent-function intercepted put?
798: if(R result=prevent(pair->value))
799: return result;
800:
801: pair->value=value;
802: return reinterpret_cast<R>(1);
803: }
804:
805: // proper pair not found
806: return 0;
807: }
808:
809: /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
810: bool put_dont_replace(K str, V value) {
811: if(!value) {
812: remove(str);
813: return false;
814: }
815: if(this->is_full())
816: this->expand();
817:
818: CORD key=str.get_cord();
819: uint code=str.get_hash_code();
820: uint index=code%this->allocated;
821: Pair **ref=&this->refs[index];
822: for(Pair *pair=*ref; pair; pair=pair->link)
823: if(pair->code==code && CORD_cmp(pair->key,key)==0) {
824: // found a pair with the same key, NOT replacing
825: return true;
826: }
827:
828: // proper pair not found -- create&link_in new pair
829: if(!*ref) // root cell were fused_refs?
830: this->fused_refs++; // not, we'll use it and record the fact
1.75 ! misha 831: NEW_PAIR(code, key, value);
1.74 misha 832: this->fpairs_count++;
833: return false;
834: }
835:
836: /// iterate over all pairs
837: template<typename I> void for_each(void callback(K, V, I), I info) const {
838: Pair **ref=this->refs;
839: for(int index=0; index<this->allocated; index++)
840: for(Pair *pair=*ref++; pair; pair=pair->link)
841: callback(String::Body(pair->key, pair->code), pair->value, info);
842: }
843:
844: /// iterate over all pairs
845: template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
846: Pair **ref=this->refs;
847: for(int index=0; index<this->allocated; index++)
848: for(Pair *pair=*ref++; pair; pair=pair->link)
849: callback(String::Body(pair->key, pair->code), pair->value, info);
850: }
851:
852: /// iterate over all pairs until condition becomes true, return that element
853: template<typename I> V first_that(bool callback(K, V, I), I info) const {
1.75 ! misha 854: #ifdef HASH_ORDER
! 855: for(Pair *pair=this->first; pair; pair=pair->next)
! 856: if(callback(String::Body(pair->key, pair->code), pair->value, info))
! 857: return pair->value;
! 858: #else
1.74 misha 859: Pair **ref=this->refs;
860: for(int index=0; index<this->allocated; index++)
861: for(Pair *pair=*ref++; pair; pair=pair->link)
862: if(callback(String::Body(pair->key, pair->code), pair->value, info))
863: return pair->value;
1.75 ! misha 864: #endif
1.74 misha 865: return V(0);
866: }
867: };
868: #else
869:
1.75 ! misha 870: template<typename V> class HASH_STRING: public HASH<const String::Body,V>{};
1.74 misha 871:
872: #endif
873:
1.75 ! misha 874: #ifndef HASH_ORDER
1.74 misha 875: /// Auto-object used to temporarily substituting/removing string hash values
1.59 paf 876: template <typename K, typename V>
1.55 paf 877: class Temp_hash_value {
1.74 misha 878: HashString<V> &fhash;
1.59 paf 879: K fname;
880: V saved_value;
1.55 paf 881: public:
1.74 misha 882: Temp_hash_value(HashString<V>& ahash, K aname, V avalue) :
1.55 paf 883: fhash(ahash),
884: fname(aname),
885: saved_value(ahash.get(aname)) {
886: fhash.put(aname, avalue);
887: }
888: ~Temp_hash_value() {
889: fhash.put(fname, saved_value);
890: }
891: };
1.75 ! misha 892: #endif
1.1 paf 893:
1.75 ! misha 894: #endif //PA_HASH_CLASS
E-mail: