--- parser3/src/include/pa_hash.h 2015/09/24 21:28:19 1.89 +++ parser3/src/include/pa_hash.h 2016/07/04 17:26:23 1.95 @@ -1,7 +1,7 @@ /** @file Parser: hash class decl. - Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ @@ -17,7 +17,7 @@ #ifndef PA_HASH_H #define PA_HASH_H -#define IDENT_PA_HASH_H "$Id: pa_hash.h,v 1.89 2015/09/24 21:28:19 moko Exp $" +#define IDENT_PA_HASH_H "$Id: pa_hash.h,v 1.95 2016/07/04 17:26:23 moko Exp $" #include "pa_memory.h" #include "pa_types.h" @@ -116,11 +116,11 @@ inline uint hash_code(int self) { #endif template class HASH: public PA_Object { +protected: + class Pair; public: - typedef K key_type; typedef V value_type; - class Pair; HASH() { allocated=Hash_allocates[allocates_index=0]; @@ -210,7 +210,6 @@ public: else last=pair->prev; #endif - delete pair; *ref=next; --fpairs_count; return true; @@ -245,7 +244,11 @@ public: #ifdef HASH_ORDER String::Body first_key() const { +#ifdef HASH_CODE_CACHING return (first) ? String::Body(first->key, first->code) : String::Body(); +#else + return (first) ? first->key : String::Body(); +#endif } V first_value() const { @@ -255,7 +258,11 @@ public: String::Body last_key() const { if (fpairs_count) { Pair* pair = (Pair*)((char *)last - offsetof(Pair, next)); +#ifdef HASH_CODE_CACHING return String::Body(pair->key, pair->code); +#else + return pair->key; +#endif } else { return String::Body(); } @@ -276,7 +283,7 @@ public: last=&(pair->next); } -#endif +#endif //HASH_ORDER /// put a [value] under the [key] if that [key] existed @returns existed or not bool put_replaced(K key, V value) { @@ -497,7 +504,6 @@ public: else this->last=pair->prev; #endif - delete pair; *ref=next; --this->fpairs_count; return true; @@ -532,6 +538,22 @@ public: return V(0); } + /// get associated [value] by the [key], optimized + V get(const char *key) const { + uint code=0; + if(key && *key){ + generic_hash_code(code, key); + } else { + key=0; + } + uint index=code%this->allocated; + for(Pair *pair=this->refs[index]; pair; pair=pair->link) + if(pair->code==code && CORD_cmp(pair->key,(CORD)key)==0) + return pair->value; + + return V(0); + } + /// put a [value] under the [key] if that [key] existed @returns existed or not bool put_replaced(K str, V value) { if(!value) { @@ -611,41 +633,57 @@ public: return V(0); } +#else //HASH_CODE_CACHING + +template class HASH_STRING: public HASH{ +public: + typedef typename HASH::Pair Pair; + +#endif //HASH_CODE_CACHING + /// simple hash iterator class Iterator { const HASH_STRING& fhash; Pair *fcurrent; +#ifndef HASH_ORDER int i; +#endif public: - Iterator(const HASH_STRING& ahash): fhash(ahash) { #ifdef HASH_ORDER + Iterator(const HASH_STRING& ahash): fhash(ahash) { fcurrent=fhash.first; + } + + void next() { + fcurrent=fcurrent->next; + } #else + Iterator(const HASH_STRING& ahash): fhash(ahash) { fcurrent=0; for(i=0; inext; -#else if(fcurrent=fcurrent->link) return; for(i++; ikey, fcurrent->code); +#else + return fcurrent->key; +#endif } V value(){ @@ -656,12 +694,8 @@ public: return fcurrent; } }; -}; -#else //HASH_CODE_CACHING -template class HASH_STRING: public HASH{}; - -#endif //HASH_CODE_CACHING +}; #ifndef HASH_ORDER /// Auto-object used to temporarily substituting/removing string hash values