Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

vuDVector.h

Go to the documentation of this file.
00001 #ifndef _vuDVector_H_
00002 #define _vuDVector_H_
00003 
00004 #include "vuSimpleTypes.h"
00005 
00006 #include <iostream.h>
00007 
00011 template <class T>
00012 class vuDVector
00013 {
00014 public:
00015 
00017     vuDVector()
00018     {
00019         m_Size     = 4;
00020         m_Length   = 0;
00021         m_StepSize = 4;
00022         createData();
00023     }
00024 
00026     vuDVector(const vuDVector<T>& v)
00027     {
00028         m_Size     = v.m_Size;
00029         m_Length   = v.m_Length;
00030         m_StepSize = v.m_StepSize;
00031         createData();
00032         copyData(v);
00033     }
00034 
00036     ~vuDVector()
00037     {
00038         destroyData();
00039     }
00040 
00041 
00043 
00046     void setStepSize(dword s)
00047     {
00048         m_StepSize = s;
00049     }
00050 
00052     dword getStepSize(void) const
00053     {
00054         return m_StepSize;
00055     }
00056 
00058 
00060     void setSize(dword s)
00061     {
00062         resizeData(s);
00063     }
00064 
00066     dword getSize(void) const
00067     {
00068         return m_Size;
00069     }
00070 
00072 
00075     dword getLength(void) const
00076     {
00077         return m_Length;
00078     }
00079 
00081     bool isMember(const T& elem) const
00082     {
00083                 for (dword i=0;i<m_Length;++i)
00084                         if (m_Data[i] == elem) return true;
00085 
00086                 return false;
00087     }
00088 
00090         dword findIndex(const T& elem) const
00091         {
00092                 dword i;
00093       for (i=0;i<m_Length;++i)
00094           if (m_Data[i] == elem) return i;
00095       
00096       return (dword)(-1);
00097         }
00098 
00100     void remove(dword index)
00101     {
00102         removeRange(index,index);
00103     }
00104 
00106     void removeAll()
00107     {
00108         removeRange(0,m_Length);
00109     }
00110 
00112 
00117     void removeRange(dword start, dword end)
00118     {
00119         dword diff;
00120         dword i;
00121 
00122         // Make sure start is <= end.
00123         if (start > end)
00124         {
00125             dword temp = end;
00126             end = start;
00127             start = temp;
00128         }
00129 
00130         // If start is out of bounds, then return.
00131         if (start >= m_Length)
00132             return;
00133 
00134         // If end is out of bounds, then shorten it.
00135         if (end >= m_Length)
00136             end = m_Length-1;
00137 
00138         // Move the data in the array.
00139         diff = end-start+1;
00140         for(i=end+1;i<m_Length;++i)
00141             m_Data[i-diff] = m_Data[i];
00142 
00143         // Change the length.
00144         m_Length -= diff;
00145     }
00146 
00148     void sizeToLength(void)
00149     {
00150         resizeData(m_Length);
00151     }
00152 
00154     void stepToLength(void)
00155     {
00156         resizeData((((m_Length-1)/m_StepSize)+1)*m_StepSize);
00157     }
00158 
00160     vuDVector<T>& operator=(const vuDVector<T>& rhs)
00161     {
00162         if (this != &rhs)
00163         {
00164             destroyData();
00165             m_Size     = rhs.m_Size;
00166             m_Length   = rhs.m_Length;
00167             m_StepSize = rhs.m_StepSize;
00168             createData();
00169             copyData(rhs);
00170         }
00171         return *this;
00172     }
00173 
00175     T& operator[](dword index)
00176     {
00177         if (index >= m_Size)
00178             resizeData(((index/m_StepSize)+1)*m_StepSize);
00179         if (index >= m_Length)
00180             m_Length = index+1;
00181         return m_Data[index];
00182     }
00183 
00185     const T& operator[](dword index) const
00186     {
00187         return m_Data[index];
00188     }
00189 
00191     void add(const T& elem)
00192     {
00193         (*this)[m_Length] = elem;
00194     }
00195 
00197 
00200     void insert(dword index, const T&elem)
00201     {
00202         if (index >= m_Length)
00203         {
00204             (*this)[index] = elem;
00205             return;
00206         }
00207 
00208         //If the array is too small, then resize it
00209         if (m_Length == m_Size)
00210             resizeData(m_Size+m_StepSize);
00211 
00212         // Move the data in the array.
00213         for(int i=m_Length-1; (i>=0) && (dword(i)>=index); i--)
00214             m_Data[i+1] = m_Data[i];
00215 
00216         //Note that there's an extra element
00217         m_Length++;
00218 
00219         // Add the new element.
00220         (*this)[index] = elem;
00221     }
00222 
00223 private:
00225     void createData(void)
00226     {
00227         if (m_Size)
00228             m_Data = new T[m_Size];
00229     }
00230 
00232     void destroyData(void)
00233     {
00234         /* These couts were to test for a seg fault that was originating around the time
00235                 that this function was being called. */
00236 //      cout << "destroying: " << int (m_Data) << endl;
00237         if (m_Data)
00238         {
00239             delete [] m_Data;
00240             m_Data = 0;
00241         }
00242 //      cout << "done destruction: " << endl;
00243     }
00244 
00246     void copyData(const vuDVector& v)
00247     {
00248         dword i;
00249 
00250         if (m_Size != v.m_Size)
00251             throw "vuDVector: vectors not the same size.";
00252 
00253         for(i=0;i<m_Length;++i)
00254             m_Data[i] = v.m_Data[i];
00255     }
00256 
00258     void resizeData(dword size)
00259     {
00260         int tsize = size / 10;
00261 
00262         if (tsize > 3)
00263                 m_StepSize = tsize;
00264         if (size == m_Size)
00265         {
00266             return;
00267         }
00268         else if (size > m_Size)
00269         {
00270             T* temp;
00271             dword i;
00272 
00273             temp = m_Data;
00274             m_Data = new T[size];
00275 
00276             for(i=0;i<m_Size;++i)
00277                 m_Data[i] = temp[i];
00278 
00279             delete [] temp;
00280             m_Size = size;
00281         }
00282         else
00283         {
00284             T* temp;
00285             dword i;
00286 
00287             temp = m_Data;
00288             m_Data = new T[size];
00289 
00290             for(i=0;i<size;++i)
00291                 m_Data[i] = temp[i];
00292 
00293             delete [] temp;
00294             m_Size = size;
00295 
00296             if (m_Size < m_Length)
00297                 m_Length = m_Size;
00298         }
00299     }
00300 
00301 private:
00302     dword m_Size; 
00304     dword m_Length; 
00305     dword m_StepSize; 
00307     T*    m_Data; 
00308 };
00309 
00310 #endif

Generated on Wed Dec 15 21:20:33 2004 for vuVolume by  doxygen 1.3.9.1