Diff for /parser3/src/include/pa_array.h between versions 1.92 and 1.95

version 1.92, 2024/09/14 22:58:50 version 1.95, 2024/09/21 23:51:04
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(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 110  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 119  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 157  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 197  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 218  public: Line 216  public:
   
 protected:  protected:
   
         bool is_full() {          inline bool is_full() {
                 return fused == fallocated;                  return fsize == fallocated;
         }          }
   
         void expand(size_t delta) {          inline void expand() {
                   resize(fallocated>0 ? fallocated+fallocated/4+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) {
                 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 296  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.fused;                  flast=farray.felements + farray.fsize;
         }          }
   
         /// there are still elements          /// there are still elements
Line 333  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.fused;                  fcurrent=farray.felements + farray.fsize;
         }          }
   
         /// there are still elements          /// there are still elements

Removed from v.1.92  
changed lines
  Added in v.1.95


E-mail: