--- parser3/src/include/pa_hash.h 2001/10/29 08:23:49 1.46 +++ parser3/src/include/pa_hash.h 2003/01/22 15:39:07 1.58.2.1 @@ -1,28 +1,29 @@ /** @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.46 2001/10/29 08:23:49 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/22 15:39:07 $"; + #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 { +class Hash: public PA_Object { public: typedef String Key; ///< hash Key type. longing for templates @@ -39,19 +40,22 @@ public: public: - Hash(Pool& apool) : Pooled(apool) { + Hash() { construct_new(); } - Hash(const Hash& source) : Pooled(source.pool()){ + Hash(const Hash& source) { construct_copy(source); } /// 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 @@ -132,11 +136,11 @@ private: friend class Hash; uint code; - const Key key; + const Key& key; Val *value; Pair *link; - void *operator new(size_t allocated, Pool& apool); + void *operator new(size_t size); Pair(uint acode, const Key& akey, Val *avalue, Pair *alink) : code(acode), @@ -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