--- parser3/src/include/pa_hash.h 2001/11/01 15:45:27 1.49 +++ parser3/src/include/pa_hash.h 2002/03/28 14:26:48 1.55 @@ -1,17 +1,16 @@ /** @file Parser: hash class decl. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Author: Alexandr Petrosian (http://paf.design.ru) - $Id: pa_hash.h,v 1.49 2001/11/01 15:45:27 paf Exp $ + $Id: pa_hash.h,v 1.55 2002/03/28 14:26:48 paf Exp $ */ #ifndef PA_HASH_H #define PA_HASH_H -#include "pa_config_includes.h" #include "pa_pool.h" #include "pa_types.h" #include "pa_string.h" @@ -20,6 +19,9 @@ Pooled hash. Automatically rehashed when almost full. + Contains no 0 values. + get returning 0 means there were no such. + "put value 0" means "remove" */ class Hash : public Pooled { public: @@ -49,11 +51,11 @@ public: /// useful generic hash function static uint generic_code(uint aresult, const char *start, uint allocated); - /// put a [value] under the [key], return existed or not + /// put a [value] under the [key] @returns existed or not bool put(const Key& key, Val *value); - /// remove the [key] - void remove(const Key& key); + /// remove the [key] @returns existed or not + bool remove(const Key& key); /* /// dirty hack to allow constant items storage. I long for Hash bool put(const Key& key, const Val *value) { @@ -63,10 +65,10 @@ public: /// get associated [value] by the [key] Val *get(const Key& key) const; - /// put a [value] under the [key] if that [key] existed, return existed or not + /// put a [value] under the [key] if that [key] existed @returns existed or not bool put_replace(const Key& key, Val *value); - /// put a [value] under the [key] if that [key] NOT existed, return existed or not + /// put a [value] under the [key] if that [key] NOT existed @returns existed or not bool put_dont_replace(const Key& key, Val *value); /// put all 'src' values if NO with same key existed @@ -158,5 +160,21 @@ private: //disabled Hash& operator = (const Hash&) { return *this; } }; +/// Auto-object used for temporarily substituting/removing hash values +class Temp_hash_value { + Hash& fhash; + const Hash::Key& fname; + Hash::Val *saved_value; +public: + Temp_hash_value(Hash& ahash, const Hash::Key& aname, Hash::Val *avalue) : + fhash(ahash), + fname(aname), + saved_value(ahash.get(aname)) { + fhash.put(aname, avalue); + } + ~Temp_hash_value() { + fhash.put(fname, saved_value); + } +}; #endif