varray.h

Go to the documentation of this file.
00001 
00002 #ifndef VARRAY_H
00003 #define VARRAY_H
00004 
00008 
00009 #include <stdlib.h>
00010 #include <string.h>
00011 
00012 #ifdef NEED_BOOL_TYPE
00013 #undef NEED_BOOL_TYPE
00014 # ifdef true
00015 #   undef true
00016 # endif
00017 # ifdef false
00018 #   undef false
00019 # endif
00020     typedef int bool;
00021 
00022     const bool true = !0;
00023     const bool false = 0;
00024 #endif
00025 
00026  
00027 #ifdef __cplusplus
00028 extern "C++" {
00029 
00030 template <class T> class VArray {
00031     
00032 public:
00033     
00034     VArray(unsigned long size=1) {
00035         m_Size = size;
00036         m_Data = new T[m_Size];
00037         m_Count = 0;
00038     };
00039     
00040     ~VArray() {
00041         delete[] m_Data;
00042     }; 
00043     
00044     inline unsigned long Count() const { return m_Count; }
00045     inline unsigned long Size() const { return m_Size; }
00046 
00047     T& operator[](unsigned long i){
00048                 if(i >= m_Count){
00049         EnsureSize(i+1);
00050                         m_Count=i+1;
00051                 }
00052         return m_Data[i];
00053     };
00054 
00055     void Append(T const & item) {
00056         EnsureSize(m_Count+1);
00057         m_Data[m_Count++] = item;
00058     };
00059 
00060     void AppendArray(T const *item_array, unsigned long n_items) {
00061         if(!n_items) return;
00062         EnsureSize(n_items+m_Count);
00063         memcpy(&m_Data[m_Count], item_array, n_items*sizeof(T));
00064         m_Count+=n_items;
00065     };
00066 
00067     T & GetData(unsigned long i) {
00068         return m_Data[i];
00069     };
00070         void GetArrayCopy(T *new_item_array) {
00071                 memcpy(new_item_array,m_Data,m_Count*sizeof(T));
00072         };
00073 
00074     T & Pop() {
00075         return m_Data[--m_Count];
00076     };
00077 
00078     void InsertAt(T const & item, unsigned long i) {
00079 
00080         if(i >= m_Count) {
00081             EnsureSize(i+1);
00082             m_Count=i+1;
00083         }else{
00084             EnsureSize(m_Count+1);
00085             memmove(&m_Data[i+1], &m_Data[i], (m_Count-i)*sizeof(T)); 
00086             m_Count++;
00087         }
00088         m_Data[i] = item;
00089     };
00090 
00091     bool RemoveAt(unsigned long i, T * old_item=0) {
00092 
00093         if(i>=m_Count) return false;
00094         m_Count--;
00095         if(old_item) *old_item = m_Data[i];
00096         if(i!=m_Count) memmove(&m_Data[i], &m_Data[i+1], (m_Count-i)*sizeof(T)); 
00097         return true;
00098     };
00099 
00100     bool RemoveAt(unsigned long i, T & old_item) {
00101         return RemoveAt(i,&old_item);
00102     };
00103 
00104     void SetCount(unsigned long count) { 
00105         EnsureSize(count);
00106         m_Count=count;
00107     };
00108 
00109 
00110     bool ReplaceAt(T const & item, unsigned long i, T * old_item=0) {
00111 
00112         if(i>=m_Count){
00113             EnsureSize(i+1);
00114             m_Count=i+1;
00115             m_Data[i]=item;
00116             return false;
00117         }
00118         if(old_item) *old_item = m_Data[i];
00119         m_Data[i]=item;
00120         return true;
00121     };
00122 
00123     bool ReplaceAt(T const & item, unsigned long i, T & old_item) {
00124         return ReplaceAt(item,i,&old_item);
00125     };
00126 
00127     void MapFunction(void(*function)(T, void*), void * user_data) const{
00128         unsigned long i;
00129         for(i=0;i<m_Count;i++)
00130             (*function)(m_Data[i],user_data);
00131     };
00132 
00133     void MapFunction(void(*function)(T&, void*), void * user_data) {
00134         unsigned long i;
00135         for(i=0;i<m_Count;i++)
00136             (*function)(m_Data[i],user_data);
00137     };
00138 
00139     void MapFunction(void(*function)(T const &, void*), void * user_data) const{
00140         unsigned long i;
00141         for(i=0;i<m_Count;i++)
00142             (*function)(m_Data[i],user_data);
00143     };
00144 
00145     void TrimSize() {
00146 
00147         m_Size=m_Count;
00148         if(!m_Size) m_Size++;
00149         T * new_data = new T[m_Size];
00150         if(m_Count) memcpy(new_data,m_Data,m_Count*sizeof(T));
00151         delete[] m_Data;
00152         m_Data=new_data;
00153     };
00154 
00155     void EnsureSize(unsigned long size) {
00156 
00157         if(size<=m_Size) return;
00158 
00159         m_Size = (unsigned long) (size * 1.5);
00160         T * new_data = new T[m_Size];
00161         memcpy(new_data,m_Data,m_Count*sizeof(T));
00162         delete[] m_Data;
00163         m_Data=new_data;
00164     };
00165     
00166 private:
00167     
00168     unsigned long m_Count;   
00169     unsigned long m_Size;
00170     T *m_Data;
00171     
00172 }; 
00173 
00174 } /* extern "C++" */
00175 #endif
00176 
00177 #endif 
00178 
00179 
00180 

Generated on Tue Jan 6 22:41:37 2009 for Autodesk DWF 3D Toolkit by  doxygen 1.4.5