Diff for /parser3/src/include/pa_hash.h between versions 1.71 and 1.90

version 1.71, 2009/04/17 13:13:09 version 1.90, 2015/09/24 21:35:29
Line 1 Line 1
 /** @file  /** @file
         Parser: hash class decl.          Parser: hash class decl.
   
         Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001-2012 Art. Lebedev Studio (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 * const IDENT_HASH_H="$Date$";  #define IDENT_PA_HASH_H "$Id$"
   
 #include "pa_memory.h"  #include "pa_memory.h"
 #include "pa_types.h"  #include "pa_types.h"
   #include "pa_string.h"
   
 const int HASH_ALLOCATES_COUNT=29;  const int HASH_ALLOCATES_COUNT=29;
   
Line 72  inline uint hash_code(int self) { Line 73  inline uint hash_code(int self) {
         return result;          return result;
 }  }
   
   #endif // PA_HASH_H
   
   #ifndef PA_HASH_CLASS
   #define PA_HASH_CLASS
 /**   /** 
         Simple hash.          Simple hash.
   
Line 80  inline uint hash_code(int self) { Line 85  inline uint hash_code(int self) {
                 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_ORDER_CLEAR
   #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_ORDER_CLEAR() first=0; last=&first
   
   #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_ORDER_CLEAR()
   
   #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;
           class Pair;
   
         Hash() {           HASH() { 
                 allocated=Hash_allocates[allocates_index=0];                  allocated=Hash_allocates[allocates_index=0];
                 threshold=allocated*THRESHOLD_PERCENT/100;  
                 fpairs_count=fused_refs=0;                  fpairs_count=fused_refs=0;
                 refs=new(UseGC) Pair*[allocated];                  refs=new Pair*[allocated];
                   HASH_ORDER_CLEAR();
         }          }
   
         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;  
                 fused_refs=source.fused_refs;                  fused_refs=source.fused_refs;
                 fpairs_count=source.fpairs_count;                  fpairs_count=source.fpairs_count;
                 refs=new(UseGC) Pair*[allocated];                  refs=new Pair*[allocated];
   
                 // clone & rehash                  // clone & rehash
                 Pair **old_ref=source.refs;  #ifdef HASH_ORDER
                   HASH_ORDER_CLEAR();
                   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
           }
   
   #ifdef USE_DESTRUCTORS
           ~HASH() {
                   Pair **ref=refs;
                 for(int index=0; index<allocated; index++)                  for(int index=0; index<allocated; index++)
                         for(Pair *pair=*old_ref++; pair; ) {                          for(Pair *pair=*ref++; pair;){
                                 Pair *next=pair->link;                                  Pair *next=pair->link;
                                   delete pair;
                                 Pair **new_ref=&refs[index];  
                                 *new_ref=new Pair(pair->code, pair->key, pair->value, *new_ref);  
   
                                 pair=next;                                  pair=next;
                         }                          }
         }  
   
         ~Hash() {  
                 delete[] refs;                  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 140  public: Line 189  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 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);
                 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 false;
         }          }
Line 294  public: Line 243  public:
                 return V(0);                  return V(0);
         }          }
   
         /// get associated [value] by the [key] + [code] (faster)  #ifdef HASH_ORDER
         V get_by_hash_code(uint code, K key) const {          String::Body first_key() const {
                 uint index=code%allocated;                  return (first) ? String::Body(first->key, first->code) : String::Body();
                 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          V first_value() const {
         bool put_replaced(K key, V value) {                  return (first) ? first->value : V(0);
                 if(!value) {          }
                         remove(key);  
                         return false;          String::Body last_key() const {
                   if (fpairs_count) {
                           Pair* pair = (Pair*)((char *)last - offsetof(Pair, next));
                           return String::Body(pair->key, pair->code);
                   } else {
                           return String::Body();
                 }                  }
                 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  
                                 pair->value=value;  
                                 return true;  
                         }  
   
                 // proper pair not found           V last_value() const {
                 return false;                  return (fpairs_count) ? ((Pair *)((char *)last - offsetof(Pair, next)))->value : V(0);
         }          }
   
           void order_clear() {
                   HASH_ORDER_CLEAR();
           }
   
           void order_next(Pair* pair) {
                   pair->prev=last;
                   pair->next=0;
                   *last=pair;
                   last=&(pair->next);
           }
   
   #endif
   
         /// 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
         template<typename R, typename F> R maybe_put_replaced(K key, V value, F prevent) {          bool put_replaced(K key, V value) {
                 if(!value) {                  if(!value) {
                         // they can come here from Temp_value_element::dctor to restore some empty value  
                         remove(key);                          remove(key);
                         // this has nothing to do with properties, doing no special property handling here                          return false;
                         return 0;   
                 }                  }
   
                 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)
                         if(pair->code==code && pair->key==key) {                          if(pair->code==code && pair->key==key) {
                                 // found a pair with the same key, replacing                                  // found a pair with the same key, replacing
                                 // prevent-function intercepted put?  
                                 if(R result=prevent(pair->value))  
                                         return result;  
                                   
                                 pair->value=value;                                  pair->value=value;
                                 return reinterpret_cast<R>(1);                                  return true;
                         }                          }
   
                 // proper pair not found                   // proper pair not found 
                 return 0;                  return false;
         }          }
   
         /// 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
Line 370  public: Line 318  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
                 @todo optimize this.allocated==src.allocated case          void merge_dont_replace(const HASH& src) {
         */  #ifdef HASH_ORDER
         void merge_dont_replace(const Hash& src) {                  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);
         }          }
   
Line 389  public: Line 339  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          /// iterate over all pairs
         template<typename I> void for_each_ref(void callback(K, V&, I), I info) const {          template<typename I> void for_each_ref(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 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 pair->value;  
                   
                 return V(0);                  return V(0);
         }          }
   
         /// remove all elements          /// remove all elements
         void clear() {          void clear() {
                 memset(refs, 0, sizeof(*refs)*allocated);                  memset(refs, 0, sizeof(*refs)*allocated);
                 fpairs_count=fused_refs=0;                        fpairs_count=fused_refs=0;
                   HASH_ORDER_CLEAR();
         }          }
   
 private:  protected:
   
         /// expand when these %% of allocated exausted  
         enum {  
                 THRESHOLD_PERCENT=75  
         };  
   
         /// the index of [allocated] in [Hash_allocates]          /// the index of [allocated] in [Hash_allocates]
         int allocates_index;          int allocates_index;
Line 433  private: Line 372  private:
         /// number of allocated pairs          /// number of allocated pairs
         int allocated;          int allocated;
   
         /// helper: expanding when fused_refs == threshold  
         int threshold;  
   
         /// used pairs          /// used pairs
         int fused_refs;          int fused_refs;
   
Line 449  private: Line 385  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;
   
         /// filled to threshold: needs expanding  #ifdef HASH_ORDER
         bool is_full() { return fused_refs==threshold; }          Pair *first;
           Pair **last;
   #endif
   
           /// filled to threshold (THRESHOLD_PERCENT=75), needs expanding
           bool is_full() { return fused_refs + allocated/4 >= allocated; }
   
         /// allocate larger buffer & rehash          /// allocate larger buffer & rehash
         void expand() {          void expand() {
                 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;                  if (allocates_index<HASH_ALLOCATES_COUNT-1) allocates_index++;
                 // allocated bigger refs array                  // allocated bigger refs array
                 allocated=Hash_allocates[allocates_index];                  allocated=Hash_allocates[allocates_index];
                 threshold=allocated*THRESHOLD_PERCENT/100;                  refs=new Pair*[allocated];
                 refs=new(UseGC) Pair*[allocated];  
   
                 // rehash                  // rehash
                 Pair **old_ref=old_refs;                  Pair **old_ref=old_refs;
Line 490  private: Line 433  private:
   
 private: //disabled  private: //disabled
   
         Hash& operator = (const Hash&) { return *this; }          HASH& operator = (const HASH&) { return *this; }
   };
   
   /** 
           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;
   #ifdef HASH_ORDER
                                   *(pair->prev)=pair->next;
                                   if(pair->next)
                                           pair->next->prev=pair->prev;
                                   else
                                           this->last=pair->prev;
   #endif
                                   delete pair;
                                   *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;
           }
   
           /// 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;
   
                   return V(0);
           }
   
           /// put a [value] under the [key] if that [key] existed @returns existed or not
           bool put_replaced(K str, V value) {
                   if(!value) {
                           remove(str);
                           return false;
                   }
   
                   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;
           }
   
           /// 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;
   #ifndef HASH_ORDER
                   int i;
   #endif
           public:
                   Iterator(const HASH_STRING<V>& ahash): fhash(ahash) {
   #ifdef HASH_ORDER
                           fcurrent=fhash.first;
   #else
                           fcurrent=0;
                           for(i=0; i<fhash.allocated; i++)
                                   if (fcurrent=fhash.refs[i])
                                           break;
   #endif
                   }
   
                   operator bool () {
                           return fcurrent != 0;
                   }
   
                   void next() {
   #ifdef HASH_ORDER
                           fcurrent=fcurrent->next;
   #else
                           if(fcurrent=fcurrent->link)
                                   return;
                           for(i++; i<fhash.allocated; i++)
                                   if (fcurrent=fhash.refs[i])
                                           break;
   #endif
                   }
   
                   String::Body key(){
                           return String::Body(fcurrent->key, fcurrent->code);
                   }
   
                   V value(){
                           return fcurrent->value;
                   }
   
                   Pair *pair(){
                           return fcurrent;
                   }
           };
 };  };
   #else //HASH_CODE_CACHING
   
   template<typename V> class HASH_STRING: public HASH<const String::Body,V>{};
   
 ///     Auto-object used to temporarily substituting/removing hash values  #endif //HASH_CODE_CACHING
 template <typename K, typename V>  
   #ifndef HASH_ORDER
   ///    Auto-object used to temporarily substituting/removing string hash values
   template <typename H, typename V>
 class Temp_hash_value {  class Temp_hash_value {
         Hash<K, V>& fhash;          H *fhash;
         K fname;          String::Body fname;
         V saved_value;          V saved_value;
 public:  public:
         Temp_hash_value(Hash<K, V>& ahash, K aname, V avalue) :           Temp_hash_value(H *ahash, String::Body aname, V avalue) : fhash(ahash), fname(aname) {
                 fhash(ahash),                  if(fhash){
                 fname(aname),                          saved_value=fhash->get(aname);
                 saved_value(ahash.get(aname)) {                          fhash->put(aname, avalue);
                 fhash.put(aname, avalue);                  }
         }          }
         ~Temp_hash_value() {           ~Temp_hash_value() {
                 fhash.put(fname, saved_value);                  if(fhash)
                           fhash->put(fname, saved_value);
         }          }
 };  };
   
 #endif  #endif
   
   #endif //PA_HASH_CLASS

Removed from v.1.71  
changed lines
  Added in v.1.90


E-mail: