Diff for /parser3/src/include/pa_hash.h between versions 1.58.2.10 and 1.73

version 1.58.2.10, 2003/01/31 12:34:30 version 1.73, 2009/04/18 00:33:56
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};
   
   /// 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;
   }
   
 /**   /** 
         Simple hash.          Simple hash.
   
Line 37  public: Line 87  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=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 99  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
                   Pair **old_ref=source.refs;
                   for(int index=0; index<allocated; index++)
                           for(Pair *pair=*old_ref++; pair; ) {
                                   Pair *next=pair->link;
   
                                   Pair **new_ref=&refs[index];
                                   *new_ref=new Pair(pair->code, pair->key, pair->value, *new_ref);
   
                                   pair=next;
                           }
         }          }
   
   #ifdef USE_DESTRUCTORS
         ~Hash() {          ~Hash() {
                 destroy_pairs();                  Pair **ref=refs;
                 delete 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) {
                   if(!value) {
                           remove(key);
                           return false;
                   }
                 if(is_full())                   if(is_full()) 
                         expand();                          expand();
   
Line 80  public: Line 154  public:
                 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
                   *ref=new Pair(code, key, value, *ref);
                   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
                   *ref=new Pair(code, key, value, *ref);
                   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);
Line 97  public: Line 280  public:
                 return false;                  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]
         V get(K key) const {          V get(K key) const {
                 uint code=hash_code(key);                  uint code=hash_code(key);
Line 105  public: Line 300  public:
                         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);
         }          }
   
           /// 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(K key, V value) {          bool put_replaced(K key, V value) {
                   if(!value) {
                           remove(key);
                           return false;
                   }
                 uint code=hash_code(key);                  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)
Line 123  public: Line 332  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) {
                           remove(key);
                           return false;
                   }
                 if(is_full())                   if(is_full()) 
                         expand();                          expand();
   
Line 145  public: Line 384  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 164  public: Line 404  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;
                 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;//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;      
         }          }
   
Line 188  private: Line 436  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 231  private: Line 474  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 251  private: Line 494  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 271  private: //disabled Line 502  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 */  
 template<typename K, typename V>  
 uint Hash<K, V>::allocates[]={  
         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};  
   
 template<typename K, typename V>  
 int Hash<K, V>::allocates_count=sizeof(allocates)/sizeof(uint);  
   
 /// useful generic hash function  
 inline uint generic_hash_code(uint aresult, const char* start, uint allocated) {  
         uint result=aresult, g;  
         const char* end=start+allocated;  
   
         while (start<end) {  
                 result=(result<<4)+*start++;  
                 if ((g=(result&0xF0000000))) {  
                         result=result^(g>>24);  
                         result=result^g;  
                 }  
         }  
         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;
         K fname;          K fname;
         V saved_value;          V saved_value;
 public:  public:

Removed from v.1.58.2.10  
changed lines
  Added in v.1.73


E-mail: