--- parser3/src/include/pa_hash.h 2007/05/18 12:44:00 1.67 +++ parser3/src/include/pa_hash.h 2009/04/18 00:33:56 1.73 @@ -17,7 +17,7 @@ #ifndef PA_HASH_H #define PA_HASH_H -static const char * const IDENT_HASH_H="$Date: 2007/05/18 12:44:00 $"; +static const char * const IDENT_HASH_H="$Date: 2009/04/18 00:33:56 $"; #include "pa_memory.h" #include "pa_types.h" @@ -34,6 +34,44 @@ static uint Hash_allocates[HASH_ALLOCATE 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; + 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 void generic_hash_code(uint& result, const char* buf, size_t size) { + const char* end=buf+size; + while(buf>24); + result=result^g; + } + } +} + +/// simple hash code of int. used by EXIF mapping +inline uint hash_code(int self) { + uint result=0; + generic_hash_code(result, (const char*)&self, sizeof(self)); + return result; +} + /** Simple hash. @@ -76,6 +114,19 @@ public: } } +#ifdef USE_DESTRUCTORS + ~Hash() { + Pair **ref=refs; + for(int index=0; indexlink; + delete pair; + pair=next; + } + delete[] refs; + } +#endif + /// put a [value] under the [key] @returns existed or not bool put(K key, V value) { if(!value) { @@ -229,20 +280,18 @@ public: return false; } - /// returns exist key or not - bool contain(K key){ + /// return true if key exists + bool contains(K key){ uint code=hash_code(key); uint index=code%allocated; - for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link){ - if((*ref)->code==code && (*ref)->key==key) { + for(Pair *pair=refs[index]; pair; pair=pair->link){ + if(pair->code==code && pair->key==key) return true; - } } return false; } - /// get associated [value] by the [key] V get(K key) const { uint code=hash_code(key); @@ -253,6 +302,16 @@ public: return V(0); } + + /// get associated [value] by the [key] + [code] (faster) + V get_by_hash_code(uint code, K key) const { + uint index=code%allocated; + for(Pair *pair=refs[index]; pair; pair=pair->link) + if(pair->code==code && pair->key==key) + return pair->value; + + return V(0); + } /// put a [value] under the [key] if that [key] existed @returns existed or not bool put_replaced(K key, V value) { @@ -443,44 +502,6 @@ private: //disabled Hash& operator = (const Hash&) { return *this; } }; -/// 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 void generic_hash_code(uint& result, const char* buf, size_t size) { - const char* end=buf+size; - while(buf>24); - result=result^g; - } - } -} - -/// simple hash code of int. used by EXIF mapping -inline uint hash_code(int self) { - uint result=0; - generic_hash_code(result, (const char*)&self, sizeof(self)); - return result; -} - /// Auto-object used to temporarily substituting/removing hash values template class Temp_hash_value {