Diff for /parser3/src/include/pa_array.h between versions 1.90 and 1.96

version 1.90, 2024/09/10 19:09:56 version 1.96, 2024/10/04 05:12:05
Line 40  protected: Line 40  protected:
         size_t fallocated;          size_t fallocated;
   
         // array size          // array size
         size_t fused;          size_t fsize;
   
 public:  public:
         typedef Array_iterator<T> Iterator;          typedef Array_iterator<T> Iterator;
Line 53  public: Line 53  public:
                 bool defined;                  bool defined;
                                   
                 Action_options(                  Action_options(
                         size_t aoffset=0,                           size_t aoffset=0,
                         size_t alimit=ARRAY_OPTION_LIMIT_ALL,                           size_t alimit=ARRAY_OPTION_LIMIT_ALL,
                         bool areverse=false):                           bool areverse=false):
                         offset(aoffset), limit(alimit), reverse(areverse),                           offset(aoffset), limit(alimit), reverse(areverse), 
                         defined(false) {}                          defined(false) {}
   
Line 71  public: Line 71  public:
                         if(!m)                          if(!m)
                                 return false;                                  return false;
                         // fix limit                          // fix limit
                         if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)                          if(limit>m)
                                 limit=m;                                  limit=m;
   
                         return true;                          return true;
                 }                  }
   
                   
         };          };
   
         typedef T element_type;          typedef T element_type;
   
         inline Array(size_t initial=0):          inline Array(size_t initial=0):
                 fallocated(initial),                  fallocated(initial),
                 fused(0)                  fsize(0)
         {          {
                 felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0;                  felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0;
         }          }
   
 #ifdef USE_DESTRUCTORS   #ifdef USE_DESTRUCTORS
         inline ~Array(){          inline ~Array(){
                 if(felements)                  if(felements)
                         pa_free(felements);                          pa_free(felements);
Line 97  public: Line 95  public:
 #endif  #endif
   
         /// how many items are in Array          /// how many items are in Array
         inline size_t count() const { return fused; }          inline size_t count() const { return fsize; }
   
         /// append to array          /// append to array
         inline Array& operator+=(T src) {          inline Array& operator+=(T src) {
                 if(is_full())                  if(is_full())
                         expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests                          expand();
   
                 felements[fused++]=src;                  felements[fsize++]=src;
   
                 return *this;                  return *this;
         }          }
Line 111  public: Line 110  public:
         /// append other Array portion to this one. starting from offset          /// append other Array portion to this one. starting from offset
         Array& append(const Array& src,           Array& append(const Array& src, 
                 size_t offset=0,                   size_t offset=0, 
                 size_t limit=ARRAY_OPTION_LIMIT_ALL) { //< negative limit means 'all'. zero limit means 'nothing'                  size_t limit=ARRAY_OPTION_LIMIT_ALL) { //< zero limit means 'nothing'
   
                 size_t src_count=src.count();                  size_t src_count=src.count();
                 // skip tivials                  // skip tivials
Line 120  public: Line 119  public:
                 // max(limit)                  // max(limit)
                 size_t m=src_count-offset;                  size_t m=src_count-offset;
                 // fix limit                  // fix limit
                 if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)                  if(limit>m)
                         limit=m;                          limit=m;
   
                 ssize_t delta=limit-(fallocated-fused);                  fit(fsize-1+limit);
                 if(delta>0)  
                         expand(delta);  
   
                 T* from=&src.felements[offset];                  T* from=&src.felements[offset];
                 T* to=&felements[fused];                  T* to=&felements[fsize];
                 for(T* from_end=from+limit; from<from_end; from++)                  for(T* from_end=from+limit; from<from_end;)
                         *to++=*from;                          *to++=*from++;
                 fused+=limit;                  fsize+=limit;
                 return *this;                  return *this;
         }          }
   
Line 158  public: Line 155  public:
                 assert(index<=count());                  assert(index<=count());
   
                 if(is_full())                  if(is_full())
                         expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests                          expand();
   
                 memmove(felements+index+1, felements+index, (fused-index) * sizeof(T));                  memmove(felements+index+1, felements+index, (fsize-index) * sizeof(T));
   
                 felements[index]=element;                  felements[index]=element;
                 fused++;                  fsize++;
         }          }
   
         /// remove index-element          /// remove index-element
         inline void remove(size_t index) {          inline void remove(size_t index) {
                 assert(index<count());                  assert(index<count());
                 if (index<--fused)                  if (index<--fsize)
                         memmove(felements+index, felements+index+1, (fused-index) * sizeof(T));                          memmove(felements+index, felements+index+1, (fsize-index) * sizeof(T));
         }          }
   
         inline T operator [](size_t index) const { return get(index); }          inline T operator [](size_t index) const { return get(index); }
   
         inline void clear() {          inline void clear() {
                 if(fused)                  if(fsize)
                         memset(felements, 0, fused * sizeof(T));                          memset(felements, 0, fsize * sizeof(T));
                 fused=0;                  fsize=0;
         }          }
   
         /// iterate over all elements          /// iterate over all elements
         template<typename I> void for_each(void (*callback)(T, I), I info) const {          template<typename I> void for_each(void (*callback)(T, I), I info) const {
                 T *last=felements+fused;                  T *last=felements+fsize;
                 for(T *current=felements; current<last; current++)                  for(T *current=felements; current<last; current++)
                         callback(*current, info);                          callback(*current, info);
         }          }
   
         /// iterate over all elements          /// iterate over all elements
         template<typename I> void for_each(bool (*callback)(T, I), I info) const {          template<typename I> void for_each(bool (*callback)(T, I), I info) const {
                 T *last=felements+fused;                  T *last=felements+fsize;
                 for(T *current=felements; current<last; current++)                  for(T *current=felements; current<last; current++)
                         if(callback(*current, info))                          if(callback(*current, info))
                                 return;                                  return;
Line 198  public: Line 195  public:
   
         /// iterate over all elements          /// iterate over all elements
         template<typename I> void for_each_ref(void (*callback)(T&, I), I info) {          template<typename I> void for_each_ref(void (*callback)(T&, I), I info) {
                 T *last=felements+fused;                  T *last=felements+fsize;
                 for(T *current=felements; current<last; current++)                  for(T *current=felements; current<last; current++)
                         callback(*current, info);                          callback(*current, info);
         }          }
   
         /// iterate over all elements until condition becomes true, return that element          /// iterate over all elements until condition becomes true, return that element
         template<typename I> T first_that(bool (*callback)(T, I), I info) const {          template<typename I> T first_that(bool (*callback)(T, I), I info) const {
                 T *last=felements+fused;                  T *last=felements+fsize;
                 for(T *current=felements; current<last; current++)                  for(T *current=felements; current<last; current++)
                         if(callback(*current, info))                          if(callback(*current, info))
                                 return *current;                                  return *current;
Line 217  public: Line 214  public:
                 return felements + index;                  return felements + index;
         }          }
   
         void fit(size_t index, T element){  protected:
                 if(index >= fallocated){  
                         size_t new_allocated=fallocated>0 ? fallocated : 3;          inline bool is_full() {
                         while(new_allocated <= index){                  return fsize == fallocated;
                                 new_allocated+=2 + new_allocated/32;  
                         }  
                         expand(new_allocated - fallocated);  
                 }  
                 felements[index]=element;  
                 if(index >= fused){  
                         fused=index+1;  
                 }  
         }          }
   
 protected:          inline void expand() {
                   resize(fallocated>0 ? fallocated+fallocated/2+2 : 3); // 3 is PAF default, confirmed by tests
           }
   
         bool is_full() {          inline void fit(size_t index){
                 return fused == fallocated;                  if(index >= fallocated)
                           resize(max(this->fallocated + this->fallocated/4, index+1));
         }          }
   
         void expand(size_t delta) {          void resize(size_t asize) {
                 if(fallocated){                  if(fallocated){
                         size_t new_allocated=fallocated+delta;                          felements=(T *)pa_realloc(felements, asize*sizeof(T));
                         felements=(T *)pa_realloc(felements, new_allocated*sizeof(T));                          fallocated=asize;
                         fallocated=new_allocated;  
                 } else {                  } else {
                         fallocated=delta;                          fallocated=asize;
                         felements=(T *)pa_malloc(fallocated*sizeof(T));                          felements=(T *)pa_malloc(asize*sizeof(T));
                 }                  }
         }          }
   
Line 255  private: //disabled Line 246  private: //disabled
 };  };
   
   
   /// Commonly used, templated to work with any integer type
   
   template<typename T> char* pa_itoa(T n, T base=10){
           char buf[MAX_NUMBER + 1];
           char* pos=buf + MAX_NUMBER;
           *pos='\0';
   
           bool negative=n < 0;
           if (n < 0){
                   n=-n;
           }
   
           do {
                   *(--pos)=(n % base) + '0';
                   n/=base;
           } while (n > 0);
   
           if (negative) {
                   *(--pos) = '-';
           }
           return pa_strdup(pos, buf + MAX_NUMBER - pos);
   }
   
   template<typename T> char* pa_uitoa(T n, T base=10){
           char buf[MAX_NUMBER + 1];
           char* pos=buf + MAX_NUMBER;
           *pos='\0';
   
           do {
                   *(--pos)=(n % base) + '0';
                   n/=base;
           } while (n > 0);
   
           return pa_strdup(pos, buf + MAX_NUMBER - pos);
   }
   
   
 /** Array iterator, usage:  /** Array iterator, usage:
         @code          @code
         // Array<T> a;          // Array<T> a;
Line 274  public: Line 302  public:
   
         Array_iterator(const Array<T>& aarray): farray(aarray) {          Array_iterator(const Array<T>& aarray): farray(aarray) {
                 fcurrent=farray.felements;                  fcurrent=farray.felements;
                 flast=farray.felements + farray.count();                  flast=farray.felements + farray.fsize;
         }          }
   
         /// there are still elements          /// there are still elements
Line 298  public: Line 326  public:
         }          }
   
         inline char *key(){          inline char *key(){
                 char local_buf[MAX_NUMBER];                  return pa_uitoa(index());
                 size_t length=snprintf(local_buf, MAX_NUMBER, "%zu", index());  
                 return pa_strdup(local_buf, length);  
         }          }
   
 };  };
Line 313  template<typename T> class Array_reverse Line 339  template<typename T> class Array_reverse
 public:  public:
   
         Array_reverse_iterator(const Array<T>& aarray): farray(aarray) {          Array_reverse_iterator(const Array<T>& aarray): farray(aarray) {
                 fcurrent=farray.felements+farray.count();                  fcurrent=farray.felements + farray.fsize;
         }          }
   
         /// there are still elements          /// there are still elements
Line 332  public: Line 358  public:
         }          }
   
         inline char *key(){          inline char *key(){
                 char local_buf[MAX_NUMBER];                  return pa_uitoa(index());
                 size_t length=snprintf(local_buf, MAX_NUMBER, "%zu", index());  
                 return pa_strdup(local_buf, length);  
         }          }
   
 };  };

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


E-mail: