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

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

E-mail: