--- parser3/src/include/pa_hash.h 2005/07/27 06:15:34 1.64 +++ parser3/src/include/pa_hash.h 2009/04/17 13:13:09 1.71 @@ -1,7 +1,7 @@ /** @file Parser: hash class decl. - Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2005 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 * const IDENT_HASH_H="$Date: 2005/07/27 06:15:34 $"; +static const char * const IDENT_HASH_H="$Date: 2009/04/17 13:13:09 $"; #include "pa_memory.h" #include "pa_types.h" @@ -34,6 +34,44 @@ static uint Hash_allocates[HASH_ALLOCATE 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>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; +} + /** Simple hash. @@ -76,6 +114,10 @@ public: } } + ~Hash() { + delete[] refs; + } + /// put a [value] under the [key] @returns existed or not bool put(K key, V value) { if(!value) { @@ -104,8 +146,13 @@ public: } /// put a [value] under the [key] @returns existed or not - template R maybe_put(K key, V value, F prevent) { - assert(value); + template 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(); @@ -116,16 +163,58 @@ public: 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(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; + } - // prevent-function intercepted put? - if(R result=prevent(pair->value)) + /// put a [value] under the [key] @returns existed or not + template + 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(1); } - // proper pair not found -- create&link_in new pair + // 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); @@ -133,6 +222,38 @@ public: return 0; } + /// put a [value] under the [key] @returns existed or not + template + 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(1); + } + + return 0; + } + /// remove the [key] @returns existed or not bool remove(K key) { uint code=hash_code(key); @@ -150,6 +271,18 @@ public: 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] V get(K key) const { uint code=hash_code(key); @@ -161,6 +294,16 @@ public: 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 bool put_replaced(K key, V value) { if(!value) { @@ -182,14 +325,18 @@ public: /// put a [value] under the [key] if that [key] existed @returns existed or not template R maybe_put_replaced(K key, V value, F prevent) { - assert(value); + 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; @@ -346,44 +493,6 @@ private: //disabled Hash& operator = (const Hash&) { return *this; } }; -/// 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>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; -} - /// Auto-object used to temporarily substituting/removing hash values template class Temp_hash_value {