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

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

E-mail: