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

1.28      paf         1: /** @file
1.29      paf         2:        Parser: hash class decl.
                      3: 
1.94    ! moko        4:        Copyright (c) 2001-2015 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.94    ! moko       20: #define IDENT_PA_HASH_H "$Id: pa_hash.h,v 1.93 2015/10/08 23:32:44 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
                    213:                                delete pair;
1.59      paf       214:                                *ref=next;
                    215:                                --fpairs_count;
                    216:                                return true;
                    217:                        }
1.75      misha     218:                }
1.8       paf       219: 
1.59      paf       220:                return false;
                    221:        }
1.48      paf       222: 
1.70      misha     223:        /// return true if key exists
1.69      misha     224:        bool contains(K key){
1.67      misha     225:                uint code=hash_code(key);
                    226:                uint index=code%allocated;
1.70      misha     227:                for(Pair *pair=refs[index]; pair; pair=pair->link){
                    228:                        if(pair->code==code && pair->key==key)
1.67      misha     229:                                return true;
                    230:                }
                    231: 
                    232:                return false;
                    233:        }
                    234: 
1.59      paf       235:        /// get associated [value] by the [key]
                    236:        V get(K key) const {
                    237:                uint code=hash_code(key);
                    238:                uint index=code%allocated;
                    239:                for(Pair *pair=refs[index]; pair; pair=pair->link)
                    240:                        if(pair->code==code && pair->key==key)
                    241:                                return pair->value;
                    242:                
                    243:                return V(0);
1.33      paf       244:        }
1.70      misha     245: 
1.82      misha     246: #ifdef HASH_ORDER
1.86      misha     247:        String::Body first_key() const {
1.92      moko      248: #ifdef HASH_CODE_CACHING
1.86      misha     249:                return (first) ? String::Body(first->key, first->code) : String::Body();
1.92      moko      250: #else
                    251:                return (first) ? first->key : String::Body();
                    252: #endif
1.86      misha     253:        }
                    254: 
1.82      misha     255:        V first_value() const {
                    256:                return (first) ? first->value : V(0);
                    257:        }
                    258: 
1.86      misha     259:        String::Body last_key() const {
                    260:                if (fpairs_count) {
                    261:                        Pair* pair = (Pair*)((char *)last - offsetof(Pair, next));
1.92      moko      262: #ifdef HASH_CODE_CACHING
1.86      misha     263:                        return String::Body(pair->key, pair->code);
1.92      moko      264: #else
                    265:                        return pair->key;
                    266: #endif
1.86      misha     267:                } else {
                    268:                        return String::Body();
                    269:                }
                    270:        }
                    271: 
1.82      misha     272:        V last_value() const {
                    273:                return (fpairs_count) ? ((Pair *)((char *)last - offsetof(Pair, next)))->value : V(0);
                    274:        }
1.88      moko      275: 
                    276:        void order_clear() {
                    277:                HASH_ORDER_CLEAR();
                    278:        }
                    279: 
                    280:        void order_next(Pair* pair) {
                    281:                pair->prev=last;
                    282:                pair->next=0;
                    283:                *last=pair;
                    284:                last=&(pair->next);
                    285:        }
                    286: 
1.92      moko      287: #endif //HASH_ORDER
1.82      misha     288: 
1.51      paf       289:        /// put a [value] under the [key] if that [key] existed @returns existed or not
1.63      paf       290:        bool put_replaced(K key, V value) {
1.59      paf       291:                if(!value) {
                    292:                        remove(key);
                    293:                        return false;
                    294:                }
                    295:                uint code=hash_code(key);
                    296:                uint index=code%allocated;
                    297:                for(Pair *pair=refs[index]; pair; pair=pair->link)
                    298:                        if(pair->code==code && pair->key==key) {
                    299:                                // found a pair with the same key, replacing
                    300:                                pair->value=value;
                    301:                                return true;
                    302:                        }
                    303: 
                    304:                // proper pair not found 
                    305:                return false;
1.64      paf       306:        }
                    307: 
1.51      paf       308:        /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
1.59      paf       309:        bool put_dont_replace(K key, V value) {
                    310:                if(!value) {
                    311:                        remove(key);
                    312:                        return false;
                    313:                }
                    314:                if(is_full()) 
                    315:                        expand();
                    316: 
                    317:                uint code=hash_code(key);
                    318:                uint index=code%allocated;
                    319:                Pair **ref=&refs[index];
                    320:                for(Pair *pair=*ref; pair; pair=pair->link)
                    321:                        if(pair->code==code && pair->key==key) {
                    322:                                // found a pair with the same key, NOT replacing
                    323:                                return true;
                    324:                        }
                    325: 
                    326:                // proper pair not found -- create&link_in new pair
                    327:                if(!*ref) // root cell were fused_refs?
                    328:                        fused_refs++; // not, we'll use it and record the fact
1.79      misha     329:                HASH_NEW_PAIR(code, key, value);
1.59      paf       330:                fpairs_count++;
                    331:                return false;
                    332:        }
1.18      paf       333: 
1.79      misha     334:        /// put all 'src' values if NO with same key existed
1.75      misha     335:        void merge_dont_replace(const HASH& src) {
1.79      misha     336: #ifdef HASH_ORDER
                    337:                for(Pair *pair=src.first; pair; pair=pair->next)
                    338: #else
1.59      paf       339:                for(int i=0; i<src.allocated; i++)
                    340:                        for(Pair *pair=src.refs[i]; pair; pair=pair->link)
1.79      misha     341: #endif
1.59      paf       342:                                put_dont_replace(pair->key, pair->value);
1.36      paf       343:        }
1.11      paf       344: 
1.29      paf       345:        /// number of elements in hash
1.59      paf       346:        int count() const { return fpairs_count; }
1.25      paf       347: 
1.59      paf       348:        /// iterate over all pairs
                    349:        template<typename I> void for_each(void callback(K, V, I), I info) const {
1.79      misha     350:                HASH_FOR_EACH
1.76      misha     351:                        callback(pair->key, pair->value, info);
1.59      paf       352:        }
1.45      paf       353: 
1.59      paf       354:        /// iterate over all pairs
                    355:        template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
1.79      misha     356:                HASH_FOR_EACH
1.76      misha     357:                        callback(pair->key, pair->value, info);
1.59      paf       358:        }
1.38      paf       359: 
1.59      paf       360:        /// iterate over all pairs until condition becomes true, return that element
                    361:        template<typename I> V first_that(bool callback(K, V, I), I info) const {
1.79      misha     362:                HASH_FOR_EACH
1.75      misha     363:                        if(callback(pair->key, pair->value, info))
                    364:                                return pair->value;
1.59      paf       365:                return V(0);
                    366:        }
1.27      paf       367: 
1.29      paf       368:        /// remove all elements
1.59      paf       369:        void clear() {
                    370:                memset(refs, 0, sizeof(*refs)*allocated);
1.88      moko      371:                fpairs_count=fused_refs=0;
                    372:                HASH_ORDER_CLEAR();
1.59      paf       373:        }
1.15      paf       374: 
1.74      misha     375: protected:
1.1       paf       376: 
1.61      paf       377:        /// the index of [allocated] in [Hash_allocates]
1.19      paf       378:        int allocates_index;
1.1       paf       379: 
1.39      paf       380:        /// number of allocated pairs
1.19      paf       381:        int allocated;
1.1       paf       382: 
1.39      paf       383:        /// used pairs
1.59      paf       384:        int fused_refs;
1.44      parser    385: 
                    386:        /// stored pairs total (including those by links)
1.59      paf       387:        int fpairs_count;
1.1       paf       388: 
1.39      paf       389:        /// pair storage
1.59      paf       390:        class Pair: public PA_Allocated {
                    391:        public:
1.1       paf       392:                uint code;
1.59      paf       393:                K key;
                    394:                V value;
1.1       paf       395:                Pair *link;
1.75      misha     396: #ifdef HASH_ORDER
                    397:                Pair **prev;
                    398:                Pair *next;
                    399: 
                    400:                Pair(uint acode, K akey, V avalue, Pair *alink, Pair **aprev) : code(acode), key(akey), value(avalue), link(alink), 
                    401:                        prev(aprev), next(0) { *aprev=this; }
                    402: #else
                    403:                Pair(uint acode, K akey, V avalue, Pair *alink) : code(acode), key(akey), value(avalue), link(alink) {}
                    404: #endif
1.2       paf       405:        } **refs;
1.1       paf       406: 
1.75      misha     407: #ifdef HASH_ORDER
                    408:        Pair *first;
                    409:        Pair **last;
                    410: #endif
                    411: 
1.83      moko      412:        /// filled to threshold (THRESHOLD_PERCENT=75), needs expanding
                    413:        bool is_full() { return fused_refs + allocated/4 >= allocated; }
1.5       paf       414: 
1.39      paf       415:        /// allocate larger buffer & rehash
1.59      paf       416:        void expand() {
                    417:                int old_allocated=allocated;
                    418:                Pair **old_refs=refs;
                    419: 
1.83      moko      420:                if (allocates_index<HASH_ALLOCATES_COUNT-1) allocates_index++;
1.59      paf       421:                // allocated bigger refs array
1.61      paf       422:                allocated=Hash_allocates[allocates_index];
1.87      moko      423:                refs=new Pair*[allocated];
1.59      paf       424: 
                    425:                // rehash
                    426:                Pair **old_ref=old_refs;
                    427:                for(int old_index=0; old_index<old_allocated; old_index++)
                    428:                        for(Pair *pair=*old_ref++; pair; ) {
                    429:                                Pair *next=pair->link;
                    430: 
                    431:                                uint new_index=pair->code%allocated;
                    432:                                Pair **new_ref=&refs[new_index];
                    433:                                pair->link=*new_ref;
                    434:                                *new_ref=pair;
                    435: 
                    436:                                pair=next;
                    437:                        }
                    438: 
                    439:                delete[] old_refs;
                    440:        }
1.4       paf       441: 
                    442: private: //disabled
                    443: 
1.75      misha     444:        HASH& operator = (const HASH&) { return *this; }
1.1       paf       445: };
1.59      paf       446: 
1.74      misha     447: /** 
1.75      misha     448:        Simple String::body hash.
                    449:        Allows hash code caching
1.74      misha     450: */
                    451: 
                    452: #ifdef HASH_CODE_CACHING
                    453: 
1.75      misha     454: template<typename V> class HASH_STRING: public HASH<const CORD,V> {
1.74      misha     455: public:
                    456: 
1.75      misha     457:        typedef typename HASH<const CORD,V>::Pair Pair; 
1.74      misha     458:        typedef const String::Body &K;
                    459: 
                    460:        typedef K key_type;
                    461:        
                    462:        /// put a [value] under the [key] @returns existed or not
                    463:        bool put(K str, V value) {
                    464:                if(!value) {
                    465:                        remove(str);
                    466:                        return false;
                    467:                }
                    468:                if(this->is_full()) 
                    469:                        this->expand();
                    470: 
                    471:                CORD key=str.get_cord();
                    472: 
                    473:                uint code=str.get_hash_code();
                    474:                uint index=code%this->allocated;
                    475:                Pair **ref=&this->refs[index];
                    476:                for(Pair *pair=*ref; pair; pair=pair->link)
                    477:                        if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                    478:                                // found a pair with the same key
                    479:                                pair->value=value;
                    480:                                return true;
                    481:                        }
                    482: 
                    483:                // proper pair not found -- create&link_in new pair
                    484:                if(!*ref) // root cell were fused_refs?
                    485:                        this->fused_refs++; // not, we'll use it and record the fact
1.79      misha     486:                HASH_NEW_PAIR(code, key, value);
1.74      misha     487:                this->fpairs_count++;
                    488:                return false;
                    489:        }
                    490: 
                    491:        /// remove the [key] @returns existed or not
                    492:        bool remove(K str) {
                    493:                CORD key=str.get_cord();
                    494:                uint code=str.get_hash_code();
                    495:                uint index=code%this->allocated;
1.75      misha     496:                for(Pair **ref=&this->refs[index]; *ref; ref=&(*ref)->link){
                    497:                        Pair *pair=*ref;
                    498:                        if(pair->code==code && CORD_cmp(pair->key,key)==0) {
1.74      misha     499:                                // found a pair with the same key
1.75      misha     500:                                Pair *next=pair->link;
                    501: #ifdef HASH_ORDER
                    502:                                *(pair->prev)=pair->next;
                    503:                                if(pair->next)
                    504:                                        pair->next->prev=pair->prev;
                    505:                                else
                    506:                                        this->last=pair->prev;
                    507: #endif
                    508:                                delete pair;
1.74      misha     509:                                *ref=next;
                    510:                                --this->fpairs_count;
                    511:                                return true;
                    512:                        }
1.75      misha     513:                }
1.74      misha     514: 
                    515:                return false;
                    516:        }
                    517: 
                    518:        /// return true if key exists
                    519:        bool contains(K str){
                    520:                CORD key=str.get_cord();
                    521:                uint code=str.get_hash_code();
                    522:                uint index=code%this->allocated;
                    523:                for(Pair *pair=this->refs[index]; pair; pair=pair->link){
                    524:                        if(pair->code==code && CORD_cmp(pair->key,key)==0)
                    525:                                return true;
                    526:                }
                    527: 
                    528:                return false;
                    529:        }
                    530: 
                    531:        /// get associated [value] by the [key]
                    532:        V get(K str) const {
                    533:                CORD key=str.get_cord();
                    534:                uint code=str.get_hash_code();
                    535:                uint index=code%this->allocated;
                    536:                for(Pair *pair=this->refs[index]; pair; pair=pair->link)
                    537:                        if(pair->code==code && CORD_cmp(pair->key,key)==0)
                    538:                                return pair->value;
                    539: 
                    540:                return V(0);
                    541:        }
                    542: 
1.93      moko      543:        /// get associated [value] by the [key], optimized
                    544:        V get(const char *key) const {
                    545:                uint code=0;
                    546:                if(key && *key){
                    547:                        generic_hash_code(code, key);
                    548:                } else {
                    549:                        key=0;
                    550:                }
                    551:                uint index=code%this->allocated;
                    552:                for(Pair *pair=this->refs[index]; pair; pair=pair->link)
                    553:                        if(pair->code==code && CORD_cmp(pair->key,(CORD)key)==0)
                    554:                                return pair->value;
                    555: 
                    556:                return V(0);
                    557:        }
                    558: 
1.74      misha     559:        /// put a [value] under the [key] if that [key] existed @returns existed or not
                    560:        bool put_replaced(K str, V value) {
                    561:                if(!value) {
                    562:                        remove(str);
                    563:                        return false;
                    564:                }
                    565: 
                    566:                CORD key=str.get_cord();
                    567:                uint code=str.get_hash_code();
                    568:                uint index=code%this->allocated;
                    569:                for(Pair *pair=this->refs[index]; pair; pair=pair->link)
                    570:                        if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                    571:                                // found a pair with the same key, replacing
                    572:                                pair->value=value;
                    573:                                return true;
                    574:                        }
                    575: 
                    576:                // proper pair not found 
                    577:                return false;
                    578:        }
                    579: 
                    580:        /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
                    581:        bool put_dont_replace(K str, V value) {
                    582:                if(!value) {
                    583:                        remove(str);
                    584:                        return false;
                    585:                }
                    586:                if(this->is_full()) 
                    587:                        this->expand();
                    588: 
                    589:                CORD key=str.get_cord();
                    590:                uint code=str.get_hash_code();
                    591:                uint index=code%this->allocated;
                    592:                Pair **ref=&this->refs[index];
                    593:                for(Pair *pair=*ref; pair; pair=pair->link)
                    594:                        if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                    595:                                // found a pair with the same key, NOT replacing
                    596:                                return true;
                    597:                        }
                    598: 
                    599:                // proper pair not found -- create&link_in new pair
                    600:                if(!*ref) // root cell were fused_refs?
                    601:                        this->fused_refs++; // not, we'll use it and record the fact
1.79      misha     602:                HASH_NEW_PAIR(code, key, value);
1.74      misha     603:                this->fpairs_count++;
                    604:                return false;
                    605:        }
                    606: 
1.79      misha     607:        /// put all 'src' values if NO with same key existed
                    608:        void merge_dont_replace(const HASH_STRING& src) {
1.76      misha     609: #ifdef HASH_ORDER
1.79      misha     610:                for(Pair *pair=src.first; pair; pair=pair->next)
1.76      misha     611: #else
1.79      misha     612:                for(int i=0; i<src.allocated; i++)
                    613:                        for(Pair *pair=src.refs[i]; pair; pair=pair->link)
1.76      misha     614: #endif
1.79      misha     615:                                put_dont_replace(String::Body(pair->key, pair->code), pair->value);
                    616:        }
                    617: 
                    618:        /// iterate over all pairs
                    619:        template<typename I> void for_each(void callback(K, V, I), I info) const {
                    620:                HASH_FOR_EACH
                    621:                        callback(String::Body(pair->key, pair->code), pair->value, info);
1.74      misha     622:        }
                    623: 
                    624:        /// iterate over all pairs
                    625:        template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
1.79      misha     626:                HASH_FOR_EACH
                    627:                        callback(String::Body(pair->key, pair->code), pair->value, info);
1.74      misha     628:        }
                    629: 
                    630:        /// iterate over all pairs until condition becomes true, return that element
                    631:        template<typename I> V first_that(bool callback(K, V, I), I info) const {
1.79      misha     632:                HASH_FOR_EACH
1.75      misha     633:                        if(callback(String::Body(pair->key, pair->code), pair->value, info))
                    634:                                return pair->value;
1.74      misha     635:                return V(0);
                    636:        }
1.80      misha     637: 
1.92      moko      638: #else //HASH_CODE_CACHING
                    639: 
                    640: template<typename V> class HASH_STRING: public HASH<const String::Body,V>{
                    641: public:
                    642:        typedef typename HASH<const String::Body,V>::Pair Pair;
                    643: 
                    644: #endif //HASH_CODE_CACHING
                    645: 
1.80      misha     646:        /// simple hash iterator
                    647:        class Iterator {
                    648:                const HASH_STRING<V>& fhash;
                    649:                Pair *fcurrent;
1.90      moko      650: #ifndef HASH_ORDER
1.89      moko      651:                int i;
1.90      moko      652: #endif
1.80      misha     653:        public:
1.92      moko      654: #ifdef HASH_ORDER
1.80      misha     655:                Iterator(const HASH_STRING<V>& ahash): fhash(ahash) {
                    656:                        fcurrent=fhash.first;
1.92      moko      657:                }
                    658: 
                    659:                void next() {
                    660:                        fcurrent=fcurrent->next;
                    661:                }
1.89      moko      662: #else
1.92      moko      663:                Iterator(const HASH_STRING<V>& ahash): fhash(ahash) {
1.89      moko      664:                        fcurrent=0;
                    665:                        for(i=0; i<fhash.allocated; i++)
                    666:                                if (fcurrent=fhash.refs[i])
                    667:                                        break;
1.80      misha     668:                }
                    669: 
                    670:                void next() {
1.89      moko      671:                        if(fcurrent=fcurrent->link)
                    672:                                return;
                    673:                        for(i++; i<fhash.allocated; i++)
                    674:                                if (fcurrent=fhash.refs[i])
                    675:                                        break;
1.92      moko      676:                }
1.89      moko      677: #endif
1.92      moko      678: 
                    679:                operator bool () {
                    680:                        return fcurrent != 0;
1.80      misha     681:                }
                    682: 
                    683:                String::Body key(){
1.92      moko      684: #ifdef HASH_CODE_CACHING
1.80      misha     685:                        return String::Body(fcurrent->key, fcurrent->code);
1.92      moko      686: #else
                    687:                        return fcurrent->key;
                    688: #endif
1.80      misha     689:                }
                    690: 
                    691:                V value(){
                    692:                        return fcurrent->value;
                    693:                }
1.88      moko      694: 
                    695:                Pair *pair(){
                    696:                        return fcurrent;
                    697:                }
1.80      misha     698:        };
1.92      moko      699: 
1.74      misha     700: };
                    701: 
1.75      misha     702: #ifndef HASH_ORDER
1.74      misha     703: ///    Auto-object used to temporarily substituting/removing string hash values
1.85      moko      704: template <typename H, typename V>
1.55      paf       705: class Temp_hash_value {
1.85      moko      706:        H *fhash;
                    707:        String::Body fname;
1.59      paf       708:        V saved_value;
1.55      paf       709: public:
1.85      moko      710:        Temp_hash_value(H *ahash, String::Body aname, V avalue) : fhash(ahash), fname(aname) {
                    711:                if(fhash){
                    712:                        saved_value=fhash->get(aname);
                    713:                        fhash->put(aname, avalue);
                    714:                }
1.55      paf       715:        }
1.85      moko      716:        ~Temp_hash_value() {
                    717:                if(fhash)
                    718:                        fhash->put(fname, saved_value);
1.55      paf       719:        }
                    720: };
1.75      misha     721: #endif
1.1       paf       722: 
1.75      misha     723: #endif //PA_HASH_CLASS

E-mail: