Diff for /parser3/src/include/pa_inline_hash.h between versions 1.2 and 1.4

version 1.2, 2026/04/25 02:28:31 version 1.4, 2026/04/29 23:30:44
Line 1 Line 1
 /** @file  /** @file
         Parser: inline-storage hash decls.          Parser: inline-storage hash decls.
   
         Copyright (c) 2001-2026 Art. Lebedev Studio (http://www.artlebedev.com)          Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
         Authors: Konstantin Morshnev <moko@design.ru>          Authors: Konstantin Morshnev <moko@design.ru>
 */  */
   
Line 12 Line 12
   
 #include "pa_hash.h"  #include "pa_hash.h"
   
 #define PA_HASH_INLINE_SIZE 4  #define PA_INLINE_HASH_N 13
   
 /// Inline-storage "hash" stores up to PA_HASH_INLINE_SIZE entries without heap allocation.  // Linear-probing inline hash with PA_INLINE_HASH_N slots before overflow to HashString<V>.
 /// Fallbacks to HashString for overflow.  // Inits 8*N bytes, while HASH allocates (and inits) at least 
   
 template<typename V> class InlineHashString: public PA_Object {  template<typename V> class InlineHashString: public PA_Object {
 public:  public:
   
         struct Pair {          InlineHashString() : fkeys{}, foverflow(0) {
                 String::Body key;          }
                 V value;  
         };  
   
         InlineHashString() : fcount(0), foverflow(0) {}  
   
           ~InlineHashString() {
 #ifdef USE_DESTRUCTORS  #ifdef USE_DESTRUCTORS
         ~InlineHashString() { if(foverflow) delete foverflow; }                  if(foverflow) delete foverflow;
 #endif  #endif
           }
   
         V get(const String& name) const {          V get(const String& name) const {
                 const String::Body& nb = name;                  const String::Body& nb = name;
                 const uint code = nb.get_hash_code();                  const uint hash = nb.get_hash_code();
                 for(int i = 0; i < fcount; i++) {                  int i = hash % PA_INLINE_HASH_N;
                         const String::Body& key = fpairs[i].key;                  for(int probe = 0; probe < PA_INLINE_HASH_N; probe++) {
                         if(key.get_hash_code() == code && key == nb)                          if(!fkeys[i]) {
                                 return fpairs[i].value;                                  return foverflow ? foverflow->get(name) : 0;
                           }
                           if(fkeys[i]->get_hash_code() == hash && *fkeys[i] == nb)
                                   return fvalues[i];
                           if(++i >= PA_INLINE_HASH_N) i = 0;
                 }                  }
                 return foverflow ? foverflow->get(name) : 0;                  return foverflow ? foverflow->get(name) : 0;
         }          }
   
         bool put(const String& name, V value) {          bool put(const String& name, V value) {
                 const String::Body& nb = name;                  const String::Body& nb = name;
                 const uint code = nb.get_hash_code();                  const uint hash = nb.get_hash_code();
                 for(int i = 0; i < fcount; i++) {                  int i = hash % PA_INLINE_HASH_N;
                         const String::Body& key = fpairs[i].key;                  for(int probe = 0; probe < PA_INLINE_HASH_N; probe++) {
                         if(key.get_hash_code() == code && key == nb) {                          if(!fkeys[i]) {
                                 fpairs[i].value = value;                                  fkeys[i] = &nb;
                                   fvalues[i] = value;
                                   return false;
                           }
                           if(fkeys[i]->get_hash_code() == hash && *fkeys[i] == nb) {
                                   fvalues[i] = value;
                                 return true;                                  return true;
                         }                          }
                 }                          if(++i >= PA_INLINE_HASH_N) i = 0;
                 if(fcount < PA_HASH_INLINE_SIZE) {  
                         fpairs[fcount].key = name;  
                         fpairs[fcount].value = value;  
                         fcount++;  
                         return false;  
                 }                  }
                 if(!foverflow)                  if(!foverflow)
                         foverflow = new HashString<V>();                          foverflow = new HashString<V>();
Line 65  public: Line 67  public:
   
         bool put_replaced(const String& name, V value) {          bool put_replaced(const String& name, V value) {
                 const String::Body& nb = name;                  const String::Body& nb = name;
                 const uint code = nb.get_hash_code();                  const uint hash = nb.get_hash_code();
                 for(int i = 0; i < fcount; i++) {                  int i = hash % PA_INLINE_HASH_N;
                         const String::Body& key = fpairs[i].key;                  for(int probe = 0; probe < PA_INLINE_HASH_N; probe++) {
                         if(key.get_hash_code() == code && key == nb) {                          if(!fkeys[i])
                                 fpairs[i].value = value;                                  return foverflow ? foverflow->put_replaced(name, value) : false;
                           if(fkeys[i]->get_hash_code() == hash && *fkeys[i] == nb) {
                                   fvalues[i] = value;
                                 return true;                                  return true;
                         }                          }
                           if(++i >= PA_INLINE_HASH_N) i = 0;
                 }                  }
                 return foverflow ? foverflow->put_replaced(name, value) : false;                  return foverflow ? foverflow->put_replaced(name, value) : false;
         }          }
   
         int count() const {  
                 return fcount + (foverflow ? foverflow->count() : 0);  
         }  
   
         template<typename I> void for_each(void callback(const String::Body&, V, I), I info) const {          template<typename I> void for_each(void callback(const String::Body&, V, I), I info) const {
                 for(int i = 0; i < fcount; i++)                  for(int i = 0; i < PA_INLINE_HASH_N; i++) {
                         callback(fpairs[i].key, fpairs[i].value, info);                          if(fkeys[i])
                                   callback(*fkeys[i], fvalues[i], info);
                   }
                 if(foverflow)                  if(foverflow)
                         foverflow->for_each(callback, info);                          foverflow->for_each(callback, info);
         }          }
   
 private:  private:
         int fcount;  
         Pair fpairs[PA_HASH_INLINE_SIZE];  
         HashString<V>* foverflow;          HashString<V>* foverflow;
           const String::Body* fkeys[PA_INLINE_HASH_N];  // NULL=empty, initialized in constructor
           V fvalues[PA_INLINE_HASH_N];
   
 };  };
   
 #endif // PA_INLINE_HASH_H  #endif // PA_INLINE_HASH_H

Removed from v.1.2  
changed lines
  Added in v.1.4


E-mail: