Annotation of parser3/src/main/pa_hash.C, revision 1.44
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.44 ! paf 7: $Id: pa_hash.C,v 1.43 2001/10/29 13:04:46 paf 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) {
1.43 paf 21: return apool.malloc(size, 6);
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;
1.43 paf 48: refs=static_cast<Pair **>(malloc(size, 7)); 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.44 ! paf 112: void Hash::remove(const Key& key) {
! 113: uint code=key.hash_code();
! 114: uint index=code%allocated;
! 115: for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link)
! 116: if((*ref)->code==code && (*ref)->key==key) {
! 117: // found a pair with the same key
! 118: *ref=(*ref)->link;
! 119: --count;
! 120: return;
! 121: }
! 122: }
! 123:
1.31 paf 124: Hash::Val *Hash::get(const Key& key) const {
1.2 paf 125: uint code=key.hash_code();
1.18 paf 126: uint index=code%allocated;
1.2 paf 127: for(Pair *pair=refs[index]; pair; pair=pair->link)
1.1 paf 128: if(pair->code==code && pair->key==key)
129: return pair->value;
130:
131: return 0;
132: }
1.16 paf 133:
1.31 paf 134: bool Hash::put_replace(const Key& key, Val *value) {
1.16 paf 135: uint code=key.hash_code();
1.18 paf 136: uint index=code%allocated;
1.16 paf 137: for(Pair *pair=refs[index]; pair; pair=pair->link)
138: if(pair->code==code && pair->key==key) {
139: // found a pair with the same key, replacing
140: pair->value=value;
141: return true;
142: }
143:
144: // proper pair not found
145: return false;
146: }
147:
1.31 paf 148: bool Hash::put_dont_replace(const Key& key, Val *value) {
1.17 paf 149: if(full())
150: expand();
151:
152: uint code=key.hash_code();
1.18 paf 153: uint index=code%allocated;
1.17 paf 154: Pair **ref=&refs[index];
155: for(Pair *pair=*ref; pair; pair=pair->link)
156: if(pair->code==code && pair->key==key) {
157: // found a pair with the same key, NOT replacing
158: return true;
159: }
160:
161: // proper pair not found -- create&link_in new pair
162: if(!*ref) // root cell were used?
163: used++; // not, we'll use it and record the fact
1.41 parser 164: *ref=NEW Pair(code, key, value, *ref);
165: count++;
1.17 paf 166: return false;
167: }
168:
1.31 paf 169: void Hash::merge_dont_replace(const Hash& src) {
1.18 paf 170: for(int i=0; i<src.allocated; i++)
1.17 paf 171: for(Pair *pair=src.refs[i]; pair; pair=pair->link)
172: put_dont_replace(pair->key, pair->value);
1.18 paf 173: // MAY:optimize this.allocated==src.allocated case
1.21 paf 174: }
175:
1.32 paf 176: void Hash::for_each(For_each_func func, void *info) const {
1.42 paf 177: Pair **ref=refs;
178: for(int index=0; index<allocated; index++)
179: for(Pair *pair=*ref++; pair; pair=pair->link)
1.44 ! paf 180: (*func)(pair->key, pair->value, info);
1.42 paf 181: }
182:
183: void Hash::for_each(For_each_func_refed func, void *info) const {
1.21 paf 184: Pair **ref=refs;
185: for(int index=0; index<allocated; index++)
186: for(Pair *pair=*ref++; pair; pair=pair->link)
1.44 ! paf 187: (*func)(pair->key, pair->value, info);
1.34 paf 188: }
189:
1.38 parser 190: void* Hash::first_that(First_that_func func, void *info) const {
1.34 paf 191: Pair **ref=refs;
192: for(int index=0; index<allocated; index++)
193: for(Pair *pair=*ref++; pair; pair=pair->link)
1.44 ! paf 194: if(void *result=(*func)(pair->key, pair->value, info))
! 195: return result;
1.34 paf 196: return 0;
1.23 paf 197: }
198:
1.31 paf 199: void Hash::clear() {
1.23 paf 200: memset(refs, 0, sizeof(*refs)*allocated);
1.41 parser 201: count=used=0;
1.17 paf 202: }
E-mail: