Main Page   Class Hierarchy   Compound List   File List   Compound Members  

TPtrVectorT.hh

00001 #ifndef _T_PTR_VECTOR_T_HH
00002 #define _T_PTR_VECTOR_T_HH
00003 
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include "TBoundsException.hh"
00007 #include "TMemoryException.hh"
00008 
00015 template <class T>
00016 class TPtrVectorT
00017 {
00018 public:
00019   
00023   TPtrVectorT ();
00024   
00031   TPtrVectorT (int n, T *a = NULL);
00032   
00036   inline TPtrVectorT (TPtrVectorT<T> &vec) { Copy (vec); }
00037   
00041   ~TPtrVectorT () { Clear (); }
00042   
00048   void Append (T *a);
00049   
00056   void Apply (void (*fn)(T *, void *), void *d);
00057   
00061   void Clear ();
00062   
00067   void ClearAndDestroy ();
00068   
00074   inline int Entries () const { return myNumElementsUsed; }
00075   
00083   void Resize (int n, T *a = NULL);
00084   
00090   void Sort (bool reverse = false);
00091   
00099   inline T*& operator[](int i) const;
00100   
00104   TPtrVectorT<T>& operator=(const TPtrVectorT<T> &vec);
00105   
00106   
00107 private:
00108   
00113   void Resize ();
00114   
00119   void Copy (TPtrVectorT<T> &vec);
00120   
00121   /* Allocate pointers in chunks of this number. */
00122   static const int BLOCK_SIZE;
00123   static const int BLOCK_MASK;
00124   
00125   /* Vector containing pointers. */
00126   T **myVector;
00127   
00128   /* Number of elements used in ptr vector. */
00129   int myNumElementsUsed;
00130   
00131   /* Number of elements available in ptr vector. */
00132   int myNumElementsAvail;
00133 };
00134 
00135 
00136 template <class T>
00137 const int TPtrVectorT<T>::BLOCK_SIZE =  16;
00138 
00139 template <class T>
00140 const int TPtrVectorT<T>::BLOCK_MASK = ~15;
00141 
00142 
00143 template <class T>
00144 TPtrVectorT<T>::TPtrVectorT ()
00145   : myVector (0),
00146     myNumElementsUsed (0),
00147     myNumElementsAvail (0)
00148 {
00149 }
00150 
00151 
00152 template <class T>
00153 TPtrVectorT<T>::TPtrVectorT (int n, T *a)
00154   : myVector (0),
00155     myNumElementsUsed (0),
00156     myNumElementsAvail (0)
00157 {
00158   Resize (n, a);
00159 }
00160 
00161 
00162 template <class T>
00163 void TPtrVectorT<T>::Append (T *a)
00164 {
00165   Resize ();
00166   
00167   myVector[myNumElementsUsed] = a;
00168   myNumElementsUsed++;
00169 }
00170 
00171 
00172 template <class T>
00173 void TPtrVectorT<T>::Apply (void (*fn)(T *, void *), void *d)
00174 {
00175   for (int i = 0; i < myNumElementsUsed; i++)
00176     fn (myVector[i], d);
00177 }
00178 
00179 
00180 template <class T>
00181 void TPtrVectorT<T>::Clear ()
00182 {
00183   if (myVector)
00184     {
00185       free (myVector);
00186       myVector = 0;
00187     }
00188   
00189   myNumElementsUsed = 0;
00190   myNumElementsAvail = 0;
00191 }
00192 
00193 
00194 template <class T>
00195 void TPtrVectorT<T>::ClearAndDestroy ()
00196 {
00197   for (int i = 0; i < myNumElementsUsed; i++)
00198     if (myVector[i])
00199       delete myVector[i];
00200   
00201   Clear ();
00202 }
00203 
00204 
00205 template <class T>
00206 void TPtrVectorT<T>::Resize (int n, T *a)
00207 {
00208   if (n > myNumElementsUsed)
00209     {
00210       if (n > myNumElementsAvail)
00211         {
00212           myNumElementsAvail = (n + (BLOCK_SIZE - 1)) & BLOCK_MASK;
00213           myVector = (T **) realloc (myVector,
00214                                      myNumElementsAvail * sizeof(T *));
00215           if (! myVector)
00216             throw TMemoryException ();
00217         }
00218       
00219       for (; myNumElementsUsed < n; myNumElementsUsed++)
00220         myVector[myNumElementsUsed] = a;
00221     }
00222   else if (myNumElementsUsed >= 0)
00223     myNumElementsUsed = n;
00224 }
00225 
00226 
00227 /* FIXME */
00228 template <class T>
00229 void TPtrVectorT<T>::Sort (bool reverse)
00230 {
00231 }
00232 
00233 
00234 template <class T>
00235 T*& TPtrVectorT<T>::operator[](int i) const
00236 {
00237 #ifdef TOOLS_BOUND_CHECK
00238   if (i >= myNumElementsUsed || i < 0)
00239     throw TBoundsException ();
00240 #endif
00241   
00242   return myVector[i];
00243 }
00244 
00245 
00246 template <class T>
00247 TPtrVectorT<T>& TPtrVectorT<T>::operator=(const TPtrVectorT<T> &vec)
00248 {
00249   /* Only copy if different objects. */
00250   if (&vec != this)
00251     {
00252       if (myVector)
00253         free (myVector);
00254       
00255       Copy (vec);
00256     }
00257   
00258   return *this;
00259 }
00260 
00261 
00262 template <class T>
00263 void TPtrVectorT<T>::Resize ()
00264 {
00265   if (myNumElementsUsed == myNumElementsAvail)
00266     {
00267       myNumElementsAvail += BLOCK_SIZE;
00268       myVector = (T **) realloc (myVector, myNumElementsAvail * sizeof(T *));
00269       if (! myVector)
00270         throw TMemoryException ();
00271     }
00272 }
00273 
00274 
00275 template <class T>
00276 void TPtrVectorT<T>::Copy (TPtrVectorT<T> &vec)
00277 {
00278   myNumElementsUsed  = vec.myNumElementsUsed;
00279   myNumElementsAvail = vec.myNumElementsAvail;
00280   
00281   myVector = (T **) malloc (myNumElementsAvail * sizeof(T *));
00282   if (! myVector)
00283     throw TMemoryException ();
00284   
00285   memcpy (myVector, vec.myVector, sizeof (T *) * myNumElementsAvail);
00286 }
00287 
00288 #endif // _T_PTR_VECTOR_T_HH

Generated on Sat Feb 15 18:37:16 2003 for Tools by doxygen1.3-rc2