sprint 1-alpha
sprint/math/dynamic_matrix.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::matrix
00003 *
00004 *   Copyright (C) 2004-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 #ifndef _DYNAMIC_MATRIX_H
00022 #define _DYNAMIC_MATRIX_H
00023 
00024 #include "matrix_common.h"
00025 
00029 namespace sprint {
00030 
00032 template<class T>
00033 class dmatrix {
00035         unsigned int m_nr;
00037         unsigned int m_nc;
00039         T * m_data;
00040         
00041         private:
00043         void alloc()
00044         {
00045                 m_data = new T[m_nr * m_nc];
00046         }
00048         void release()
00049                 {
00050                         delete [] m_data;
00051                         m_data = NULL;
00052                 }
00053         
00054         public:
00055                 
00056                 typedef T data_type;
00057                 
00058     public:
00060         dmatrix() : m_nr(0), m_nc(), m_data(0) { }
00061                 dmatrix(unsigned int nr, unsigned int nc) :m_nr(nr), m_nc(nc) { alloc(); }
00062                 ~dmatrix() { release(); }
00063                 
00064                 void resize(unsigned int nr, unsigned int nc, const T def = T())
00065                         {
00066                 T *nb = new T[nr * nc];
00067 
00068                 for(int j=0;j<nr; ++j)                
00069                  for(int i=0;i<nc; ++i)
00070                  {
00071                  nb[i + j * nc] = (i<m_nc && j <m_nr) ? m_data[i + j * m_nc] : def;
00072                  }
00073                 
00074                         delete [] m_data;
00075                         m_data = NULL;
00076                                 m_nc = nc;
00077                                 m_nr = nr;
00078                                 m_data = nb;
00079 
00080                         }
00081                 
00082                 inline unsigned int rows() const { return m_nr; }
00083                 inline unsigned int cols() const { return m_nc; }
00084                                 
00085                 inline T & operator() (unsigned int r, unsigned int c) { return m_data[r * m_nc + c]; }
00086                 inline const T & operator()  (unsigned int r, unsigned int c) const { return m_data[r * m_nc + c]; }
00087                 
00088                 inline T * values() { return m_data; }
00089                 inline const T * values() const { return m_data; }              
00090 };
00091 
00093 typedef dmatrix<float> dmatrixf;
00094 
00096 typedef dmatrix<double> dmatrixd;
00097 
00098 }
00099 /*
00100 namespace std {
00101           
00102 template<class T>          
00103 void swap(dmatrix<T> & a, dmatrix<T> & b)
00104 {
00105 std::swap(a.m_nr, b.m_nr);
00106 std::swap(a.m_nr, b.m_nr);
00107 }
00108           
00109 };
00110 */
00111 
00112 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines