--- parser3/src/include/pa_hash.h 2001/10/29 14:13:14 1.47 +++ parser3/src/include/pa_hash.h 2003/01/21 15:51:10 1.58 @@ -1,26 +1,27 @@ /** @file Parser: hash class decl. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) - - $Id: pa_hash.h,v 1.47 2001/10/29 14:13:14 paf Exp $ + Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_HASH_H #define PA_HASH_H -#include "pa_config_includes.h" +static const char* IDENT_HASH_H="$Date: 2003/01/21 15:51:10 $"; + #include "pa_pool.h" #include "pa_types.h" #include "pa_string.h" - /** 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: @@ -50,8 +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] @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) { @@ -61,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 @@ -156,4 +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