|
|
| version 1.58.2.3, 2003/01/23 15:38:05 | version 1.62.12.1, 2005/08/05 13:02:59 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: hash class decl. | Parser: hash class decl. |
| Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 17 | Line 17 |
| #ifndef PA_HASH_H | #ifndef PA_HASH_H |
| #define PA_HASH_H | #define PA_HASH_H |
| static const char* IDENT_HASH_H="$Date$"; | static const char * const IDENT_HASH_H="$Date$"; |
| #include "pa_pool.h" | #include "pa_memory.h" |
| #include "pa_types.h" | #include "pa_types.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}; | |
| /** | /** |
| Simple hash. | Simple hash. |
| Line 33 static const char* IDENT_HASH_H="$Date$" | Line 45 static const char* IDENT_HASH_H="$Date$" |
| template<typename K, typename V> class Hash: public PA_Object { | template<typename K, typename V> class Hash: public PA_Object { |
| public: | public: |
| typename K key_type; | typedef K key_type; |
| typename V value_type; | typedef V value_type; |
| Hash() { | Hash() { |
| allocated=allocates[allocates_index=0]; | allocated=Hash_allocates[allocates_index=0]; |
| threshold=allocated*THRESHOLD_PERCENT/100; | threshold=allocated*THRESHOLD_PERCENT/100; |
| fpairs_count=fused_refs=0; | fpairs_count=fused_refs=0; |
| refs=static_cast<Pair **>(pa_calloc(sizeof(Pair *)*allocated)); | refs=new(UseGC) Pair*[allocated]; |
| } | } |
| Hash(const Hash& source) { | Hash(const Hash& source) { |
| Line 49 public: | Line 61 public: |
| threshold=source.threshold; | threshold=source.threshold; |
| fused_refs=source.fused_refs; | fused_refs=source.fused_refs; |
| fpairs_count=source.fpairs_count; | fpairs_count=source.fpairs_count; |
| refs=new Pair*[allocated]; | refs=new(UseGC) Pair*[allocated]; |
| memcpy(refs, source.refs, sizeof(Pair *)*allocated); | |
| } | // clone & rehash |
| ~Hash() { | Pair **old_ref=source.refs; |
| destroy_pairs(); | for(int index=0; index<allocated; index++) |
| delete refs; | for(Pair *pair=*old_ref++; pair; ) { |
| } | Pair *next=pair->link; |
| /// useful generic hash function | Pair **new_ref=&refs[index]; |
| static uint generic_code(uint aresult, const char *start, uint allocated) { | *new_ref=new Pair(pair->code, pair->key, pair->value, *new_ref); |
| uint result=aresult, g; | |
| const char *end=start+allocated; | pair=next; |
| while (start<end) { | |
| result=(result<<4)+*start++; | |
| if ((g=(result&0xF0000000))) { | |
| result=result^(g>>24); | |
| result=result^g; | |
| } | } |
| } | |
| return result; | |
| } | } |
| /// put a [value] under the [key] @returns existed or not | /// put a [value] under the [key] @returns existed or not |
| bool put(const K key, V value) { | bool put(K key, V value) { |
| if(!value) { | |
| remove(key); | |
| return false; | |
| } | |
| if(is_full()) | if(is_full()) |
| expand(); | expand(); |
| uint code=key.hash_code(); | uint code=hash_code(key); |
| uint index=code%allocated; | uint index=code%allocated; |
| Pair **ref=&refs[index]; | Pair **ref=&refs[index]; |
| for(Pair *pair=*ref; pair; pair=pair->link) | for(Pair *pair=*ref; pair; pair=pair->link) |
| Line 96 public: | Line 104 public: |
| } | } |
| /// remove the [key] @returns existed or not | /// remove the [key] @returns existed or not |
| bool remove(const K key) { | bool remove(K key) { |
| uint code=key.hash_code(); | uint code=hash_code(key); |
| uint index=code%allocated; | uint index=code%allocated; |
| for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link) | for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link) |
| if((*ref)->code==code && (*ref)->key==key) { | if((*ref)->code==code && (*ref)->key==key) { |
| Line 113 public: | Line 121 public: |
| } | } |
| /// get associated [value] by the [key] | /// get associated [value] by the [key] |
| V get(const K key) const { | V get(K key) const { |
| uint code=key.hash_code(); | uint code=hash_code(key); |
| uint index=code%allocated; | uint index=code%allocated; |
| for(Pair *pair=refs[index]; pair; pair=pair->link) | for(Pair *pair=refs[index]; pair; pair=pair->link) |
| if(pair->code==code && pair->key==key) | if(pair->code==code && pair->key==key) |
| return pair->value; | return pair->value; |
| return 0; | 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 K key, V value) { | bool put_replace(K key, V value) { |
| uint code=key.hash_code(); | if(!value) { |
| remove(key); | |
| return false; | |
| } | |
| uint code=hash_code(key); | |
| uint index=code%allocated; | uint index=code%allocated; |
| for(Pair *pair=refs[index]; pair; pair=pair->link) | for(Pair *pair=refs[index]; pair; pair=pair->link) |
| if(pair->code==code && pair->key==key) { | if(pair->code==code && pair->key==key) { |
| Line 139 public: | Line 151 public: |
| } | } |
| /// put a [value] under the [key] if that [key] NOT existed @returns existed or not | /// 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(!value) { | |
| remove(key); | |
| return false; | |
| } | |
| if(is_full()) | if(is_full()) |
| expand(); | expand(); |
| uint code=key.hash_code(); | uint code=hash_code(key); |
| uint index=code%allocated; | uint index=code%allocated; |
| Pair **ref=&refs[index]; | Pair **ref=&refs[index]; |
| for(Pair *pair=*ref; pair; pair=pair->link) | for(Pair *pair=*ref; pair; pair=pair->link) |
| Line 160 public: | Line 176 public: |
| return false; | return false; |
| } | } |
| /// put all 'src' values if NO with same key existed | /** put all 'src' values if NO with same key existed |
| @todo optimize this.allocated==src.allocated case | |
| */ | |
| void merge_dont_replace(const Hash& src) { | void merge_dont_replace(const Hash& src) { |
| for(int i=0; i<src.allocated; i++) | for(int i=0; i<src.allocated; i++) |
| for(Pair *pair=src.refs[i]; pair; pair=pair->link) | for(Pair *pair=src.refs[i]; pair; pair=pair->link) |
| put_dont_replace(pair->key, pair->value); | put_dont_replace(pair->key, pair->value); |
| // MAY:optimize this.allocated==src.allocated case | |
| } | } |
| /// number of elements in hash | /// number of elements in hash |
| Line 179 public: | Line 196 public: |
| callback(pair->key, pair->value, info); | callback(pair->key, pair->value, info); |
| } | } |
| /// iterate over all pairs, passing references | /// iterate over all pairs |
| template<typename I> void for_each(void callback(K, V&, I), I info) const { | template<typename I> void for_each_ref(void callback(K, V&, I), I info) const { |
| Pair **ref=refs; | Pair **ref=refs; |
| for(int index=0; index<allocated; index++) | for(int index=0; index<allocated; index++) |
| for(Pair *pair=*ref++; pair; pair=pair->link) | for(Pair *pair=*ref++; pair; pair=pair->link) |
| Line 188 public: | Line 205 public: |
| } | } |
| /// iterate over all pairs until condition becomes true, return that element | /// 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 { | template<typename I> V first_that(bool callback(K, V, I), I info) const { |
| Pair **ref=refs; | Pair **ref=refs; |
| for(int index=0; index<allocated; index++) | for(int index=0; index<allocated; index++) |
| for(Pair *pair=*ref++; pair; pair=pair->link) | for(Pair *pair=*ref++; pair; pair=pair->link) |
| if(callback(pair->key, pair->value, info)) | if(callback(pair->key, pair->value, info)) |
| return &pair->value; | return pair->value; |
| return 0; | return V(0); |
| } | } |
| /// remove all elements | /// remove all elements |
| void clear() { | void clear() { |
| destroy_pairs(); memset(refs, 0, sizeof(*refs)*allocated); | memset(refs, 0, sizeof(*refs)*allocated); |
| fpairs_count=fused_refs=0; | fpairs_count=fused_refs=0; |
| } | } |
| Line 211 private: | Line 228 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; |
| Line 233 private: | Line 245 private: |
| /// pair storage | /// pair storage |
| class Pair: public PA_Allocated { | class Pair: public PA_Allocated { |
| //friend class Hash; | public: |
| uint code; | uint code; |
| const K key; | K key; |
| V value; | V value; |
| Pair *link; | Pair *link; |
| Pair(uint acode, const K akey, V avalue, Pair *alink) : | Pair(uint acode, K akey, V avalue, Pair *alink) : |
| code(acode), | code(acode), |
| key(akey), | key(akey), |
| value(avalue), | value(avalue), |
| Line 255 private: | Line 266 private: |
| int old_allocated=allocated; | int old_allocated=allocated; |
| Pair **old_refs=refs; | Pair **old_refs=refs; |
| allocates_index=allocates_index+1<HASH_ALLOCATES_COUNT?allocates_index+1:HASH_ALLOCATES_COUNT-1; | |
| // allocated bigger refs array | // allocated bigger refs array |
| allocates_index=allocates_index+1<allocates_count?allocates_index+1:allocates_count-1; | allocated=Hash_allocates[allocates_index]; |
| allocated=allocates[allocates_index]; | |
| threshold=allocated*THRESHOLD_PERCENT/100; | threshold=allocated*THRESHOLD_PERCENT/100; |
| refs=static_cast<Pair **>(pa_calloc(sizeof(Pair *)*allocated)); | refs=new(UseGC) Pair*[allocated]; |
| // rehash | // rehash |
| Pair **old_ref=old_refs; | Pair **old_ref=old_refs; |
| Line 275 private: | Line 286 private: |
| pair=next; | pair=next; |
| } | } |
| delete old_refs; | delete[] old_refs; |
| } | |
| 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 |
| Line 295 private: //disabled | Line 294 private: //disabled |
| 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 */ | /// useful generic hash function |
| template<typename K, typename V> | inline void generic_hash_code(uint& result, char c) { |
| uint Hash<K, V>::allocates[]={ | result=(result<<4)+c; |
| 5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963, | if(uint g=(result&0xF0000000)) { |
| 16229, 32531, 65407, 130987, 262237, 524521, 1048793, | result=result^(g>>24); |
| 2097397, 4194103, 8388857, 16777447, 33554201, 67108961, | result=result^g; |
| 134217487, 268435697, 536870683, 1073741621, 2147483399}; | } |
| } | |
| /// 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; | |
| } | |
| } | |
| } | |
| template<typename K, typename V> | /// simple hash code of int. used by EXIF mapping |
| int Hash<K, V>::allocates_count=sizeof(allocates)/sizeof(uint); | inline uint hash_code(int self) { |
| uint result=0; | |
| generic_hash_code(result, (const char*)&self, sizeof(self)); | |
| return result; | |
| } | |
| /// Auto-object used to temporarily substituting/removing hash values | /// Auto-object used to temporarily substituting/removing hash values |
| template <typename K, typename V> | template <typename K, typename V> |
| class Temp_hash_value { | class Temp_hash_value { |
| Hash& fhash; | Hash<K, V>& fhash; |
| const K fname; | K fname; |
| V saved_value; | V saved_value; |
| public: | public: |
| Temp_hash_value(Hash<K, V>& ahash, const K aname, V avalue) : | Temp_hash_value(Hash<K, V>& ahash, K aname, V avalue) : |
| fhash(ahash), | fhash(ahash), |
| fname(aname), | fname(aname), |
| saved_value(ahash.get(aname)) { | saved_value(ahash.get(aname)) { |