--- parser3/src/include/pa_hash.h 2001/05/17 10:22:24 1.41 +++ parser3/src/include/pa_hash.h 2002/02/08 07:27:44 1.53 @@ -1,26 +1,27 @@ /** @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: Alexander Petrosyan (http://paf.design.ru) - $Id: pa_hash.h,v 1.41 2001/05/17 10:22:24 parser Exp $ + $Id: pa_hash.h,v 1.53 2002/02/08 07:27:44 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" - /** 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: @@ -31,20 +32,30 @@ public: /// for_each iterator function type typedef void (*For_each_func)(const Key& key, Val *value, void *info); + /// for_each iterator function type + typedef void (*For_each_func_refed)(const Key& key, Val *& value, void *info); + /// first_that iterator function type - typedef bool (*First_that_func)(const Key& key, Val *value, void *info); + typedef void *(*First_that_func)(const Key& key, Val *value, void *info); public: Hash(Pool& apool) : Pooled(apool) { - construct(apool); + construct_new(); + } + + Hash(const Hash& source) : Pooled(source.pool()){ + 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) { @@ -54,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 @@ -75,20 +86,24 @@ public: //@} /// number of elements in hash - int size() const { return used; } + int size() const { return count; } /// iterate over all not zero elements void for_each(For_each_func func, void *info=0) const; + /// iterate over all not zero elements, passing references + void for_each(For_each_func_refed func, void *info=0) const; + /// iterate over all elements until condition - Val* first_that(First_that_func func, void *info=0) const; + void *first_that(First_that_func func, void *info=0) const; /// remove all elements void clear(); protected: - void construct(Pool& apool); + void construct_new(); + void construct_copy(const Hash& source); private: @@ -113,12 +128,15 @@ private: /// used pairs int used; + /// stored pairs total (including those by links) + int count; + /// pair storage class Pair { - friend Hash; + friend class Hash; uint code; - const Key key; + const Key& key; Val *value; Pair *link; @@ -139,8 +157,8 @@ private: private: //disabled - //Hash(Hash&) {} Hash& operator = (const Hash&) { return *this; } }; + #endif