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

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

E-mail: