--- parser3/src/include/pa_hash.h 2003/03/21 16:16:43 1.58.2.18.2.5 +++ parser3/src/include/pa_hash.h 2003/06/27 08:36:59 1.58.2.18.2.9 @@ -17,11 +17,13 @@ #ifndef PA_HASH_H #define PA_HASH_H -static const char* IDENT_HASH_H="$Date: 2003/03/21 16:16:43 $"; +static const char* IDENT_HASH_H="$Date: 2003/06/27 08:36:59 $"; #include "pa_memory.h" #include "pa_types.h" +const int HASH_ALLOCATES_COUNT=29; + /** Simple hash. @@ -40,7 +42,7 @@ public: allocated=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 +51,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; @@ -184,6 +186,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; @@ -214,8 +224,6 @@ private: /// possible [allocates]. prime numbers static uint allocates[]; - static int allocates_count; - /// number of allocated pairs int allocated; @@ -251,7 +259,7 @@ private: int old_allocated=allocated; Pair **old_refs=refs; - allocates_index=allocates_index+1 -uint Hash::allocates[]={ +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}; -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++) {