Annotation of parser3/src/include/pa_hash.h, revision 1.58.2.18
1.28 paf 1: /** @file
1.29 paf 2: Parser: hash class decl.
3:
1.58.2.10 paf 4: Copyright (c) 2001-2003 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.58.2.2 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.58.2.18! paf 20: static const char* IDENT_HASH_H="$Date: 2003/02/26 10:17:30 $";
1.1 paf 21:
1.14 paf 22: #include "pa_pool.h"
1.1 paf 23: #include "pa_types.h"
24:
1.29 paf 25: /**
1.58.2.2 paf 26: Simple hash.
1.29 paf 27:
1.58.2.2 paf 28: Automatically rehashed when almost is_full.
1.51 paf 29: Contains no 0 values.
30: get returning 0 means there were no such.
31: "put value 0" means "remove"
1.29 paf 32: */
1.58.2.2 paf 33: template<typename K, typename V> class Hash: public PA_Object {
1.1 paf 34: public:
35:
1.58.2.6 paf 36: typedef K key_type;
37: typedef V value_type;
1.8 paf 38:
1.58.2.1 paf 39: Hash() {
1.58.2.2 paf 40: allocated=allocates[allocates_index=0];
41: threshold=allocated*THRESHOLD_PERCENT/100;
42: fpairs_count=fused_refs=0;
1.58.2.17 paf 43: refs=new(0) Pair*[allocated];
1.43 parser 44: }
45:
1.58.2.1 paf 46: Hash(const Hash& source) {
1.58.2.2 paf 47: allocates_index=source.allocates_index;
48: allocated=source.allocated;
49: threshold=source.threshold;
50: fused_refs=source.fused_refs;
51: fpairs_count=source.fpairs_count;
1.58.2.17 paf 52: refs=new(0) Pair*[allocated];
53:
54: // clone & rehash
55: Pair **old_ref=source.refs;
56: for(int index=0; index<allocated; index++)
57: for(Pair *pair=*old_ref++; pair; ) {
58: Pair *next=pair->link;
59:
60: Pair **new_ref=&refs[index];
61: *new_ref=new Pair(pair->code, pair->key, pair->value, *new_ref);
62:
63: pair=next;
64: }
1.58.2.2 paf 65: }
66: ~Hash() {
67: destroy_pairs();
68: delete refs;
1.24 paf 69: }
1.10 paf 70:
1.51 paf 71: /// put a [value] under the [key] @returns existed or not
1.58.2.8 paf 72: bool put(K key, V value) {
1.58.2.18! paf 73: if(!value) {
! 74: remove(key);
! 75: return false;
! 76: }
1.58.2.2 paf 77: if(is_full())
78: expand();
79:
1.58.2.7 paf 80: uint code=hash_code(key);
1.58.2.2 paf 81: uint index=code%allocated;
82: Pair **ref=&refs[index];
83: for(Pair *pair=*ref; pair; pair=pair->link)
84: if(pair->code==code && pair->key==key) {
85: // found a pair with the same key
86: pair->value=value;
87: return true;
88: }
89:
90: // proper pair not found -- create&link_in new pair
91: if(!*ref) // root cell were fused_refs?
92: fused_refs++; // not, we'll use it and record the fact
93: *ref=new Pair(code, key, value, *ref);
94: fpairs_count++;
95: return false;
96: }
1.48 paf 97:
1.51 paf 98: /// remove the [key] @returns existed or not
1.58.2.8 paf 99: bool remove(K key) {
1.58.2.7 paf 100: uint code=hash_code(key);
1.58.2.2 paf 101: uint index=code%allocated;
102: for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link)
103: if((*ref)->code==code && (*ref)->key==key) {
104: // found a pair with the same key
105: Pair *next=(*ref)->link;
106: delete *ref;
107: *ref=next;
108: --fpairs_count;
109: return true;
110: }
111:
112: return false;
1.33 paf 113: }
1.58.2.2 paf 114:
1.29 paf 115: /// get associated [value] by the [key]
1.58.2.8 paf 116: V get(K key) const {
1.58.2.7 paf 117: uint code=hash_code(key);
1.58.2.2 paf 118: uint index=code%allocated;
119: for(Pair *pair=refs[index]; pair; pair=pair->link)
120: if(pair->code==code && pair->key==key)
121: return pair->value;
122:
1.58.2.12 paf 123: return V(0);
1.58.2.2 paf 124: }
1.17 paf 125:
1.51 paf 126: /// put a [value] under the [key] if that [key] existed @returns existed or not
1.58.2.8 paf 127: bool put_replace(K key, V value) {
1.58.2.18! paf 128: if(!value) {
! 129: remove(key);
! 130: return false;
! 131: }
1.58.2.7 paf 132: uint code=hash_code(key);
1.58.2.2 paf 133: uint index=code%allocated;
134: for(Pair *pair=refs[index]; pair; pair=pair->link)
135: if(pair->code==code && pair->key==key) {
136: // found a pair with the same key, replacing
137: pair->value=value;
138: return true;
139: }
140:
141: // proper pair not found
142: return false;
143: }
1.18 paf 144:
1.51 paf 145: /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
1.58.2.8 paf 146: bool put_dont_replace(K key, V value) {
1.58.2.18! paf 147: if(!value) {
! 148: remove(key);
! 149: return false;
! 150: }
1.58.2.2 paf 151: if(is_full())
152: expand();
153:
1.58.2.7 paf 154: uint code=hash_code(key);
1.58.2.2 paf 155: uint index=code%allocated;
156: Pair **ref=&refs[index];
157: for(Pair *pair=*ref; pair; pair=pair->link)
158: if(pair->code==code && pair->key==key) {
159: // found a pair with the same key, NOT replacing
160: return true;
161: }
162:
163: // proper pair not found -- create&link_in new pair
164: if(!*ref) // root cell were fused_refs?
165: fused_refs++; // not, we'll use it and record the fact
166: *ref=new Pair(code, key, value, *ref);
167: fpairs_count++;
168: return false;
169: }
1.18 paf 170:
1.58.2.17 paf 171: /** put all 'src' values if NO with same key existed
172: @todo optimize this.allocated==src.allocated case
173: */
1.58.2.2 paf 174: void merge_dont_replace(const Hash& src) {
175: for(int i=0; i<src.allocated; i++)
176: for(Pair *pair=src.refs[i]; pair; pair=pair->link)
177: put_dont_replace(pair->key, pair->value);
1.36 paf 178: }
1.11 paf 179:
1.29 paf 180: /// number of elements in hash
1.58.2.2 paf 181: int count() const { return fpairs_count; }
1.25 paf 182:
1.58.2.2 paf 183: /// iterate over all pairs
184: template<typename I> void for_each(void callback(K, V, I), I info) const {
185: Pair **ref=refs;
186: for(int index=0; index<allocated; index++)
187: for(Pair *pair=*ref++; pair; pair=pair->link)
188: callback(pair->key, pair->value, info);
189: }
1.45 paf 190:
1.58.2.2 paf 191: /// iterate over all pairs until condition becomes true, return that element
1.58.2.9 paf 192: template<typename I> V first_that(bool callback(K, V, I), I info) const {
1.58.2.2 paf 193: Pair **ref=refs;
194: for(int index=0; index<allocated; index++)
195: for(Pair *pair=*ref++; pair; pair=pair->link)
196: if(callback(pair->key, pair->value, info))
1.58.2.14 paf 197: return pair->value;
1.58.2.2 paf 198:
1.58.2.13 paf 199: return V(0);
1.58.2.2 paf 200: }
1.27 paf 201:
1.29 paf 202: /// remove all elements
1.58.2.2 paf 203: void clear() {
204: destroy_pairs(); memset(refs, 0, sizeof(*refs)*allocated);
205: fpairs_count=fused_refs=0;
206: }
1.15 paf 207:
1.1 paf 208: private:
209:
1.39 paf 210: /// expand when these %% of allocated exausted
1.1 paf 211: enum {
212: THRESHOLD_PERCENT=75
213: };
1.9 paf 214:
1.39 paf 215: /// the index of [allocated] in [allocates]
1.19 paf 216: int allocates_index;
1.1 paf 217:
1.39 paf 218: /// possible [allocates]. prime numbers
1.58.2.3 paf 219: static uint allocates[];
220:
221: static int allocates_count;
1.1 paf 222:
1.39 paf 223: /// number of allocated pairs
1.19 paf 224: int allocated;
1.1 paf 225:
1.58.2.2 paf 226: /// helper: expanding when fused_refs == threshold
1.1 paf 227: int threshold;
228:
1.39 paf 229: /// used pairs
1.58.2.2 paf 230: int fused_refs;
1.44 parser 231:
232: /// stored pairs total (including those by links)
1.58.2.2 paf 233: int fpairs_count;
1.1 paf 234:
1.39 paf 235: /// pair storage
1.58.2.2 paf 236: class Pair: public PA_Allocated {
1.58.2.4 paf 237: public:
1.1 paf 238: uint code;
1.58.2.8 paf 239: K key;
1.58.2.2 paf 240: V value;
1.1 paf 241: Pair *link;
1.2 paf 242:
1.58.2.8 paf 243: Pair(uint acode, K akey, V avalue, Pair *alink) :
1.1 paf 244: code(acode),
245: key(akey),
246: value(avalue),
1.2 paf 247: link(alink) {}
248: } **refs;
1.1 paf 249:
1.39 paf 250: /// filled to threshold: needs expanding
1.58.2.2 paf 251: bool is_full() { return fused_refs==threshold; }
1.5 paf 252:
1.39 paf 253: /// allocate larger buffer & rehash
1.58.2.2 paf 254: void expand() {
255: int old_allocated=allocated;
256: Pair **old_refs=refs;
257:
258: allocates_index=allocates_index+1<allocates_count?allocates_index+1:allocates_count-1;
1.58.2.17 paf 259: // allocated bigger refs array
1.58.2.2 paf 260: allocated=allocates[allocates_index];
261: threshold=allocated*THRESHOLD_PERCENT/100;
1.58.2.17 paf 262: refs=new(0) Pair*[allocated];
1.58.2.2 paf 263:
264: // rehash
265: Pair **old_ref=old_refs;
266: for(int old_index=0; old_index<old_allocated; old_index++)
267: for(Pair *pair=*old_ref++; pair; ) {
268: Pair *next=pair->link;
269:
270: uint new_index=pair->code%allocated;
271: Pair **new_ref=&refs[new_index];
272: pair->link=*new_ref;
273: *new_ref=pair;
274:
275: pair=next;
276: }
277:
278: delete old_refs;
279: }
280:
281: void destroy_pairs() {
282: Pair **ref=refs;
283: for(int index=0; index<allocated; index++) {
284: Pair *pair=*ref++;
285: while(pair) {
286: Pair *next=pair->link;
287: delete pair;
288: pair=next;
289: }
290: }
291: }
1.4 paf 292:
293: private: //disabled
294:
1.12 paf 295: Hash& operator = (const Hash&) { return *this; }
1.1 paf 296: };
1.58.2.3 paf 297:
298: /* Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster */
299: template<typename K, typename V>
300: uint Hash<K, V>::allocates[]={
301: 5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963,
302: 16229, 32531, 65407, 130987, 262237, 524521, 1048793,
303: 2097397, 4194103, 8388857, 16777447, 33554201, 67108961,
304: 134217487, 268435697, 536870683, 1073741621, 2147483399};
305:
306: template<typename K, typename V>
307: int Hash<K, V>::allocates_count=sizeof(allocates)/sizeof(uint);
1.58.2.5 paf 308:
309: /// useful generic hash function
1.58.2.15 paf 310: inline uint generic_hash_code(uint aresult, const char* start, size_t allocated) {
1.58.2.5 paf 311: uint result=aresult, g;
1.58.2.10 paf 312: const char* end=start+allocated;
1.58.2.5 paf 313:
314: while (start<end) {
315: result=(result<<4)+*start++;
316: if ((g=(result&0xF0000000))) {
317: result=result^(g>>24);
318: result=result^g;
319: }
320: }
321: return result;
1.58.2.15 paf 322: }
323:
324: /// simple hash code of int. used by EXIF mapping
325: inline uint hash_code(int self) {
1.58.2.16 paf 326: return generic_hash_code(0, (const char*)&self, sizeof(self));
1.58.2.5 paf 327: }
1.49 paf 328:
1.58.2.2 paf 329: /// Auto-object used to temporarily substituting/removing hash values
330: template <typename K, typename V>
1.55 paf 331: class Temp_hash_value {
1.58.2.11 paf 332: Hash<K, V>& fhash;
1.58.2.8 paf 333: K fname;
1.58.2.2 paf 334: V saved_value;
1.55 paf 335: public:
1.58.2.8 paf 336: Temp_hash_value(Hash<K, V>& ahash, K aname, V avalue) :
1.55 paf 337: fhash(ahash),
338: fname(aname),
339: saved_value(ahash.get(aname)) {
340: fhash.put(aname, avalue);
341: }
342: ~Temp_hash_value() {
343: fhash.put(fname, saved_value);
344: }
345: };
1.1 paf 346:
347: #endif
E-mail: