--- parser3/src/include/pa_hash.h 2009/12/04 04:19:58 1.80 +++ parser3/src/include/pa_hash.h 2010/08/11 16:17:27 1.83 @@ -17,7 +17,7 @@ #ifndef PA_HASH_H #define PA_HASH_H -static const char * const IDENT_HASH_H="$Date: 2009/12/04 04:19:58 $"; +static const char * const IDENT_HASH_H="$Date: 2010/08/11 16:17:27 $"; #include "pa_memory.h" #include "pa_types.h" @@ -120,7 +120,6 @@ public: HASH() { allocated=Hash_allocates[allocates_index=0]; - threshold=allocated*THRESHOLD_PERCENT/100; fpairs_count=fused_refs=0; refs=new(UseGC) Pair*[allocated]; #ifdef HASH_ORDER @@ -132,25 +131,27 @@ public: HASH(const HASH& source) { allocates_index=source.allocates_index; allocated=source.allocated; - threshold=source.threshold; fused_refs=source.fused_refs; fpairs_count=source.fpairs_count; refs=new(UseGC) Pair*[allocated]; + // clone & rehash #ifdef HASH_ORDER first=0; last=&first; -#endif - // clone & rehash - Pair **old_ref=source.refs; - for(int index=0; indexlink; - - Pair **ref=&refs[index]; + for(Pair *pair=source.first; pair; pair=pair->next) + { + uint index=pair->code%allocated; + Pair **ref=&refs[index]; + HASH_NEW_PAIR(pair->code, pair->key, pair->value); + } +#else + for(int i=0; ilink) + { + Pair **ref=&refs[i]; HASH_NEW_PAIR(pair->code, pair->key, pair->value); - - pair=next; } +#endif } #ifdef USE_DESTRUCTORS @@ -242,6 +243,16 @@ public: return V(0); } +#ifdef HASH_ORDER + V first_value() const { + return (first) ? first->value : V(0); + } + + V last_value() const { + return (fpairs_count) ? ((Pair *)((char *)last - offsetof(Pair, next)))->value : V(0); + } +#endif + /// put a [value] under the [key] if that [key] existed @returns existed or not bool put_replaced(K key, V value) { if(!value) { @@ -333,20 +344,12 @@ public: protected: - /// expand when these %% of allocated exausted - enum { - THRESHOLD_PERCENT=75 - }; - /// the index of [allocated] in [Hash_allocates] int allocates_index; /// number of allocated pairs int allocated; - /// helper: expanding when fused_refs == threshold - int threshold; - /// used pairs int fused_refs; @@ -376,18 +379,17 @@ protected: Pair **last; #endif - /// filled to threshold: needs expanding - bool is_full() { return fused_refs==threshold; } + /// filled to threshold (THRESHOLD_PERCENT=75), needs expanding + bool is_full() { return fused_refs + allocated/4 >= allocated; } /// allocate larger buffer & rehash void expand() { int old_allocated=allocated; Pair **old_refs=refs; - allocates_index=allocates_index+1