Annotation of parser3/src/include/pa_hash.h, revision 1.64.2.1
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.1! paf 20: static const char * const IDENT_HASH_H="$Date: 2005/07/27 06:15:34 $";
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
107: template<typename R, typename F> R maybe_put(K key, V value, F prevent) {
1.64 paf 108: assert(value);
109:
1.63 paf 110: if(is_full())
111: expand();
112:
113: uint code=hash_code(key);
114: uint index=code%allocated;
115: Pair **ref=&refs[index];
116: for(Pair *pair=*ref; pair; pair=pair->link)
117: if(pair->code==code && pair->key==key) {
118: // found a pair with the same key
119:
120: // prevent-function intercepted put?
121: if(R result=prevent(pair->value))
122: return result;
123:
124: pair->value=value;
125: return reinterpret_cast<R>(1);
126: }
127:
128: // proper pair not found -- create&link_in new pair
129: if(!*ref) // root cell were fused_refs?
130: fused_refs++; // not, we'll use it and record the fact
131: *ref=new Pair(code, key, value, *ref);
132: fpairs_count++;
133: return 0;
134: }
135:
1.64.2.1! paf 136: /// put a [value] under the [key] @returns existed or not
! 137: template<typename R, typename F, typename I> R maybe_append(K key, V value, F prevent, I info) {
! 138: assert(value);
! 139:
! 140: if(is_full())
! 141: expand();
! 142:
! 143: uint code=hash_code(key);
! 144: uint index=code%allocated;
! 145: Pair **ref=&refs[index];
! 146: for(Pair *pair=*ref; pair; pair=pair->link)
! 147: if(pair->code==code && pair->key==key) {
! 148: // found a pair with the same key
! 149: pair->value=value;
! 150: return reinterpret_cast<R>(1);
! 151: }
! 152:
! 153: // proper pair not found
! 154: // prevent-function intercepted put?
! 155: if(R result=prevent(value, info))
! 156: return result;
! 157:
! 158: //create&link_in new pair
! 159: if(!*ref) // root cell were fused_refs?
! 160: fused_refs++; // not, we'll use it and record the fact
! 161: *ref=new Pair(code, key, value, *ref);
! 162: fpairs_count++;
! 163: return 0;
! 164: }
! 165:
1.59 paf 166: /// remove the [key] @returns existed or not
167: bool remove(K key) {
168: uint code=hash_code(key);
169: uint index=code%allocated;
170: for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link)
171: if((*ref)->code==code && (*ref)->key==key) {
172: // found a pair with the same key
173: Pair *next=(*ref)->link;
174: delete *ref;
175: *ref=next;
176: --fpairs_count;
177: return true;
178: }
1.8 paf 179:
1.59 paf 180: return false;
181: }
1.48 paf 182:
1.59 paf 183: /// get associated [value] by the [key]
184: V get(K key) const {
185: uint code=hash_code(key);
186: uint index=code%allocated;
187: for(Pair *pair=refs[index]; pair; pair=pair->link)
188: if(pair->code==code && pair->key==key)
189: return pair->value;
190:
191: return V(0);
1.33 paf 192: }
1.17 paf 193:
1.51 paf 194: /// put a [value] under the [key] if that [key] existed @returns existed or not
1.63 paf 195: bool put_replaced(K key, V value) {
1.59 paf 196: if(!value) {
197: remove(key);
198: return false;
199: }
200: uint code=hash_code(key);
201: uint index=code%allocated;
202: for(Pair *pair=refs[index]; pair; pair=pair->link)
203: if(pair->code==code && pair->key==key) {
204: // found a pair with the same key, replacing
205: pair->value=value;
206: return true;
207: }
208:
209: // proper pair not found
210: return false;
1.64 paf 211: }
212:
213: /// put a [value] under the [key] if that [key] existed @returns existed or not
214: template<typename R, typename F> R maybe_put_replaced(K key, V value, F prevent) {
215: assert(value);
216:
217: uint code=hash_code(key);
218: uint index=code%allocated;
219: for(Pair *pair=refs[index]; pair; pair=pair->link)
220: if(pair->code==code && pair->key==key) {
221: // found a pair with the same key, replacing
222: // prevent-function intercepted put?
223: if(R result=prevent(pair->value))
224: return result;
225:
226: pair->value=value;
227: return reinterpret_cast<R>(1);
228: }
229:
230: // proper pair not found
231: return 0;
1.59 paf 232: }
1.18 paf 233:
1.51 paf 234: /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
1.59 paf 235: bool put_dont_replace(K key, V value) {
236: if(!value) {
237: remove(key);
238: return false;
239: }
240: if(is_full())
241: expand();
242:
243: uint code=hash_code(key);
244: uint index=code%allocated;
245: Pair **ref=&refs[index];
246: for(Pair *pair=*ref; pair; pair=pair->link)
247: if(pair->code==code && pair->key==key) {
248: // found a pair with the same key, NOT replacing
249: return true;
250: }
251:
252: // proper pair not found -- create&link_in new pair
253: if(!*ref) // root cell were fused_refs?
254: fused_refs++; // not, we'll use it and record the fact
255: *ref=new Pair(code, key, value, *ref);
256: fpairs_count++;
257: return false;
258: }
1.18 paf 259:
1.59 paf 260: /** put all 'src' values if NO with same key existed
261: @todo optimize this.allocated==src.allocated case
262: */
263: void merge_dont_replace(const Hash& src) {
264: for(int i=0; i<src.allocated; i++)
265: for(Pair *pair=src.refs[i]; pair; pair=pair->link)
266: put_dont_replace(pair->key, pair->value);
1.36 paf 267: }
1.11 paf 268:
1.29 paf 269: /// number of elements in hash
1.59 paf 270: int count() const { return fpairs_count; }
1.25 paf 271:
1.59 paf 272: /// iterate over all pairs
273: template<typename I> void for_each(void callback(K, V, I), I info) const {
274: Pair **ref=refs;
275: for(int index=0; index<allocated; index++)
276: for(Pair *pair=*ref++; pair; pair=pair->link)
277: callback(pair->key, pair->value, info);
278: }
1.45 paf 279:
1.59 paf 280: /// iterate over all pairs
281: template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
282: Pair **ref=refs;
283: for(int index=0; index<allocated; index++)
284: for(Pair *pair=*ref++; pair; pair=pair->link)
285: callback(pair->key, pair->value, info);
286: }
1.38 paf 287:
1.59 paf 288: /// iterate over all pairs until condition becomes true, return that element
289: template<typename I> V first_that(bool callback(K, V, I), I info) const {
290: Pair **ref=refs;
291: for(int index=0; index<allocated; index++)
292: for(Pair *pair=*ref++; pair; pair=pair->link)
293: if(callback(pair->key, pair->value, info))
294: return pair->value;
295:
296: return V(0);
297: }
1.27 paf 298:
1.29 paf 299: /// remove all elements
1.59 paf 300: void clear() {
301: memset(refs, 0, sizeof(*refs)*allocated);
302: fpairs_count=fused_refs=0;
303: }
1.15 paf 304:
1.1 paf 305: private:
306:
1.39 paf 307: /// expand when these %% of allocated exausted
1.1 paf 308: enum {
309: THRESHOLD_PERCENT=75
310: };
1.9 paf 311:
1.61 paf 312: /// the index of [allocated] in [Hash_allocates]
1.19 paf 313: int allocates_index;
1.1 paf 314:
1.39 paf 315: /// number of allocated pairs
1.19 paf 316: int allocated;
1.1 paf 317:
1.59 paf 318: /// helper: expanding when fused_refs == threshold
1.1 paf 319: int threshold;
320:
1.39 paf 321: /// used pairs
1.59 paf 322: int fused_refs;
1.44 parser 323:
324: /// stored pairs total (including those by links)
1.59 paf 325: int fpairs_count;
1.1 paf 326:
1.39 paf 327: /// pair storage
1.59 paf 328: class Pair: public PA_Allocated {
329: public:
1.1 paf 330: uint code;
1.59 paf 331: K key;
332: V value;
1.1 paf 333: Pair *link;
1.2 paf 334:
1.59 paf 335: Pair(uint acode, K akey, V avalue, Pair *alink) :
1.1 paf 336: code(acode),
337: key(akey),
338: value(avalue),
1.2 paf 339: link(alink) {}
340: } **refs;
1.1 paf 341:
1.39 paf 342: /// filled to threshold: needs expanding
1.59 paf 343: bool is_full() { return fused_refs==threshold; }
1.5 paf 344:
1.39 paf 345: /// allocate larger buffer & rehash
1.59 paf 346: void expand() {
347: int old_allocated=allocated;
348: Pair **old_refs=refs;
349:
350: allocates_index=allocates_index+1<HASH_ALLOCATES_COUNT?allocates_index+1:HASH_ALLOCATES_COUNT-1;
351: // allocated bigger refs array
1.61 paf 352: allocated=Hash_allocates[allocates_index];
1.59 paf 353: threshold=allocated*THRESHOLD_PERCENT/100;
354: refs=new(UseGC) Pair*[allocated];
355:
356: // rehash
357: Pair **old_ref=old_refs;
358: for(int old_index=0; old_index<old_allocated; old_index++)
359: for(Pair *pair=*old_ref++; pair; ) {
360: Pair *next=pair->link;
361:
362: uint new_index=pair->code%allocated;
363: Pair **new_ref=&refs[new_index];
364: pair->link=*new_ref;
365: *new_ref=pair;
366:
367: pair=next;
368: }
369:
370: delete[] old_refs;
371: }
1.4 paf 372:
373: private: //disabled
374:
1.12 paf 375: Hash& operator = (const Hash&) { return *this; }
1.1 paf 376: };
1.59 paf 377:
378: /// useful generic hash function
379: inline void generic_hash_code(uint& result, char c) {
380: result=(result<<4)+c;
381: if(uint g=(result&0xF0000000)) {
382: result=result^(g>>24);
383: result=result^g;
384: }
385: }
386: /// useful generic hash function
387: inline void generic_hash_code(uint& result, const char* s) {
388: while(char c=*s++) {
389: result=(result<<4)+c;
390: if(uint g=(result&0xF0000000)) {
391: result=result^(g>>24);
392: result=result^g;
393: }
394: }
395: }
396:
397: /// useful generic hash function
398: inline void generic_hash_code(uint& result, const char* buf, size_t size) {
399: const char* end=buf+size;
400: while(buf<end) {
401: result=(result<<4)+*buf++;
402: if(uint g=(result&0xF0000000)) {
403: result=result^(g>>24);
404: result=result^g;
405: }
406: }
407: }
408:
409: /// simple hash code of int. used by EXIF mapping
410: inline uint hash_code(int self) {
411: uint result=0;
412: generic_hash_code(result, (const char*)&self, sizeof(self));
413: return result;
414: }
415:
416: /// Auto-object used to temporarily substituting/removing hash values
417: template <typename K, typename V>
1.55 paf 418: class Temp_hash_value {
1.59 paf 419: Hash<K, V>& fhash;
420: K fname;
421: V saved_value;
1.55 paf 422: public:
1.59 paf 423: Temp_hash_value(Hash<K, V>& ahash, K aname, V avalue) :
1.55 paf 424: fhash(ahash),
425: fname(aname),
426: saved_value(ahash.get(aname)) {
427: fhash.put(aname, avalue);
428: }
429: ~Temp_hash_value() {
430: fhash.put(fname, saved_value);
431: }
432: };
1.1 paf 433:
434: #endif
E-mail: