Back to index

fet  5.18.0
matrix.h
Go to the documentation of this file.
00001 //
00002 //
00003 // Description: This file is part of FET
00004 //
00005 //
00006 // Author: Liviu Lalescu <Please see http://lalescu.ro/liviu/ for details about contacting Liviu Lalescu (in particular, you can find here the e-mail address)>
00007 // Copyright (C) 2009 Liviu Lalescu <http://lalescu.ro/liviu/>
00008 //
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 //REFERENCES:
00019 //
00020 //            Hints from Ted Jensen's Tutorial on Pointers and Arrays in C -
00021 //                   - Chapter 9: Pointers and Dynamic Allocation of Memory - improvement so that the elements of a matrix are in a contiguous memory location.
00022 //
00023 //            Hints from C++ FAQ LITE by Marshall Cline -
00024 //                   - Section [13] - Operator overloading, article [13.12] - advice about the () operator for matrices.
00025 //
00026 //            You may find more information on the FET documentation web page, http://lalescu.ro/liviu/fet/doc/
00027 
00028 #ifndef MATRIX_H
00029 #define MATRIX_H
00030 
00031 #include "timetable_defs.h"
00032 
00033 template <typename T>
00034 class Matrix3D
00035 {
00036 private:
00037        int d1;
00038        int d2;
00039        int d3;
00040        
00041        T*** a;
00042        T* content;
00043        
00044 public:
00045        Matrix3D();
00046        ~Matrix3D();
00047 
00048        void clear();
00049        void resize(int _d1, int _d2, int _d3);
00050        T** operator[](int i);
00051        T& operator()(int i, int j, int k);
00052 };
00053 
00054 template <typename T>
00055 class Matrix2D
00056 {
00057 private:
00058        int d1;
00059        int d2;
00060        
00061        T** a;
00062        T* content;
00063        
00064 public:
00065        Matrix2D();
00066        ~Matrix2D();
00067 
00068        void clear();
00069        void resize(int _d1, int _d2);
00070        T* operator[](int i);
00071        T& operator()(int i, int j);
00072 };
00073 
00074 template <typename T>
00075 class Matrix1D
00076 {
00077 private:
00078        int d1;
00079        
00080        T* a;
00081        
00082 public:
00083        Matrix1D();
00084        ~Matrix1D();
00085 
00086        void clear();
00087        void resize(int _d1);
00088        T& operator[](int i);
00089        //T& operator()(int i);
00090 };
00091 
00092 
00093 template <typename T> Matrix3D<T>::Matrix3D()
00094 {
00095        d1=d2=d3=-1;
00096 }
00097 
00098 template <typename T> Matrix3D<T>::~Matrix3D()
00099 {
00100        this->clear();
00101 }
00102 
00103 template <typename T> void Matrix3D<T>::clear()
00104 {
00105        if(d1>=0 || d2>=0 || d3>=0){
00106               assert(d1>0 && d2>0 && d3>0);
00107               
00108               for(int i=0; i<d1; i++)
00109                      delete[] a[i];
00110               delete[]a;
00111               
00112               delete[] content;
00113        }
00114        d1=d2=d3=-1;
00115 }
00116 
00117 template <typename T> void Matrix3D<T>::resize(int _d1, int _d2, int _d3)
00118 {
00119        if(_d1<=0 || _d2<=0 || _d3<=0){
00120               this->clear();
00121               return;
00122        }
00123        
00124        if(d1!=_d1 || d2!=_d2 || d3!=_d3){
00125               this->clear();
00126               
00127               d1=_d1;
00128               d2=_d2;
00129               d3=_d3;
00130 
00131               content=new T[d1*d2*d3];
00132               a=new T**[d1];
00133               for(int i=0; i<d1; i++){
00134                      a[i]=new T*[d2];
00135                      for(int j=0; j<d2; j++)
00136                             a[i][j]=content+i*d2*d3+j*d3;
00137               }
00138        }
00139 }
00140 
00141 template <typename T> inline T** Matrix3D<T>::operator[](int i)
00142 {
00143        return a[i];
00144 }
00145 
00146 template <typename T> inline T& Matrix3D<T>::operator()(int i, int j, int k)
00147 {
00148        //return content[i*d2*d3+j*d3+k];
00149        return content[(i*d2+j)*d3+k];
00150 }
00151 
00152 
00153 template <typename T> Matrix2D<T>::Matrix2D()
00154 {
00155        d1=d2=-1;
00156 }
00157 
00158 template <typename T> Matrix2D<T>::~Matrix2D()
00159 {
00160        this->clear();
00161 }
00162 
00163 template <typename T> void Matrix2D<T>::clear()
00164 {
00165        if(d1>=0 || d2>=0){
00166               assert(d1>0 && d2>0);
00167               
00168               delete[] a;
00169               
00170               delete[] content;
00171        }
00172        d1=d2=-1;
00173 }
00174 
00175 template <typename T> void Matrix2D<T>::resize(int _d1, int _d2)
00176 {
00177        if(_d1<=0 || _d2<=0){
00178               this->clear();
00179               return;
00180        }
00181 
00182        if(d1!=_d1 || d2!=_d2){
00183               this->clear();
00184 
00185               d1=_d1;
00186               d2=_d2;
00187 
00188               content=new T[d1*d2];
00189               a=new T*[d1];
00190               for(int i=0; i<d1; i++)
00191                      a[i]=content+i*d2;
00192        }
00193 }
00194 
00195 template <typename T> inline T* Matrix2D<T>::operator[](int i)
00196 {
00197        return a[i];
00198 }
00199 
00200 template <typename T> inline T& Matrix2D<T>::operator()(int i, int j)
00201 {
00202        return content[i*d2+j];
00203 }
00204 
00205 
00206 template <typename T> Matrix1D<T>::Matrix1D()
00207 {
00208        d1=-1;
00209 }
00210 
00211 template <typename T> Matrix1D<T>::~Matrix1D()
00212 {
00213        this->clear();
00214 }
00215 
00216 template <typename T> void Matrix1D<T>::clear()
00217 {
00218        if(d1>=0){
00219               assert(d1>0);
00220               delete[] a;
00221        }
00222        d1=-1;
00223 }
00224 
00225 template <typename T> void Matrix1D<T>::resize(int _d1)
00226 {
00227        if(_d1<=0){
00228               this->clear();
00229               return;
00230        }
00231 
00232        if(d1!=_d1){
00233               this->clear();
00234 
00235               d1=_d1;
00236 
00237               a=new T[d1];
00238        }
00239 }
00240 
00241 template <typename T> inline T& Matrix1D<T>::operator[](int i)
00242 {
00243        return a[i];
00244 }
00245 
00246 /*template <typename T> inline T& Matrix1D<T>::operator()(int i)
00247 {
00248        return a[i];
00249 }*/
00250 
00251 #endif