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

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

E-mail: