The Original CHomP Software
flatmatr.h
Go to the documentation of this file.
1
3
15
16// Copyright (C) 1997-2020 by Pawel Pilarczyk.
17//
18// This file is part of my research software package. This is free software:
19// you can redistribute it and/or modify it under the terms of the GNU
20// General Public License as published by the Free Software Foundation,
21// either version 3 of the License, or (at your option) any later version.
22//
23// This software is distributed in the hope that it will be useful,
24// but WITHOUT ANY WARRANTY; without even the implied warranty of
25// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26// GNU General Public License for more details.
27//
28// You should have received a copy of the GNU General Public License
29// along with this software; see the file "license.txt". If not,
30// please, see <https://www.gnu.org/licenses/>.
31
32// Started on August 25, 2006. Last revision: October 8, 2008.
33
34
35#ifndef _CHOMP_STRUCT_FLATMATR_H_
36#define _CHOMP_STRUCT_FLATMATR_H_
37
38#include <new>
39
40namespace chomp {
41namespace homology {
42
43
44// --------------------------------------------------
45// ------------------ FLAT MATRIX -------------------
46// --------------------------------------------------
47
53template <class element>
55{
56public:
60 flatMatrix (int size): n (size), tab (new element [n * n]) {return;}
61
64 n (M. n), tab (new element [n * n])
65 {
66 int memSize = n * n;
67 for (int i = 0; i < memSize; ++ i)
68 tab [i] = M. tab [i];
69 return;
70 }
71
75 {
76 if (n != M. n)
77 throw "Different matrix sizes in operator =.";
78 int memSize = n * n;
79 for (int i = 0; i < memSize; ++ i)
80 tab [i] = M. tab [i];
81 return *this;
82 }
83
85 ~flatMatrix () {delete [] tab;}
86
88 class row
89 {
90 public:
92 row (int _offset, element *_v):
93 offset (_offset), v (_v) {}
94
96 element &operator [] (int j) {return v [offset + j];}
97
98 protected:
102
105 element *v;
106 };
107
110 {return row (n * i, tab);}
111
114 {
115 public:
117 const_row (int _offset, const element *_v):
118 offset (_offset), v (_v) {}
119
121 const element &operator [] (int m) {return v [offset + m];}
122
123 protected:
127
130 const element *v;
131 };
132
135 {return const_row (n * i, tab);}
136
138 void clear (const element &elem)
139 {
140 int size = n * n;
141 for (int i = 0; i < size; ++ i)
142 tab [i] = elem;
143 return;
144 }
145
148 {
149 int this_n = n;
150 element *this_tab = tab;
151 n = M. n;
152 tab = M. tab;
153 M. n = this_n;
154 M. tab = this_tab;
155 return;
156 }
157
160 const element *memory () const {return tab;}
161
164 element *memory () {return tab;}
165
166protected:
168 int n;
169
171 element *tab;
172
173}; /* class flatMatrix */
174
175
176} // namespace homology
177} // namespace chomp
178
179#endif // _CHOMP_STRUCT_FLATMATR_H_
180
182
The class that represents a constant single row of the matrix.
Definition: flatmatr.h:114
const_row(int _offset, const element *_v)
The constructor of a constant row of the matrix.
Definition: flatmatr.h:117
const element * v
A reference to the vector that stores all the entries of the matrix.
Definition: flatmatr.h:130
int offset
The offset in the vector of all the entries of the matrix.
Definition: flatmatr.h:126
const element & operator[](int m)
Returns a reference to the element at the given position.
Definition: flatmatr.h:121
The class that represents a single row of the matrix.
Definition: flatmatr.h:89
element * v
A reference to the vector that stores all the entries of the matrix.
Definition: flatmatr.h:105
int offset
The offset in the vector of all the entries of the matrix.
Definition: flatmatr.h:101
element & operator[](int j)
Returns a reference to the element at the given position.
Definition: flatmatr.h:96
row(int _offset, element *_v)
The constructor of a row of the matrix.
Definition: flatmatr.h:92
This class defines a simple data structure for a flat 2-dim square matrix whose entries are stored in...
Definition: flatmatr.h:55
flatMatrix(const flatMatrix< element > &M)
The copy constructor which copies all the entries of the matrix.
Definition: flatmatr.h:63
int n
The size of the matrix.
Definition: flatmatr.h:168
const element * memory() const
Returns the address of the memory buffer with the elements of the matrix for reading only.
Definition: flatmatr.h:160
void clear(const element &elem)
Clears all the entries of the matrix with the provided value.
Definition: flatmatr.h:138
~flatMatrix()
The destructor which deallocates the memory.
Definition: flatmatr.h:85
void swap(flatMatrix< element > &M)
Swaps the memory of two flat matrices.
Definition: flatmatr.h:147
element * memory()
Returns the address of the memory buffer with the elements of the matrix for reading and writing.
Definition: flatmatr.h:164
flatMatrix & operator=(const flatMatrix< element > &M)
The assignment operator.
Definition: flatmatr.h:74
element * tab
The array of elements.
Definition: flatmatr.h:171
flatMatrix(int size)
The main constructor.
Definition: flatmatr.h:60
row operator[](int i)
Returns a row of the matrix.
Definition: flatmatr.h:109
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51