00001 #ifndef __TMatrix__
00002 #define __TMatrix__
00003
00004
00005
00006 #include "../Basics/CCountedObject.hpp"
00007 using Exponent::Basics::CCountedObject;
00008
00009
00010 namespace Exponent
00011 {
00012 namespace Collections
00013 {
00031 template<class TypeName> class TMatrix : public CCountedObject
00032 {
00034 EXPONENT_CLASS_DECLARATION;
00037
00038
00039 public:
00040
00041
00042
00048 TMatrix(const long d1, const long d2);
00049
00053 TMatrix();
00054
00058 ~TMatrix();
00059
00060
00061
00067 TMatrix &operator = (const TMatrix<TypeName> &other);
00068
00074 TMatrix &operator = (const TypeName &element);
00075
00080 TypeName *operator [] (const long index) { return m_pointers[index]; }
00081
00082
00083
00089 void initialise(const long d1, const long d2);
00090
00095 void setAllElementsTo(const TypeName &element);
00096
00103 TypeName *elementAtIndex(const long d1, const long d2);
00104
00111 void addElementAtIndex(const long d1, const long d2, const TypeName &object);
00112
00113
00114
00120 bool isElementInArray(const TypeName &object);
00121
00126 long getDimension1Size() const { return m_dimension1; }
00127
00132 long getDimension2Size() const { return m_dimension2; }
00133
00137 void clearArray();
00138
00143 const TypeName **getInternalBuffer() const { return m_pointers; }
00144
00149 TypeName **getMutableInternalBuffer() { return m_pointers; }
00150
00151
00152
00153 protected:
00154
00155
00156
00160 void freePointers();
00161
00162
00163
00164 TypeName **m_pointers;
00165 long m_dimension1;
00166 long m_dimension2;
00168
00169
00170 };
00171
00176 EXPONENT_TEMPLATE_CLASS_IMPLEMENTATION(TMatrix<TypeName>, TypeName, CCountedObject);
00177
00178
00179 template<class TypeName> TMatrix<TypeName>::TMatrix(const long d1, const long d2) : m_pointers(NULL), m_dimension1(0), m_dimension2(0)
00180 {
00181 EXPONENT_CLASS_CONSTRUCTION(TMatrix<TypeName>);
00182 NULL_POINTER(m_pointers);
00183 this->initialise(d1, d2);
00184 }
00185
00186
00187 template<class TypeName> TMatrix<TypeName>::TMatrix() : m_pointers(NULL), m_dimension1(0), m_dimension2(0)
00188 {
00189 EXPONENT_CLASS_CONSTRUCTION(TMatrix<TypeName>);
00190 NULL_POINTER(m_pointers);
00191 m_dimension1 = 0;
00192 m_dimension2 = 0;
00193 }
00194
00195
00196 template<class TypeName> TMatrix<TypeName>::~TMatrix()
00197 {
00198 EXPONENT_CLASS_DESTRUCTION(TMatrix<TypeName>);
00199 this->freePointers();
00200 }
00201
00202
00203 template<class TypeName> TMatrix<TypeName> &TMatrix<TypeName>::operator = (const TMatrix<TypeName> &other)
00204 {
00205 if (&other != this)
00206 {
00207
00208 this->freePointers();
00209
00210
00211 m_dimension1 = other.m_dimension1;
00212 m_dimension2 = other.m_dimension2;
00213
00214
00215 m_pointers = new TypeName*[m_dimension1];
00216
00217
00218 for (long i = 0; i < m_dimension1; i++)
00219 {
00220
00221 m_pointers[i] = new TypeName[m_dimension2];
00222
00223
00224 memcpy(m_pointers[i], other.m_pointers[i], m_dimension2 * sizeof(TypeName));
00225 }
00226 }
00227
00228 return *this;
00229 }
00230
00231
00232 template<class TypeName> TMatrix<TypeName> &TMatrix<TypeName>::operator = (const TypeName &element)
00233 {
00234 this->setAllElementsTo(element);
00235 return *this;
00236 }
00237
00238
00239 template<class TypeName> void TMatrix<TypeName>::initialise(const long d1, const long d2)
00240 {
00241
00242 this->freePointers();
00243
00244
00245 m_dimension1 = d1;
00246 m_dimension2 = d2;
00247
00248
00249 m_pointers = new TypeName*[m_dimension1];
00250
00251
00252 for (long i = 0; i < m_dimension1; i++)
00253 {
00254 m_pointers[i] = new TypeName[m_dimension2];
00255 }
00256 }
00257
00258
00259 template<class TypeName> void TMatrix<TypeName>::setAllElementsTo(const TypeName &element)
00260 {
00261 for (long i = 0; i < m_dimension1; i++)
00262 {
00263 for (long j = 0; j < m_dimension2; j++)
00264 {
00265 m_pointers[i][j] = element;
00266 }
00267 }
00268 }
00269
00270
00271 template<class TypeName> TypeName *TMatrix<TypeName>::elementAtIndex(const long d1, const long d2)
00272 {
00273 if (m_pointers && (d1 >= 0 && d1 < m_dimension1) && (d2 >= 0 && d2 < m_dimension2))
00274 {
00275 return &m_pointers[d1][d2];
00276 }
00277 return NULL;
00278 }
00279
00280
00281 template<class TypeName> void TMatrix<TypeName>::addElementAtIndex(const long d1, const long d2, const TypeName &object)
00282 {
00283 if (m_pointers && (d1 >= 0 && d1 < m_dimension1) && (d2 >= 0 && d2 < m_dimension2))
00284 {
00285 m_pointers[d1][d2] = object;
00286 }
00287 }
00288
00289
00290 template<class TypeName> void TMatrix<TypeName>::clearArray()
00291 {
00292 this->freePointers();
00293 }
00294
00295
00296 template<class TypeName> void TMatrix<TypeName>::freePointers()
00297 {
00298 if (m_pointers)
00299 {
00300 for (long i = 0; i < m_dimension1; i++)
00301 {
00302 FREE_ARRAY_POINTER(m_pointers[i]);
00303 }
00304 FREE_ARRAY_POINTER(m_pointers);
00305 m_dimension1 = 0;
00306 m_dimension2 = 0;
00307 }
00308 }
00309
00311 }
00312 }
00313 #endif // End of TMatrix.hpp