Matrix.h
Go to the documentation of this file.
00001 #ifndef Matrix_H
00002 #define Matrix_H
00003 #include <sys/types.h>
00004 #include <assert.h>
00005 #include <iostream>
00006 #include "Vector.h"
00007 
00008 namespace puma2 {
00009 
00019 template <class T>
00020   class Matrix {
00025         typedef T ElemType;
00026   public:
00030     Matrix(const Matrix&);
00034     explicit Matrix(int =0, int =0);
00040     Matrix(int, int, Matrix*, int xo, int yo);
00044     virtual ~Matrix();
00048     void operator= (const Matrix&);
00052     void operator= (const T& v);
00056     int getWidth() const {return mXSize;}
00060     int getHeight() const {return mYSize;}
00061 #if 1
00062 
00065         void resize(int x, int y) ;
00071         void resize(int x, int y, Matrix* m, int xo, int yo) ;
00072 #else
00073 
00078         void resize(int x, int y, Matrix* m = 0, int xo = 0, int yo = 0) ;
00079 #endif
00080 
00083     inline Vector<T>& operator[] (int);
00087     inline const Vector<T>& operator[] (int) const;
00091     operator T**(){ return matrix; }
00095     operator const T**() const { return (const T**) matrix; }
00099     bool isSubMatrix() const { return master != NULL; }
00100   private:
00101     int mXSize;
00102     int mYSize;
00103     T ** matrix;
00104     Vector<T> ** vp;
00105     Matrix<T> * master;
00106     int rcount;
00107 };
00108 
00109 template <class T>
00110   inline Vector<T>& Matrix<T>::operator[] (int i){
00111     assert((i>=0) && (i<mYSize));
00112     return *vp[i];
00113 }
00114 
00115 
00116 #if 1
00117 // ????
00118 // should we unite the two resize functions to
00119 // void Matrix<T>::resize(int x, int y, Matrix* m = 0, int xo = 0, int yo = 0)
00120 // ????
00121 
00122 template <class T>
00123   void Matrix<T>::resize(int x, int y, Matrix* m, int xo, int yo)
00124 {
00125     if ((getWidth() != 0) || (getHeight() != 0))
00126       throw "cannot resize allocated matrix";
00127     mXSize = x;
00128     mYSize = y;
00129     master = m;
00130     m->rcount++;
00131     rcount = 0;
00132     T ** array = m->matrix;
00133     vp = new Vector<T>*[y];
00134     matrix = new T*[y];
00135     for (int i = 0; i < y; ++i) {
00136       T* cp = matrix[i] = & (array[i+yo][xo]);
00137       vp[i] = new Vector<T>(cp,x);
00138     }
00139 }
00140 
00141 template <class T>
00142   void Matrix<T>::resize(int x, int y)
00143 {
00144     if ((getWidth() == x) && (getHeight() == y))
00145       return;   // redundant call -- just ignore it.
00146     if ((getWidth() != 0) || (getHeight() != 0))
00147       throw "cannot resize allocated matrix";
00148     mXSize = x;
00149     mYSize = y;
00150     rcount = 0;
00151     master = NULL;
00152     T * array = new T[x*y];
00153     vp = new Vector<T>*[y];
00154     matrix = new T*[y];
00155     for (int i = 0; i < y; ++i) {
00156       T* cp = matrix[i] = & (array[i*x]);
00157       vp[i] = new Vector<T>(cp,x);
00158     }
00159 }
00160 
00161 #else
00162 
00163 // not correct, yet
00164 template <class T>
00165   void Matrix<T>::resize(int x, int y, Matrix* m, int xo, int yo)
00166 {
00167     if ((getWidth() == x) && (getHeight() == y))
00168       return;   // redundant call -- just ignore it.
00169     if ((getWidth() != 0) || (getHeight() != 0))
00170       throw "cannot resize allocated matrix";
00171     mXSize = x;
00172     mYSize = y;
00173     master = m;
00174     rcount = 0;
00175     T ** array;
00176         if (master == NULL) {
00177            assert(xo == 0);
00178            assert(yo == 0);
00179        array = new T[x*y];
00180         } else {
00181        m->rcount++;
00182        array = m->matrix;
00183     }
00184     vp = new Vector<T>*[y];
00185     matrix = new T*[y];
00186     for (int i = 0; i < y; ++i) {
00187       T* cp = matrix[i] = & (array[i+yo][xo]);
00188       vp[i] = new Vector<T>(cp,x);
00189     }
00190 }
00191 #endif
00192 
00193 template <class T>
00194   const Vector<T>& Matrix<T>::operator[](int i) const{
00195     assert((i>=0) && (i<mYSize));
00196     return *vp[i];
00197 }
00198 
00199 template <class T>
00200   Matrix<T> operator*(const Matrix<T>&, const Matrix<T>&);
00201 
00202 template <class T>
00203   Vector<T> operator*(const Matrix<T>&, const Vector<T>&);
00204 
00205 template <class T>
00206   Vector<T> operator*(const T&, const Vector<T>&);
00207 
00208 template <class T>
00209   T operator*(const Vector<T>&, const Vector<T>&);
00210 
00211 template <class T>
00212   Matrix<T>::Matrix(int x, int y){
00213     mXSize = x;
00214     mYSize = y;
00215     rcount = 0;
00216     master = NULL;
00217     T * array = new T[x*y];
00218     vp = new Vector<T>*[y];
00219     matrix = new T*[y];
00220     for (int i = 0; i < y; ++i) {
00221       T* cp = matrix[i] = & (array[i*x]);
00222       vp[i] = new Vector<T>(cp,x);
00223     }
00224 }
00225 
00226 template <class T>
00227   Matrix<T>::~Matrix(){
00228     if (rcount > 0) throw "try to delete referenced matrix";
00229     if (master == NULL) {
00230       if (mYSize>0) delete [] matrix[0];
00231     } else {
00232       master->rcount--;
00233     }
00234         // should also work for 0 x 0 matrices
00235 
00236     delete[] matrix;
00237     for (int i = 0; i < mYSize; ++i) delete vp[i];
00238     delete[] vp;
00239 }
00240 
00241 template <class T>
00242   Matrix<T>::Matrix(int x, int y, Matrix* m, int xo, int yo)
00243 {
00244     mXSize = x;
00245     mYSize = y;
00246     master = m;
00247     m->rcount++;
00248     rcount = 0;
00249     T ** array = m->matrix;
00250     vp = new Vector<T>*[y];
00251     matrix = new T*[y];
00252     for (int i = 0; i < y; ++i) {
00253       T* cp = matrix[i] = & (array[i+yo][xo]);
00254       vp[i] = new Vector<T>(cp,x);
00255     }
00256 }
00257 
00258 template <class T>
00259   Matrix<T>::Matrix(const Matrix& m)
00260 {
00261     u_int x = mXSize = m.mXSize;
00262     u_int y = mYSize = m.mYSize;
00263     master = NULL;
00264     rcount = 0;
00265     T * array = new T[x*y];
00266     matrix = new T*[y];
00267     vp = new Vector<T>*[y];
00268     for (u_int i = 0; i < y; ++i) {
00269       T* cp = matrix[i] = & (array[i*x]);
00270       vp[i] = new Vector<T>(cp,x);
00271       for (u_int j = 0; j < x; ++j)
00272         cp[j] = m.matrix[i][j];
00273         //Vector<T>::copy(m.matrix[i][j],cp[j]);
00274     }
00275 }
00276 
00277 template <class T>
00278   void Matrix<T>::operator= (const Matrix& m){
00279     //assert(mYSize == m.mYSize && mXSize == m.mXSize);
00280                 resize( m.mXSize, m.mYSize);
00281     for (int i = 0; i < mYSize; ++i){
00282       for (int j = 0; j < mXSize; ++j){
00283         matrix[i][j] = m.matrix[i][j];
00284       }
00285     }
00286 }
00287 
00288 }
00289 
00290 #endif


obj_rec_gui
Author(s): AGAS/agas@uni-koblenz.de
autogenerated on Mon Oct 6 2014 02:53:43