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

version 1.89, 2024/09/07 16:30:26 version 1.93, 2024/09/15 00:38:27
Line 13 Line 13
 // includes  // includes
   
 #include "pa_memory.h"  #include "pa_memory.h"
   #include "pa_types.h"
 #include "pa_exception.h"  #include "pa_exception.h"
   
 // forwards  // forwards
   
 template<typename T> class Array_iterator;  template<typename T> class Array_iterator;
   template<typename T> class Array_reverse_iterator;
   
 // defines  // defines
   
Line 27  template<typename T> class Array_iterato Line 29  template<typename T> class Array_iterato
 template<typename T> class Array: public PA_Object {  template<typename T> class Array: public PA_Object {
   
         friend class Array_iterator<T>;          friend class Array_iterator<T>;
           friend class Array_reverse_iterator<T>;
   
 protected:  protected:
   
Line 41  protected: Line 44  protected:
   
 public:  public:
         typedef Array_iterator<T> Iterator;          typedef Array_iterator<T> Iterator;
           typedef Array_reverse_iterator<T> ReverseIterator;
   
         struct Action_options {          struct Action_options {
                 size_t offset;                  size_t offset;
Line 72  public: Line 76  public:
   
                         return true;                          return true;
                 }                  }
   
                   
         };          };
   
         typedef T element_type;          typedef T element_type;
Line 85  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 94  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 119  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 154  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 172  public: Line 175  public:
         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)
                           memset(felements, 0, fused * sizeof(T));
                 fused=0;                  fused=0;
         }          }
   
Line 211  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 249  private: //disabled Line 243  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 268  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
         operator bool () {          inline operator bool () {
                 return fcurrent<flast;                  return fcurrent < flast;
         }          }
   
         /// returns the current element and advances the iterator          /// returns the current element and advances the iterator
         T next() {          inline T next() {
                 return *(fcurrent++);                  return *(fcurrent++);
         }          }
   
         /// returns the current element          /// returns the current element
         T value() {          inline T value() {
                 return *(fcurrent);                  return *(fcurrent);
         }          }
   
         // returns the current index of the iterator          // returns the current index of the iterator
         size_t index() {          inline size_t index() {
                 return fcurrent - farray.felements;                  return fcurrent - farray.felements;
         }          }
   
           inline char *key(){
                   return pa_uitoa(index());
           }
   
   };
   
   template<typename T> class Array_reverse_iterator {
   
           const Array<T>& farray;
           T *fcurrent;
   
   public:
   
           Array_reverse_iterator(const Array<T>& aarray): farray(aarray) {
                   fcurrent=farray.felements + farray.fused;
           }
   
           /// there are still elements
           inline operator bool () {
                   return fcurrent > farray.felements;
           }
   
           /// returns the current element and advances the iterator
           inline T prev() {
                   return *(--fcurrent);
           }
   
           // returns the current index of the iterator
           inline size_t index() {
                   return fcurrent - farray.felements;
           }
   
           inline char *key(){
                   return pa_uitoa(index());
           }
   
 };  };
 #endif  #endif

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


E-mail: