MeshKit
1.0
|
00001 00006 #ifndef __MESHKIT_VECTORTEMPLATE_H__ 00007 #define __MESHKIT_VECTORTEMPLATE_H__ 00008 00009 #include <string> 00010 #include <iostream> 00011 #include <cassert> 00012 #include <stdlib.h> 00013 00014 // defines the vector template class 00015 template <class T> 00016 class CVector 00017 { 00018 private: 00019 int m_nRows; // number of rows in the vector 00020 T *m_pCells; // address where the vector of 00021 // type T is stored 00022 void ErrorHandler (int,int nR=0) const; 00023 // handles error conditions 00024 void Release (); // similar to destructor 00025 std::string m_szName; // vector name 00026 00027 public: 00028 CVector (); // default constructor 00029 CVector (int); // constructor 00030 CVector (const char*); // constructor 00031 CVector (const char*, int); // constructor 00032 CVector (const CVector<T>&); // copy constructor 00033 ~CVector (); // destructor 00034 void SetSize (int); // sets the size of the vector 00035 // used with the default constructor 00036 00037 // helper functions 00038 // gets the current size of the vector 00039 int GetSize () const; 00040 // gets the vector name 00041 void GetName (std::string& szName) const; 00042 00043 // vector manipulations (mutator) 00044 void Set (T); // sets the value of all 00045 // elements of a vector 00046 void SetName (const std::string&); // sets the name of the vector 00047 00048 // overloaded operators 00049 T& operator() (int); // row access 00050 const T& operator() (int) const;// row access 00051 CVector<T>& operator= (const CVector<T>&); // overloaded = operator 00052 }; 00053 00054 // =============== definitions =========================================== 00055 template <class T> 00056 CVector<T>::CVector () 00057 // --------------------------------------------------------------------------- 00058 // Function: default ctor 00059 // Input: none 00060 // Output: none 00061 // --------------------------------------------------------------------------- 00062 { 00063 m_pCells = NULL; 00064 m_nRows = 0; 00065 } 00066 00067 template <class T> 00068 CVector<T>::CVector (int n) 00069 // --------------------------------------------------------------------------- 00070 // Function: overloaded ctor 00071 // Input: size of the vector 00072 // Output: none 00073 // --------------------------------------------------------------------------- 00074 { 00075 m_pCells = NULL; 00076 m_nRows = 0; 00077 SetSize (n); 00078 } 00079 00080 template <class T> 00081 CVector<T>::CVector (const char* szName) 00082 // --------------------------------------------------------------------------- 00083 // Function: overloaded ctor 00084 // Input: vector name 00085 // Output: none 00086 // --------------------------------------------------------------------------- 00087 { 00088 m_pCells = NULL; 00089 m_nRows = 0; 00090 m_szName = szName; 00091 } 00092 00093 template <class T> 00094 CVector<T>::CVector (const char* szName, int n) 00095 // --------------------------------------------------------------------------- 00096 // Function: overloaded ctor 00097 // Input: vector name and size 00098 // Output: none 00099 // --------------------------------------------------------------------------- 00100 { 00101 m_pCells = NULL; 00102 m_nRows = 0; 00103 m_szName = szName; 00104 SetSize (n); 00105 } 00106 00107 template <class T> 00108 CVector<T>::CVector (const CVector<T>& A) 00109 // --------------------------------------------------------------------------- 00110 // Function: copy ctor 00111 // Input: vector 00112 // Output: none 00113 // --------------------------------------------------------------------------- 00114 { 00115 m_pCells = NULL; 00116 m_nRows = A.GetSize(); 00117 m_szName = A.m_szName; 00118 SetSize (m_nRows); 00119 for (int i=1; i <= m_nRows; i++) 00120 m_pCells[i] = A.m_pCells[i]; 00121 } 00122 00123 template <class T> 00124 void CVector<T>::SetSize (int nR) 00125 // --------------------------------------------------------------------------- 00126 // Function: dynamically allocates memory 00127 // Input: vector size 00128 // Output: none 00129 // --------------------------------------------------------------------------- 00130 { 00131 // check whether NR is legal 00132 if (nR <= 0) ErrorHandler (3); 00133 Release (); 00134 try {m_pCells = new T [nR + 1];} 00135 catch (std::bad_alloc) {ErrorHandler (1);} 00136 m_nRows = nR; 00137 #ifdef __RGGMETER 00138 m_dAllocated += static_cast<double>(sizeof(T)*(nR+1)); 00139 #endif 00140 } 00141 00142 template <class T> 00143 CVector<T>::~CVector () 00144 // --------------------------------------------------------------------------- 00145 // Function: dtor 00146 // Input: none 00147 // Output: none 00148 // --------------------------------------------------------------------------- 00149 { 00150 // deallocate storage 00151 Release (); 00152 } 00153 00154 template <class T> 00155 void CVector<T>::Release () 00156 // --------------------------------------------------------------------------- 00157 // Function: dynamically deallocates memory 00158 // Input: none 00159 // Output: none 00160 // --------------------------------------------------------------------------- 00161 { 00162 // deallocate storage 00163 if (m_pCells != NULL) 00164 { 00165 delete [] m_pCells; 00166 #ifdef __RGGMETER 00167 m_dDeAllocated += static_cast<double>(sizeof(T)*(m_nRows+1)); 00168 #endif 00169 m_pCells = NULL; 00170 m_nRows = 0; 00171 } 00172 } 00173 00174 // =============== member functions =========================================== 00175 template <class T> 00176 void CVector<T>::SetName (const std::string& szName) 00177 // --------------------------------------------------------------------------- 00178 // Function: sets the name of the vector 00179 // Input: vector name 00180 // Output: none 00181 // --------------------------------------------------------------------------- 00182 { 00183 m_szName = szName; 00184 } 00185 00186 template <class T> 00187 void CVector<T>::GetName (std::string& szName) const 00188 // --------------------------------------------------------------------------- 00189 // Function: gets the name of the vector 00190 // Input: string to hold vector name 00191 // Output: vector name 00192 // --------------------------------------------------------------------------- 00193 { 00194 szName = m_szName; 00195 } 00196 00197 template <class T> 00198 void CVector<T>::Set (T dV) 00199 // --------------------------------------------------------------------------- 00200 // Function: sets the value of all the elements in the vector to the 00201 // specified value 00202 // Input: specified value 00203 // Output: none 00204 // --------------------------------------------------------------------------- 00205 { 00206 for (int i=1; i <= m_nRows; i++) 00207 m_pCells[i] = dV; 00208 } 00209 00210 template <class T> 00211 int CVector<T>::GetSize () const 00212 // --------------------------------------------------------------------------- 00213 // Function: gets the size of the vector 00214 // Input: none 00215 // Output: returns the size 00216 // --------------------------------------------------------------------------- 00217 { 00218 return m_nRows; 00219 } 00220 00221 // ==================== Overloaded Operators ======================== 00222 #ifdef _DEBUG 00223 template <class T> 00224 T& CVector<T>::operator() (int nR) // T& is reference 00225 // --------------------------------------------------------------------------- 00226 // Function: overloaded () operator to access vector contents 00227 // carries out bound checking 00228 // Input: index or location 00229 // Output: value at the specified index 00230 // --------------------------------------------------------------------------- 00231 { 00232 // row-column reference in bounds? 00233 if (nR <= 0 || nR > m_nRows) 00234 { 00235 ErrorHandler (2,nR); 00236 return m_pCells[1]; 00237 } 00238 else 00239 return m_pCells[nR]; 00240 } 00241 #else 00242 template <class T> 00243 inline T& CVector<T>::operator() (int nR) 00244 // --------------------------------------------------------------------------- 00245 // Function: overloaded () operator to access vector contents 00246 // Input: index or location 00247 // Output: value at the specified index 00248 // --------------------------------------------------------------------------- 00249 { 00250 return m_pCells[nR]; 00251 } 00252 #endif 00253 00254 #ifdef _DEBUG 00255 template <class T> 00256 const T& CVector<T>::operator() (int nR) const 00257 // --------------------------------------------------------------------------- 00258 // Function: overloaded () operator to access vector contents 00259 // Input: index or location 00260 // Output: value at the specified index 00261 // --------------------------------------------------------------------------- 00262 { 00263 // row-column reference in bounds? 00264 if (nR <= 0 || nR > m_nRows) { 00265 ErrorHandler (2,nR); 00266 return m_pCells[1]; 00267 } 00268 else 00269 return m_pCells[nR]; 00270 } 00271 #else 00272 template <class T> 00273 inline const T& CVector<T>::operator() (int nR) const 00274 // --------------------------------------------------------------------------- 00275 // Function: overloaded () operator to access vector contents 00276 // Input: index or location 00277 // Output: value at the specified index 00278 // --------------------------------------------------------------------------- 00279 { 00280 return m_pCells[nR]; 00281 } 00282 #endif 00283 00284 template <class T> 00285 CVector<T>& CVector<T>::operator= (const CVector& matarg) 00286 // --------------------------------------------------------------------------- 00287 // Function: overloaded = operator 00288 // Input: vector to use as rvalue 00289 // Output: modified values 00290 // --------------------------------------------------------------------------- 00291 { 00292 // check whether vector is assigned to itself 00293 if (this != &matarg) 00294 { 00295 // compatible vectors? 00296 if (m_nRows != matarg.m_nRows) 00297 { 00298 ErrorHandler (4); 00299 return *this; 00300 } 00301 // now copy 00302 for (int i=1; i <= matarg.m_nRows; i++) 00303 m_pCells[i] = matarg.m_pCells[i]; 00304 } 00305 00306 return *this; 00307 } 00308 00309 // ==================== Error Handler ======================== 00310 template <class T> 00311 void CVector<T>::ErrorHandler (int nErrorCode, int nR) const 00312 // --------------------------------------------------------------------------- 00313 // Function: channels error message via std:err 00314 // Input: error code and optional int value 00315 // Output: none 00316 // --------------------------------------------------------------------------- 00317 { 00318 std::cerr << "CVector::Vector Name: " << m_szName << ".\n"; 00319 switch (nErrorCode) 00320 { 00321 case 1: 00322 std::cerr << "Memory allocation failure.\n"; 00323 break; 00324 case 2: 00325 std::cerr << "Row index is out of bounds.\n"; 00326 break; 00327 case 3: 00328 std::cerr << "Constructor. Invalid number of rows " 00329 "or columns.\n"; 00330 break; 00331 case 4: 00332 std::cerr << "Constructor. Incompatible vectors.\n"; 00333 break; 00334 } 00335 std::cerr << "Unable to populate vectors." << std::endl; 00336 exit (1); 00337 } 00338 00339 #endif