core/Matrix.hpp
Go to the documentation of this file.
00001 /*
00002   This file is part of OpenFLUID software
00003   Copyright (c) 2007-2010 INRA-Montpellier SupAgro
00004 
00005 
00006  == GNU General Public License Usage ==
00007 
00008   OpenFLUID is free software: you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation, either version 3 of the License, or
00011   (at your option) any later version.
00012 
00013   OpenFLUID is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017 
00018   You should have received a copy of the GNU General Public License
00019   along with OpenFLUID.  If not, see <http://www.gnu.org/licenses/>.
00020 
00021   In addition, as a special exception, INRA gives You the additional right
00022   to dynamically link the code of OpenFLUID with code not covered
00023   under the GNU General Public License ("Non-GPL Code") and to distribute
00024   linked combinations including the two, subject to the limitations in this
00025   paragraph. Non-GPL Code permitted under this exception must only link to
00026   the code of OpenFLUID dynamically through the OpenFLUID libraries
00027   interfaces, and only for building OpenFLUID plugins. The files of
00028   Non-GPL Code may be link to the OpenFLUID libraries without causing the
00029   resulting work to be covered by the GNU General Public License. You must
00030   obey the GNU General Public License in all respects for all of the
00031   OpenFLUID code and other code used in conjunction with OpenFLUID
00032   except the Non-GPL Code covered by this exception. If you modify
00033   this OpenFLUID, you may extend this exception to your version of the file,
00034   but you are not obligated to do so. If you do not wish to provide this
00035   exception without modification, you must delete this exception statement
00036   from your version and license this OpenFLUID solely under the GPL without
00037   exception.
00038 
00039 
00040  == Other Usage ==
00041 
00042   Other Usage means a use of OpenFLUID that is inconsistent with the GPL
00043   license, and requires a written agreement between You and INRA.
00044   Licensees for Other Usage of OpenFLUID may use this file in accordance
00045   with the terms contained in the written agreement between You and INRA.
00046 */
00047 
00048 
00057 #ifndef __MATRIX_HPP___
00058 #define __MATRIX_HPP___
00059 
00060 
00061 
00062 #include <iostream>
00063 #include <openfluid/dllexport.hpp>
00064 #include <openfluid/base/OFException.hpp>
00065 #include <boost/multi_array.hpp>
00066 
00067 namespace openfluid { namespace core {
00068 
00069 
00073 template <class T>
00074 class DLLEXPORT Matrix
00075 {
00076   protected :
00077 
00078     boost::multi_array<T, 2> m_Data;
00079 
00080     unsigned long m_ColsNbr;
00081 
00082     unsigned long m_RowsNbr;
00083 
00084 
00085   public :
00086 
00090     Matrix();
00091 
00095     Matrix(const Matrix &Matrix);
00096 
00100     Matrix(unsigned long ColsNbr,unsigned long RowsNbr);
00101 
00105     Matrix(unsigned long ColsNbr,unsigned long RowsNbr, T InitValue);
00106 
00110     virtual ~Matrix() {};
00111 
00116     inline unsigned long getColsNbr() const { return m_ColsNbr; };
00117 
00122     inline unsigned long getRowsNbr() const { return m_RowsNbr; };
00123 
00128     inline unsigned long getSize() const { return (m_ColsNbr * m_RowsNbr); };
00129 
00133     inline unsigned long size() const { return (m_ColsNbr * m_RowsNbr); };
00134 
00135 
00139     T* getData() const { return (T*)(m_Data.data()); };
00140 
00144     void setData(T* Data);
00145 
00149     T getElement(unsigned long ColIndex, unsigned long RowIndex) const;
00150 
00154     inline T at(unsigned long ColIndex, unsigned long RowIndex) const { return getElement(ColIndex,RowIndex); };
00155 
00159     inline T get(unsigned long ColIndex, unsigned long RowIndex) const { return getElement(ColIndex,RowIndex); };
00160 
00161 
00165     void setElement(unsigned long ColIndex, unsigned long RowIndex, T Element);
00166 
00170     inline void set(unsigned long ColIndex, unsigned long RowIndex, T Element) { setElement(ColIndex,RowIndex,Element); };
00171 
00175     Matrix<T>& operator = (const Matrix &A);
00176 
00180     void fill(const T& Val);
00181 
00185     void clear();
00186 
00187 };
00188 
00189 // =====================================================================
00190 // =====================================================================
00191 
00192 template <class T>
00193 Matrix<T>::Matrix():
00194   m_Data(boost::extents[0][0],boost::fortran_storage_order()),
00195   m_ColsNbr(0),m_RowsNbr(0)
00196 {
00197 }
00198 
00199 
00200 // =====================================================================
00201 // =====================================================================
00202 
00203 template <class T>
00204 Matrix<T>::Matrix(const Matrix &A):
00205   m_Data(boost::extents[A.m_ColsNbr][A.m_RowsNbr],boost::fortran_storage_order()),
00206   m_ColsNbr(A.m_ColsNbr),m_RowsNbr(A.m_RowsNbr)
00207 {
00208   m_Data = A.m_Data;
00209 }
00210 
00211 
00212 // =====================================================================
00213 // =====================================================================
00214 
00215 template <class T>
00216 Matrix<T>::Matrix(unsigned long ColsNbr, unsigned long RowsNbr) :
00217   m_Data(boost::extents[ColsNbr][RowsNbr],boost::fortran_storage_order()),
00218   m_ColsNbr(ColsNbr),m_RowsNbr(RowsNbr)
00219 {
00220   fill(0);
00221 }
00222 
00223 
00224 // =====================================================================
00225 // =====================================================================
00226 
00227 
00228 template <class T>
00229 Matrix<T>::Matrix(unsigned long ColsNbr, unsigned long RowsNbr, T InitValue) :
00230   m_Data(boost::extents[ColsNbr][RowsNbr],boost::fortran_storage_order()),
00231   m_ColsNbr(ColsNbr),m_RowsNbr(RowsNbr)
00232 {
00233   fill(InitValue);
00234 }
00235 
00236 
00237 // =====================================================================
00238 // =====================================================================
00239 
00240 
00241 template <class T>
00242 void Matrix<T>::setData(T* Data)
00243 {
00244   for (unsigned long j=0; j < m_RowsNbr;j++)
00245   {
00246     for (unsigned long i=0; i < m_ColsNbr;i++)
00247     {
00248       m_Data[i][j] = Data[i+(j*m_ColsNbr)];
00249     }
00250   }
00251 }
00252 
00253 
00254 // =====================================================================
00255 // =====================================================================
00256 
00257 
00258 template <class T>
00259 T Matrix<T>::getElement(unsigned long ColIndex, unsigned long RowIndex) const
00260 {
00261   if (ColIndex >= m_ColsNbr || RowIndex >= m_RowsNbr) throw openfluid::base::OFException("OpenFLUID framework","Matrix::getElement","element access range error");
00262   return m_Data[ColIndex][RowIndex];
00263 }
00264 
00265 
00266 // =====================================================================
00267 // =====================================================================
00268 
00269 
00270 template <class T>
00271 void Matrix<T>::setElement(unsigned long ColIndex, unsigned long RowIndex, T Element)
00272 {
00273   if (ColIndex >= m_ColsNbr || RowIndex >= m_RowsNbr) throw openfluid::base::OFException("OpenFLUID framework","Matrix::setElement","element access range error");
00274   m_Data[ColIndex][RowIndex] = Element;
00275 }
00276 
00277 
00278 
00279 // =====================================================================
00280 // =====================================================================
00281 
00282 template <class T>
00283 Matrix<T>& Matrix<T>::operator=(const Matrix &A)
00284 {
00285 
00286   if (this == &A) return *this; // in case somebody tries assign array to itself
00287 
00288   m_Data.resize(boost::extents[A.m_ColsNbr][A.m_RowsNbr]);
00289   m_Data = A.m_Data;
00290   m_ColsNbr = A.m_ColsNbr;
00291   m_RowsNbr = A.m_RowsNbr;
00292 
00293   return *this;
00294 }
00295 
00296 
00297 // =====================================================================
00298 // =====================================================================
00299 
00300 
00301 template <class T>
00302 void Matrix<T>::fill(const T& Val)
00303 {
00304   for (unsigned long i=0;i<m_ColsNbr;i++)
00305     for (unsigned long j=0;j<m_RowsNbr;j++)
00306       m_Data[i][j] = Val;
00307 }
00308 
00309 
00310 // =====================================================================
00311 // =====================================================================
00312 
00313 template <class T>
00314 void Matrix<T>::clear()
00315 {
00316   m_Data.resize(boost::extents[0][0]);
00317   m_ColsNbr=0;
00318   m_RowsNbr=0;
00319 }
00320 
00321 
00322 
00323 } }
00324 
00325 #endif /* __MATRIX_HPP___ */

Generated using Doxygen 1.7.6.1
Creative Commons License Creative Commons By-NC-ND license