--- parser3/src/include/pa_hash.h 2003/02/26 10:17:30 1.58.2.17 +++ parser3/src/include/pa_hash.h 2004/02/11 15:33:14 1.62 @@ -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,11 +17,23 @@ #ifndef PA_HASH_H #define PA_HASH_H -static const char* IDENT_HASH_H="$Date: 2003/02/26 10:17:30 $"; +static const char * const IDENT_HASH_H="$Date: 2004/02/11 15:33:14 $"; -#include "pa_pool.h" +#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. @@ -37,10 +49,10 @@ 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(0) Pair*[allocated]; + refs=new(UseGC) Pair*[allocated]; } Hash(const Hash& source) { @@ -49,7 +61,7 @@ public: threshold=source.threshold; fused_refs=source.fused_refs; fpairs_count=source.fpairs_count; - refs=new(0) Pair*[allocated]; + refs=new(UseGC) Pair*[allocated]; // clone & rehash Pair **old_ref=source.refs; @@ -63,13 +75,13 @@ public: pair=next; } } - ~Hash() { - destroy_pairs(); - delete refs; - } /// put a [value] under the [key] @returns existed or not bool put(K key, V value) { + if(!value) { + remove(key); + return false; + } if(is_full()) expand(); @@ -121,6 +133,10 @@ public: /// put a [value] under the [key] if that [key] existed @returns existed or not bool put_replace(K key, V value) { + if(!value) { + remove(key); + return false; + } uint code=hash_code(key); uint index=code%allocated; for(Pair *pair=refs[index]; pair; pair=pair->link) @@ -136,6 +152,10 @@ public: /// put a [value] under the [key] if that [key] NOT existed @returns existed or not bool put_dont_replace(K key, V value) { + if(!value) { + remove(key); + return false; + } if(is_full()) expand(); @@ -176,6 +196,14 @@ public: callback(pair->key, pair->value, info); } + /// iterate over all pairs + template void for_each_ref(void callback(K, V&, I), I info) const { + Pair **ref=refs; + for(int index=0; indexlink) + callback(pair->key, pair->value, info); + } + /// iterate over all pairs until condition becomes true, return that element template V first_that(bool callback(K, V, I), I info) const { Pair **ref=refs; @@ -189,7 +217,7 @@ public: /// remove all elements void clear() { - destroy_pairs(); memset(refs, 0, sizeof(*refs)*allocated); + memset(refs, 0, sizeof(*refs)*allocated); fpairs_count=fused_refs=0; } @@ -200,14 +228,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[]; - - static int allocates_count; - /// number of allocated pairs int allocated; @@ -243,11 +266,11 @@ private: int old_allocated=allocated; Pair **old_refs=refs; - allocates_index=allocates_index+1link; - delete pair; - pair=next; - } - } + delete[] old_refs; } private: //disabled @@ -283,35 +294,42 @@ private: //disabled Hash& operator = (const Hash&) { return *this; } }; -/* Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster */ -template -uint Hash::allocates[]={ - 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}; - -template -int Hash::allocates_count=sizeof(allocates)/sizeof(uint); +/// useful generic hash function +inline void generic_hash_code(uint& result, char c) { + result=(result<<4)+c; + if(uint g=(result&0xF0000000)) { + result=result^(g>>24); + result=result^g; + } +} +/// useful generic hash function +inline void generic_hash_code(uint& result, const char* s) { + while(char c=*s++) { + result=(result<<4)+c; + if(uint g=(result&0xF0000000)) { + result=result^(g>>24); + result=result^g; + } + } +} /// useful generic hash function -inline uint generic_hash_code(uint aresult, const char* start, size_t allocated) { - uint result=aresult, g; - const char* end=start+allocated; - - while (start>24); result=result^g; } } - return result; } /// simple hash code of int. used by EXIF mapping inline uint hash_code(int self) { - return generic_hash_code(0, (const char*)&self, sizeof(self)); + uint result=0; + generic_hash_code(result, (const char*)&self, sizeof(self)); + return result; } /// Auto-object used to temporarily substituting/removing hash values