Annotation of parser3/src/main/pa_hash.C, revision 1.40
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.40 ! parser 7: $Id: $
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.1 paf 37: 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;
46: size_t size=sizeof(Pair *)*allocated;
47: refs=static_cast<Pair **>(malloc(size)); memcpy(refs, source.refs, size);
1.1 paf 48: }
49:
50: void Hash::expand() {
1.18 paf 51: int old_size=allocated;
1.2 paf 52: Pair **old_refs=refs;
1.4 paf 53:
1.2 paf 54: // allocated bigger refs array
1.18 paf 55: allocates_index=allocates_index+1<allocates_count?allocates_index+1:allocates_count-1;
56: allocated=allocates[allocates_index];
1.33 paf 57: threshold=allocated*THRESHOLD_PERCENT/100;
1.18 paf 58: refs=static_cast<Pair **>(calloc(sizeof(Pair *)*allocated));
1.1 paf 59:
60: // rehash
1.2 paf 61: Pair **old_ref=old_refs;
62: for(int old_index=0; old_index<old_size; old_index++)
63: for(Pair *pair=*old_ref++; pair; ) {
64: Pair *linked_pair=pair->link;
65:
1.18 paf 66: uint new_index=pair->code%allocated;
1.2 paf 67: Pair **new_ref=&refs[new_index];
68: pair->link=*new_ref;
69: *new_ref=pair;
1.1 paf 70:
1.2 paf 71: pair=linked_pair;
72: }
1.1 paf 73: }
74:
1.18 paf 75: uint Hash::generic_code(uint aresult, const char *start, uint allocated) {
1.1 paf 76: uint result=aresult, g;
1.18 paf 77: const char *end=start+allocated;
1.1 paf 78:
79: while (start<end) {
80: result=(result<<4)+*start++;
81: if ((g=(result&0xF0000000))) {
82: result=result^(g>>24);
83: result=result^g;
84: }
85: }
86: return result;
87: }
88:
1.31 paf 89: bool Hash::put(const Key& key, Val *value) {
1.1 paf 90: if(full())
91: expand();
92:
1.2 paf 93: uint code=key.hash_code();
1.18 paf 94: uint index=code%allocated;
1.2 paf 95: Pair **ref=&refs[index];
96: for(Pair *pair=*ref; pair; pair=pair->link)
97: if(pair->code==code && pair->key==key) {
98: // found a pair with the same key
99: pair->value=value;
1.17 paf 100: return true;
1.1 paf 101: }
1.2 paf 102:
1.7 paf 103: // proper pair not found -- create&link_in new pair
1.17 paf 104: if(!*ref) // root cell were used?
105: used++; // not, we'll use it and record the fact
1.15 paf 106: *ref=NEW Pair(code, key, value, *ref);
1.17 paf 107: return false;
1.1 paf 108: }
109:
1.31 paf 110: Hash::Val *Hash::get(const Key& key) const {
1.2 paf 111: uint code=key.hash_code();
1.18 paf 112: uint index=code%allocated;
1.2 paf 113: for(Pair *pair=refs[index]; pair; pair=pair->link)
1.1 paf 114: if(pair->code==code && pair->key==key)
115: return pair->value;
116:
117: return 0;
118: }
1.16 paf 119:
1.31 paf 120: bool Hash::put_replace(const Key& key, Val *value) {
1.16 paf 121: uint code=key.hash_code();
1.18 paf 122: uint index=code%allocated;
1.16 paf 123: for(Pair *pair=refs[index]; pair; pair=pair->link)
124: if(pair->code==code && pair->key==key) {
125: // found a pair with the same key, replacing
126: pair->value=value;
127: return true;
128: }
129:
130: // proper pair not found
131: return false;
132: }
133:
1.31 paf 134: bool Hash::put_dont_replace(const Key& key, Val *value) {
1.17 paf 135: if(full())
136: expand();
137:
138: uint code=key.hash_code();
1.18 paf 139: uint index=code%allocated;
1.17 paf 140: Pair **ref=&refs[index];
141: for(Pair *pair=*ref; pair; pair=pair->link)
142: if(pair->code==code && pair->key==key) {
143: // found a pair with the same key, NOT replacing
144: return true;
145: }
146:
147: // proper pair not found -- create&link_in new pair
148: *ref=NEW Pair(code, key, value, *ref);
149: if(!*ref) // root cell were used?
150: used++; // not, we'll use it and record the fact
151: return false;
152: }
153:
1.31 paf 154: void Hash::merge_dont_replace(const Hash& src) {
1.18 paf 155: for(int i=0; i<src.allocated; i++)
1.17 paf 156: for(Pair *pair=src.refs[i]; pair; pair=pair->link)
157: put_dont_replace(pair->key, pair->value);
1.18 paf 158: // MAY:optimize this.allocated==src.allocated case
1.21 paf 159: }
160:
1.32 paf 161: void Hash::for_each(For_each_func func, void *info) const {
1.21 paf 162: Pair **ref=refs;
163: for(int index=0; index<allocated; index++)
164: for(Pair *pair=*ref++; pair; pair=pair->link)
1.24 paf 165: if(pair->value)
166: (*func)(pair->key, pair->value, info);
1.34 paf 167: }
168:
1.38 parser 169: void* Hash::first_that(First_that_func func, void *info) const {
1.34 paf 170: Pair **ref=refs;
171: for(int index=0; index<allocated; index++)
172: for(Pair *pair=*ref++; pair; pair=pair->link)
173: if(pair->value)
1.38 parser 174: if(void *result=(*func)(pair->key, pair->value, info))
175: return result;
1.34 paf 176: return 0;
1.23 paf 177: }
178:
1.31 paf 179: void Hash::clear() {
1.23 paf 180: memset(refs, 0, sizeof(*refs)*allocated);
181: used=0;
1.17 paf 182: }
E-mail: