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

1.28      paf         1: /** @file
1.29      paf         2:        Parser: hash class decl.
                      3: 
1.66      paf         4:        Copyright (c) 2001-2005 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.68    ! misha      20: static const char * const IDENT_HASH_H="$Date: 2007/05/18 12:44:00 $";
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.68    ! misha      37: /// useful generic hash function
        !            38: inline void generic_hash_code(uint& result, char c) {
        !            39:        result=(result<<4)+c;
        !            40:        if(uint g=(result&0xF0000000)) {
        !            41:                result=result^(g>>24);
        !            42:                result=result^g;
        !            43:        }
        !            44: }
        !            45: /// useful generic hash function
        !            46: inline void generic_hash_code(uint& result, const char* s) {
        !            47:        while(char c=*s++) {
        !            48:                result=(result<<4)+c;
        !            49:                if(uint g=(result&0xF0000000)) {
        !            50:                        result=result^(g>>24);
        !            51:                        result=result^g;
        !            52:                }
        !            53:        }
        !            54: }
        !            55: 
        !            56: /// useful generic hash function
        !            57: inline void generic_hash_code(uint& result, const char* buf, size_t size) {
        !            58:        const char* end=buf+size;
        !            59:        while(buf<end) {
        !            60:                result=(result<<4)+*buf++;
        !            61:                if(uint g=(result&0xF0000000)) {
        !            62:                        result=result^(g>>24);
        !            63:                        result=result^g;
        !            64:                }
        !            65:        }
        !            66: }
        !            67: 
        !            68: /// simple hash code of int. used by EXIF mapping
        !            69: inline uint hash_code(int self) {
        !            70:        uint result=0;
        !            71:        generic_hash_code(result, (const char*)&self, sizeof(self));
        !            72:        return result;
        !            73: }
        !            74: 
1.29      paf        75: /** 
1.59      paf        76:        Simple hash.
1.29      paf        77: 
1.59      paf        78:        Automatically rehashed when almost is_full.
1.51      paf        79:        Contains no 0 values. 
                     80:                get returning 0 means there were no such.
                     81:                "put value 0" means "remove"
1.29      paf        82: */
1.59      paf        83: template<typename K, typename V> class Hash: public PA_Object {
1.1       paf        84: public:
                     85: 
1.59      paf        86:        typedef K key_type;
                     87:        typedef V value_type;
1.3       paf        88: 
1.59      paf        89:        Hash() { 
1.61      paf        90:                allocated=Hash_allocates[allocates_index=0];
1.59      paf        91:                threshold=allocated*THRESHOLD_PERCENT/100;
                     92:                fpairs_count=fused_refs=0;
                     93:                refs=new(UseGC) Pair*[allocated];
                     94:        }
1.25      paf        95: 
1.59      paf        96:        Hash(const Hash& source) {
                     97:                allocates_index=source.allocates_index;
                     98:                allocated=source.allocated;
                     99:                threshold=source.threshold;
                    100:                fused_refs=source.fused_refs;
                    101:                fpairs_count=source.fpairs_count;
                    102:                refs=new(UseGC) Pair*[allocated];
                    103: 
                    104:                // clone & rehash
                    105:                Pair **old_ref=source.refs;
                    106:                for(int index=0; index<allocated; index++)
                    107:                        for(Pair *pair=*old_ref++; pair; ) {
                    108:                                Pair *next=pair->link;
1.45      paf       109: 
1.59      paf       110:                                Pair **new_ref=&refs[index];
                    111:                                *new_ref=new Pair(pair->code, pair->key, pair->value, *new_ref);
1.38      paf       112: 
1.59      paf       113:                                pair=next;
                    114:                        }
1.43      parser    115:        }
                    116: 
1.59      paf       117:        /// put a [value] under the [key] @returns existed or not
                    118:        bool put(K key, V value) {
                    119:                if(!value) {
                    120:                        remove(key);
                    121:                        return false;
                    122:                }
                    123:                if(is_full()) 
                    124:                        expand();
                    125: 
                    126:                uint code=hash_code(key);
                    127:                uint index=code%allocated;
                    128:                Pair **ref=&refs[index];
                    129:                for(Pair *pair=*ref; pair; pair=pair->link)
                    130:                        if(pair->code==code && pair->key==key) {
                    131:                                // found a pair with the same key
                    132:                                pair->value=value;
                    133:                                return true;
                    134:                        }
                    135:                
                    136:                // proper pair not found -- create&link_in new pair
                    137:                if(!*ref) // root cell were fused_refs?
                    138:                        fused_refs++; // not, we'll use it and record the fact
                    139:                *ref=new Pair(code, key, value, *ref);
                    140:                fpairs_count++;
                    141:                return false;
1.24      paf       142:        }
1.10      paf       143: 
1.63      paf       144:        /// put a [value] under the [key] @returns existed or not
1.65      paf       145:        template<typename R, typename F, typename I> R replace_maybe_append(K key, V value, F prevent, I info) {
                    146:                if(!value) {
                    147:                        // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel)
                    148:                        remove(key);
                    149:                        // this has nothing to do with properties, doing no special property handling here
                    150:                        return 0; 
                    151:                }
1.64      paf       152: 
1.63      paf       153:                if(is_full()) 
                    154:                        expand();
                    155: 
                    156:                uint code=hash_code(key);
                    157:                uint index=code%allocated;
                    158:                Pair **ref=&refs[index];
                    159:                for(Pair *pair=*ref; pair; pair=pair->link)
                    160:                        if(pair->code==code && pair->key==key) {
                    161:                                // found a pair with the same key
1.65      paf       162:                                pair->value=value;
                    163:                                return reinterpret_cast<R>(1);
                    164:                        }
                    165:                
                    166:                // proper pair not found 
                    167:                // prevent-function intercepted append?
                    168:                if(R result=prevent(value, info))
                    169:                        return result;
                    170:                
                    171:                //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 0;
                    177:        }
1.63      paf       178: 
1.65      paf       179:        /// put a [value] under the [key] @returns existed or not
                    180:        template<typename R, typename F1, typename F2, typename I> 
                    181:                R maybe_replace_maybe_append(K key, V value, F1 prevent_replace, F2 prevent_append, I info) 
                    182:        {
                    183:                if(!value) {
                    184:                        // they can come here from Temp_value_element::dctor to restore some empty value
                    185:                        remove(key);
                    186:                        // this has nothing to do with properties, doing no special property handling here
                    187:                        return 0; 
                    188:                }
                    189: 
                    190:                if(is_full()) 
                    191:                        expand();
                    192: 
                    193:                uint code=hash_code(key);
                    194:                uint index=code%allocated;
                    195:                Pair **ref=&refs[index];
                    196:                for(Pair *pair=*ref; pair; pair=pair->link)
                    197:                        if(pair->code==code && pair->key==key) {
                    198:                                // found a pair with the same key
                    199: 
                    200:                                // prevent-function intercepted replace?
                    201:                                if(R result=prevent_replace(pair->value, info))
1.63      paf       202:                                        return result;
                    203:                                
                    204:                                pair->value=value;
                    205:                                return reinterpret_cast<R>(1);
                    206:                        }
                    207:                
1.65      paf       208:                // proper pair not found 
                    209:                // prevent-function intercepted append?
                    210:                if(R result=prevent_append(value, info))
                    211:                        return result;
                    212:                
                    213:                //create&link_in new pair
1.63      paf       214:                if(!*ref) // root cell were fused_refs?
                    215:                        fused_refs++; // not, we'll use it and record the fact
                    216:                *ref=new Pair(code, key, value, *ref);
                    217:                fpairs_count++;
                    218:                return 0;
                    219:        }
                    220: 
1.65      paf       221:        /// put a [value] under the [key] @returns existed or not
                    222:        template<typename R, typename F1, typename I> 
                    223:                R maybe_replace_never_append(K key, V value, F1 prevent_replace, I info) 
                    224:        {
                    225:                if(!value) {
                    226:                        // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel)
                    227:                        remove(key);
                    228:                        // this has nothing to do with properties, doing no special property handling here
                    229:                        return 0; 
                    230:                }
                    231: 
                    232:                if(is_full()) 
                    233:                        expand();
                    234: 
                    235:                uint code=hash_code(key);
                    236:                uint index=code%allocated;
                    237:                Pair **ref=&refs[index];
                    238:                for(Pair *pair=*ref; pair; pair=pair->link)
                    239:                        if(pair->code==code && pair->key==key) {
                    240:                                // found a pair with the same key
                    241: 
                    242:                                // prevent-function intercepted replace?
                    243:                                if(R result=prevent_replace(pair->value, info))
                    244:                                        return result;
                    245:                                
                    246:                                pair->value=value;
                    247:                                return reinterpret_cast<R>(1);
                    248:                        }
                    249:                
                    250:                return 0;
                    251:        }
                    252: 
1.59      paf       253:        /// remove the [key] @returns existed or not
                    254:        bool remove(K key) {
                    255:                uint code=hash_code(key);
                    256:                uint index=code%allocated;
                    257:                for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link)
                    258:                        if((*ref)->code==code && (*ref)->key==key) {
                    259:                                // found a pair with the same key
                    260:                                Pair *next=(*ref)->link;
                    261:                                delete *ref;
                    262:                                *ref=next;
                    263:                                --fpairs_count;
                    264:                                return true;
                    265:                        }
1.8       paf       266: 
1.59      paf       267:                return false;
                    268:        }
1.48      paf       269: 
1.67      misha     270:        /// returns exist key or not
                    271:        bool contain(K key){
                    272:                uint code=hash_code(key);
                    273:                uint index=code%allocated;
                    274:                for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link){
                    275:                        if((*ref)->code==code && (*ref)->key==key) {
                    276:                                return true;
                    277:                        }
                    278:                }
                    279: 
                    280:                return false;
                    281:        }
                    282: 
                    283: 
1.59      paf       284:        /// get associated [value] by the [key]
                    285:        V get(K key) const {
                    286:                uint code=hash_code(key);
                    287:                uint index=code%allocated;
                    288:                for(Pair *pair=refs[index]; pair; pair=pair->link)
                    289:                        if(pair->code==code && pair->key==key)
                    290:                                return pair->value;
                    291:                
                    292:                return V(0);
1.33      paf       293:        }
1.65      paf       294:  
1.51      paf       295:        /// put a [value] under the [key] if that [key] existed @returns existed or not
1.63      paf       296:        bool put_replaced(K key, V value) {
1.59      paf       297:                if(!value) {
                    298:                        remove(key);
                    299:                        return false;
                    300:                }
                    301:                uint code=hash_code(key);
                    302:                uint index=code%allocated;
                    303:                for(Pair *pair=refs[index]; pair; pair=pair->link)
                    304:                        if(pair->code==code && pair->key==key) {
                    305:                                // found a pair with the same key, replacing
                    306:                                pair->value=value;
                    307:                                return true;
                    308:                        }
                    309: 
                    310:                // proper pair not found 
                    311:                return false;
1.64      paf       312:        }
                    313: 
                    314:        /// put a [value] under the [key] if that [key] existed @returns existed or not
                    315:        template<typename R, typename F> R maybe_put_replaced(K key, V value, F prevent) {
1.65      paf       316:                if(!value) {
                    317:                        // they can come here from Temp_value_element::dctor to restore some empty value
                    318:                        remove(key);
                    319:                        // this has nothing to do with properties, doing no special property handling here
                    320:                        return 0; 
                    321:                }
1.64      paf       322: 
                    323:                uint code=hash_code(key);
                    324:                uint index=code%allocated;
                    325:                for(Pair *pair=refs[index]; pair; pair=pair->link)
                    326:                        if(pair->code==code && pair->key==key) {
                    327:                                // found a pair with the same key, replacing
                    328:                                // prevent-function intercepted put?
                    329:                                if(R result=prevent(pair->value))
                    330:                                        return result;
                    331:                                
                    332:                                pair->value=value;
                    333:                                return reinterpret_cast<R>(1);
                    334:                        }
                    335: 
                    336:                // proper pair not found 
                    337:                return 0;
1.59      paf       338:        }
1.18      paf       339: 
1.51      paf       340:        /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
1.59      paf       341:        bool put_dont_replace(K key, V value) {
                    342:                if(!value) {
                    343:                        remove(key);
                    344:                        return false;
                    345:                }
                    346:                if(is_full()) 
                    347:                        expand();
                    348: 
                    349:                uint code=hash_code(key);
                    350:                uint index=code%allocated;
                    351:                Pair **ref=&refs[index];
                    352:                for(Pair *pair=*ref; pair; pair=pair->link)
                    353:                        if(pair->code==code && pair->key==key) {
                    354:                                // found a pair with the same key, NOT replacing
                    355:                                return true;
                    356:                        }
                    357: 
                    358:                // proper pair not found -- create&link_in new pair
                    359:                if(!*ref) // root cell were fused_refs?
                    360:                        fused_refs++; // not, we'll use it and record the fact
                    361:                *ref=new Pair(code, key, value, *ref);
                    362:                fpairs_count++;
                    363:                return false;
                    364:        }
1.18      paf       365: 
1.59      paf       366:        /** put all 'src' values if NO with same key existed
                    367:                @todo optimize this.allocated==src.allocated case
                    368:        */
                    369:        void merge_dont_replace(const Hash& src) {
                    370:                for(int i=0; i<src.allocated; i++)
                    371:                        for(Pair *pair=src.refs[i]; pair; pair=pair->link)
                    372:                                put_dont_replace(pair->key, pair->value);
1.36      paf       373:        }
1.11      paf       374: 
1.29      paf       375:        /// number of elements in hash
1.59      paf       376:        int count() const { return fpairs_count; }
1.25      paf       377: 
1.59      paf       378:        /// iterate over all pairs
                    379:        template<typename I> void for_each(void callback(K, V, I), I info) const {
                    380:                Pair **ref=refs;
                    381:                for(int index=0; index<allocated; index++)
                    382:                        for(Pair *pair=*ref++; pair; pair=pair->link)
                    383:                                callback(pair->key, pair->value, info);
                    384:        }
1.45      paf       385: 
1.59      paf       386:        /// iterate over all pairs
                    387:        template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
                    388:                Pair **ref=refs;
                    389:                for(int index=0; index<allocated; index++)
                    390:                        for(Pair *pair=*ref++; pair; pair=pair->link)
                    391:                                callback(pair->key, pair->value, info);
                    392:        }
1.38      paf       393: 
1.59      paf       394:        /// iterate over all pairs until condition becomes true, return that element
                    395:        template<typename I> V first_that(bool callback(K, V, I), I info) const {
                    396:                Pair **ref=refs;
                    397:                for(int index=0; index<allocated; index++)
                    398:                        for(Pair *pair=*ref++; pair; pair=pair->link)
                    399:                                if(callback(pair->key, pair->value, info))
                    400:                                        return pair->value;
                    401:                
                    402:                return V(0);
                    403:        }
1.27      paf       404: 
1.29      paf       405:        /// remove all elements
1.59      paf       406:        void clear() {
                    407:                memset(refs, 0, sizeof(*refs)*allocated);
                    408:                fpairs_count=fused_refs=0;      
                    409:        }
1.15      paf       410: 
1.1       paf       411: private:
                    412: 
1.39      paf       413:        /// expand when these %% of allocated exausted
1.1       paf       414:        enum {
                    415:                THRESHOLD_PERCENT=75
                    416:        };
1.9       paf       417: 
1.61      paf       418:        /// the index of [allocated] in [Hash_allocates]
1.19      paf       419:        int allocates_index;
1.1       paf       420: 
1.39      paf       421:        /// number of allocated pairs
1.19      paf       422:        int allocated;
1.1       paf       423: 
1.59      paf       424:        /// helper: expanding when fused_refs == threshold
1.1       paf       425:        int threshold;
                    426: 
1.39      paf       427:        /// used pairs
1.59      paf       428:        int fused_refs;
1.44      parser    429: 
                    430:        /// stored pairs total (including those by links)
1.59      paf       431:        int fpairs_count;
1.1       paf       432: 
1.39      paf       433:        /// pair storage
1.59      paf       434:        class Pair: public PA_Allocated {
                    435:        public:
1.1       paf       436:                uint code;
1.59      paf       437:                K key;
                    438:                V value;
1.1       paf       439:                Pair *link;
1.2       paf       440:                
1.59      paf       441:                Pair(uint acode, K akey, V avalue, Pair *alink) :
1.1       paf       442:                        code(acode),
                    443:                        key(akey),
                    444:                        value(avalue),
1.2       paf       445:                        link(alink) {}
                    446:        } **refs;
1.1       paf       447: 
1.39      paf       448:        /// filled to threshold: needs expanding
1.59      paf       449:        bool is_full() { return fused_refs==threshold; }
1.5       paf       450: 
1.39      paf       451:        /// allocate larger buffer & rehash
1.59      paf       452:        void expand() {
                    453:                int old_allocated=allocated;
                    454:                Pair **old_refs=refs;
                    455: 
                    456:                allocates_index=allocates_index+1<HASH_ALLOCATES_COUNT?allocates_index+1:HASH_ALLOCATES_COUNT-1;
                    457:                // allocated bigger refs array
1.61      paf       458:                allocated=Hash_allocates[allocates_index];
1.59      paf       459:                threshold=allocated*THRESHOLD_PERCENT/100;
                    460:                refs=new(UseGC) Pair*[allocated];
                    461: 
                    462:                // rehash
                    463:                Pair **old_ref=old_refs;
                    464:                for(int old_index=0; old_index<old_allocated; old_index++)
                    465:                        for(Pair *pair=*old_ref++; pair; ) {
                    466:                                Pair *next=pair->link;
                    467: 
                    468:                                uint new_index=pair->code%allocated;
                    469:                                Pair **new_ref=&refs[new_index];
                    470:                                pair->link=*new_ref;
                    471:                                *new_ref=pair;
                    472: 
                    473:                                pair=next;
                    474:                        }
                    475: 
                    476:                delete[] old_refs;
                    477:        }
1.4       paf       478: 
                    479: private: //disabled
                    480: 
1.12      paf       481:        Hash& operator = (const Hash&) { return *this; }
1.1       paf       482: };
1.59      paf       483: 
                    484: ///    Auto-object used to temporarily substituting/removing hash values
                    485: template <typename K, typename V>
1.55      paf       486: class Temp_hash_value {
1.59      paf       487:        Hash<K, V>& fhash;
                    488:        K fname;
                    489:        V saved_value;
1.55      paf       490: public:
1.59      paf       491:        Temp_hash_value(Hash<K, V>& ahash, K aname, V avalue) : 
1.55      paf       492:                fhash(ahash),
                    493:                fname(aname),
                    494:                saved_value(ahash.get(aname)) {
                    495:                fhash.put(aname, avalue);
                    496:        }
                    497:        ~Temp_hash_value() { 
                    498:                fhash.put(fname, saved_value);
                    499:        }
                    500: };
1.1       paf       501: 
                    502: #endif

E-mail: