|
|
| version 1.58.2.18.2.4, 2003/03/20 13:09:53 | version 1.64, 2005/07/27 06:15:34 |
|---|---|
| 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-2004 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_memory.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 37 public: | Line 49 public: |
| typedef 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=new(0) 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(0) Pair*[allocated]; | refs=new(UseGC) Pair*[allocated]; |
| // clone & rehash | // clone & rehash |
| Pair **old_ref=source.refs; | Pair **old_ref=source.refs; |
| Line 91 public: | Line 103 public: |
| return false; | return false; |
| } | } |
| /// put a [value] under the [key] @returns existed or not | |
| template<typename R, typename F> R maybe_put(K key, V value, F prevent) { | |
| assert(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 | |
| // prevent-function intercepted put? | |
| if(R result=prevent(pair->value)) | |
| return result; | |
| pair->value=value; | |
| return reinterpret_cast<R>(1); | |
| } | |
| // 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 0; | |
| } | |
| /// remove the [key] @returns existed or not | /// remove the [key] @returns existed or not |
| bool remove(K key) { | bool remove(K key) { |
| uint code=hash_code(key); | uint code=hash_code(key); |
| Line 120 public: | Line 162 public: |
| } | } |
| /// 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(K key, V value) { | bool put_replaced(K key, V value) { |
| if(!value) { | if(!value) { |
| remove(key); | remove(key); |
| return false; | return false; |
| Line 138 public: | Line 180 public: |
| return false; | return false; |
| } | } |
| /// put a [value] under the [key] if that [key] existed @returns existed or not | |
| template<typename R, typename F> R maybe_put_replaced(K key, V value, F prevent) { | |
| assert(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 | |
| // prevent-function intercepted put? | |
| if(R result=prevent(pair->value)) | |
| return result; | |
| pair->value=value; | |
| return reinterpret_cast<R>(1); | |
| } | |
| // proper pair not found | |
| return 0; | |
| } | |
| /// 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(K key, V value) { | bool put_dont_replace(K key, V value) { |
| if(!value) { | if(!value) { |
| Line 184 public: | Line 248 public: |
| callback(pair->key, pair->value, info); | callback(pair->key, pair->value, info); |
| } | } |
| /// iterate over all pairs | |
| 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 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; |
| Line 197 public: | Line 269 public: |
| /// 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 208 private: | Line 280 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 251 private: | Line 318 private: |
| int old_allocated=allocated; | int old_allocated=allocated; |
| Pair **old_refs=refs; | Pair **old_refs=refs; |
| allocates_index=allocates_index+1<allocates_count?allocates_index+1:allocates_count-1; | allocates_index=allocates_index+1<HASH_ALLOCATES_COUNT?allocates_index+1:HASH_ALLOCATES_COUNT-1; |
| // allocated bigger refs array | // allocated bigger refs array |
| allocated=allocates[allocates_index]; | allocated=Hash_allocates[allocates_index]; |
| threshold=allocated*THRESHOLD_PERCENT/100; | threshold=allocated*THRESHOLD_PERCENT/100; |
| refs=new(UseGC) Pair*[allocated]; | refs=new(UseGC) Pair*[allocated]; |
| Line 279 private: //disabled | Line 346 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}; | } |
| } | |
| template<typename K, typename V> | |
| int Hash<K, V>::allocates_count=sizeof(allocates)/sizeof(uint); | |
| /// useful generic hash function | /// useful generic hash function |
| inline void generic_hash_code(uint& result, const char* s) { | inline void generic_hash_code(uint& result, const char* s) { |
| while(char c=*s++) { | while(char c=*s++) { |