--- parser3/src/include/pa_hash.h 2003/02/04 15:59:04 1.58.2.13 +++ parser3/src/include/pa_hash.h 2003/03/18 15:14:16 1.58.2.18.2.1 @@ -17,9 +17,9 @@ #ifndef PA_HASH_H #define PA_HASH_H -static const char* IDENT_HASH_H="$Date: 2003/02/04 15:59:04 $"; +static const char* IDENT_HASH_H="$Date: 2003/03/18 15:14:16 $"; -#include "pa_pool.h" +#include "pa_memory.h" #include "pa_types.h" /** @@ -40,7 +40,7 @@ public: allocated=allocates[allocates_index=0]; threshold=allocated*THRESHOLD_PERCENT/100; fpairs_count=fused_refs=0; - refs=static_cast(pa_calloc(sizeof(Pair *)*allocated)); + refs=new(0) Pair*[allocated]; } Hash(const Hash& source) { @@ -49,8 +49,19 @@ public: threshold=source.threshold; fused_refs=source.fused_refs; fpairs_count=source.fpairs_count; - refs=new Pair*[allocated]; - memcpy(refs, source.refs, sizeof(Pair *)*allocated); + refs=new(0) Pair*[allocated]; + + // clone & rehash + Pair **old_ref=source.refs; + for(int index=0; indexlink; + + Pair **new_ref=&refs[index]; + *new_ref=new Pair(pair->code, pair->key, pair->value, *new_ref); + + pair=next; + } } ~Hash() { destroy_pairs(); @@ -59,6 +70,10 @@ public: /// 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(); @@ -110,6 +125,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) @@ -125,6 +144,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(); @@ -145,12 +168,13 @@ public: return false; } - /// put all 'src' values if NO with same key existed + /** put all 'src' values if NO with same key existed + @todo optimize this.allocated==src.allocated case + */ void merge_dont_replace(const Hash& src) { for(int i=0; ilink) put_dont_replace(pair->key, pair->value); - // MAY:optimize this.allocated==src.allocated case } /// number of elements in hash @@ -170,7 +194,7 @@ public: for(int index=0; indexlink) if(callback(pair->key, pair->value, info)) - return &pair->value; + return pair->value; return V(0); } @@ -231,11 +255,11 @@ private: int old_allocated=allocated; Pair **old_refs=refs; - // allocated bigger refs array allocates_index=allocates_index+1(pa_calloc(sizeof(Pair *)*allocated)); + refs=new(0) Pair*[allocated]; // rehash Pair **old_ref=old_refs; @@ -283,7 +307,7 @@ template int Hash::allocates_count=sizeof(allocates)/sizeof(uint); /// useful generic hash function -inline uint generic_hash_code(uint aresult, const char* start, uint allocated) { +inline uint generic_hash_code(uint aresult, const char* start, size_t allocated) { uint result=aresult, g; const char* end=start+allocated; @@ -297,6 +321,11 @@ inline uint generic_hash_code(uint aresu 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)); +} + /// Auto-object used to temporarily substituting/removing hash values template class Temp_hash_value {