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