00001 #ifndef _T_VAL_VECTOR_T_HH
00002 #define _T_VAL_VECTOR_T_HH
00003
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include "TBoundsException.hh"
00007 #include "TMemoryException.hh"
00008
00009
00016 template <class T>
00017 class TValVectorT
00018 {
00019 public:
00020
00024 TValVectorT ();
00025
00031 TValVectorT (int n);
00032
00039 TValVectorT (int n, const T& a);
00040
00044 inline TValVectorT (const TValVectorT<T>& vec) { Copy (vec); }
00045
00049 ~TValVectorT () { Clear (); }
00050
00056 void Append (const T& a);
00057
00064 void Apply (void (*fn)(T *, void *), void *d = 0);
00065
00069 void Clear ();
00070
00076 inline int Entries () const { return myNumElementsUsed; }
00077
00083 inline void Resize (int n);
00084
00091 void Resize (int n, const T& a);
00092
00098 void Sort (bool ascending = true);
00099
00107 inline T& operator[](int i) const;
00108
00112 TValVectorT<T>& operator=(const TValVectorT<T> &vec);
00113
00114
00115 private:
00116
00121 void Resize ();
00122
00127 void Copy (const TValVectorT<T> &vec);
00128
00129
00130 static const int BLOCK_SIZE;
00131 static const int BLOCK_MASK;
00132
00133
00134 T **myVector;
00135
00136
00137 int myNumElementsUsed;
00138
00139
00140 int myNumElementsAvail;
00141 };
00142
00143
00144 template <class T>
00145 const int TValVectorT<T>::BLOCK_SIZE = 16;
00146
00147 template <class T>
00148 const int TValVectorT<T>::BLOCK_MASK = ~15;
00149
00150
00151 template <class T>
00152 TValVectorT<T>::TValVectorT ()
00153 : myVector (0),
00154 myNumElementsUsed (0),
00155 myNumElementsAvail (0)
00156 {
00157 }
00158
00159
00160 template <class T>
00161 TValVectorT<T>::TValVectorT (int n)
00162 : myVector (0),
00163 myNumElementsUsed (0),
00164 myNumElementsAvail (0)
00165 {
00166 Resize (n);
00167 }
00168
00169
00170 template <class T>
00171 TValVectorT<T>::TValVectorT (int n, const T& a)
00172 : myVector (0),
00173 myNumElementsUsed (0),
00174 myNumElementsAvail (0)
00175 {
00176 Resize (n, a);
00177 }
00178
00179
00180 template <class T>
00181 void TValVectorT<T>::Append (const T& a)
00182 {
00183 Resize ();
00184
00185 myVector[myNumElementsUsed] = new T (a);
00186 myNumElementsUsed++;
00187 }
00188
00189
00190 template <class T>
00191 void TValVectorT<T>::Apply (void (*fn)(T *, void *), void *d)
00192 {
00193 for (int i = 0; i < myNumElementsUsed; i++)
00194 fn (myVector[i], d);
00195 }
00196
00197
00198 template <class T>
00199 void TValVectorT<T>::Clear ()
00200 {
00201 if (myVector)
00202 {
00203 for (int i = 0; i < myNumElementsUsed; i++)
00204 if (myVector[i])
00205 delete myVector[i];
00206
00207 free (myVector);
00208 myVector = 0;
00209
00210 myNumElementsUsed = 0;
00211 myNumElementsAvail = 0;
00212 }
00213 }
00214
00215
00216 template <class T>
00217 void TValVectorT<T>::Resize (int n)
00218 {
00219 if (n > myNumElementsUsed)
00220 {
00221 if (n > myNumElementsAvail)
00222 {
00223 myNumElementsAvail = (n + (BLOCK_SIZE - 1)) & BLOCK_MASK;
00224 myVector = (T **) realloc (myVector,
00225 myNumElementsAvail * sizeof(T *));
00226 if (! myVector)
00227 throw TMemoryException ();
00228 }
00229
00230 for (; myNumElementsUsed < n; myNumElementsUsed++)
00231 myVector[myNumElementsUsed] = new T;
00232 }
00233 else if (myNumElementsUsed >= 0)
00234 myNumElementsUsed = n;
00235 }
00236
00237
00238 template <class T>
00239 void TValVectorT<T>::Resize (int n, const T& a)
00240 {
00241 if (n > myNumElementsUsed)
00242 {
00243 if (n > myNumElementsAvail)
00244 {
00245 myNumElementsAvail = (n + (BLOCK_SIZE - 1)) & BLOCK_MASK;
00246 myVector = (T **) realloc (myVector,
00247 myNumElementsAvail * sizeof(T *));
00248 if (! myVector)
00249 throw TMemoryException ();
00250 }
00251
00252 for (; myNumElementsUsed < n; myNumElementsUsed++)
00253 myVector[myNumElementsUsed] = new T (a);
00254 }
00255 else if (myNumElementsUsed >= 0)
00256 myNumElementsUsed = n;
00257 }
00258
00259
00260
00261 template <class T>
00262 void TValVectorT<T>::Sort (bool ascending)
00263 {
00264 }
00265
00266
00267 template <class T>
00268 T& TValVectorT<T>::operator[](int i) const
00269 {
00270 #ifdef TOOLS_BOUND_CHECK
00271 if (i >= myNumElementsUsed || i < 0)
00272 throw TBoundsException ();
00273 #endif
00274
00275 return *myVector[i];
00276 }
00277
00278
00279 template <class T>
00280 TValVectorT<T>& TValVectorT<T>::operator=(const TValVectorT<T> &vec)
00281 {
00282
00283 if (&vec != this)
00284 {
00285 Clear ();
00286 Copy (vec);
00287 }
00288
00289 return *this;
00290 }
00291
00292
00293 template <class T>
00294 void TValVectorT<T>::Resize ()
00295 {
00296 if (myNumElementsUsed == myNumElementsAvail)
00297 {
00298 myNumElementsAvail += BLOCK_SIZE;
00299 myVector = (T **) realloc (myVector, myNumElementsAvail * sizeof(T *));
00300 if (! myVector)
00301 throw TMemoryException ();
00302 }
00303 }
00304
00305
00306 template <class T>
00307 void TValVectorT<T>::Copy (const TValVectorT<T>& vec)
00308 {
00309 myNumElementsUsed = vec.myNumElementsUsed;
00310 myNumElementsAvail = vec.myNumElementsAvail;
00311
00312 myVector = (T **) malloc (vec.myNumElementsAvail * sizeof(T *));
00313 if (! myVector)
00314 throw TMemoryException ();
00315
00316 for (int i = 0; i < vec.myNumElementsUsed; i++)
00317 myVector[i] = new T (*vec.myVector[i]);
00318 }
00319
00320 #endif // _T_VAL_VECTOR_T_HH