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

version 1.89, 2024/09/07 16:30:26 version 1.92, 2024/09/14 22:58:50
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())
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() {
Line 249  private: //disabled Line 240  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 296  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.92


E-mail: