--- parser3/src/include/pa_hash.h 2007/12/28 11:23:20 1.69 +++ parser3/src/include/pa_hash.h 2009/04/17 23:21:51 1.72 @@ -17,7 +17,7 @@ #ifndef PA_HASH_H #define PA_HASH_H -static const char * const IDENT_HASH_H="$Date: 2007/12/28 11:23:20 $"; +static const char * const IDENT_HASH_H="$Date: 2009/04/17 23:21:51 $"; #include "pa_memory.h" #include "pa_types.h" @@ -114,6 +114,17 @@ public: } } + ~Hash() { + Pair **ref=refs; + for(int index=0; indexlink; + delete pair; + pair=next; + } + delete[] refs; + } + /// put a [value] under the [key] @returns existed or not bool put(K key, V value) { if(!value) { @@ -267,26 +278,34 @@ public: return false; } - /// returns exist key or not + /// 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); 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); + } + + /// 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);