|
|
| version 1.52, 2001/12/15 21:28:19 | version 1.70, 2009/04/15 07:46:43 |
|---|---|
| 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-2005 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru) | 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 "pa_pool.h" | static const char * const IDENT_HASH_H="$Date$"; |
| #include "pa_memory.h" | |
| #include "pa_types.h" | #include "pa_types.h" |
| #include "pa_string.h" | |
| const int HASH_ALLOCATES_COUNT=29; | |
| /** Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster | |
| paf: HPUX ld could not handle static member: unsatisfied symbols | |
| */ | |
| static uint Hash_allocates[HASH_ALLOCATES_COUNT]={ | |
| 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}; | |
| /// useful generic hash function | |
| inline void generic_hash_code(uint& result, char c) { | |
| result=(result<<4)+c; | |
| if(uint g=(result&0xF0000000)) { | |
| result=result^(g>>24); | |
| result=result^g; | |
| } | |
| } | |
| /// useful generic hash function | |
| inline void generic_hash_code(uint& result, const char* s) { | |
| while(char c=*s++) { | |
| result=(result<<4)+c; | |
| if(uint g=(result&0xF0000000)) { | |
| result=result^(g>>24); | |
| result=result^g; | |
| } | |
| } | |
| } | |
| /// useful generic hash function | |
| inline void generic_hash_code(uint& result, const char* buf, size_t size) { | |
| const char* end=buf+size; | |
| while(buf<end) { | |
| result=(result<<4)+*buf++; | |
| if(uint g=(result&0xF0000000)) { | |
| result=result^(g>>24); | |
| result=result^g; | |
| } | |
| } | |
| } | |
| /// simple hash code of int. used by EXIF mapping | |
| inline uint hash_code(int self) { | |
| uint result=0; | |
| generic_hash_code(result, (const char*)&self, sizeof(self)); | |
| return result; | |
| } | |
| /** | /** |
| Pooled hash. | Simple hash. |
| Automatically rehashed when almost full. | Automatically rehashed when almost is_full. |
| Contains no 0 values. | Contains no 0 values. |
| get returning 0 means there were no such. | get returning 0 means there were no such. |
| "put value 0" means "remove" | "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=Hash_allocates[allocates_index=0]; |
| threshold=allocated*THRESHOLD_PERCENT/100; | |
| fpairs_count=fused_refs=0; | |
| refs=new(UseGC) Pair*[allocated]; | |
| } | |
| /// for_each iterator function type | Hash(const Hash& source) { |
| typedef void (*For_each_func_refed)(const Key& key, Val *& value, void *info); | allocates_index=source.allocates_index; |
| allocated=source.allocated; | |
| threshold=source.threshold; | |
| fused_refs=source.fused_refs; | |
| fpairs_count=source.fpairs_count; | |
| refs=new(UseGC) Pair*[allocated]; | |
| // clone & rehash | |
| Pair **old_ref=source.refs; | |
| for(int index=0; index<allocated; index++) | |
| for(Pair *pair=*old_ref++; pair; ) { | |
| Pair *next=pair->link; | |
| /// first_that iterator function type | Pair **new_ref=&refs[index]; |
| typedef void *(*First_that_func)(const Key& key, Val *value, void *info); | *new_ref=new Pair(pair->code, pair->key, pair->value, *new_ref); |
| public: | pair=next; |
| } | |
| } | |
| Hash(Pool& apool) : Pooled(apool) { | /// put a [value] under the [key] @returns existed or not |
| construct_new(); | bool put(K key, V value) { |
| if(!value) { | |
| remove(key); | |
| return false; | |
| } | |
| 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; | |
| } | } |
| Hash(const Hash& source) : Pooled(source.pool()){ | /// put a [value] under the [key] @returns existed or not |
| construct_copy(source); | template<typename R, typename F, typename I> R replace_maybe_append(K key, V value, F prevent, I info) { |
| if(!value) { | |
| // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel) | |
| remove(key); | |
| // this has nothing to do with properties, doing no special property handling here | |
| return 0; | |
| } | |
| 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 reinterpret_cast<R>(1); | |
| } | |
| // proper pair not found | |
| // prevent-function intercepted append? | |
| if(R result=prevent(value, info)) | |
| return result; | |
| //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 0; | |
| } | } |
| /// useful generic hash function | /// put a [value] under the [key] @returns existed or not |
| static uint generic_code(uint aresult, const char *start, uint allocated); | template<typename R, typename F1, typename F2, typename I> |
| R maybe_replace_maybe_append(K key, V value, F1 prevent_replace, F2 prevent_append, I info) | |
| { | |
| if(!value) { | |
| // they can come here from Temp_value_element::dctor to restore some empty value | |
| remove(key); | |
| // this has nothing to do with properties, doing no special property handling here | |
| return 0; | |
| } | |
| 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 | |
| // prevent-function intercepted replace? | |
| if(R result=prevent_replace(pair->value, info)) | |
| return result; | |
| pair->value=value; | |
| return reinterpret_cast<R>(1); | |
| } | |
| // proper pair not found | |
| // prevent-function intercepted append? | |
| if(R result=prevent_append(value, info)) | |
| return result; | |
| //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 0; | |
| } | |
| /// put a [value] under the [key] @returns existed or not | /// put a [value] under the [key] @returns existed or not |
| bool put(const Key& key, Val *value); | template<typename R, typename F1, typename I> |
| R maybe_replace_never_append(K key, V value, F1 prevent_replace, I info) | |
| { | |
| if(!value) { | |
| // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel) | |
| remove(key); | |
| // this has nothing to do with properties, doing no special property handling here | |
| return 0; | |
| } | |
| 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 | |
| // prevent-function intercepted replace? | |
| if(R result=prevent_replace(pair->value, info)) | |
| return result; | |
| pair->value=value; | |
| return reinterpret_cast<R>(1); | |
| } | |
| return 0; | |
| } | |
| /// remove the [key] @returns existed or not | /// remove the [key] @returns existed or not |
| bool remove(const Key& key); | bool remove(K key) { |
| /* | uint code=hash_code(key); |
| /// dirty hack to allow constant items storage. I long for Hash<const Val*> | uint index=code%allocated; |
| bool put(const Key& key, const Val *value) { | for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link) |
| return put(key, const_cast<Val *>(value)); | 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; | |
| } | |
| return false; | |
| } | } |
| */ | |
| /// return true if key exists | |
| bool contains(K key){ | |
| 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 true; | |
| } | |
| return false; | |
| } | |
| /// get associated [value] by the [key] | /// get associated [value] by the [key] |
| Val *get(const Key& 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 V(0); | |
| } | |
| /// get associated [value] by the [key] + [code] (faster) | |
| V get_by_hash_code(uint code, K key) const { | |
| uint index=code%allocated; | |
| for(Pair *pair=refs[index]; pair; pair=pair->link) | |
| if(pair->code==code && pair->key==key) | |
| return pair->value; | |
| return V(0); | |
| } | |
| /// put a [value] under the [key] if that [key] existed @returns 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); | bool put_replaced(K key, V value) { |
| if(!value) { | |
| remove(key); | |
| return false; | |
| } | |
| 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 a [value] under the [key] if that [key] NOT existed @returns existed or not | // proper pair not found |
| bool put_dont_replace(const Key& key, Val *value); | return false; |
| } | |
| /// put all 'src' values if NO with same key existed | /// put a [value] under the [key] if that [key] existed @returns existed or not |
| void merge_dont_replace(const Hash& src); | template<typename R, typename F> R maybe_put_replaced(K key, V value, F prevent) { |
| if(!value) { | |
| // they can come here from Temp_value_element::dctor to restore some empty value | |
| remove(key); | |
| // this has nothing to do with properties, doing no special property handling here | |
| return 0; | |
| } | |
| 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 | |
| // prevent-function intercepted put? | |
| if(R result=prevent(pair->value)) | |
| return result; | |
| pair->value=value; | |
| return reinterpret_cast<R>(1); | |
| } | |
| void put(const Key& key, int value) { put(key, reinterpret_cast<Val *>(value)); } | // proper pair not found |
| void put(const Key& key, const String *value) { | return 0; |
| put(key, static_cast<Val *>(const_cast<String *>(value))); | |
| } | } |
| //@{ | /// put a [value] under the [key] if that [key] NOT existed @returns existed or not |
| /// handy get, longing for Hash<int>, Hash<String *> | bool put_dont_replace(K key, V value) { |
| int get_int(const Key& key) const { return reinterpret_cast<int>(get(key)); } | if(!value) { |
| const String *get_string(const Key& key) const { return static_cast<String *>(get(key)); } | remove(key); |
| //@} | return false; |
| } | |
| 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 | |
| @todo optimize this.allocated==src.allocated case | |
| */ | |
| void merge_dont_replace(const Hash& src) { | |
| for(int i=0; i<src.allocated; i++) | |
| for(Pair *pair=src.refs[i]; pair; pair=pair->link) | |
| put_dont_replace(pair->key, pair->value); | |
| } | |
| /// number of elements in hash | /// number of elements in hash |
| int size() const { return count; } | 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) const; | 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); | |
| } | |
| /// iterate over all not zero elements, passing references | /// iterate over all pairs |
| void for_each(For_each_func_refed func, void *info=0) const; | template<typename I> void for_each_ref(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); | |
| } | |
| /// iterate over all elements until condition | /// iterate over all pairs until condition becomes true, return that element |
| void *first_that(First_that_func func, void *info=0) const; | 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 V(0); | |
| } | |
| /// remove all elements | /// remove all elements |
| void clear(); | void clear() { |
| memset(refs, 0, sizeof(*refs)*allocated); | |
| protected: | fpairs_count=fused_refs=0; |
| } | |
| void construct_new(); | |
| void construct_copy(const Hash& source); | |
| private: | private: |
| Line 112 private: | Line 423 private: |
| THRESHOLD_PERCENT=75 | THRESHOLD_PERCENT=75 |
| }; | }; |
| /// the index of [allocated] in [allocates] | /// the index of [allocated] in [Hash_allocates] |
| int allocates_index; | int allocates_index; |
| /// possible [allocates]. prime numbers | |
| static uint allocates[]; | |
| 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; |
| /// stored pairs total (including those by links) | /// stored pairs total (including those by links) |
| int count; | int fpairs_count; |
| /// pair storage | /// pair storage |
| class Pair { | class Pair: public PA_Allocated { |
| friend class Hash; | 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), |
| Line 150 private: | Line 454 private: |
| } **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 | /// allocate larger buffer & rehash |
| void expand(); | void expand() { |
| int old_allocated=allocated; | |
| Pair **old_refs=refs; | |
| allocates_index=allocates_index+1<HASH_ALLOCATES_COUNT?allocates_index+1:HASH_ALLOCATES_COUNT-1; | |
| // allocated bigger refs array | |
| allocated=Hash_allocates[allocates_index]; | |
| threshold=allocated*THRESHOLD_PERCENT/100; | |
| refs=new(UseGC) 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; | |
| } | |
| delete[] old_refs; | |
| } | |
| private: //disabled | private: //disabled |
| Hash& operator = (const Hash&) { return *this; } | Hash& operator = (const Hash&) { return *this; } |
| }; | }; |
| /// Auto-object used to temporarily substituting/removing hash values | |
| template <typename K, typename V> | |
| class Temp_hash_value { | |
| Hash<K, V>& 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 |