Diff for /parser3/src/main/Attic/pa_hash.C between versions 1.37 and 1.49

version 1.37, 2001/06/28 07:44:17 version 1.49, 2002/02/08 07:27:48
Line 1 Line 1
 /** @file  /** @file
         Parser: hash class.          Parser: hash class.
   
         Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
           Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
   
         Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)          $Id$
 */  */
 static const char *RCSId="$Id$";   
   
 /*  /*
         The prime numbers used from zend_hash.c,          The prime numbers used from zend_hash.c,
Line 18  static const char *RCSId="$Id$"; Line 18  static const char *RCSId="$Id$";
 #include "pa_hash.h"  #include "pa_hash.h"
   
 void *Hash::Pair::operator new(size_t size, Pool& apool) {  void *Hash::Pair::operator new(size_t size, Pool& apool) {
         return apool.malloc(size);          return apool.malloc(size, 6);
 }  }
   
 /* Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster */  /* Zend comment: Generated on an Octa-ALPHA 300MHz CPU & 2.5GB RAM monster */
Line 31  int Hash::allocates_count= Line 31  int Hash::allocates_count=
         sizeof(allocates)/sizeof(uint);          sizeof(allocates)/sizeof(uint);
   
   
 void Hash::construct(Pool& apool) {  void Hash::construct_new() {
         allocated=allocates[allocates_index=0];          allocated=allocates[allocates_index=0];
         threshold=allocated*THRESHOLD_PERCENT/100;          threshold=allocated*THRESHOLD_PERCENT/100;
         used=0;          count=used=0;
         refs=static_cast<Pair **>(calloc(sizeof(Pair *)*allocated));          refs=static_cast<Pair **>(calloc(sizeof(Pair *)*allocated));
 }  }
   
   void Hash::construct_copy(const Hash& source) {
           allocates_index=source.allocates_index;
           allocated=source.allocated;
           threshold=source.threshold;
           used=source.used;
           count=source.count;
           size_t size=sizeof(Pair *)*allocated;
           refs=static_cast<Pair **>(malloc(size, 7));  memcpy(refs, source.refs, size);
   }
   
 void Hash::expand() {  void Hash::expand() {
         int old_size=allocated;          int old_size=allocated;
         Pair **old_refs=refs;          Pair **old_refs=refs;
Line 78  uint Hash::generic_code(uint aresult, co Line 88  uint Hash::generic_code(uint aresult, co
 }  }
   
 bool Hash::put(const Key& key, Val *value) {  bool Hash::put(const Key& key, Val *value) {
           if(!value)
                   return remove(key);
   
         if(full())           if(full()) 
                 expand();                  expand();
   
Line 95  bool Hash::put(const Key& key, Val *valu Line 108  bool Hash::put(const Key& key, Val *valu
         if(!*ref) // root cell were used?          if(!*ref) // root cell were used?
                 used++; // not, we'll use it and record the fact                  used++; // not, we'll use it and record the fact
         *ref=NEW Pair(code, key, value, *ref);          *ref=NEW Pair(code, key, value, *ref);
           count++;
           return false;
   }
   
   bool Hash::remove(const Key& key) {
           uint code=key.hash_code();
           uint index=code%allocated;
           for(Pair **ref=&refs[index]; *ref; ref=&(*ref)->link)
                   if((*ref)->code==code && (*ref)->key==key) {
                           // found a pair with the same key
                           *ref=(*ref)->link;
                           --count;
                           return true;
                   }
   
         return false;          return false;
 }  }
   
Line 123  bool Hash::put_replace(const Key& key, V Line 151  bool Hash::put_replace(const Key& key, V
 }  }
   
 bool Hash::put_dont_replace(const Key& key, Val *value) {  bool Hash::put_dont_replace(const Key& key, Val *value) {
           if(!value)
                   return remove(key);
   
         if(full())           if(full()) 
                 expand();                  expand();
   
Line 136  bool Hash::put_dont_replace(const Key& k Line 167  bool Hash::put_dont_replace(const Key& k
                 }                  }
   
         // proper pair not found -- create&link_in new pair          // proper pair not found -- create&link_in new pair
         *ref=NEW Pair(code, key, value, *ref);  
         if(!*ref) // root cell were used?          if(!*ref) // root cell were used?
                 used++; // not, we'll use it and record the fact                  used++; // not, we'll use it and record the fact
           *ref=NEW Pair(code, key, value, *ref);
           count++;
         return false;          return false;
 }  }
   
Line 153  void Hash::for_each(For_each_func func, Line 185  void Hash::for_each(For_each_func func,
         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(pair->value)                          (*func)(pair->key, pair->value, info);
                                 (*func)(pair->key, pair->value, info);  }
   
   void Hash::for_each(For_each_func_refed func, void *info) const {
           Pair **ref=refs;
           for(int index=0; index<allocated; index++)
                   for(Pair *pair=*ref++; pair; pair=pair->link)
                           (*func)(pair->key, pair->value, info);
 }  }
   
 Hash::Val* Hash::first_that(First_that_func func, void *info) const {  void* Hash::first_that(First_that_func func, void *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(pair->value)                          if(void *result=(*func)(pair->key, pair->value, info))
                                 if((*func)(pair->key, pair->value, info))                                  return result;
                                         return pair->value;  
         return 0;          return 0;
 }  }
   
 void Hash::clear() {  void Hash::clear() {
         memset(refs, 0, sizeof(*refs)*allocated);          memset(refs, 0, sizeof(*refs)*allocated);
         used=0;          count=used=0;   
 }  }

Removed from v.1.37  
changed lines
  Added in v.1.49


E-mail: