Diff for /parser3/src/include/pa_array.h between versions 1.91 and 1.93

version 1.91, 2024/09/11 21:07:36 version 1.93, 2024/09/15 00:38:27
Line 76  public: Line 76  public:
   
                         return true;                          return true;
                 }                  }
   
                   
         };          };
   
         typedef T element_type;          typedef T element_type;
Line 89  public: Line 87  public:
                 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 98  public: Line 96  public:
   
         /// 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 fused; }
   
         /// 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[fused++]=src;
   
Line 123  public: Line 122  public:
                 if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)                  if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m)
                         limit=m;                          limit=m;
   
                 ssize_t delta=limit-(fallocated-fused);                  size_t required=fused+limit;
                 if(delta>0)                  if(required>fallocated)
                         expand(delta);                          resize(required);
   
                 T* from=&src.felements[offset];                  T* from=&src.felements[offset];
                 T* to=&felements[fused];                  T* to=&felements[fused];
                 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;                  fused+=limit;
                 return *this;                  return *this;
         }          }
Line 158  public: Line 157  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, (fused-index) * sizeof(T));
   
Line 217  public: Line 216  public:
                 return felements + index;                  return felements + index;
         }          }
   
         void fit(size_t index, T element){  
                 if(index >= fallocated){  
                         size_t new_allocated=fallocated>0 ? fallocated : 3;  
                         while(new_allocated <= index){  
                                 new_allocated+=2 + new_allocated/32;  
                         }  
                         expand(new_allocated - fallocated);  
                 }  
                 felements[index]=element;  
                 if(index >= fused){  
                         fused=index+1;  
                 }  
         }  
   
 protected:  protected:
   
         bool is_full() {          bool is_full() {
                 return fused == fallocated;                  return fused == fallocated;
         }          }
   
         void expand(size_t delta) {          inline void expand() {
                   resize(fallocated>0 ? fallocated+fallocated/4+2 : 3); // 3 is PAF default, confirmed by tests
           }
   
           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 254  private: //disabled Line 242  private: //disabled
         Array& operator = (const Array&) { return *this; }          Array& operator = (const Array&) { return *this; }
 };  };
   
   
 /// Commonly used, templated to work with any integer type  /// Commonly used, templated to work with any integer type
   
 template<typename T> char* pa_itoa(T n, T base=10){  template<typename T> char* pa_itoa(T n, T base=10){
Line 310  public: Line 299  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.fused;
         }          }
   
         /// there are still elements          /// there are still elements
Line 347  template<typename T> class Array_reverse Line 336  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.fused;
         }          }
   
         /// there are still elements          /// there are still elements

Removed from v.1.91  
changed lines
  Added in v.1.93


E-mail: