Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

varray.h

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

Generated on Tue May 17 12:06:00 2005 for Autodesk DWF 3D Toolkit by  doxygen 1.4.1