Annotation of parser3/src/include/pa_hash.h, revision 1.62

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

E-mail: