Diff for /parser3/src/include/pa_hash.h between versions 1.58.2.18 and 1.78

version 1.58.2.18, 2003/03/18 11:54:55 version 1.78, 2009/07/29 05:01:46
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-2009 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"
   #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;
   }
   
   #endif // PA_HASH_H
   
   #ifndef PA_HASH_CLASS
   #define PA_HASH_CLASS
 /**   /** 
         Simple hash.          Simple hash.
   
Line 30  static const char* IDENT_HASH_H="$Date$" Line 85  static const char* IDENT_HASH_H="$Date$"
                 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"
 */  */
 template<typename K, typename V> class Hash: public PA_Object {  #ifdef HASH_ORDER
   
   #undef HASH
   #undef HASH_STRING
   #undef NEW_PAIR
   
   #define HASH OrderedHash
   #define HASH_STRING OrderedHashString
   #define NEW_PAIR(code, key, value) *ref=new Pair(code, key, value, *ref, this->last); this->last=&((*ref)->next)
   
   #else
   
   #define HASH Hash
   #define HASH_STRING HashString
   #define NEW_PAIR(code, key, value) *ref=new Pair(code, key, value, *ref)
   
   #endif
   
   template<typename K, typename V> class HASH: public PA_Object {
 public:  public:
   
         typedef K key_type;          typedef K key_type;
         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];
   #ifdef HASH_ORDER
                   first=0;
                   last=&first;
   #endif
         }          }
   
         Hash(const Hash& source) {          HASH(const HASH& source) {
                 allocates_index=source.allocates_index;                  allocates_index=source.allocates_index;
                 allocated=source.allocated;                  allocated=source.allocated;
                 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];
   #ifdef HASH_ORDER
                   first=0;
                   last=&first;
   #endif
                 // clone & rehash                  // clone & rehash
                 Pair **old_ref=source.refs;                  Pair **old_ref=source.refs;
                 for(int index=0; index<allocated; index++)                  for(int index=0; index<allocated; index++)
                         for(Pair *pair=*old_ref++; pair; ) {                          for(Pair *pair=*old_ref++; pair; ) {
                                 Pair *next=pair->link;                                  Pair *next=pair->link;
   
                                 Pair **new_ref=&refs[index];                                  Pair **ref=&refs[index];
                                 *new_ref=new Pair(pair->code, pair->key, pair->value, *new_ref);                                  NEW_PAIR(pair->code, pair->key, pair->value);
   
                                 pair=next;                                  pair=next;
                         }                          }
         }          }
         ~Hash() {  
                 destroy_pairs();  #ifdef USE_DESTRUCTORS
                 delete refs;          ~HASH() {
                   Pair **ref=refs;
                   for(int index=0; index<allocated; index++)
                           for(Pair *pair=*ref++; pair;){
                                   Pair *next=pair->link;
                                   delete pair;
                                   pair=next;
                           }
                   delete[] refs;
         }          }
   #endif
   
         /// put a [value] under the [key] @returns existed or not          /// put a [value] under the [key] @returns existed or not
         bool put(K key, V value) {          bool put(K key, V value) {
Line 90  public: Line 179  public:
                 // proper pair not found -- create&link_in new pair                  // proper pair not found -- create&link_in new pair
                 if(!*ref) // root cell were fused_refs?                  if(!*ref) // root cell were fused_refs?
                         fused_refs++; // not, we'll use it and record the fact                          fused_refs++; // not, we'll use it and record the fact
                 *ref=new Pair(code, key, value, *ref);                  NEW_PAIR(code, key, value);
                 fpairs_count++;                  fpairs_count++;
                 return false;                  return false;
         }          }
   
           /// put a [value] under the [key] @returns existed or not
           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
                   NEW_PAIR(code, key, value);
                   fpairs_count++;
                   return 0;
           }
   
           /// put a [value] under the [key] @returns existed or not
           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
                   NEW_PAIR(code, key, value);
                   fpairs_count++;
                   return 0;
           }
   
           /// put a [value] under the [key] @returns existed or not
           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(K key) {          bool remove(K key) {
                 uint code=hash_code(key);                  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) {                          Pair *pair=*ref;
                           if(pair->code==code && pair->key==key) {
                                 // found a pair with the same key                                  // found a pair with the same key
                                 Pair *next=(*ref)->link;                                  Pair *next=pair->link;
                                 delete *ref;  #ifdef HASH_ORDER
                                   *(pair->prev)=pair->next;
                                   if(pair->next)
                                           pair->next->prev=pair->prev;
                                   else
                                           last=pair->prev;
   #endif
                                   delete pair;
                                 *ref=next;                                  *ref=next;
                                 --fpairs_count;                                  --fpairs_count;
                                 return true;                                  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;                  return false;
         }          }
Line 124  public: Line 343  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 142  public: Line 361  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) {
                   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);
                           }
   
                   // 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 163  public: Line 408  public:
                 // proper pair not found -- create&link_in new pair                  // proper pair not found -- create&link_in new pair
                 if(!*ref) // root cell were fused_refs?                  if(!*ref) // root cell were fused_refs?
                         fused_refs++; // not, we'll use it and record the fact                          fused_refs++; // not, we'll use it and record the fact
                 *ref=new Pair(code, key, value, *ref);                  NEW_PAIR(code, key, value);
                 fpairs_count++;                  fpairs_count++;
                 return false;                  return false;
         }          }
Line 171  public: Line 416  public:
         /** 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                  @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);
Line 182  public: Line 427  public:
   
         /// iterate over all pairs          /// iterate over all pairs
         template<typename I> void for_each(void callback(K, V, I), I info) const {          template<typename I> void for_each(void callback(K, V, I), I info) const {
   #ifdef HASH_ORDER
                   for(Pair *pair=first; pair; pair=pair->next)
                           callback(pair->key, pair->value, info);
   #else
                   Pair **ref=refs;
                   for(int index=0; index<allocated; index++)
                           for(Pair *pair=*ref++; pair; pair=pair->link)
                                   callback(pair->key, pair->value, info);
   #endif
           }
   
           /// iterate over all pairs
           template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
   #ifdef HASH_ORDER
                   for(Pair *pair=first; pair; pair=pair->next)
                           callback(pair->key, pair->value, info);
   #else
                 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)
                                 callback(pair->key, pair->value, info);                                  callback(pair->key, pair->value, info);
   #endif
         }          }
   
         /// 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 {
   #ifdef HASH_ORDER
                   for(Pair *pair=first; pair; pair=pair->next)
                           if(callback(pair->key, pair->value, info))
                                   return pair->value;
   #else
                 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;
                   #endif
                 return V(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;      
   #ifdef HASH_ORDER
                   first=0;
                   last=&first;
   #endif
         }          }
   
 private:  protected:
   
         /// expand when these %% of allocated exausted          /// expand when these %% of allocated exausted
         enum {          enum {
                 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 239  private: Line 506  private:
                 K key;                  K key;
                 V value;                  V value;
                 Pair *link;                  Pair *link;
                   #ifdef HASH_ORDER
                 Pair(uint acode, K akey, V avalue, Pair *alink) :                  Pair **prev;
                         code(acode),                  Pair *next;
                         key(akey),  
                         value(avalue),                  Pair(uint acode, K akey, V avalue, Pair *alink, Pair **aprev) : code(acode), key(akey), value(avalue), link(alink), 
                         link(alink) {}                          prev(aprev), next(0) { *aprev=this; }
   #else
                   Pair(uint acode, K akey, V avalue, Pair *alink) : code(acode), key(akey), value(avalue), link(alink) {}
   #endif
         } **refs;          } **refs;
   
   #ifdef HASH_ORDER
           Pair *first;
           Pair **last;
   #endif
   
         /// filled to threshold: needs expanding          /// filled to threshold: needs expanding
         bool is_full() { return fused_refs==threshold; }          bool is_full() { return fused_refs==threshold; }
   
Line 255  private: Line 530  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(0) Pair*[allocated];                  refs=new(UseGC) Pair*[allocated];
   
                 // rehash                  // rehash
                 Pair **old_ref=old_refs;                  Pair **old_ref=old_refs;
Line 275  private: Line 550  private:
                                 pair=next;                                  pair=next;
                         }                          }
   
                 delete old_refs;                  delete[] old_refs;
         }          }
   
         void destroy_pairs() {  private: //disabled
                 Pair **ref=refs;  
                 for(int index=0; index<allocated; index++) {          HASH& operator = (const HASH&) { return *this; }
                         Pair *pair=*ref++;   };
                         while(pair) {  
   /** 
           Simple String::body hash.
           Allows hash code caching
   */
   
   #ifdef HASH_CODE_CACHING
   
   template<typename V> class HASH_STRING: public HASH<const CORD,V> {
   public:
   
           typedef typename HASH<const CORD,V>::Pair Pair; 
           typedef const String::Body &K;
   
           typedef K key_type;
           
           /// put a [value] under the [key] @returns existed or not
           bool put(K str, V value) {
                   if(!value) {
                           remove(str);
                           return false;
                   }
                   if(this->is_full()) 
                           this->expand();
   
                   CORD key=str.get_cord();
   
                   uint code=str.get_hash_code();
                   uint index=code%this->allocated;
                   Pair **ref=&this->refs[index];
                   for(Pair *pair=*ref; pair; pair=pair->link)
                           if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                                   // 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?
                           this->fused_refs++; // not, we'll use it and record the fact
                   NEW_PAIR(code, key, value);
                   this->fpairs_count++;
                   return false;
           }
   
           /// put a [value] under the [key] @returns existed or not
           template<typename R, typename F, typename I> R replace_maybe_append(K str, V value, F prevent, I info) {
                   if(!value) {
                           // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel)
                           remove(str);
                           // this has nothing to do with properties, doing no special property handling here
                           return 0; 
                   }
   
                   if(this->is_full()) 
                           this->expand();
   
                   CORD key=str.get_cord();
   
                   uint code=str.get_hash_code();
                   uint index=code%this->allocated;
                   Pair **ref=&this->refs[index];
                   for(Pair *pair=*ref; pair; pair=pair->link)
                           if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                                   // 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?
                           this->fused_refs++; // not, we'll use it and record the fact
                   NEW_PAIR(code, key, value);
                   this->fpairs_count++;
                   return 0;
           }
   
           /// put a [value] under the [key] @returns existed or not
           template<typename R, typename F1, typename F2, typename I> 
                   R maybe_replace_maybe_append(K str, 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(str);
                           // this has nothing to do with properties, doing no special property handling here
                           return 0; 
                   }
   
                   if(this->is_full()) 
                           this->expand();
   
                   CORD key=str.get_cord();
   
                   uint code=str.get_hash_code();
                   uint index=code%this->allocated;
                   Pair **ref=&this->refs[index];
                   for(Pair *pair=*ref; pair; pair=pair->link)
                           if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                                   // 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?
                           this->fused_refs++; // not, we'll use it and record the fact
                   NEW_PAIR(code, key, value);
                   this->fpairs_count++;
                   return 0;
           }
   
           /// put a [value] under the [key] @returns existed or not
           template<typename R, typename F1, typename I> 
                   R maybe_replace_never_append(K str, V value, F1 prevent_replace, I info) {
                   if(!value) {
                           // they can come here from somewhere (true with maybe_replace_maybe_append, keeping parallel)
                           remove(str);
                           // this has nothing to do with properties, doing no special property handling here
                           return 0; 
                   }
   
                   if(this->is_full()) 
                           this->expand();
   
                   CORD key=str.get_cord();
   
                   uint code=str.get_hash_code();
                   uint index=code%this->allocated;
                   Pair **ref=&this->refs[index];
                   for(Pair *pair=*ref; pair; pair=pair->link)
                           if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                                   // 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
           bool remove(K str) {
                   CORD key=str.get_cord();
                   uint code=str.get_hash_code();
                   uint index=code%this->allocated;
                   for(Pair **ref=&this->refs[index]; *ref; ref=&(*ref)->link){
                           Pair *pair=*ref;
                           if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                                   // found a pair with the same key
                                 Pair *next=pair->link;                                  Pair *next=pair->link;
   #ifdef HASH_ORDER
                                   *(pair->prev)=pair->next;
                                   if(pair->next)
                                           pair->next->prev=pair->prev;
                                   else
                                           this->last=pair->prev;
   #endif
                                 delete pair;                                  delete pair;
                                 pair=next;                                  *ref=next;
                                   --this->fpairs_count;
                                   return true;
                         }                          }
                 }                  }
   
                   return false;
           }
   
           /// return true if key exists
           bool contains(K str){
                   CORD key=str.get_cord();
                   uint code=str.get_hash_code();
                   uint index=code%this->allocated;
                   for(Pair *pair=this->refs[index]; pair; pair=pair->link){
                           if(pair->code==code && CORD_cmp(pair->key,key)==0)
                                   return true;
                   }
   
                   return false;
         }          }
   
 private: //disabled          /// get associated [value] by the [key]
           V get(K str) const {
                   CORD key=str.get_cord();
                   uint code=str.get_hash_code();
                   uint index=code%this->allocated;
                   for(Pair *pair=this->refs[index]; pair; pair=pair->link)
                           if(pair->code==code && CORD_cmp(pair->key,key)==0)
                                   return pair->value;
   
         Hash& operator = (const Hash&) { return *this; }                  return V(0);
 };          }
   
 /* Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster */          /// put a [value] under the [key] if that [key] existed @returns existed or not
 template<typename K, typename V>          bool put_replaced(K str, V value) {
 uint Hash<K, V>::allocates[]={                  if(!value) {
         5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963,                           remove(str);
         16229, 32531, 65407, 130987, 262237, 524521, 1048793,                           return false;
         2097397, 4194103, 8388857, 16777447, 33554201, 67108961,                   }
         134217487, 268435697, 536870683, 1073741621, 2147483399};  
   
 template<typename K, typename V>                  CORD key=str.get_cord();
 int Hash<K, V>::allocates_count=sizeof(allocates)/sizeof(uint);                  uint code=str.get_hash_code();
                   uint index=code%this->allocated;
                   for(Pair *pair=this->refs[index]; pair; pair=pair->link)
                           if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                                   // found a pair with the same key, replacing
                                   pair->value=value;
                                   return true;
                           }
   
 /// useful generic hash function                  // proper pair not found 
 inline uint generic_hash_code(uint aresult, const char* start, size_t allocated) {                  return false;
         uint result=aresult, g;          }
         const char* end=start+allocated;  
           /// put a [value] under the [key] if that [key] existed @returns existed or not
         while (start<end) {          template<typename R, typename F> R maybe_put_replaced(K str, V value, F prevent) {
                 result=(result<<4)+*start++;                  if(!value) {
                 if ((g=(result&0xF0000000))) {                          // they can come here from Temp_value_element::dctor to restore some empty value
                         result=result^(g>>24);                          remove(str);
                         result=result^g;                          // this has nothing to do with properties, doing no special property handling here
                           return 0; 
                 }                  }
   
                   CORD key=str.get_cord();
                   uint code=str.get_hash_code();
                   uint index=code%this->allocated;
                   for(Pair *pair=this->refs[index]; pair; pair=pair->link)
                           if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                                   // 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;
         }          }
         return result;  
 }  
   
 /// simple hash code of int. used by EXIF mapping          /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
 inline uint hash_code(int self) {          bool put_dont_replace(K str, V value) {
         return generic_hash_code(0, (const char*)&self, sizeof(self));                  if(!value) {
 }                          remove(str);
                           return false;
                   }
                   if(this->is_full()) 
                           this->expand();
   
                   CORD key=str.get_cord();
                   uint code=str.get_hash_code();
                   uint index=code%this->allocated;
                   Pair **ref=&this->refs[index];
                   for(Pair *pair=*ref; pair; pair=pair->link)
                           if(pair->code==code && CORD_cmp(pair->key,key)==0) {
                                   // 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?
                           this->fused_refs++; // not, we'll use it and record the fact
                   NEW_PAIR(code, key, value);
                   this->fpairs_count++;
                   return false;
           }
   
           /// iterate over all pairs
           template<typename I> void for_each(void callback(K, V, I), I info) const {
   #ifdef HASH_ORDER
                   for(Pair *pair=this->first; pair; pair=pair->next)
                           callback(pair->key, pair->value, info);
   #else
                   Pair **ref=this->refs;
                   for(int index=0; index<this->allocated; index++)
                           for(Pair *pair=*ref++; pair; pair=pair->link)
                                   callback(String::Body(pair->key, pair->code), pair->value, info);
   #endif
           }
   
 ///     Auto-object used to temporarily substituting/removing hash values          /// iterate over all pairs
           template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
   #ifdef HASH_ORDER
                   for(Pair *pair=this->first; pair; pair=pair->next)
                           callback(pair->key, pair->value, info);
   #else
                   Pair **ref=this->refs;
                   for(int index=0; index<this->allocated; index++)
                           for(Pair *pair=*ref++; pair; pair=pair->link)
                                   callback(String::Body(pair->key, pair->code), pair->value, info);
   #endif
           }
   
           /// 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 {
   #ifdef HASH_ORDER
                   for(Pair *pair=this->first; pair; pair=pair->next)
                           if(callback(String::Body(pair->key, pair->code), pair->value, info))
                                   return pair->value;
   #else
                   Pair **ref=this->refs;
                   for(int index=0; index<this->allocated; index++)
                           for(Pair *pair=*ref++; pair; pair=pair->link)
                                   if(callback(String::Body(pair->key, pair->code), pair->value, info))
                                           return pair->value;
   #endif
                   return V(0);
           }
   };
   #else //HASH_CODE_CACHING
   
   template<typename V> class HASH_STRING: public HASH<const String::Body,V>{};
   
   #endif //HASH_CODE_CACHING
   
   #ifndef HASH_ORDER
   ///    Auto-object used to temporarily substituting/removing string hash values
 template <typename K, typename V>  template <typename K, typename V>
 class Temp_hash_value {  class Temp_hash_value {
         Hash<K, V>& fhash;          HashString<V> &fhash;
         K fname;          K fname;
         V saved_value;          V saved_value;
 public:  public:
         Temp_hash_value(Hash<K, V>& ahash, K aname, V avalue) :           Temp_hash_value(HashString<V>& ahash, K aname, V avalue) :
                 fhash(ahash),                  fhash(ahash),
                 fname(aname),                  fname(aname),
                 saved_value(ahash.get(aname)) {                  saved_value(ahash.get(aname)) {
Line 343  public: Line 909  public:
                 fhash.put(fname, saved_value);                  fhash.put(fname, saved_value);
         }          }
 };  };
   
 #endif  #endif
   
   #endif //PA_HASH_CLASS

Removed from v.1.58.2.18  
changed lines
  Added in v.1.78


E-mail: