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

version 1.93, 2024/09/15 00:38:27 version 1.98, 2024/10/26 15:46:52
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;
Line 82  public: Line 82  public:
   
         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;
         }          }
Line 95  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();                          expand();
   
                 felements[fused++]=src;                  felements[fsize++]=src;
   
                 return *this;                  return *this;
         }          }
   
         /// 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,           void append(const Array& src, size_t offset=0, size_t limit=ARRAY_OPTION_LIMIT_ALL) { //< zero limit means 'nothing'
                 size_t offset=0,   
                 size_t limit=ARRAY_OPTION_LIMIT_ALL) { //< negative limit means 'all'. zero limit means 'nothing'  
   
                 size_t src_count=src.count();                  size_t src_count=src.count();
                 // skip tivials                  // skip tivials
                 if(!src_count || !limit || offset>=src_count)                  if(!src_count || !limit || offset>=src_count)
                         return *this;                          return;
                 // 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;
   
                 size_t required=fused+limit;                  fit(fsize-1+limit);
                 if(required>fallocated)                  memcpy(felements+fsize, src.felements+offset, limit * sizeof(T));
                         resize(required);                  fsize+=limit;
   
                 T* from=&src.felements[offset];  
                 T* to=&felements[fused];  
                 for(T* from_end=from+limit; from<from_end;)  
                         *to++=*from++;  
                 fused+=limit;  
                 return *this;  
         }          }
   
         /// get index-element          /// get index-element
Line 159  public: Line 149  public:
                 if(is_full())                  if(is_full())
                         expand();                          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 197  public: Line 187  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 218  public: Line 208  public:
   
 protected:  protected:
   
         bool is_full() {          inline bool is_full() {
                 return fused == fallocated;                  return fsize == fallocated;
         }          }
   
         inline void expand() {          inline void expand() {
                 resize(fallocated>0 ? fallocated+fallocated/4+2 : 3); // 3 is PAF default, confirmed by tests                  resize(fallocated>0 ? fallocated+fallocated/2+2 : 3); // 3 is PAF default, confirmed by tests
           }
   
           inline void fit(size_t index){
                   if(index >= fallocated)
                           resize(max(this->fallocated + this->fallocated/4, index+1));
         }          }
   
         void resize(size_t asize) {          void resize(size_t asize) {
Line 256  template<typename T> char* pa_itoa(T n, Line 251  template<typename T> char* pa_itoa(T n,
         }          }
   
         do {          do {
                 *(--pos)=(n % base) + '0';                  *(--pos)=(char)(n % base) + '0';
                 n/=base;                  n/=base;
         } while (n > 0);          } while (n > 0);
   
Line 272  template<typename T> char* pa_uitoa(T n, Line 267  template<typename T> char* pa_uitoa(T n,
         *pos='\0';          *pos='\0';
   
         do {          do {
                 *(--pos)=(n % base) + '0';                  *(--pos)=(char)(n % base) + '0';
                 n/=base;                  n/=base;
         } while (n > 0);          } while (n > 0);
   
Line 299  public: Line 294  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.fused;                  flast=farray.felements + farray.fsize;
         }          }
   
         /// there are still elements          /// there are still elements
Line 336  template<typename T> class Array_reverse Line 331  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.fused;                  fcurrent=farray.felements + farray.fsize;
         }          }
   
         /// there are still elements          /// there are still elements

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


E-mail: