00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef pointerVector_H
00015 #define pointerVector_H
00016
00017
00018
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
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
00077
00078
00079
00080 template <class T> pointerVector<T>::pointerVector()
00081 :data( (T**) 0l), size( 0 ), step(1)
00082 {
00083 ;
00084 }
00085
00086
00087
00088
00089
00090 template <class T> pointerVector<T>::~pointerVector()
00091 {
00092 removeAll();
00093 }
00094
00095
00096
00097
00098
00099
00100 template <class T> void pointerVector<T>::add( T * input )
00101 {
00102 if( !size || size < step*increment )
00103 {
00104
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
00119
00120
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 }
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
00150
00151
00152
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
00167
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
00222
00223
00224
00225 template <class T> garbageCollector<T>::~garbageCollector()
00226 {
00227 removeAll();
00228 }
00229
00230
00231
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 }
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