--- parser3/src/include/pa_hash.h 2003/01/27 15:07:47 1.58.2.7 +++ parser3/src/include/pa_hash.h 2003/02/17 09:56:15 1.58.2.16 @@ -1,7 +1,7 @@ /** @file Parser: hash class decl. - Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ @@ -17,7 +17,7 @@ #ifndef PA_HASH_H #define PA_HASH_H -static const char* IDENT_HASH_H="$Date: 2003/01/27 15:07:47 $"; +static const char* IDENT_HASH_H="$Date: 2003/02/17 09:56:15 $"; #include "pa_pool.h" #include "pa_types.h" @@ -58,7 +58,7 @@ public: } /// put a [value] under the [key] @returns existed or not - bool put(const K key, V value) { + bool put(K key, V value) { if(is_full()) expand(); @@ -81,7 +81,7 @@ public: } /// remove the [key] @returns existed or not - bool remove(const K key) { + bool remove(K key) { uint code=hash_code(key); uint index=code%allocated; for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link) @@ -98,18 +98,18 @@ public: } /// get associated [value] by the [key] - V get(const K key) const { + V get(K key) const { uint code=hash_code(key); uint index=code%allocated; for(Pair *pair=refs[index]; pair; pair=pair->link) if(pair->code==code && pair->key==key) return pair->value; - return 0; + return V(0); } /// put a [value] under the [key] if that [key] existed @returns existed or not - bool put_replace(const K key, V value) { + bool put_replace(K key, V value) { uint code=hash_code(key); uint index=code%allocated; for(Pair *pair=refs[index]; pair; pair=pair->link) @@ -124,7 +124,7 @@ public: } /// put a [value] under the [key] if that [key] NOT existed @returns existed or not - bool put_dont_replace(const K key, V value) { + bool put_dont_replace(K key, V value) { if(is_full()) expand(); @@ -164,23 +164,15 @@ public: callback(pair->key, pair->value, info); } - /// iterate over all pairs, passing references - template void for_each(void callback(K, V&, I), I info) const { - Pair **ref=refs; - for(int index=0; indexlink) - callback(pair->key, pair->value, info); - } - /// iterate over all pairs until condition becomes true, return that element - template V *first_that(bool callback(K, V, I), I info) const { + template V first_that(bool callback(K, V, I), I info) const { Pair **ref=refs; for(int index=0; indexlink) if(callback(pair->key, pair->value, info)) - return &pair->value; + return pair->value; - return 0; + return V(0); } /// remove all elements @@ -220,11 +212,11 @@ private: class Pair: public PA_Allocated { public: uint code; - const K key; + K key; V value; Pair *link; - Pair(uint acode, const K akey, V avalue, Pair *alink) : + Pair(uint acode, K akey, V avalue, Pair *alink) : code(acode), key(akey), value(avalue), @@ -291,9 +283,9 @@ template int Hash::allocates_count=sizeof(allocates)/sizeof(uint); /// useful generic hash function -inline uint generic_hash_code(uint aresult, const char *start, uint allocated) { +inline uint generic_hash_code(uint aresult, const char* start, size_t allocated) { uint result=aresult, g; - const char *end=start+allocated; + const char* end=start+allocated; while (start class Temp_hash_value { - Hash& fhash; - const K fname; + Hash& fhash; + K fname; V saved_value; public: - Temp_hash_value(Hash& ahash, const K aname, V avalue) : + Temp_hash_value(Hash& ahash, K aname, V avalue) : fhash(ahash), fname(aname), saved_value(ahash.get(aname)) {