Annotation of parser3/src/include/pa_hash.h, revision 1.64.2.4
1.28 paf 1: /** @file
1.29 paf 2: Parser: hash class decl.
3:
1.62 paf 4: Copyright (c) 2001-2004 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.64.2.4! paf 20: static const char * const IDENT_HASH_H="$Date: 2005/07/27 14:04:15 $";
1.1 paf 21:
1.59 paf 22: #include "pa_memory.h"
1.1 paf 23: #include "pa_types.h"
1.59 paf 24:
25: const int HASH_ALLOCATES_COUNT=29;
1.1 paf 26:
1.61 paf 27: /** Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster
28:
29: paf: HPUX ld could not handle static member: unsatisfied symbols
30: */
31: static uint Hash_allocates[HASH_ALLOCATES_COUNT]={
32: 5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963,
33: 16229, 32531, 65407, 130987, 262237, 524521, 1048793,
34: 2097397, 4194103, 8388857, 16777447, 33554201, 67108961,
35: 134217487, 268435697, 536870683, 1073741621, 2147483399};
36:
1.29 paf 37: /**
1.59 paf 38: Simple hash.
1.29 paf 39:
1.59 paf 40: Automatically rehashed when almost is_full.
1.51 paf 41: Contains no 0 values.
42: get returning 0 means there were no such.
43: "put value 0" means "remove"
1.29 paf 44: */
1.59 paf 45: template<typename K, typename V> class Hash: public PA_Object {
1.1 paf 46: public:
47:
1.59 paf 48: typedef K key_type;
49: typedef V value_type;
1.3 paf 50:
1.59 paf 51: Hash() {
1.61 paf 52: allocated=Hash_allocates[allocates_index=0];
1.59 paf 53: threshold=allocated*THRESHOLD_PERCENT/100;
54: fpairs_count=fused_refs=0;
55: refs=new(UseGC) Pair*[allocated];
56: }
1.25 paf 57:
1.59 paf 58: Hash(const Hash& source) {
59: allocates_index=source.allocates_index;
60: allocated=source.allocated;
61: threshold=source.threshold;
62: fused_refs=source.fused_refs;
63: fpairs_count=source.fpairs_count;
64: refs=new(UseGC) Pair*[allocated];
65:
66: // clone & rehash
67: Pair **old_ref=source.refs;
68: for(int index=0; index<allocated; index++)
69: for(Pair *pair=*old_ref++; pair; ) {
70: Pair *next=pair->link;
1.45 paf 71:
1.59 paf 72: Pair **new_ref=&refs[index];
73: *new_ref=new Pair(pair->code, pair->key, pair->value, *new_ref);
1.38 paf 74:
1.59 paf 75: pair=next;
76: }
1.43 parser 77: }
78:
1.59 paf 79: /// put a [value] under the [key] @returns existed or not
80: bool put(K key, V value) {
81: if(!value) {
82: remove(key);
83: return false;
84: }
85: if(is_full())
86: expand();
87:
88: uint code=hash_code(key);
89: uint index=code%allocated;
90: Pair **ref=&refs[index];
91: for(Pair *pair=*ref; pair; pair=pair->link)
92: if(pair->code==code && pair->key==key) {
93: // found a pair with the same key
94: pair->value=value;
95: return true;
96: }
97:
98: // proper pair not found -- create&link_in new pair
99: if(!*ref) // root cell were fused_refs?
100: fused_refs++; // not, we'll use it and record the fact
101: *ref=new Pair(code, key, value, *ref);
102: fpairs_count++;
103: return false;
1.24 paf 104: }
1.10 paf 105:
1.63 paf 106: /// put a [value] under the [key] @returns existed or not
1.64.2.4! paf 107: template<typename R, typename F, typename I> R replace_maybe_append(K key, V value, F prevent, I info) {
1.64.2.3 paf 108: if(!value) {
1.64.2.4! paf 109: // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel)
1.64.2.3 paf 110: remove(key);
111: // this has nothing to do with properties, doing no special property handling here
112: return 0;
113: }
1.64 paf 114:
1.63 paf 115: if(is_full())
116: expand();
117:
118: uint code=hash_code(key);
119: uint index=code%allocated;
120: Pair **ref=&refs[index];
121: for(Pair *pair=*ref; pair; pair=pair->link)
122: if(pair->code==code && pair->key==key) {
123: // found a pair with the same key
124: pair->value=value;
125: return reinterpret_cast<R>(1);
126: }
127:
1.64.2.4! paf 128: // proper pair not found
! 129: // prevent-function intercepted put?
! 130: if(R result=prevent(value, info))
! 131: return result;
! 132:
! 133: //create&link_in new pair
1.63 paf 134: if(!*ref) // root cell were fused_refs?
135: fused_refs++; // not, we'll use it and record the fact
136: *ref=new Pair(code, key, value, *ref);
137: fpairs_count++;
138: return 0;
139: }
140:
1.64.2.1 paf 141: /// put a [value] under the [key] @returns existed or not
1.64.2.4! paf 142: template<typename R, typename F1, typename F2, typename I>
! 143: R maybe_replace_maybe_append(K key, V value, F1 prevent_replace, F2 prevent_append, I info)
! 144: {
1.64.2.3 paf 145: if(!value) {
1.64.2.4! paf 146: // they can come here from Temp_value_element::dctor to restore some empty value
1.64.2.3 paf 147: remove(key);
148: // this has nothing to do with properties, doing no special property handling here
149: return 0;
150: }
1.64.2.1 paf 151:
152: if(is_full())
153: expand();
154:
155: uint code=hash_code(key);
156: uint index=code%allocated;
157: Pair **ref=&refs[index];
158: for(Pair *pair=*ref; pair; pair=pair->link)
159: if(pair->code==code && pair->key==key) {
160: // found a pair with the same key
1.64.2.4! paf 161:
! 162: // prevent-function intercepted put?
! 163: if(R result=prevent_replace(pair->value, info))
! 164: return result;
! 165:
1.64.2.1 paf 166: pair->value=value;
167: return reinterpret_cast<R>(1);
168: }
169:
170: // proper pair not found
171: // prevent-function intercepted put?
1.64.2.4! paf 172: if(R result=prevent_append(value, info))
1.64.2.2 paf 173: return result;
174:
175: //create&link_in new pair
176: if(!*ref) // root cell were fused_refs?
177: fused_refs++; // not, we'll use it and record the fact
178: *ref=new Pair(code, key, value, *ref);
179: fpairs_count++;
180: return 0;
181: }
182:
183: /// put a [value] under the [key] @returns existed or not
1.64.2.4! paf 184: template<typename R, typename F1, typename I>
! 185: R maybe_replace_never_append(K key, V value, F1 prevent_replace, I info)
1.64.2.2 paf 186: {
1.64.2.3 paf 187: if(!value) {
1.64.2.4! paf 188: // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel)
1.64.2.3 paf 189: remove(key);
190: // this has nothing to do with properties, doing no special property handling here
191: return 0;
192: }
1.64.2.2 paf 193:
194: if(is_full())
195: expand();
196:
197: uint code=hash_code(key);
198: uint index=code%allocated;
199: Pair **ref=&refs[index];
200: for(Pair *pair=*ref; pair; pair=pair->link)
201: if(pair->code==code && pair->key==key) {
202: // found a pair with the same key
203:
204: // prevent-function intercepted put?
205: if(R result=prevent_replace(pair->value, info))
206: return result;
207:
208: pair->value=value;
209: return reinterpret_cast<R>(1);
210: }
211:
1.64.2.1 paf 212: return 0;
213: }
214:
1.59 paf 215: /// remove the [key] @returns existed or not
216: bool remove(K key) {
217: uint code=hash_code(key);
218: uint index=code%allocated;
219: for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link)
220: if((*ref)->code==code && (*ref)->key==key) {
221: // found a pair with the same key
222: Pair *next=(*ref)->link;
223: delete *ref;
224: *ref=next;
225: --fpairs_count;
226: return true;
227: }
1.8 paf 228:
1.59 paf 229: return false;
230: }
1.48 paf 231:
1.59 paf 232: /// get associated [value] by the [key]
233: V get(K key) const {
234: uint code=hash_code(key);
235: uint index=code%allocated;
236: for(Pair *pair=refs[index]; pair; pair=pair->link)
237: if(pair->code==code && pair->key==key)
238: return pair->value;
239:
240: return V(0);
1.33 paf 241: }
1.64.2.3 paf 242:
1.51 paf 243: /// put a [value] under the [key] if that [key] existed @returns existed or not
1.63 paf 244: bool put_replaced(K key, V value) {
1.59 paf 245: if(!value) {
246: remove(key);
247: return false;
248: }
249: uint code=hash_code(key);
250: uint index=code%allocated;
251: for(Pair *pair=refs[index]; pair; pair=pair->link)
252: if(pair->code==code && pair->key==key) {
253: // found a pair with the same key, replacing
254: pair->value=value;
255: return true;
256: }
257:
258: // proper pair not found
259: return false;
1.64 paf 260: }
261:
262: /// put a [value] under the [key] if that [key] existed @returns existed or not
263: template<typename R, typename F> R maybe_put_replaced(K key, V value, F prevent) {
1.64.2.3 paf 264: if(!value) {
265: // they can come here from Temp_value_element::dctor to restore some empty value
266: remove(key);
267: // this has nothing to do with properties, doing no special property handling here
268: return 0;
269: }
1.64 paf 270:
271: uint code=hash_code(key);
272: uint index=code%allocated;
273: for(Pair *pair=refs[index]; pair; pair=pair->link)
274: if(pair->code==code && pair->key==key) {
275: // found a pair with the same key, replacing
276: // prevent-function intercepted put?
277: if(R result=prevent(pair->value))
278: return result;
279:
280: pair->value=value;
281: return reinterpret_cast<R>(1);
282: }
283:
284: // proper pair not found
285: return 0;
1.59 paf 286: }
1.18 paf 287:
1.51 paf 288: /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
1.59 paf 289: bool put_dont_replace(K key, V value) {
290: if(!value) {
291: remove(key);
292: return false;
293: }
294: if(is_full())
295: expand();
296:
297: uint code=hash_code(key);
298: uint index=code%allocated;
299: Pair **ref=&refs[index];
300: for(Pair *pair=*ref; pair; pair=pair->link)
301: if(pair->code==code && pair->key==key) {
302: // found a pair with the same key, NOT replacing
303: return true;
304: }
305:
306: // proper pair not found -- create&link_in new pair
307: if(!*ref) // root cell were fused_refs?
308: fused_refs++; // not, we'll use it and record the fact
309: *ref=new Pair(code, key, value, *ref);
310: fpairs_count++;
311: return false;
312: }
1.18 paf 313:
1.59 paf 314: /** put all 'src' values if NO with same key existed
315: @todo optimize this.allocated==src.allocated case
316: */
317: void merge_dont_replace(const Hash& src) {
318: for(int i=0; i<src.allocated; i++)
319: for(Pair *pair=src.refs[i]; pair; pair=pair->link)
320: put_dont_replace(pair->key, pair->value);
1.36 paf 321: }
1.11 paf 322:
1.29 paf 323: /// number of elements in hash
1.59 paf 324: int count() const { return fpairs_count; }
1.25 paf 325:
1.59 paf 326: /// iterate over all pairs
327: template<typename I> void for_each(void callback(K, V, I), I info) const {
328: Pair **ref=refs;
329: for(int index=0; index<allocated; index++)
330: for(Pair *pair=*ref++; pair; pair=pair->link)
331: callback(pair->key, pair->value, info);
332: }
1.45 paf 333:
1.59 paf 334: /// iterate over all pairs
335: template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
336: Pair **ref=refs;
337: for(int index=0; index<allocated; index++)
338: for(Pair *pair=*ref++; pair; pair=pair->link)
339: callback(pair->key, pair->value, info);
340: }
1.38 paf 341:
1.59 paf 342: /// iterate over all pairs until condition becomes true, return that element
343: template<typename I> V first_that(bool callback(K, V, I), I info) const {
344: Pair **ref=refs;
345: for(int index=0; index<allocated; index++)
346: for(Pair *pair=*ref++; pair; pair=pair->link)
347: if(callback(pair->key, pair->value, info))
348: return pair->value;
349:
350: return V(0);
351: }
1.27 paf 352:
1.29 paf 353: /// remove all elements
1.59 paf 354: void clear() {
355: memset(refs, 0, sizeof(*refs)*allocated);
356: fpairs_count=fused_refs=0;
357: }
1.15 paf 358:
1.1 paf 359: private:
360:
1.39 paf 361: /// expand when these %% of allocated exausted
1.1 paf 362: enum {
363: THRESHOLD_PERCENT=75
364: };
1.9 paf 365:
1.61 paf 366: /// the index of [allocated] in [Hash_allocates]
1.19 paf 367: int allocates_index;
1.1 paf 368:
1.39 paf 369: /// number of allocated pairs
1.19 paf 370: int allocated;
1.1 paf 371:
1.59 paf 372: /// helper: expanding when fused_refs == threshold
1.1 paf 373: int threshold;
374:
1.39 paf 375: /// used pairs
1.59 paf 376: int fused_refs;
1.44 parser 377:
378: /// stored pairs total (including those by links)
1.59 paf 379: int fpairs_count;
1.1 paf 380:
1.39 paf 381: /// pair storage
1.59 paf 382: class Pair: public PA_Allocated {
383: public:
1.1 paf 384: uint code;
1.59 paf 385: K key;
386: V value;
1.1 paf 387: Pair *link;
1.2 paf 388:
1.59 paf 389: Pair(uint acode, K akey, V avalue, Pair *alink) :
1.1 paf 390: code(acode),
391: key(akey),
392: value(avalue),
1.2 paf 393: link(alink) {}
394: } **refs;
1.1 paf 395:
1.39 paf 396: /// filled to threshold: needs expanding
1.59 paf 397: bool is_full() { return fused_refs==threshold; }
1.5 paf 398:
1.39 paf 399: /// allocate larger buffer & rehash
1.59 paf 400: void expand() {
401: int old_allocated=allocated;
402: Pair **old_refs=refs;
403:
404: allocates_index=allocates_index+1<HASH_ALLOCATES_COUNT?allocates_index+1:HASH_ALLOCATES_COUNT-1;
405: // allocated bigger refs array
1.61 paf 406: allocated=Hash_allocates[allocates_index];
1.59 paf 407: threshold=allocated*THRESHOLD_PERCENT/100;
408: refs=new(UseGC) Pair*[allocated];
409:
410: // rehash
411: Pair **old_ref=old_refs;
412: for(int old_index=0; old_index<old_allocated; old_index++)
413: for(Pair *pair=*old_ref++; pair; ) {
414: Pair *next=pair->link;
415:
416: uint new_index=pair->code%allocated;
417: Pair **new_ref=&refs[new_index];
418: pair->link=*new_ref;
419: *new_ref=pair;
420:
421: pair=next;
422: }
423:
424: delete[] old_refs;
425: }
1.4 paf 426:
427: private: //disabled
428:
1.12 paf 429: Hash& operator = (const Hash&) { return *this; }
1.1 paf 430: };
1.59 paf 431:
432: /// useful generic hash function
433: inline void generic_hash_code(uint& result, char c) {
434: result=(result<<4)+c;
435: if(uint g=(result&0xF0000000)) {
436: result=result^(g>>24);
437: result=result^g;
438: }
439: }
440: /// useful generic hash function
441: inline void generic_hash_code(uint& result, const char* s) {
442: while(char c=*s++) {
443: result=(result<<4)+c;
444: if(uint g=(result&0xF0000000)) {
445: result=result^(g>>24);
446: result=result^g;
447: }
448: }
449: }
450:
451: /// useful generic hash function
452: inline void generic_hash_code(uint& result, const char* buf, size_t size) {
453: const char* end=buf+size;
454: while(buf<end) {
455: result=(result<<4)+*buf++;
456: if(uint g=(result&0xF0000000)) {
457: result=result^(g>>24);
458: result=result^g;
459: }
460: }
461: }
462:
463: /// simple hash code of int. used by EXIF mapping
464: inline uint hash_code(int self) {
465: uint result=0;
466: generic_hash_code(result, (const char*)&self, sizeof(self));
467: return result;
468: }
469:
470: /// Auto-object used to temporarily substituting/removing hash values
471: template <typename K, typename V>
1.55 paf 472: class Temp_hash_value {
1.59 paf 473: Hash<K, V>& fhash;
474: K fname;
475: V saved_value;
1.55 paf 476: public:
1.59 paf 477: Temp_hash_value(Hash<K, V>& ahash, K aname, V avalue) :
1.55 paf 478: fhash(ahash),
479: fname(aname),
480: saved_value(ahash.get(aname)) {
481: fhash.put(aname, avalue);
482: }
483: ~Temp_hash_value() {
484: fhash.put(fname, saved_value);
485: }
486: };
1.1 paf 487:
488: #endif
E-mail: