Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

grow_array.h

Go to the documentation of this file.
00001 
00002 #ifndef _GROW_ARRAY_H_
00003 #define _GROW_ARRAY_H_
00004 
00036 
00037 #include "blockpool.h"
00038 
00068 template <class T>
00069 class GrowableArray {
00070 private:
00071   typedef enum { StartArraySize = 128 } bogus;
00073   size_t num_elem;
00075   size_t array_size;
00076   T *pArray;
00077 
00078 private:
00086   bool twice()
00087   {
00088     bool rslt;
00089     T *old_array = pArray;
00090     size_t new_size = array_size * 2;
00091     
00092     pArray = new T [ new_size ];
00093     if (pArray != 0) {
00094       rslt = true;
00095       for (int i = 0; i < array_size; i++) {
00096         pArray[i] = old_array[i];
00097       }
00098     
00099       delete [] old_array;
00100     
00101       array_size = new_size;
00102     }
00103     else {
00104       rslt = false;
00105     }
00106 
00107     return rslt;
00108   } // twice
00109 
00110 
00111 public:
00112   GrowableArray()
00113   {
00114     pArray = new T[ StartArraySize ];
00115     num_elem = 0;
00116     array_size = StartArraySize;
00117   } // GrowableArray constructor
00118 
00120   ~GrowableArray()
00121   {
00122     if (pArray != NULL) {
00123       delete [] pArray;
00124     }
00125   } // GrowableArray destructor
00126 
00129   const size_t length(void) const { return num_elem; }
00130 
00132   void set_to_zero() 
00133   {
00134     num_elem = 0;
00135   }
00136 
00138   T &operator[](const size_t i)
00139   {
00140     assert( i < num_elem );
00141     return pArray[ i ];
00142   }
00143 
00145   T operator[](const size_t i ) const
00146   {
00147     assert( i < num_elem );
00148     return pArray[ i ];
00149   }
00150 
00152   const T *getData() const { return pArray; }
00153 
00155   void append( T item )
00156   {
00157 
00158     if (num_elem == array_size) {
00159       bool allocOK = twice();
00160       assert( allocOK );
00161     }
00162 
00163     pArray[ num_elem ] = item;
00164     num_elem++;
00165   } // append
00166 
00167 
00179   void expand( size_t amount )
00180   {
00181     bool allocOK = true;
00182 
00183     while (allocOK && num_elem + amount >= array_size) {
00184       allocOK = twice();
00185       assert( allocOK );
00186     }
00187     num_elem += amount;
00188   } // expand
00189 
00190 
00192   void remove(void)
00193   {
00194     if (num_elem > 0)
00195       num_elem--;
00196   }
00197 
00198 }; // GrowableArray
00199 
00200 #endif

Generated at Tue May 27 21:56:16 2003 for Wavelet compression, determinism and time series forecasting by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001