Vector.h

Go to the documentation of this file.
00001 #ifndef __VECTOR_H__
00002 #define __VECTOR_H__
00003 
00004 #include <string.h> //for memcpy()
00005 #include "libRefCount.h"
00006 
00007 #undef DEBUG
00008 #define DEBUG 0
00009 #include "libDebug.h"
00010 
00011 template<class T>
00012 class VectorOf: public Counted
00013 {
00014 protected:
00015   T* m_vt;
00016   int m_ctAlloc;
00017   int m_ct;
00018 
00019 protected:
00020   // private constructor to use wrap a
00021   VectorOf(T* vt, int ct, bool bPrivate) :
00022     Counted()
00023   {
00024     m_vt = vt;
00025     m_ct = ct;
00026     m_ctAlloc = m_ct;
00027   }
00028 
00029   void Release()
00030   {
00031     if (AboutToDie())
00032       {
00033       SafeDeleteArray(m_vt);
00034       }
00035   }
00036 
00037   // this leaves undefined values at the end if c>m_ct
00038   void SetSizeUnsafe(int c)
00039   {
00040     ASSERT(c < m_ctAlloc);
00041     m_ct = c;
00042   }
00043 
00044 public:
00045   VectorOf() :
00046     Counted()
00047   {
00048     m_ctAlloc = 0;
00049     m_ct = 0;
00050     m_vt = NULL;
00051   }
00052 
00053   ~VectorOf() __attribute__((always_inline))
00054   {
00055     Release();
00056   }
00057 
00058   VectorOf(const T* vt, int ct)__attribute__((always_inline))
00059   : Counted()
00060     {
00061     m_ctAlloc = max(1<<int(ceil(log2(ct))),32);
00062     m_ct=ct;
00063     m_vt = new T [m_ctAlloc];
00064     if (vt!=NULL)
00065       {
00066       memcpy(m_vt,vt,m_ct*sizeof(T));
00067       }
00068     }
00069 
00070   VectorOf(const VectorOf& vec) __attribute__((always_inline))
00071   : Counted(vec)
00072     {
00073     m_vt=vec.m_vt;
00074     m_ct=vec.m_ct;
00075     m_ctAlloc=vec.m_ctAlloc;
00076     }
00077 
00078   VectorOf& operator = (const VectorOf& vec) __attribute__((always_inline))
00079     {
00080     Release();
00081     Counted::operator=(vec);
00082     m_vt=vec.m_vt;
00083     m_ct=vec.m_ct;
00084     m_ctAlloc=vec.m_ctAlloc;
00085     return *this;
00086     }
00087 
00088   // TODO: this might not be safe if multiple holders of object?
00089   void Clear()
00090     {
00091     SafeDeleteArray(m_vt);
00092     m_vt=NULL;
00093     m_ctAlloc=0;
00094     m_ct=0;
00095     }
00096 
00097   const int& C() const
00098     {
00099     return m_ct;
00100     }
00101 
00102   T* V()
00103     {
00104     return m_vt;
00105     }
00106 
00107   const T* V() const
00108     {
00109     return m_vt;
00110     }
00111 
00112   const T& Get(const int& n) const
00113     {
00114     return m_vt[n];
00115     }
00116 
00117   T& Get(const int& n)
00118     {
00119     return m_vt[n];
00120     }
00121 
00122   operator T*()
00123     {
00124     return m_vt;
00125     }
00126 
00127   operator const T *() const
00128     {
00129     return m_vt;
00130     }
00131 
00132   const T& operator[](const int& n) const
00133     {
00134     return m_vt[n];
00135     }
00136 
00137   T& operator[](const int& n)
00138     {
00139     return m_vt[n];
00140     }
00141 
00142   VectorOf<T> operator + (const VectorOf<T>& vec)
00143     {
00144     VectorOf<T> vecOut;
00145     vecOut.EnsureSpace(C()+vec.C());
00146     vecOut.Append(V(),C());
00147     vecOut.Append(vec.V(),vec.C());
00148     return vecOut;
00149     }
00150 
00151   VectorOf<T>& operator += (const VectorOf<T>& vec)
00152     {
00153     Append(vec.V(),vec.C());
00154     return *this;
00155     }
00156 
00157   void Append(const T* vt, int c)
00158     {
00159     if (vt==NULL)
00160       {
00161       ASSERTf(
00162           c<=0,
00163           "trying to append null pointer which is reported as not empty %d",
00164           c);
00165       return;
00166       }
00167     if (c<=0)
00168       {
00169       return;
00170       }
00171 
00172     EnsureSpace(m_ct + c);
00173     memcpy(&m_vt[m_ct],vt,c*sizeof(T));
00174     m_ct+=c;
00175     }
00176 
00177   void Append(const T& t)
00178     {
00179     EnsureSpace(m_ct+1);
00180     m_vt[m_ct]=t;
00181     m_ct++;
00182     }
00183 
00184   void EnsureSpace(int c)
00185     {
00186     if (c>m_ctAlloc)
00187       {
00188       m_ctAlloc = max(1<<int(ceil(log2(c))),32);
00189       T* vtT = new T [m_ctAlloc];
00190       if (m_vt!=NULL)
00191         {
00192         memcpy(vtT,m_vt,m_ct*sizeof(T));
00193         delete [] m_vt;
00194         }
00195       m_vt=vtT;
00196       }
00197     }
00198 
00199   // this leaves undefined values at the end if c>m_ct
00200   void SetSize(int c)
00201     {
00202     EnsureSpace(c);
00203     m_ct=c;
00204     }
00205   };
00206 
00207 #endif

Generated on 6 Apr 2011 for Slicer3 by  doxygen 1.6.1