flatmatr.h

Go to the documentation of this file.
00001 
00002 
00003 
00015 
00016 // Copyright (C) 1997-2010 by Pawel Pilarczyk.
00017 //
00018 // This file is part of the Homology Library.  This library is free software;
00019 // you can redistribute it and/or modify it under the terms of the GNU
00020 // General Public License as published by the Free Software Foundation;
00021 // either version 2 of the License, or (at your option) any later version.
00022 //
00023 // This library is distributed in the hope that it will be useful,
00024 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026 // GNU General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License along
00029 // with this software; see the file "license.txt".  If not, write to the
00030 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00031 // MA 02111-1307, USA.
00032 
00033 // Started on August 25, 2006. Last revision: October 8, 2008.
00034 
00035 
00036 #ifndef _CHOMP_STRUCT_FLATMATR_H_
00037 #define _CHOMP_STRUCT_FLATMATR_H_
00038 
00039 #include <new>
00040 
00041 namespace chomp {
00042 namespace homology {
00043 
00044 
00045 // --------------------------------------------------
00046 // ------------------ FLAT MATRIX -------------------
00047 // --------------------------------------------------
00048 
00054 template <class element>
00055 class flatMatrix
00056 {
00057 public:
00061         flatMatrix (int size): n (size), tab (new element [n * n]) {return;}
00062 
00064         flatMatrix (const flatMatrix<element> &M):
00065                 n (M. n), tab (new element [n * n])
00066         {
00067                 int memSize = n * n;
00068                 for (int i = 0; i < memSize; ++ i)
00069                         tab [i] = M. tab [i];
00070                 return;
00071         }
00072 
00075         flatMatrix &operator = (const flatMatrix<element> &M)
00076         {
00077                 if (n != M. n)
00078                         throw "Different matrix sizes in operator =.";
00079                 int memSize = n * n;
00080                 for (int i = 0; i < memSize; ++ i)
00081                         tab [i] = M. tab [i];
00082                 return *this;
00083         }
00084 
00086         ~flatMatrix () {delete [] tab;}
00087 
00089         class row
00090         {
00091         public:
00093                 row (int _offset, element *_v):
00094                         offset (_offset), v (_v) {}
00095 
00097                 element &operator [] (int j) {return v [offset + j];}
00098 
00099         protected:
00102                 int offset;
00103 
00106                 element *v;
00107         };
00108 
00110         row operator [] (int i)
00111                 {return row (n * i, tab);}
00112 
00114         class const_row
00115         {
00116         public:
00118                 const_row (int _offset, const element *_v):
00119                         offset (_offset), v (_v) {}
00120 
00122                 const element &operator [] (int m) {return v [offset + m];}
00123 
00124         protected:
00127                 int offset;
00128 
00131                 const element *v;
00132         };
00133 
00135         const_row operator [] (int i ) const
00136                 {return const_row (n * i, tab);}
00137 
00139         void clear (const element &elem)
00140         {
00141                 int size = n * n;
00142                 for (int i = 0; i < size; ++ i)
00143                         tab [i] = elem;
00144                 return;
00145         }
00146 
00148         void swap (flatMatrix<element> &M)
00149         {
00150                 int this_n = n;
00151                 element *this_tab = tab;
00152                 n = M. n;
00153                 tab = M. tab;
00154                 M. n = this_n;
00155                 M. tab = this_tab;
00156                 return;
00157         }
00158 
00161         const element *memory () const {return tab;}
00162 
00165         element *memory () {return tab;}
00166 
00167 protected:
00169         int n;
00170 
00172         element *tab;
00173 
00174 }; /* class flatMatrix */
00175 
00176 
00177 } // namespace homology
00178 } // namespace chomp
00179 
00180 #endif // _CHOMP_STRUCT_FLATMATR_H_
00181 
00183