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