--- parser3/src/include/pa_hash.h 2001/10/05 08:20:26 1.44 +++ 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.44 2001/10/05 08:20:26 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,6 +32,9 @@ 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 void *(*First_that_func)(const Key& key, Val *value, void *info); @@ -47,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) { @@ -58,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 @@ -84,6 +91,9 @@ public: /// 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 void *first_that(First_that_func func, void *info=0) const; @@ -123,10 +133,10 @@ private: /// pair storage class Pair { - friend Hash; + friend class Hash; uint code; - const Key key; + const Key& key; Val *value; Pair *link; @@ -150,4 +160,5 @@ private: //disabled Hash& operator = (const Hash&) { return *this; } }; + #endif