--- parser3/src/include/pa_hash.h 2003/11/20 16:34:25 1.60 +++ parser3/src/include/pa_hash.h 2005/07/26 12:43:05 1.63 @@ -1,7 +1,7 @@ /** @file Parser: hash class decl. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ @@ -17,13 +17,23 @@ #ifndef PA_HASH_H #define PA_HASH_H -static const char * const IDENT_HASH_H="$Date: 2003/11/20 16:34:25 $"; +static const char * const IDENT_HASH_H="$Date: 2005/07/26 12:43:05 $"; #include "pa_memory.h" #include "pa_types.h" const int HASH_ALLOCATES_COUNT=29; +/** Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster + + paf: HPUX ld could not handle static member: unsatisfied symbols +*/ +static uint Hash_allocates[HASH_ALLOCATES_COUNT]={ + 5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963, + 16229, 32531, 65407, 130987, 262237, 524521, 1048793, + 2097397, 4194103, 8388857, 16777447, 33554201, 67108961, + 134217487, 268435697, 536870683, 1073741621, 2147483399}; + /** Simple hash. @@ -39,7 +49,7 @@ public: typedef V value_type; Hash() { - allocated=allocates[allocates_index=0]; + allocated=Hash_allocates[allocates_index=0]; threshold=allocated*THRESHOLD_PERCENT/100; fpairs_count=fused_refs=0; refs=new(UseGC) Pair*[allocated]; @@ -93,6 +103,38 @@ public: return false; } + /// put a [value] under the [key] @returns existed or not + template R maybe_put(K key, V value, F prevent) { + if(!value) { + remove(key); + return 0; + } + if(is_full()) + expand(); + + uint code=hash_code(key); + uint index=code%allocated; + Pair **ref=&refs[index]; + for(Pair *pair=*ref; pair; pair=pair->link) + if(pair->code==code && pair->key==key) { + // found a pair with the same key + + // prevent-function intercepted put? + if(R result=prevent(pair->value)) + return result; + + pair->value=value; + return reinterpret_cast(1); + } + + // proper pair not found -- create&link_in new pair + if(!*ref) // root cell were fused_refs? + fused_refs++; // not, we'll use it and record the fact + *ref=new Pair(code, key, value, *ref); + fpairs_count++; + return 0; + } + /// remove the [key] @returns existed or not bool remove(K key) { uint code=hash_code(key); @@ -122,7 +164,7 @@ public: } /// put a [value] under the [key] if that [key] existed @returns existed or not - bool put_replace(K key, V value) { + bool put_replaced(K key, V value) { if(!value) { remove(key); return false; @@ -218,12 +260,9 @@ private: THRESHOLD_PERCENT=75 }; - /// the index of [allocated] in [allocates] + /// the index of [allocated] in [Hash_allocates] int allocates_index; - /// possible [allocates]. prime numbers - static uint allocates[]; - /// number of allocated pairs int allocated; @@ -261,7 +300,7 @@ private: allocates_index=allocates_index+1 -uint Hash::allocates[HASH_ALLOCATES_COUNT]={ - 5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963, - 16229, 32531, 65407, 130987, 262237, 524521, 1048793, - 2097397, 4194103, 8388857, 16777447, 33554201, 67108961, - 134217487, 268435697, 536870683, 1073741621, 2147483399}; - /// useful generic hash function inline void generic_hash_code(uint& result, char c) { result=(result<<4)+c;