00001 /// @addtogroup struct 00002 /// @{ 00003 00004 ///////////////////////////////////////////////////////////////////////////// 00005 /// 00006 /// @file flatmatr.h 00007 /// 00008 /// This file contains the definition of a simple matrix class which is 00009 /// stored in a single vector, but its elements can be accessed in the 00010 /// double indexing style, e.g., M[0][2]. 00011 /// 00012 /// @author Pawel Pilarczyk 00013 /// 00014 ///////////////////////////////////////////////////////////////////////////// 00015 00016 // Copyright (C) 1997-2013 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 00049 /// This class defines a simple data structure for a flat 2-dim square matrix 00050 /// whose entries are stored in a single array. Additional classes for a row 00051 /// and a constant row are defined within this class which allow to use the 00052 /// usual double indexing to get to the entries of the matrix, both for 00053 /// reading only and for modifying the entries, e.g., M[0][1]. 00054 template <class element> 00055 class flatMatrix 00056 { 00057 public: 00058 /// The main constructor. The size of the matrix 00059 /// (the number of rows and collumns) must be given 00060 /// at initialization. 00061 flatMatrix (int size): n (size), tab (new element [n * n]) {return;} 00062 00063 /// The copy constructor which copies all the entries of the matrix. 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 00073 /// The assignment operator. It is permitted only for matrices 00074 /// of the same size. It copies all the entries of the matrix. 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 00085 /// The destructor which deallocates the memory. 00086 ~flatMatrix () {delete [] tab;} 00087 00088 /// The class that represents a single row of the matrix. 00089 class row 00090 { 00091 public: 00092 /// The constructor of a row of the matrix. 00093 row (int _offset, element *_v): 00094 offset (_offset), v (_v) {} 00095 00096 /// Returns a reference to the element at the given position. 00097 element &operator [] (int j) {return v [offset + j];} 00098 00099 protected: 00100 /// The offset in the vector of all the entries 00101 /// of the matrix. 00102 int offset; 00103 00104 /// A reference to the vector that stores all the entries 00105 /// of the matrix. 00106 element *v; 00107 }; 00108 00109 /// Returns a row of the matrix. 00110 row operator [] (int i) 00111 {return row (n * i, tab);} 00112 00113 /// The class that represents a constant single row of the matrix. 00114 class const_row 00115 { 00116 public: 00117 /// The constructor of a constant row of the matrix. 00118 const_row (int _offset, const element *_v): 00119 offset (_offset), v (_v) {} 00120 00121 /// Returns a reference to the element at the given position. 00122 const element &operator [] (int m) {return v [offset + m];} 00123 00124 protected: 00125 /// The offset in the vector of all the entries 00126 /// of the matrix. 00127 int offset; 00128 00129 /// A reference to the vector that stores all the entries 00130 /// of the matrix. 00131 const element *v; 00132 }; 00133 00134 /// Returns a constant row of the matrix. 00135 const_row operator [] (int i ) const 00136 {return const_row (n * i, tab);} 00137 00138 /// Clears all the entries of the matrix with the provided value. 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 00147 /// Swaps the memory of two flat matrices. 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 00159 /// Returns the address of the memory buffer with the elements 00160 /// of the matrix for reading only. 00161 const element *memory () const {return tab;} 00162 00163 /// Returns the address of the memory buffer with the elements 00164 /// of the matrix for reading and writing. 00165 element *memory () {return tab;} 00166 00167 protected: 00168 /// The size of the matrix. 00169 int n; 00170 00171 /// The array of elements. 00172 element *tab; 00173 00174 }; /* class flatMatrix */ 00175 00176 00177 } // namespace homology 00178 } // namespace chomp 00179 00180 #endif // _CHOMP_STRUCT_FLATMATR_H_ 00181 00182 /// @} 00183