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

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

E-mail: