Annotation of parser3/src/include/pa_hash.h, revision 1.58.2.3
1.28 paf 1: /** @file
1.29 paf 2: Parser: hash class decl.
3:
1.58 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.3! paf 20: static const char* IDENT_HASH_H="$Date: 2003/01/23 11:37:55 $";
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.2 paf 36: typename K key_type;
37: typename 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.29 paf 60: /// useful generic hash function
1.58.2.2 paf 61: static uint generic_code(uint aresult, const char *start, uint allocated) {
62: uint result=aresult, g;
63: const char *end=start+allocated;
64:
65: while (start<end) {
66: result=(result<<4)+*start++;
67: if ((g=(result&0xF0000000))) {
68: result=result^(g>>24);
69: result=result^g;
70: }
71: }
72: return result;
73: }
1.8 paf 74:
1.51 paf 75: /// put a [value] under the [key] @returns existed or not
1.58.2.2 paf 76: bool put(const K key, V value) {
77: if(is_full())
78: expand();
79:
80: uint code=key.hash_code();
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.2 paf 99: bool remove(const K key) {
100: uint code=key.hash_code();
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.2 paf 116: V get(const K key) const {
117: uint code=key.hash_code();
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:
123: return 0;
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.2 paf 127: bool put_replace(const K key, V value) {
128: uint code=key.hash_code();
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.2 paf 142: bool put_dont_replace(const K key, V value) {
143: if(is_full())
144: expand();
145:
146: uint code=key.hash_code();
147: uint index=code%allocated;
148: Pair **ref=&refs[index];
149: for(Pair *pair=*ref; pair; pair=pair->link)
150: if(pair->code==code && pair->key==key) {
151: // found a pair with the same key, NOT replacing
152: return true;
153: }
154:
155: // proper pair not found -- create&link_in new pair
156: if(!*ref) // root cell were fused_refs?
157: fused_refs++; // not, we'll use it and record the fact
158: *ref=new Pair(code, key, value, *ref);
159: fpairs_count++;
160: return false;
161: }
1.18 paf 162:
1.29 paf 163: /// put all 'src' values if NO with same key existed
1.58.2.2 paf 164: void merge_dont_replace(const Hash& src) {
165: for(int i=0; i<src.allocated; i++)
166: for(Pair *pair=src.refs[i]; pair; pair=pair->link)
167: put_dont_replace(pair->key, pair->value);
168: // MAY:optimize this.allocated==src.allocated case
1.36 paf 169: }
1.11 paf 170:
1.29 paf 171: /// number of elements in hash
1.58.2.2 paf 172: int count() const { return fpairs_count; }
1.25 paf 173:
1.58.2.2 paf 174: /// iterate over all pairs
175: template<typename I> void for_each(void callback(K, V, I), I info) const {
176: Pair **ref=refs;
177: for(int index=0; index<allocated; index++)
178: for(Pair *pair=*ref++; pair; pair=pair->link)
179: callback(pair->key, pair->value, info);
180: }
1.45 paf 181:
1.58.2.2 paf 182: /// iterate over all pairs, passing references
183: template<typename I> void for_each(void callback(K, V&, I), I info) const {
184: Pair **ref=refs;
185: for(int index=0; index<allocated; index++)
186: for(Pair *pair=*ref++; pair; pair=pair->link)
187: callback(pair->key, pair->value, info);
188: }
1.38 paf 189:
1.58.2.2 paf 190: /// iterate over all pairs until condition becomes true, return that element
191: template<typename I> V *first_that(bool callback(K, V, I), I info) const {
192: Pair **ref=refs;
193: for(int index=0; index<allocated; index++)
194: for(Pair *pair=*ref++; pair; pair=pair->link)
195: if(callback(pair->key, pair->value, info))
196: return &pair->value;
197:
198: return 0;
199: }
1.27 paf 200:
1.29 paf 201: /// remove all elements
1.58.2.2 paf 202: void clear() {
203: destroy_pairs(); memset(refs, 0, sizeof(*refs)*allocated);
204: fpairs_count=fused_refs=0;
205: }
1.15 paf 206:
1.1 paf 207: private:
208:
1.39 paf 209: /// expand when these %% of allocated exausted
1.1 paf 210: enum {
211: THRESHOLD_PERCENT=75
212: };
1.9 paf 213:
1.39 paf 214: /// the index of [allocated] in [allocates]
1.19 paf 215: int allocates_index;
1.1 paf 216:
1.39 paf 217: /// possible [allocates]. prime numbers
1.58.2.3! paf 218: static uint allocates[];
! 219:
! 220: static int allocates_count;
1.1 paf 221:
1.39 paf 222: /// number of allocated pairs
1.19 paf 223: int allocated;
1.1 paf 224:
1.58.2.2 paf 225: /// helper: expanding when fused_refs == threshold
1.1 paf 226: int threshold;
227:
1.39 paf 228: /// used pairs
1.58.2.2 paf 229: int fused_refs;
1.44 parser 230:
231: /// stored pairs total (including those by links)
1.58.2.2 paf 232: int fpairs_count;
1.1 paf 233:
1.39 paf 234: /// pair storage
1.58.2.2 paf 235: class Pair: public PA_Allocated {
236: //friend class Hash;
1.2 paf 237:
1.1 paf 238: uint code;
1.58.2.2 paf 239: const K key;
240: V value;
1.1 paf 241: Pair *link;
1.2 paf 242:
1.58.2.2 paf 243: Pair(uint acode, const 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: // allocated bigger refs array
259: allocates_index=allocates_index+1<allocates_count?allocates_index+1:allocates_count-1;
260: allocated=allocates[allocates_index];
261: threshold=allocated*THRESHOLD_PERCENT/100;
262: refs=static_cast<Pair **>(pa_calloc(sizeof(Pair *)*allocated));
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.49 paf 308:
1.58.2.2 paf 309: /// Auto-object used to temporarily substituting/removing hash values
310: template <typename K, typename V>
1.55 paf 311: class Temp_hash_value {
312: Hash& fhash;
1.58.2.2 paf 313: const K fname;
314: V saved_value;
1.55 paf 315: public:
1.58.2.2 paf 316: Temp_hash_value(Hash<K, V>& ahash, const K aname, V avalue) :
1.55 paf 317: fhash(ahash),
318: fname(aname),
319: saved_value(ahash.get(aname)) {
320: fhash.put(aname, avalue);
321: }
322: ~Temp_hash_value() {
323: fhash.put(fname, saved_value);
324: }
325: };
1.1 paf 326:
327: #endif
E-mail: