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

version 1.2, 2026/04/25 02:28:31 version 1.7, 2026/04/30 16:06:48
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  // Low overflow rate, high first-hit ratio
   #define PA_PROBE_LIMIT 8     // Prevents long probe chains from degrading the inline table
   
 /// 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.  
   
 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;  
                         if(key.get_hash_code() == code && key == nb)                  // Optimized first, >50% hits
                                 return fpairs[i].value;                  if(!fkeys[i]) return 0;  // NULL implies foverflow==NULL
                   if(fkeys[i]->get_hash_code() == hash && *fkeys[i] == nb) return fvalues[i];
                   // Сollision
                   for(int n = 0; n < PA_PROBE_LIMIT-1; n++) {
                           if(++i >= PA_INLINE_HASH_N) i = 0;
                           if(!fkeys[i]) return 0;
                           if(fkeys[i]->get_hash_code() == hash && *fkeys[i] == nb) return fvalues[i];
                 }                  }
                 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;  
                         if(key.get_hash_code() == code && key == nb) {                  // Optimized first
                                 fpairs[i].value = value;                  if(!fkeys[i]) { fkeys[i] = &nb; fvalues[i] = value; return false; }
                                 return true;                  if(fkeys[i]->get_hash_code() == hash && *fkeys[i] == nb) { fvalues[i] = value; return true; }
                         }                  // Сollision
                 }                  for(int n = 0; n < PA_PROBE_LIMIT-1; n++) {
                 if(fcount < PA_HASH_INLINE_SIZE) {                          if(++i >= PA_INLINE_HASH_N) i = 0;
                         fpairs[fcount].key = name;                          if(!fkeys[i]) { fkeys[i] = &nb; fvalues[i] = value; return false; }
                         fpairs[fcount].value = value;                          if(fkeys[i]->get_hash_code() == hash && *fkeys[i] == nb) { fvalues[i] = value; return true; }
                         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;  
                         if(key.get_hash_code() == code && key == nb) {                  // Optimized first
                                 fpairs[i].value = value;                  if(!fkeys[i]) return false;
                                 return true;                  if(fkeys[i]->get_hash_code() == hash && *fkeys[i] == nb) { fvalues[i] = value; return true; }
                         }                  // Сollision
                   for(int n = 0; n < PA_PROBE_LIMIT-1; n++) {
                           if(++i >= PA_INLINE_HASH_N) i = 0;
                           if(!fkeys[i]) return false;
                           if(fkeys[i]->get_hash_code() == hash && *fkeys[i] == nb) { fvalues[i] = value; return true; }
                 }                  }
                 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.7


E-mail: