Main Page   Class Hierarchy   File List  

Vector.h

00001 /*
00002    pointerVector.h : pointerVector class template interface and implementation.
00003 
00004   copyright (c) 1996 - 2001 
00005   
00006   David McCombs davidmc@newcottage.com
00007 
00008   Nuclear Core Class Library
00009 
00010   freely re-usable, redistributable.
00011 
00012 */
00013 
00014 #ifndef pointerVector_H                                                   
00015 #define pointerVector_H
00016 
00017 //
00018 // Intended for holding pointers to data.
00019 //
00020 
00021 #include <iostream>
00022 
00023 using namespace std;
00024 
00025 template <class T> 
00026 class pointerVector
00027 {
00028 
00029 private:
00030   int step;
00031   enum{ increment = 10 };
00032 
00033 protected:
00034   int size;
00035   T ** data;
00036 
00037 public:
00038 
00039   pointerVector();
00040   pointerVector( pointerVector<T> & input );
00041 
00042   virtual ~pointerVector();
00043 
00044   pointerVector<T> & operator = ( pointerVector<T> & input );
00045 
00046   void add( T * input );
00047   virtual void remove( int n );
00048   virtual void removeAll( void );
00049   
00050   T ** subArray ( int i );
00051  
00052   T * operator [] ( int i );
00053  
00054   int Size( void );
00055 
00056   friend ostream & operator << <T>( ostream & s, pointerVector<T> & input );
00057   friend istream & operator >> <T>( istream & s, pointerVector<T> & input );
00058 
00059 };
00060 
00061 
00062 //
00063 // Version of pointerVector that does garbage collection.
00064 //
00065 
00066 template <class T>
00067 class garbageCollector: public pointerVector<T>
00068 {
00069 public:
00070   virtual ~garbageCollector();
00071   virtual void remove( int n );
00072   virtual void removeAll( void );
00073 };
00074 
00075 
00076 // Constructor constructs with zero size.
00077 // The first add will cause the allocation of  
00078 // an array of 10 pointers
00079 //
00080 template <class T> pointerVector<T>::pointerVector()
00081 :data( (T**) 0l), size( 0 ), step(1) 
00082 {
00083   ;
00084 }
00085 
00086 // The destructor first deletes all of the 
00087 // elements pointed to, then it deletes the 
00088 // array of pointers.
00089 //
00090 template <class T> pointerVector<T>::~pointerVector()
00091 {
00092   removeAll();
00093 }
00094 
00095 // The add method places the pointer in the current 
00096 // position in the array.  If the array is full
00097 // (or null) it is (re)allocated for another 'increment'
00098 // number of elements.
00099 // 
00100 template <class T> void pointerVector<T>::add( T * input )
00101 {
00102   if( !size || size < step*increment )
00103   {
00104     // time to add pointers
00105     step++;
00106     T** temp = new T* [ step*increment];
00107     if( data )
00108     {
00109       memcpy( temp, data, sizeof(T**)*(step-1)*increment );
00110       delete [] data;
00111     }
00112     data = temp;
00113   }
00114   data[size] = input;
00115   size++;
00116 }
00117 
00118 // The remove method removes an element from the array
00119 // by copying all elements but the one to be deleted,
00120 // then decrementing the size.
00121 //
00122 template <class T> void pointerVector<T>::remove( int n )
00123 {
00124   if( size > n)
00125   {
00126     T* temp = (T*)0L;
00127     for( int i = 0, j = 0; i < size; i++ )
00128     {
00129       if( i != n )
00130       {
00131         temp = data[i];
00132         data[j] = temp;
00133         j++;
00134       }
00135     } // end for all elements
00136 
00137     --size;
00138     data[size]= (T*) 0L;
00139   }
00140 }
00141 
00142 template <class T> void pointerVector<T>::removeAll( void )
00143 {
00144   delete [] data;
00145   data = NULL;
00146   size = 0;
00147 }
00148 
00149 // operator accesses the element at the (zero based)
00150 // requested position.  It does not first check 
00151 // the size of the array.  the 'Size, method should 
00152 // be used to determine the size fo the array.
00153 //
00154 template <class T> T * pointerVector<T>::operator [] ( int i )
00155 {
00156   return data[i];
00157 }
00158 
00159 template <class T> T ** pointerVector<T>::subArray( int i )
00160 
00161 {
00162   return & data[i];
00163 }
00164 
00165 
00166 // returns the size of the array.  Valid elements
00167 // to be accessed are up to [Size()-1].
00168 //
00169 template <class T> int pointerVector<T>::Size( void )
00170 {
00171   return size;
00172 }
00173 
00174 
00175 template <class T> pointerVector<T>::pointerVector( pointerVector<T> & input )
00176 :data( (T**) 0l), size( 0 ), step(1) 
00177 {
00178   for( int i=0; i < input.Size(); i++)
00179   {
00180     add( new T ( *input.data[i] ) );
00181   }
00182 }
00183 
00184 template <class T> pointerVector<T> & pointerVector<T>::operator = ( pointerVector<T> & input )
00185 {
00186   if( this != &input )
00187   {
00188     removeAll();
00189 
00190     for( int i=0; i < input.Size(); i++)
00191     {
00192       add( new T ( *input.data[i] ) );
00193     }
00194   }
00195   return *this;
00196 }
00197 
00198 template <class T> ostream & operator << ( ostream & s, pointerVector<T> & input )
00199 {
00200   s << input.size << endl;
00201   for( int i=0; i < input.Size(); i++)
00202   {
00203     s << *(input.data[i]);
00204   }
00205   return s;
00206 }
00207 
00208 template <class T> istream & operator >> ( istream & s, pointerVector<T> & input )
00209 {
00210   size_t tempSize;
00211   s >> tempSize >> ws;
00212   for( size_t i=0; i < tempSize; i++)
00213   {
00214     T * tdata = new T;
00215     s >> *tdata;
00216     input.add( tdata );
00217   }
00218   return s;
00219 }
00220 
00221 // The destructor first deletes all of the 
00222 // elements pointed to, then it deletes the 
00223 // array of pointers.
00224 //
00225 template <class T> garbageCollector<T>::~garbageCollector()
00226 {
00227   removeAll();
00228 }
00229 // The remove method removes an element from the array
00230 // by copying all elements but the one to be deleted, 
00231 // then deleting the item, then decrementing the size.
00232 //
00233 template <class T> void garbageCollector<T>::remove( int n )
00234 {
00235   if( size > n)
00236   {
00237     T* temp = (T*)0L;
00238     for( int i = 0, j = 0; i < size; i++ )
00239     {
00240       if( i != n )
00241       {
00242         temp = data[i];
00243         data[j] = temp;
00244         j++;
00245       }
00246       else
00247       {
00248         delete data[i];
00249         data[i]=NULL;
00250       }
00251     } // end for all elements
00252 
00253     --size;
00254     data[size]= (T*) 0L;
00255   }
00256 }
00257 
00258 template <class T> void garbageCollector<T>::removeAll( void )
00259 {
00260   for( int i=0; i<size; i++ )
00261   {
00262     delete data[i];
00263     data[i]=NULL;
00264   }
00265   delete [] data;
00266   data = NULL;
00267   size = 0;
00268 }
00269 
00270 #endif
00271 

Generated on Tue Jan 20 09:03:27 2004 for OpenTools by doxygen1.2.18