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-2007 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: July 8, 2007. 00034 00035 00036 #ifndef _FLATMATR_H_ 00037 #define _FLATMATR_H_ 00038 00039 #include <new> 00040 #include <vector> 00041 00042 namespace chomp { 00043 namespace homology { 00044 00045 00046 // -------------------------------------------------- 00047 // ------------------ FLAT MATRIX ------------------- 00048 // -------------------------------------------------- 00049 00050 /// This class defines a simple data structure for a flat 2-dim square matrix 00051 /// whose entries are stored in a single array. Additional classes for a row 00052 /// and a constant row are defined within this class which allow to use the 00053 /// usual double indexing to get to the entries of the matrix, both for 00054 /// reading only and for modifying the entries, e.g., M[0][1]. 00055 template <class element> 00056 class flatMatrix 00057 { 00058 public: 00059 /// The only allowed constructor. The size of the matrix 00060 /// (the number of rows and collumns) must be given 00061 /// at initialization. 00062 flatMatrix (int size): n (size), tab (n * n) {return;} 00063 00064 // the destructor 00065 // ~flatMatrix (); 00066 00067 /// The class that represents a single row of the matrix. 00068 class row 00069 { 00070 public: 00071 /// The constructor of a row of the matrix. 00072 row (int _offset, std::vector<element> &_v): 00073 offset (_offset), v (_v) {} 00074 00075 /// Returns a reference to the element at the given position. 00076 element &operator [] (int j) {return v [offset + j];} 00077 00078 protected: 00079 /// The offset in the vector of all the entries 00080 /// of the matrix. 00081 int offset; 00082 00083 /// A reference to the vector that stores all the entries 00084 /// of the matrix. 00085 std::vector<element> &v; 00086 }; 00087 00088 /// Returns a row of the matrix. 00089 row operator [] (int i) 00090 {return row (n * i, tab);} 00091 00092 /// The class that represents a constant single row of the matrix. 00093 class const_row 00094 { 00095 public: 00096 /// The constructor of a constant row of the matrix. 00097 const_row (int _offset, const std::vector<element> &_v): 00098 offset (_offset), v (_v) {} 00099 00100 /// Returns a reference to the element at the given position. 00101 const element &operator [] (int m) {return v [offset + m];} 00102 00103 protected: 00104 /// The offset in the vector of all the entries 00105 /// of the matrix. 00106 int offset; 00107 00108 /// A reference to the vector that stores all the entries 00109 /// of the matrix. 00110 const std::vector<element> &v; 00111 }; 00112 00113 /// Returns a constant row of the matrix. 00114 const_row operator [] (int i ) const 00115 {return const_row (n * i, tab);} 00116 00117 protected: 00118 /// The size of the matrix. 00119 int n; 00120 00121 /// The array of elements. 00122 std::vector<element> tab; 00123 00124 }; /* class flatMatrix */ 00125 00126 00127 } // namespace homology 00128 } // namespace chomp 00129 00130 #endif // _FLATMATR_H_ 00131 00132 /// @} 00133