|
|
| version 1.34, 2001/04/04 10:50:34 | version 1.58.2.8, 2003/01/27 16:00:41 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: hash class decl. | 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 <paf@design.ru> (http://design.ru/paf) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | |
| $Id$ | /* |
| The prime numbers used from zend_hash.c, | |
| the part of Zend scripting engine library, | |
| Copyrighted (C) 1999-2000 Zend Technologies Ltd. | |
| http://www.zend.com/license/0_92.txt | |
| For more information about Zend please visit http://www.zend.com/ | |
| */ | */ |
| #ifndef PA_HASH_H | #ifndef PA_HASH_H |
| #define PA_HASH_H | #define PA_HASH_H |
| #include <stddef.h> | static const char* IDENT_HASH_H="$Date$"; |
| #include "pa_pool.h" | #include "pa_pool.h" |
| #include "pa_types.h" | #include "pa_types.h" |
| #include "pa_string.h" | |
| /** | /** |
| Pooled hash. | Simple hash. |
| Automatically rehashed when almost full. | Automatically rehashed when almost is_full. |
| Contains no 0 values. | |
| can be thread safe, wich is specified in constructor, | get returning 0 means there were no such. |
| default 'not safe'. | "put value 0" means "remove" |
| */ | */ |
| class Hash : public Pooled { | template<typename K, typename V> class Hash: public PA_Object { |
| public: | public: |
| typedef String Key; ///< hash Key type. longing for templates | typedef K key_type; |
| typedef void Val; ///< hash Val type. longing for templates | typedef V value_type; |
| /// for_each iterator function type | Hash() { |
| typedef void (*For_each_func)(const Key& key, Val *value, void *info); | allocated=allocates[allocates_index=0]; |
| threshold=allocated*THRESHOLD_PERCENT/100; | |
| fpairs_count=fused_refs=0; | |
| refs=static_cast<Pair **>(pa_calloc(sizeof(Pair *)*allocated)); | |
| } | |
| public: | Hash(const Hash& source) { |
| allocates_index=source.allocates_index; | |
| allocated=source.allocated; | |
| threshold=source.threshold; | |
| fused_refs=source.fused_refs; | |
| fpairs_count=source.fpairs_count; | |
| refs=new Pair*[allocated]; | |
| memcpy(refs, source.refs, sizeof(Pair *)*allocated); | |
| } | |
| ~Hash() { | |
| destroy_pairs(); | |
| delete refs; | |
| } | |
| Hash(Pool& apool,bool athread_safe=false) : Pooled(apool) { | /// put a [value] under the [key] @returns existed or not |
| construct(apool, athread_safe); | bool put(K key, V value) { |
| if(is_full()) | |
| expand(); | |
| uint code=hash_code(key); | |
| uint index=code%allocated; | |
| Pair **ref=&refs[index]; | |
| for(Pair *pair=*ref; pair; pair=pair->link) | |
| if(pair->code==code && pair->key==key) { | |
| // found a pair with the same key | |
| pair->value=value; | |
| return true; | |
| } | |
| // proper pair not found -- create&link_in new pair | |
| if(!*ref) // root cell were fused_refs? | |
| fused_refs++; // not, we'll use it and record the fact | |
| *ref=new Pair(code, key, value, *ref); | |
| fpairs_count++; | |
| return false; | |
| } | } |
| /// useful generic hash function | /// remove the [key] @returns existed or not |
| static uint generic_code(uint aresult, const char *start, uint allocated); | bool remove(K key) { |
| uint code=hash_code(key); | |
| uint index=code%allocated; | |
| for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link) | |
| if((*ref)->code==code && (*ref)->key==key) { | |
| // found a pair with the same key | |
| Pair *next=(*ref)->link; | |
| delete *ref; | |
| *ref=next; | |
| --fpairs_count; | |
| return true; | |
| } | |
| /// put a [value] under the [key], return existed or not | return false; |
| /*SYNCHRONIZED*/ bool put(const Key& key, Val *value); | |
| /* | |
| /// dirty hack to allow constant items storage. I long for Hash<const Val*> | |
| /*SYNCHRONIZED* / bool put(const Key& key, const Val *value) { | |
| return put(key, const_cast<Val *>(value)); | |
| } | } |
| */ | |
| /// get associated [value] by the [key] | |
| /*SYNCHRONIZED*/ Val *get(const Key& key) const; | |
| /// put a [value] under the [key] if that [key] existed, return existed or not | /// get associated [value] by the [key] |
| /*SYNCHRONIZED*/ bool put_replace(const Key& key, Val *value); | 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; | |
| } | |
| /// put a [value] under the [key] if that [key] NOT existed, return existed or not | /// put a [value] under the [key] if that [key] existed @returns existed or not |
| /*SYNCHRONIZED*/ bool put_dont_replace(const Key& key, Val *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) | |
| if(pair->code==code && pair->key==key) { | |
| // found a pair with the same key, replacing | |
| pair->value=value; | |
| return true; | |
| } | |
| /// put all 'src' values if NO with same key existed | // proper pair not found |
| /*SYNCHRONIZED*/ void merge_dont_replace(const Hash& src); | return false; |
| } | |
| void put(const Key& key, int value) { put(key, reinterpret_cast<Val *>(value)); } | /// put a [value] under the [key] if that [key] NOT existed @returns existed or not |
| void put(const Key& key, String *value) { put(key, static_cast<Val *>(value)); } | bool put_dont_replace(K key, V value) { |
| if(is_full()) | |
| expand(); | |
| uint code=hash_code(key); | |
| uint index=code%allocated; | |
| Pair **ref=&refs[index]; | |
| for(Pair *pair=*ref; pair; pair=pair->link) | |
| if(pair->code==code && pair->key==key) { | |
| // found a pair with the same key, NOT replacing | |
| return true; | |
| } | |
| // proper pair not found -- create&link_in new pair | |
| if(!*ref) // root cell were fused_refs? | |
| fused_refs++; // not, we'll use it and record the fact | |
| *ref=new Pair(code, key, value, *ref); | |
| fpairs_count++; | |
| return false; | |
| } | |
| //@{ | /// put all 'src' values if NO with same key existed |
| /// handy get, longing for Hash<int>, Hash<String *> | void merge_dont_replace(const Hash& src) { |
| int get_int(const Key& key) const { return reinterpret_cast<int>(get(key)); } | for(int i=0; i<src.allocated; i++) |
| const String *get_string(const Key& key) const { return static_cast<String *>(get(key)); } | for(Pair *pair=src.refs[i]; pair; pair=pair->link) |
| //@} | put_dont_replace(pair->key, pair->value); |
| // MAY:optimize this.allocated==src.allocated case | |
| } | |
| /// number of elements in hash | /// number of elements in hash |
| int size() { return used; } | int count() const { return fpairs_count; } |
| /// iterate over all not zero elements | /// iterate over all pairs |
| void for_each(For_each_func func, void *info=0); | template<typename I> void for_each(void callback(K, V, I), I info) const { |
| Pair **ref=refs; | |
| for(int index=0; index<allocated; index++) | |
| for(Pair *pair=*ref++; pair; pair=pair->link) | |
| callback(pair->key, pair->value, info); | |
| } | |
| /// remove all elements | /// iterate over all pairs, passing references |
| void clear(); | template<typename I> void for_each(void callback(K, V&, I), I info) const { |
| Pair **ref=refs; | |
| for(int index=0; index<allocated; index++) | |
| for(Pair *pair=*ref++; pair; pair=pair->link) | |
| callback(pair->key, pair->value, info); | |
| } | |
| protected: | /// iterate over all pairs until condition becomes true, return that element |
| template<typename I> V *first_that(bool callback(K, V, I), I info) const { | |
| Pair **ref=refs; | |
| for(int index=0; index<allocated; index++) | |
| for(Pair *pair=*ref++; pair; pair=pair->link) | |
| if(callback(pair->key, pair->value, info)) | |
| return &pair->value; | |
| return 0; | |
| } | |
| void construct(Pool& apool, bool athread_safe); | /// remove all elements |
| void clear() { | |
| destroy_pairs(); memset(refs, 0, sizeof(*refs)*allocated); | |
| fpairs_count=fused_refs=0; | |
| } | |
| private: | private: |
| // expand when these %% of allocated exausted | /// expand when these %% of allocated exausted |
| enum { | enum { |
| THRESHOLD_PERCENT=75 | THRESHOLD_PERCENT=75 |
| }; | }; |
| // am I thread-safe? | /// the index of [allocated] in [allocates] |
| bool fthread_safe; | |
| // the index of [allocated] in [allocates] | |
| int allocates_index; | int allocates_index; |
| // possible [allocates]. prime numbers | /// possible [allocates]. prime numbers |
| static uint allocates[]; | static uint allocates[]; |
| static int allocates_count; | static int allocates_count; |
| // number of allocated pairs | /// number of allocated pairs |
| int allocated; | int allocated; |
| // helper: expanding when used == threshold | /// helper: expanding when fused_refs == threshold |
| int threshold; | int threshold; |
| // used pairs | /// used pairs |
| int used; | int fused_refs; |
| // main storage | /// stored pairs total (including those by links) |
| class Pair { | int fpairs_count; |
| friend Hash; | |
| /// pair storage | |
| class Pair: public PA_Allocated { | |
| public: | |
| uint code; | uint code; |
| const Key key; | K key; |
| Val *value; | V value; |
| Pair *link; | Pair *link; |
| void *operator new(size_t allocated, Pool& apool); | Pair(uint acode, K akey, V avalue, Pair *alink) : |
| Pair(uint acode, const Key& akey, Val *avalue, Pair *alink) : | |
| code(acode), | code(acode), |
| key(akey), | key(akey), |
| value(avalue), | value(avalue), |
| link(alink) {} | link(alink) {} |
| } **refs; | } **refs; |
| // filled to threshold: needs expanding | /// filled to threshold: needs expanding |
| bool full() { return used==threshold; } | bool is_full() { return fused_refs==threshold; } |
| /// allocate larger buffer & rehash | |
| void expand() { | |
| int old_allocated=allocated; | |
| Pair **old_refs=refs; | |
| // allocated bigger refs array | |
| allocates_index=allocates_index+1<allocates_count?allocates_index+1:allocates_count-1; | |
| allocated=allocates[allocates_index]; | |
| threshold=allocated*THRESHOLD_PERCENT/100; | |
| refs=static_cast<Pair **>(pa_calloc(sizeof(Pair *)*allocated)); | |
| // rehash | |
| Pair **old_ref=old_refs; | |
| for(int old_index=0; old_index<old_allocated; old_index++) | |
| for(Pair *pair=*old_ref++; pair; ) { | |
| Pair *next=pair->link; | |
| uint new_index=pair->code%allocated; | |
| Pair **new_ref=&refs[new_index]; | |
| pair->link=*new_ref; | |
| *new_ref=pair; | |
| pair=next; | |
| } | |
| // allocate larger buffer & rehash | delete old_refs; |
| void expand(); | } |
| void destroy_pairs() { | |
| Pair **ref=refs; | |
| for(int index=0; index<allocated; index++) { | |
| Pair *pair=*ref++; | |
| while(pair) { | |
| Pair *next=pair->link; | |
| delete pair; | |
| pair=next; | |
| } | |
| } | |
| } | |
| private: //disabled | private: //disabled |
| //Hash(Hash&) {} | |
| Hash& operator = (const Hash&) { return *this; } | Hash& operator = (const Hash&) { return *this; } |
| }; | }; |
| /* Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster */ | |
| template<typename K, typename V> | |
| uint Hash<K, V>::allocates[]={ | |
| 5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963, | |
| 16229, 32531, 65407, 130987, 262237, 524521, 1048793, | |
| 2097397, 4194103, 8388857, 16777447, 33554201, 67108961, | |
| 134217487, 268435697, 536870683, 1073741621, 2147483399}; | |
| template<typename K, typename V> | |
| int Hash<K, V>::allocates_count=sizeof(allocates)/sizeof(uint); | |
| /// useful generic hash function | |
| inline uint generic_hash_code(uint aresult, const char *start, uint allocated) { | |
| uint result=aresult, g; | |
| const char *end=start+allocated; | |
| while (start<end) { | |
| result=(result<<4)+*start++; | |
| if ((g=(result&0xF0000000))) { | |
| result=result^(g>>24); | |
| result=result^g; | |
| } | |
| } | |
| return result; | |
| } | |
| /// Auto-object used to temporarily substituting/removing hash values | |
| template <typename K, typename V> | |
| class Temp_hash_value { | |
| Hash& fhash; | |
| K fname; | |
| V saved_value; | |
| public: | |
| Temp_hash_value(Hash<K, V>& ahash, K aname, V avalue) : | |
| fhash(ahash), | |
| fname(aname), | |
| saved_value(ahash.get(aname)) { | |
| fhash.put(aname, avalue); | |
| } | |
| ~Temp_hash_value() { | |
| fhash.put(fname, saved_value); | |
| } | |
| }; | |
| #endif | #endif |