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

1.28      paf         1: /** @file
1.29      paf         2:        Parser: hash class decl.
                      3: 
1.96    ! moko        4:        Copyright (c) 2001-2017 Art. Lebedev Studio (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.96    ! moko       20: #define IDENT_PA_HASH_H "$Id: pa_hash.h,v 1.95 2016/07/04 17:26:23 moko Exp $"
1.1       paf        21: 
1.59      paf        22: #include "pa_memory.h"
1.1       paf        23: #include "pa_types.h"
1.74      misha      24: #include "pa_string.h"
1.59      paf        25: 
                     26: const int HASH_ALLOCATES_COUNT=29;
1.1       paf        27: 
1.61      paf        28: /** Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster 
                     29: 
                     30:        paf: HPUX ld could not handle static member: unsatisfied symbols
                     31: */
                     32: static uint Hash_allocates[HASH_ALLOCATES_COUNT]={
                     33:        5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963, 
                     34:        16229, 32531, 65407, 130987, 262237, 524521, 1048793, 
                     35:        2097397, 4194103, 8388857, 16777447, 33554201, 67108961, 
                     36:        134217487, 268435697, 536870683, 1073741621, 2147483399};
                     37: 
1.68      misha      38: /// useful generic hash function
                     39: inline void generic_hash_code(uint& result, char c) {
                     40:        result=(result<<4)+c;
                     41:        if(uint g=(result&0xF0000000)) {
                     42:                result=result^(g>>24);
                     43:                result=result^g;
                     44:        }
                     45: }
                     46: /// useful generic hash function
                     47: inline void generic_hash_code(uint& result, const char* s) {
                     48:        while(char c=*s++) {
                     49:                result=(result<<4)+c;
                     50:                if(uint g=(result&0xF0000000)) {
                     51:                        result=result^(g>>24);
                     52:                        result=result^g;
                     53:                }
                     54:        }
                     55: }
                     56: 
                     57: /// useful generic hash function
                     58: inline void generic_hash_code(uint& result, const char* buf, size_t size) {
                     59:        const char* end=buf+size;
                     60:        while(buf<end) {
                     61:                result=(result<<4)+*buf++;
                     62:                if(uint g=(result&0xF0000000)) {
                     63:                        result=result^(g>>24);
                     64:                        result=result^g;
                     65:                }
                     66:        }
                     67: }
                     68: 
                     69: /// simple hash code of int. used by EXIF mapping
                     70: inline uint hash_code(int self) {
                     71:        uint result=0;
                     72:        generic_hash_code(result, (const char*)&self, sizeof(self));
                     73:        return result;
                     74: }
                     75: 
1.75      misha      76: #endif // PA_HASH_H
                     77: 
                     78: #ifndef PA_HASH_CLASS
                     79: #define PA_HASH_CLASS
1.29      paf        80: /** 
1.59      paf        81:        Simple hash.
1.29      paf        82: 
1.59      paf        83:        Automatically rehashed when almost is_full.
1.51      paf        84:        Contains no 0 values. 
                     85:                get returning 0 means there were no such.
                     86:                "put value 0" means "remove"
1.29      paf        87: */
1.75      misha      88: #ifdef HASH_ORDER
                     89: 
                     90: #undef HASH
                     91: #undef HASH_STRING
1.79      misha      92: #undef HASH_NEW_PAIR
1.88      moko       93: #undef HASH_ORDER_CLEAR
1.79      misha      94: #undef HASH_FOR_EACH
1.75      misha      95: 
                     96: #define HASH OrderedHash
                     97: #define HASH_STRING OrderedHashString
1.79      misha      98: #define HASH_NEW_PAIR(code, key, value) *ref=new Pair(code, key, value, *ref, this->last); this->last=&((*ref)->next)
1.88      moko       99: #define HASH_ORDER_CLEAR() first=0; last=&first
1.79      misha     100: 
                    101: #define HASH_FOR_EACH \
                    102:        for(Pair *pair=this->first; pair; pair=pair->next)
1.75      misha     103: 
                    104: #else
                    105: 
                    106: #define HASH Hash
                    107: #define HASH_STRING HashString
1.79      misha     108: #define HASH_NEW_PAIR(code, key, value) *ref=new Pair(code, key, value, *ref)
1.88      moko      109: #define HASH_ORDER_CLEAR()
1.79      misha     110: 
                    111: #define HASH_FOR_EACH \
                    112:        Pair **ref=this->refs; \
                    113:        for(int index=0; index<this->allocated; index++) \
                    114:                for(Pair *pair=*ref++; pair; pair=pair->link)
1.75      misha     115: 
                    116: #endif
                    117: 
                    118: template<typename K, typename V> class HASH: public PA_Object {
1.91      moko      119: protected:
                    120:        class Pair;
1.1       paf       121: public:
1.59      paf       122:        typedef K key_type;
                    123:        typedef V value_type;
1.3       paf       124: 
1.75      misha     125:        HASH() { 
1.61      paf       126:                allocated=Hash_allocates[allocates_index=0];
1.59      paf       127:                fpairs_count=fused_refs=0;
1.87      moko      128:                refs=new Pair*[allocated];
1.88      moko      129:                HASH_ORDER_CLEAR();
1.59      paf       130:        }
1.25      paf       131: 
1.75      misha     132:        HASH(const HASH& source) {
1.59      paf       133:                allocates_index=source.allocates_index;
                    134:                allocated=source.allocated;
                    135:                fused_refs=source.fused_refs;
                    136:                fpairs_count=source.fpairs_count;
1.87      moko      137:                refs=new Pair*[allocated];
1.81      moko      138:                // clone & rehash
1.75      misha     139: #ifdef HASH_ORDER
1.88      moko      140:                HASH_ORDER_CLEAR();
1.81      moko      141:                for(Pair *pair=source.first; pair; pair=pair->next)
                    142:                {
                    143:                        uint index=pair->code%allocated;
                    144:                        Pair **ref=&refs[index];
                    145:                        HASH_NEW_PAIR(pair->code, pair->key, pair->value);
                    146:                }
                    147: #else
                    148:                for(int i=0; i<source.allocated; i++)
                    149:                        for(Pair *pair=source.refs[i]; pair; pair=pair->link)
                    150:                        {
                    151:                                Pair **ref=&refs[i];
1.79      misha     152:                                HASH_NEW_PAIR(pair->code, pair->key, pair->value);
1.59      paf       153:                        }
1.81      moko      154: #endif
1.43      parser    155:        }
                    156: 
1.73      misha     157: #ifdef USE_DESTRUCTORS
1.75      misha     158:        ~HASH() {
1.72      misha     159:                Pair **ref=refs;
                    160:                for(int index=0; index<allocated; index++)
                    161:                        for(Pair *pair=*ref++; pair;){
                    162:                                Pair *next=pair->link;
                    163:                                delete pair;
                    164:                                pair=next;
                    165:                        }
1.71      misha     166:                delete[] refs;
                    167:        }
1.73      misha     168: #endif
1.71      misha     169: 
1.59      paf       170:        /// put a [value] under the [key] @returns existed or not
                    171:        bool put(K key, V value) {
                    172:                if(!value) {
                    173:                        remove(key);
                    174:                        return false;
                    175:                }
                    176:                if(is_full()) 
                    177:                        expand();
                    178: 
                    179:                uint code=hash_code(key);
                    180:                uint index=code%allocated;
                    181:                Pair **ref=&refs[index];
                    182:                for(Pair *pair=*ref; pair; pair=pair->link)
                    183:                        if(pair->code==code && pair->key==key) {
                    184:                                // found a pair with the same key
                    185:                                pair->value=value;
                    186:                                return true;
                    187:                        }
                    188:                
                    189:                // proper pair not found -- create&link_in new pair
                    190:                if(!*ref) // root cell were fused_refs?
                    191:                        fused_refs++; // not, we'll use it and record the fact
1.79      misha     192:                HASH_NEW_PAIR(code, key, value);
1.59      paf       193:                fpairs_count++;
                    194:                return false;
1.24      paf       195:        }
1.10      paf       196: 
1.59      paf       197:        /// remove the [key] @returns existed or not
                    198:        bool remove(K key) {
                    199:                uint code=hash_code(key);
                    200:                uint index=code%allocated;
1.75      misha     201:                for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link){
                    202:                        Pair *pair=*ref;
                    203:                        if(pair->code==code && pair->key==key) {
1.59      paf       204:                                // found a pair with the same key
1.75      misha     205:                                Pair *next=pair->link;
                    206: #ifdef HASH_ORDER
                    207:                                *(pair->prev)=pair->next;
                    208:                                if(pair->next)
                    209:                                        pair->next->prev=pair->prev;
                    210:                                else
                    211:                                        last=pair->prev;
                    212: #endif
1.59      paf       213:                                *ref=next;
                    214:                                --fpairs_count;
                    215:                                return true;
                    216:                        }
1.75      misha     217:                }
1.8       paf       218: 
1.59      paf       219:                return false;
                    220:        }
1.48      paf       221: 
1.70      misha     222:        /// return true if key exists
1.69      misha     223:        bool contains(K key){
1.67      misha     224:                uint code=hash_code(key);
                    225:                uint index=code%allocated;
1.70      misha     226:                for(Pair *pair=refs[index]; pair; pair=pair->link){
                    227:                        if(pair->code==code && pair->key==key)
1.67      misha     228:                                return true;
                    229:                }
                    230: 
                    231:                return false;
                    232:        }
                    233: 
1.59      paf       234:        /// get associated [value] by the [key]
                    235:        V get(K key) const {
                    236:                uint code=hash_code(key);
                    237:                uint index=code%allocated;
                    238:                for(Pair *pair=refs[index]; pair; pair=pair->link)
                    239:                        if(pair->code==code && pair->key==key)
                    240:                                return pair->value;
                    241:                
                    242:                return V(0);
1.33      paf       243:        }
1.70      misha     244: 
1.82      misha     245: #ifdef HASH_ORDER
1.86      misha     246:        String::Body first_key() const {
1.92      moko      247: #ifdef HASH_CODE_CACHING
1.86      misha     248:                return (first) ? String::Body(first->key, first->code) : String::Body();
1.92      moko      249: #else
                    250:                return (first) ? first->key : String::Body();
                    251: #endif
1.86      misha     252:        }
                    253: 
1.82      misha     254:        V first_value() const {
                    255:                return (first) ? first->value : V(0);
                    256:        }
                    257: 
1.86      misha     258:        String::Body last_key() const {
                    259:                if (fpairs_count) {
                    260:                        Pair* pair = (Pair*)((char *)last - offsetof(Pair, next));
1.92      moko      261: #ifdef HASH_CODE_CACHING
1.86      misha     262:                        return String::Body(pair->key, pair->code);
1.92      moko      263: #else
                    264:                        return pair->key;
                    265: #endif
1.86      misha     266:                } else {
                    267:                        return String::Body();
                    268:                }
                    269:        }
                    270: 
1.82      misha     271:        V last_value() const {
                    272:                return (fpairs_count) ? ((Pair *)((char *)last - offsetof(Pair, next)))->value : V(0);
                    273:        }
1.88      moko      274: 
                    275:        void order_clear() {
                    276:                HASH_ORDER_CLEAR();
                    277:        }
                    278: 
                    279:        void order_next(Pair* pair) {
                    280:                pair->prev=last;
                    281:                pair->next=0;
                    282:                *last=pair;
                    283:                last=&(pair->next);
                    284:        }
                    285: 
1.92      moko      286: #endif //HASH_ORDER
1.82      misha     287: 
1.51      paf       288:        /// put a [value] under the [key] if that [key] existed @returns existed or not
1.63      paf       289:        bool put_replaced(K key, V value) {
1.59      paf       290:                if(!value) {
                    291:                        remove(key);
                    292:                        return false;
                    293:                }
                    294:                uint code=hash_code(key);
                    295:                uint index=code%allocated;
                    296:                for(Pair *pair=refs[index]; pair; pair=pair->link)
                    297:                        if(pair->code==code && pair->key==key) {
                    298:                                // found a pair with the same key, replacing
                    299:                                pair->value=value;
                    300:                                return true;
                    301:                        }
                    302: 
                    303:                // proper pair not found 
                    304:                return false;
1.64      paf       305:        }
                    306: 
1.51      paf       307:        /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
1.59      paf       308:        bool put_dont_replace(K key, V value) {
                    309:                if(!value) {
                    310:                        remove(key);
                    311:                        return false;
                    312:                }
                    313:                if(is_full()) 
                    314:                        expand();
                    315: 
                    316:                uint code=hash_code(key);
                    317:                uint index=code%allocated;
                    318:                Pair **ref=&refs[index];
                    319:                for(Pair *pair=*ref; pair; pair=pair->link)
                    320:                        if(pair->code==code && pair->key==key) {
                    321:                                // found a pair with the same key, NOT replacing
                    322:                                return true;
                    323:                        }
                    324: 
                    325:                // proper pair not found -- create&link_in new pair
                    326:                if(!*ref) // root cell were fused_refs?
                    327:                        fused_refs++; // not, we'll use it and record the fact
1.79      misha     328:                HASH_NEW_PAIR(code, key, value);
1.59      paf       329:                fpairs_count++;
                    330:                return false;
                    331:        }
1.18      paf       332: 
1.79      misha     333:        /// put all 'src' values if NO with same key existed
1.75      misha     334:        void merge_dont_replace(const HASH& src) {
1.79      misha     335: #ifdef HASH_ORDER
                    336:                for(Pair *pair=src.first; pair; pair=pair->next)
                    337: #else
1.59      paf       338:                for(int i=0; i<src.allocated; i++)
                    339:                        for(Pair *pair=src.refs[i]; pair; pair=pair->link)
1.79      misha     340: #endif
1.59      paf       341:                                put_dont_replace(pair->key, pair->value);
1.36      paf       342:        }
1.11      paf       343: 
1.29      paf       344:        /// number of elements in hash
1.59      paf       345:        int count() const { return fpairs_count; }
1.25      paf       346: 
1.59      paf       347:        /// iterate over all pairs
                    348:        template<typename I> void for_each(void callback(K, V, I), I info) const {
1.79      misha     349:                HASH_FOR_EACH
1.76      misha     350:                        callback(pair->key, pair->value, info);
1.59      paf       351:        }
1.45      paf       352: 
1.59      paf       353:        /// iterate over all pairs
                    354:        template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
1.79      misha     355:                HASH_FOR_EACH
1.76      misha     356:                        callback(pair->key, pair->value, info);
1.59      paf       357:        }
1.38      paf       358: 
1.59      paf       359:        /// iterate over all pairs until condition becomes true, return that element
                    360:        template<typename I> V first_that(bool callback(K, V, I), I info) const {
1.79      misha     361:                HASH_FOR_EACH
1.75      misha     362:                        if(callback(pair->key, pair->value, info))
                    363:                                return pair->value;
1.59      paf       364:                return V(0);
                    365:        }
1.27      paf       366: 
1.29      paf       367:        /// remove all elements
1.59      paf       368:        void clear() {
                    369:                memset(refs, 0, sizeof(*refs)*allocated);
1.88      moko      370:                fpairs_count=fused_refs=0;
                    371:                HASH_ORDER_CLEAR();
1.59      paf       372:        }
1.15      paf       373: 
1.74      misha     374: protected:
1.1       paf       375: 
1.61      paf       376:        /// the index of [allocated] in [Hash_allocates]
1.19      paf       377:        int allocates_index;
1.1       paf       378: 
1.39      paf       379:        /// number of allocated pairs
1.19      paf       380:        int allocated;
1.1       paf       381: 
1.39      paf       382:        /// used pairs
1.59      paf       383:        int fused_refs;
1.44      parser    384: 
                    385:        /// stored pairs total (including those by links)
1.59      paf       386:        int fpairs_count;
1.1       paf       387: 
1.39      paf       388:        /// pair storage
1.59      paf       389:        class Pair: public PA_Allocated {
                    390:        public:
1.1       paf       391:                uint code;
1.59      paf       392:                K key;
                    393:                V value;
1.1       paf       394:                Pair *link;
1.75      misha     395: #ifdef HASH_ORDER
                    396:                Pair **prev;
                    397:                Pair *next;
                    398: 
                    399:                Pair(uint acode, K akey, V avalue, Pair *alink, Pair **aprev) : code(acode), key(akey), value(avalue), link(alink), 
                    400:                        prev(aprev), next(0) { *aprev=this; }
                    401: #else
                    402:                Pair(uint acode, K akey, V avalue, Pair *alink) : code(acode), key(akey), value(avalue), link(alink) {}
                    403: #endif
1.2       paf       404:        } **refs;
1.1       paf       405: 
1.75      misha     406: #ifdef HASH_ORDER
                    407:        Pair *first;
                    408:        Pair **last;
                    409: #endif
                    410: 
1.83      moko      411:        /// filled to threshold (THRESHOLD_PERCENT=75), needs expanding
                    412:        bool is_full() { return fused_refs + allocated/4 >= allocated; }
1.5       paf       413: 
1.39      paf       414:        /// allocate larger buffer & rehash
1.59      paf       415:        void expand() {
                    416:                int old_allocated=allocated;
                    417:                Pair **old_refs=refs;
                    418: 
1.83      moko      419:                if (allocates_index<HASH_ALLOCATES_COUNT-1) allocates_index++;
1.59      paf       420:                // allocated bigger refs array
1.61      paf       421:                allocated=Hash_allocates[allocates_index];
1.87      moko      422:                refs=new Pair*[allocated];
1.59      paf       423: 
                    424:                // rehash
                    425:                Pair **old_ref=old_refs;
                    426:                for(int old_index=0; old_index<old_allocated; old_index++)
                    427:                        for(Pair *pair=*old_ref++; pair; ) {
                    428:                                Pair *next=pair->link;
                    429: 
                    430:                                uint new_index=pair->code%allocated;
                    431:                                Pair **new_ref=&refs[new_index];
                    432:                                pair->link=*new_ref;
                    433:                                *new_ref=pair;
                    434: 
                    435:                                pair=next;
                    436:                        }
                    437: 
                    438:                delete[] old_refs;
                    439:        }
1.4       paf       440: 
                    441: private: //disabled
                    442: 
1.75      misha     443:        HASH& operator = (const HASH&) { return *this; }
1.1       paf       444: };
1.59      paf       445: 
1.74      misha     446: /** 
1.75      misha     447:        Simple String::body hash.
                    448:        Allows hash code caching
1.74      misha     449: */
                    450: 
                    451: #ifdef HASH_CODE_CACHING
                    452: 
1.75      misha     453: template<typename V> class HASH_STRING: public HASH<const CORD,V> {
1.74      misha     454: public:
                    455: 
1.75      misha     456:        typedef typename HASH<const CORD,V>::Pair Pair; 
1.74      misha     457:        typedef const String::Body &K;
                    458: 
                    459:        typedef K key_type;
                    460:        
                    461:        /// put a [value] under the [key] @returns existed or not
                    462:        bool put(K str, V value) {
                    463:                if(!value) {
                    464:                        remove(str);
                    465:                        return false;
                    466:                }
                    467:                if(this->is_full()) 
                    468:                        this->expand();
                    469: 
                    470:                CORD key=str.get_cord();
                    471: 
                    472:                uint code=str.get_hash_code();
                    473:                uint index=code%this->allocated;
                    474:                Pair **ref=&this->refs[index];
                    475:                for(Pair *pair=*ref; pair; pair=pair->link)
                    476:                        if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                    477:                                // found a pair with the same key
                    478:                                pair->value=value;
                    479:                                return true;
                    480:                        }
                    481: 
                    482:                // proper pair not found -- create&link_in new pair
                    483:                if(!*ref) // root cell were fused_refs?
                    484:                        this->fused_refs++; // not, we'll use it and record the fact
1.79      misha     485:                HASH_NEW_PAIR(code, key, value);
1.74      misha     486:                this->fpairs_count++;
                    487:                return false;
                    488:        }
                    489: 
                    490:        /// remove the [key] @returns existed or not
                    491:        bool remove(K str) {
                    492:                CORD key=str.get_cord();
                    493:                uint code=str.get_hash_code();
                    494:                uint index=code%this->allocated;
1.75      misha     495:                for(Pair **ref=&this->refs[index]; *ref; ref=&(*ref)->link){
                    496:                        Pair *pair=*ref;
                    497:                        if(pair->code==code && CORD_cmp(pair->key,key)==0) {
1.74      misha     498:                                // found a pair with the same key
1.75      misha     499:                                Pair *next=pair->link;
                    500: #ifdef HASH_ORDER
                    501:                                *(pair->prev)=pair->next;
                    502:                                if(pair->next)
                    503:                                        pair->next->prev=pair->prev;
                    504:                                else
                    505:                                        this->last=pair->prev;
                    506: #endif
1.74      misha     507:                                *ref=next;
                    508:                                --this->fpairs_count;
                    509:                                return true;
                    510:                        }
1.75      misha     511:                }
1.74      misha     512: 
                    513:                return false;
                    514:        }
                    515: 
                    516:        /// return true if key exists
                    517:        bool contains(K str){
                    518:                CORD key=str.get_cord();
                    519:                uint code=str.get_hash_code();
                    520:                uint index=code%this->allocated;
                    521:                for(Pair *pair=this->refs[index]; pair; pair=pair->link){
                    522:                        if(pair->code==code && CORD_cmp(pair->key,key)==0)
                    523:                                return true;
                    524:                }
                    525: 
                    526:                return false;
                    527:        }
                    528: 
                    529:        /// get associated [value] by the [key]
                    530:        V get(K str) const {
                    531:                CORD key=str.get_cord();
                    532:                uint code=str.get_hash_code();
                    533:                uint index=code%this->allocated;
                    534:                for(Pair *pair=this->refs[index]; pair; pair=pair->link)
                    535:                        if(pair->code==code && CORD_cmp(pair->key,key)==0)
                    536:                                return pair->value;
                    537: 
                    538:                return V(0);
                    539:        }
                    540: 
1.93      moko      541:        /// get associated [value] by the [key], optimized
                    542:        V get(const char *key) const {
                    543:                uint code=0;
                    544:                if(key && *key){
                    545:                        generic_hash_code(code, key);
                    546:                } else {
                    547:                        key=0;
                    548:                }
                    549:                uint index=code%this->allocated;
                    550:                for(Pair *pair=this->refs[index]; pair; pair=pair->link)
                    551:                        if(pair->code==code && CORD_cmp(pair->key,(CORD)key)==0)
                    552:                                return pair->value;
                    553: 
                    554:                return V(0);
                    555:        }
                    556: 
1.74      misha     557:        /// put a [value] under the [key] if that [key] existed @returns existed or not
                    558:        bool put_replaced(K str, V value) {
                    559:                if(!value) {
                    560:                        remove(str);
                    561:                        return false;
                    562:                }
                    563: 
                    564:                CORD key=str.get_cord();
                    565:                uint code=str.get_hash_code();
                    566:                uint index=code%this->allocated;
                    567:                for(Pair *pair=this->refs[index]; pair; pair=pair->link)
                    568:                        if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                    569:                                // found a pair with the same key, replacing
                    570:                                pair->value=value;
                    571:                                return true;
                    572:                        }
                    573: 
                    574:                // proper pair not found 
                    575:                return false;
                    576:        }
                    577: 
                    578:        /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
                    579:        bool put_dont_replace(K str, V value) {
                    580:                if(!value) {
                    581:                        remove(str);
                    582:                        return false;
                    583:                }
                    584:                if(this->is_full()) 
                    585:                        this->expand();
                    586: 
                    587:                CORD key=str.get_cord();
                    588:                uint code=str.get_hash_code();
                    589:                uint index=code%this->allocated;
                    590:                Pair **ref=&this->refs[index];
                    591:                for(Pair *pair=*ref; pair; pair=pair->link)
                    592:                        if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                    593:                                // found a pair with the same key, NOT replacing
                    594:                                return true;
                    595:                        }
                    596: 
                    597:                // proper pair not found -- create&link_in new pair
                    598:                if(!*ref) // root cell were fused_refs?
                    599:                        this->fused_refs++; // not, we'll use it and record the fact
1.79      misha     600:                HASH_NEW_PAIR(code, key, value);
1.74      misha     601:                this->fpairs_count++;
                    602:                return false;
                    603:        }
                    604: 
1.79      misha     605:        /// put all 'src' values if NO with same key existed
                    606:        void merge_dont_replace(const HASH_STRING& src) {
1.76      misha     607: #ifdef HASH_ORDER
1.79      misha     608:                for(Pair *pair=src.first; pair; pair=pair->next)
1.76      misha     609: #else
1.79      misha     610:                for(int i=0; i<src.allocated; i++)
                    611:                        for(Pair *pair=src.refs[i]; pair; pair=pair->link)
1.76      misha     612: #endif
1.79      misha     613:                                put_dont_replace(String::Body(pair->key, pair->code), pair->value);
                    614:        }
                    615: 
                    616:        /// iterate over all pairs
                    617:        template<typename I> void for_each(void callback(K, V, I), I info) const {
                    618:                HASH_FOR_EACH
                    619:                        callback(String::Body(pair->key, pair->code), pair->value, info);
1.74      misha     620:        }
                    621: 
                    622:        /// iterate over all pairs
                    623:        template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
1.79      misha     624:                HASH_FOR_EACH
                    625:                        callback(String::Body(pair->key, pair->code), pair->value, info);
1.74      misha     626:        }
                    627: 
                    628:        /// iterate over all pairs until condition becomes true, return that element
                    629:        template<typename I> V first_that(bool callback(K, V, I), I info) const {
1.79      misha     630:                HASH_FOR_EACH
1.75      misha     631:                        if(callback(String::Body(pair->key, pair->code), pair->value, info))
                    632:                                return pair->value;
1.74      misha     633:                return V(0);
                    634:        }
1.80      misha     635: 
1.92      moko      636: #else //HASH_CODE_CACHING
                    637: 
                    638: template<typename V> class HASH_STRING: public HASH<const String::Body,V>{
                    639: public:
                    640:        typedef typename HASH<const String::Body,V>::Pair Pair;
                    641: 
                    642: #endif //HASH_CODE_CACHING
                    643: 
1.80      misha     644:        /// simple hash iterator
                    645:        class Iterator {
                    646:                const HASH_STRING<V>& fhash;
                    647:                Pair *fcurrent;
1.90      moko      648: #ifndef HASH_ORDER
1.89      moko      649:                int i;
1.90      moko      650: #endif
1.80      misha     651:        public:
1.92      moko      652: #ifdef HASH_ORDER
1.80      misha     653:                Iterator(const HASH_STRING<V>& ahash): fhash(ahash) {
                    654:                        fcurrent=fhash.first;
1.92      moko      655:                }
                    656: 
                    657:                void next() {
                    658:                        fcurrent=fcurrent->next;
                    659:                }
1.89      moko      660: #else
1.92      moko      661:                Iterator(const HASH_STRING<V>& ahash): fhash(ahash) {
1.89      moko      662:                        fcurrent=0;
                    663:                        for(i=0; i<fhash.allocated; i++)
                    664:                                if (fcurrent=fhash.refs[i])
                    665:                                        break;
1.80      misha     666:                }
                    667: 
                    668:                void next() {
1.89      moko      669:                        if(fcurrent=fcurrent->link)
                    670:                                return;
                    671:                        for(i++; i<fhash.allocated; i++)
                    672:                                if (fcurrent=fhash.refs[i])
                    673:                                        break;
1.92      moko      674:                }
1.89      moko      675: #endif
1.92      moko      676: 
                    677:                operator bool () {
                    678:                        return fcurrent != 0;
1.80      misha     679:                }
                    680: 
                    681:                String::Body key(){
1.92      moko      682: #ifdef HASH_CODE_CACHING
1.80      misha     683:                        return String::Body(fcurrent->key, fcurrent->code);
1.92      moko      684: #else
                    685:                        return fcurrent->key;
                    686: #endif
1.80      misha     687:                }
                    688: 
                    689:                V value(){
                    690:                        return fcurrent->value;
                    691:                }
1.88      moko      692: 
                    693:                Pair *pair(){
                    694:                        return fcurrent;
                    695:                }
1.80      misha     696:        };
1.92      moko      697: 
1.74      misha     698: };
                    699: 
1.75      misha     700: #ifndef HASH_ORDER
1.74      misha     701: ///    Auto-object used to temporarily substituting/removing string hash values
1.85      moko      702: template <typename H, typename V>
1.55      paf       703: class Temp_hash_value {
1.85      moko      704:        H *fhash;
                    705:        String::Body fname;
1.59      paf       706:        V saved_value;
1.55      paf       707: public:
1.85      moko      708:        Temp_hash_value(H *ahash, String::Body aname, V avalue) : fhash(ahash), fname(aname) {
                    709:                if(fhash){
                    710:                        saved_value=fhash->get(aname);
                    711:                        fhash->put(aname, avalue);
                    712:                }
1.55      paf       713:        }
1.85      moko      714:        ~Temp_hash_value() {
                    715:                if(fhash)
                    716:                        fhash->put(fname, saved_value);
1.55      paf       717:        }
                    718: };
1.75      misha     719: #endif
1.1       paf       720: 
1.75      misha     721: #endif //PA_HASH_CLASS

E-mail: