Diff for /parser3/src/include/pa_hash.h between versions 1.58.2.11 and 1.81

version 1.58.2.11, 2003/02/03 13:01:06 version 1.81, 2010/07/23 22:23:24
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 HASH_NEW_PAIR
   #undef HASH_FOR_EACH
   
   #define HASH OrderedHash
   #define HASH_STRING OrderedHashString
   #define HASH_NEW_PAIR(code, key, value) *ref=new Pair(code, key, value, *ref, this->last); this->last=&((*ref)->next)
   
   #define HASH_FOR_EACH \
           for(Pair *pair=this->first; pair; pair=pair->next)
   
   #else
   
   #define HASH Hash
   #define HASH_STRING HashString
   #define HASH_NEW_PAIR(code, key, value) *ref=new Pair(code, key, value, *ref)
   
   #define HASH_FOR_EACH \
           Pair **ref=this->refs; \
           for(int index=0; index<this->allocated; index++) \
                   for(Pair *pair=*ref++; pair; pair=pair->link)
   
   #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=static_cast<Pair **>(pa_calloc(sizeof(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 Pair*[allocated];                  refs=new(UseGC) Pair*[allocated];
                 memcpy(refs, source.refs, sizeof(Pair *)*allocated);                  // clone & rehash
   #ifdef HASH_ORDER
                   first=0;
                   last=&first;
                   for(Pair *pair=source.first; pair; pair=pair->next)
                   {
                           uint index=pair->code%allocated;
                           Pair **ref=&refs[index];
                           HASH_NEW_PAIR(pair->code, pair->key, pair->value);
                   }
   #else
                   for(int i=0; i<source.allocated; i++)
                           for(Pair *pair=source.refs[i]; pair; pair=pair->link)
                           {
                                   Pair **ref=&refs[i];
                                   HASH_NEW_PAIR(pair->code, pair->key, pair->value);
                           }
   #endif
         }          }
         ~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) {
                   if(!value) {
                           remove(key);
                           return false;
                   }
                 if(is_full())                   if(is_full()) 
                         expand();                          expand();
   
Line 75  public: Line 191  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);                  HASH_NEW_PAIR(code, key, value);
                 fpairs_count++;                  fpairs_count++;
                 return false;                  return false;
         }          }
Line 84  public: Line 200  public:
         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 105  public: Line 242  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);
         }          }
   
         /// 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 125  public: Line 266  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(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 140  public: Line 285  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);                  HASH_NEW_PAIR(code, key, value);
                 fpairs_count++;                  fpairs_count++;
                 return false;                  return false;
         }          }
   
         /// put all 'src' values if NO with same key existed          /// put all 'src' values if NO with same key existed
         void merge_dont_replace(const Hash& src) {          void merge_dont_replace(const HASH& src) {
   #ifdef HASH_ORDER
                   for(Pair *pair=src.first; pair; pair=pair->next)
   #else
                 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)
   #endif
                                 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 158  public: Line 306  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 {
                 Pair **ref=refs;                  HASH_FOR_EACH
                 for(int index=0; index<allocated; index++)                          callback(pair->key, pair->value, info);
                         for(Pair *pair=*ref++; pair; pair=pair->link)          }
                                 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 {
                   HASH_FOR_EACH
                           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;                  HASH_FOR_EACH
                 for(int index=0; index<allocated; index++)                          if(callback(pair->key, pair->value, info))
                         for(Pair *pair=*ref++; pair; pair=pair->link)                                  return pair->value;
                                 if(callback(pair->key, pair->value, info))                  return V(0);
                                         return &pair->value;  
                   
                 return 0;//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 215  private: Line 363  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 231  private: Line 387  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 407  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
                   HASH_NEW_PAIR(code, key, value);
                   this->fpairs_count++;
                   return false;
           }
   
           /// 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;
         }          }
   
 private: //disabled          /// 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;
                   }
   
         Hash& operator = (const Hash&) { return *this; }                  return false;
 };          }
   
 /* Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster */          /// get associated [value] by the [key]
 template<typename K, typename V>          V get(K str) const {
 uint Hash<K, V>::allocates[]={                  CORD key=str.get_cord();
         5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963,                   uint code=str.get_hash_code();
         16229, 32531, 65407, 130987, 262237, 524521, 1048793,                   uint index=code%this->allocated;
         2097397, 4194103, 8388857, 16777447, 33554201, 67108961,                   for(Pair *pair=this->refs[index]; pair; pair=pair->link)
         134217487, 268435697, 536870683, 1073741621, 2147483399};                          if(pair->code==code && CORD_cmp(pair->key,key)==0)
                                   return pair->value;
   
 template<typename K, typename V>                  return V(0);
 int Hash<K, V>::allocates_count=sizeof(allocates)/sizeof(uint);          }
   
 /// useful generic hash function          /// put a [value] under the [key] if that [key] existed @returns existed or not
 inline uint generic_hash_code(uint aresult, const char* start, uint allocated) {          bool put_replaced(K str, V value) {
         uint result=aresult, g;                  if(!value) {
         const char* end=start+allocated;                          remove(str);
                           return false;
         while (start<end) {  
                 result=(result<<4)+*start++;  
                 if ((g=(result&0xF0000000))) {  
                         result=result^(g>>24);  
                         result=result^g;  
                 }                  }
   
                   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
                                   pair->value=value;
                                   return true;
                           }
   
                   // proper pair not found 
                   return false;
         }          }
         return result;  
 }  
   
 ///     Auto-object used to temporarily substituting/removing hash values          /// put a [value] under the [key] if that [key] NOT existed @returns existed or not
           bool put_dont_replace(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, 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
                   HASH_NEW_PAIR(code, key, value);
                   this->fpairs_count++;
                   return false;
           }
   
           /// put all 'src' values if NO with same key existed
           void merge_dont_replace(const HASH_STRING& src) {
   #ifdef HASH_ORDER
                   for(Pair *pair=src.first; pair; pair=pair->next)
   #else
                   for(int i=0; i<src.allocated; i++)
                           for(Pair *pair=src.refs[i]; pair; pair=pair->link)
   #endif
                                   put_dont_replace(String::Body(pair->key, pair->code), pair->value);
           }
   
           /// iterate over all pairs
           template<typename I> void for_each(void callback(K, V, I), I info) const {
                   HASH_FOR_EACH
                           callback(String::Body(pair->key, pair->code), pair->value, info);
           }
   
           /// iterate over all pairs
           template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {
                   HASH_FOR_EACH
                           callback(String::Body(pair->key, pair->code), pair->value, info);
           }
   
           /// 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 {
                   HASH_FOR_EACH
                           if(callback(String::Body(pair->key, pair->code), pair->value, info))
                                   return pair->value;
                   return V(0);
           }
   
           /// simple hash iterator
           class Iterator {
                   const HASH_STRING<V>& fhash;
                   Pair *fcurrent;
           public:
                   Iterator(const HASH_STRING<V>& ahash): fhash(ahash) {
                           fcurrent=fhash.first;
                   }
   
                   operator bool () {
                           return fcurrent != 0;
                   }
   
                   void next() {
                           fcurrent=fcurrent->next;
                   }
   
                   String::Body key(){
                           return String::Body(fcurrent->key, fcurrent->code);
                   }
   
                   V value(){
                           return fcurrent->value;
                   }
           };
   };
   #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 314  public: Line 640  public:
                 fhash.put(fname, saved_value);                  fhash.put(fname, saved_value);
         }          }
 };  };
   
 #endif  #endif
   
   #endif //PA_HASH_CLASS

Removed from v.1.58.2.11  
changed lines
  Added in v.1.81


E-mail: