--- parser3/src/include/pa_hash.h 2015/09/24 21:28:19 1.89 +++ parser3/src/include/pa_hash.h 2017/02/15 17:05:21 1.97 @@ -1,7 +1,7 @@ /** @file Parser: hash class decl. - Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2017 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.97 2017/02/15 17:05:21 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,24 +244,37 @@ 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 { return (first) ? first->value : V(0); } + inline Pair* last_pair() const { + return (fpairs_count) ? (Pair*)((char *)last - offsetof(Pair, next)) : NULL; + } + String::Body last_key() const { - if (fpairs_count) { - Pair* pair = (Pair*)((char *)last - offsetof(Pair, next)); + if(Pair* pair = last_pair()) { +#ifdef HASH_CODE_CACHING return String::Body(pair->key, pair->code); +#else + return pair->key; +#endif } else { return String::Body(); } } V last_value() const { - return (fpairs_count) ? ((Pair *)((char *)last - offsetof(Pair, next)))->value : V(0); + if(Pair* pair = last_pair()) + return pair->value; + return NULL; } void order_clear() { @@ -275,8 +287,7 @@ public: *last=pair; 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 +508,6 @@ public: else this->last=pair->prev; #endif - delete pair; *ref=next; --this->fpairs_count; return true; @@ -532,6 +542,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 +637,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 +698,40 @@ public: return fcurrent; } }; -}; -#else //HASH_CODE_CACHING -template class HASH_STRING: public HASH{}; +#ifdef HASH_ORDER + /// simple reverse hash iterator + class ReverseIterator { + const HASH_STRING& fhash; + Pair *fcurrent; + public: + ReverseIterator(const HASH_STRING& ahash): fhash(ahash) { + fcurrent=fhash.last_pair(); + } -#endif //HASH_CODE_CACHING + void prev() { + fcurrent=(fcurrent->prev == &fhash.first) ? NULL : (Pair*)((char *)fcurrent->prev - offsetof(Pair, next)); + } + + operator bool () { + return fcurrent != NULL; + } + + String::Body key(){ + return String::Body(fcurrent->key, fcurrent->code); + } + + V value(){ + return fcurrent->value; + } + + Pair *pair(){ + return fcurrent; + } + }; +#endif + +}; #ifndef HASH_ORDER /// Auto-object used to temporarily substituting/removing string hash values