Annotation of parser3/src/main/pa_hash.C, revision 1.34

1.25      paf         1: /** @file
1.26      paf         2:        Parser: hash class.
                      3: 
1.19      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.26      paf         5: 
1.20      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.19      paf         7: 
1.34    ! paf         8:        $Id: pa_hash.C,v 1.33 2001/04/12 14:50:29 paf Exp $
1.1       paf         9: */
                     10: 
                     11: /*
                     12:        The prime numbers used from zend_hash.c,
                     13:        the part of Zend scripting engine library,
                     14:        Copyrighted (C) 1999-2000    Zend Technologies Ltd.
                     15:        http://www.zend.com/license/0_92.txt
                     16:        For more information about Zend please visit http://www.zend.com/
                     17: */
                     18: 
1.28      paf        19: #include "pa_config_includes.h"
1.23      paf        20: 
1.11      paf        21: #include "pa_hash.h"
1.2       paf        22: 
1.8       paf        23: void *Hash::Pair::operator new(size_t size, Pool& apool) {
                     24:        return apool.malloc(size);
1.2       paf        25: }
                     26: 
1.1       paf        27: /* Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster */
1.18      paf        28: uint Hash::allocates[]={
1.1       paf        29:        5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963, 
                     30:        16229, 32531, 65407, 130987, 262237, 524521, 1048793, 
                     31:        2097397, 4194103, 8388857, 16777447, 33554201, 67108961, 
                     32:        134217487, 268435697, 536870683, 1073741621, 2147483399};
1.18      paf        33: int Hash::allocates_count=
                     34:        sizeof(allocates)/sizeof(uint);
1.1       paf        35: 
                     36: 
1.31      paf        37: void Hash::construct(Pool& apool) {
1.18      paf        38:        allocated=allocates[allocates_index=0];
                     39:        threshold=allocated*THRESHOLD_PERCENT/100;
1.1       paf        40:        used=0;
1.18      paf        41:        refs=static_cast<Pair **>(calloc(sizeof(Pair *)*allocated));
1.1       paf        42: }
                     43: 
                     44: void Hash::expand() {
1.18      paf        45:        int old_size=allocated;
1.2       paf        46:        Pair **old_refs=refs;
1.4       paf        47: 
1.2       paf        48:        // allocated bigger refs array
1.18      paf        49:        allocates_index=allocates_index+1<allocates_count?allocates_index+1:allocates_count-1;
                     50:        allocated=allocates[allocates_index];
1.33      paf        51:        threshold=allocated*THRESHOLD_PERCENT/100;
1.18      paf        52:        refs=static_cast<Pair **>(calloc(sizeof(Pair *)*allocated));
1.1       paf        53: 
                     54:        // rehash
1.2       paf        55:        Pair **old_ref=old_refs;
                     56:        for(int old_index=0; old_index<old_size; old_index++)
                     57:                for(Pair *pair=*old_ref++; pair; ) {
                     58:                        Pair *linked_pair=pair->link;
                     59: 
1.18      paf        60:                        uint new_index=pair->code%allocated;
1.2       paf        61:                        Pair **new_ref=&refs[new_index];
                     62:                        pair->link=*new_ref;
                     63:                        *new_ref=pair;
1.1       paf        64: 
1.2       paf        65:                        pair=linked_pair;
                     66:                }
1.1       paf        67: }
                     68: 
1.18      paf        69: uint Hash::generic_code(uint aresult, const char *start, uint allocated) {
1.1       paf        70:        uint result=aresult, g;
1.18      paf        71:        const char *end=start+allocated;
1.1       paf        72: 
                     73:        while (start<end) {
                     74:                result=(result<<4)+*start++;
                     75:                if ((g=(result&0xF0000000))) {
                     76:                        result=result^(g>>24);
                     77:                        result=result^g;
                     78:                }
                     79:        }
                     80:        return result;
                     81: }
                     82: 
1.31      paf        83: bool Hash::put(const Key& key, Val *value) {
1.1       paf        84:        if(full()) 
                     85:                expand();
                     86: 
1.2       paf        87:        uint code=key.hash_code();
1.18      paf        88:        uint index=code%allocated;
1.2       paf        89:        Pair **ref=&refs[index];
                     90:        for(Pair *pair=*ref; pair; pair=pair->link)
                     91:                if(pair->code==code && pair->key==key) {
                     92:                        // found a pair with the same key
                     93:                        pair->value=value;
1.17      paf        94:                        return true;
1.1       paf        95:                }
1.2       paf        96:        
1.7       paf        97:        // proper pair not found -- create&link_in new pair
1.17      paf        98:        if(!*ref) // root cell were used?
                     99:                used++; // not, we'll use it and record the fact
1.15      paf       100:        *ref=NEW Pair(code, key, value, *ref);
1.17      paf       101:        return false;
1.1       paf       102: }
                    103: 
1.31      paf       104: Hash::Val *Hash::get(const Key& key) const {
1.2       paf       105:        uint code=key.hash_code();
1.18      paf       106:        uint index=code%allocated;
1.2       paf       107:        for(Pair *pair=refs[index]; pair; pair=pair->link)
1.1       paf       108:                if(pair->code==code && pair->key==key)
                    109:                        return pair->value;
                    110:        
                    111:        return 0;
                    112: }
1.16      paf       113: 
1.31      paf       114: bool Hash::put_replace(const Key& key, Val *value) {
1.16      paf       115:        uint code=key.hash_code();
1.18      paf       116:        uint index=code%allocated;
1.16      paf       117:        for(Pair *pair=refs[index]; pair; pair=pair->link)
                    118:                if(pair->code==code && pair->key==key) {
                    119:                        // found a pair with the same key, replacing
                    120:                        pair->value=value;
                    121:                        return true;
                    122:                }
                    123: 
                    124:        // proper pair not found 
                    125:        return false;
                    126: }
                    127: 
1.31      paf       128: bool Hash::put_dont_replace(const Key& key, Val *value) {
1.17      paf       129:        if(full()) 
                    130:                expand();
                    131: 
                    132:        uint code=key.hash_code();
1.18      paf       133:        uint index=code%allocated;
1.17      paf       134:        Pair **ref=&refs[index];
                    135:        for(Pair *pair=*ref; pair; pair=pair->link)
                    136:                if(pair->code==code && pair->key==key) {
                    137:                        // found a pair with the same key, NOT replacing
                    138:                        return true;
                    139:                }
                    140: 
                    141:        // proper pair not found -- create&link_in new pair
                    142:        *ref=NEW Pair(code, key, value, *ref);
                    143:        if(!*ref) // root cell were used?
                    144:                used++; // not, we'll use it and record the fact
                    145:        return false;
                    146: }
                    147: 
1.31      paf       148: void Hash::merge_dont_replace(const Hash& src) {
1.18      paf       149:        for(int i=0; i<src.allocated; i++)
1.17      paf       150:                for(Pair *pair=src.refs[i]; pair; pair=pair->link)
                    151:                        put_dont_replace(pair->key, pair->value);
1.18      paf       152:        // MAY:optimize this.allocated==src.allocated case
1.21      paf       153: }
                    154: 
1.32      paf       155: void Hash::for_each(For_each_func func, void *info) const {
1.21      paf       156:        Pair **ref=refs;
                    157:        for(int index=0; index<allocated; index++)
                    158:                for(Pair *pair=*ref++; pair; pair=pair->link)
1.24      paf       159:                        if(pair->value)
                    160:                                (*func)(pair->key, pair->value, info);
1.34    ! paf       161: }
        !           162: 
        !           163: Hash::Val* Hash::first_that(First_that_func func, void *info) const {
        !           164:        Pair **ref=refs;
        !           165:        for(int index=0; index<allocated; index++)
        !           166:                for(Pair *pair=*ref++; pair; pair=pair->link)
        !           167:                        if(pair->value)
        !           168:                                if((*func)(pair->key, pair->value, info))
        !           169:                                        return pair->value;
        !           170:        return 0;
1.23      paf       171: }
                    172: 
1.31      paf       173: void Hash::clear() {
1.23      paf       174:        memset(refs, 0, sizeof(*refs)*allocated);
                    175:        used=0;
1.17      paf       176: }

E-mail: