Aleph-w  1.9
General library for algorithms and data structures
tpl_dynMat.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 #ifndef TPL_DYNMAT_H
28 #define TPL_DYNMAT_H
29 
30 # include <ahDry.H>
31 # include <tpl_dynArray.H>
32 
33 namespace Aleph {
34 
45  template <typename T>
46 class DynMatrix
47  : public LocateFunctions<DynMatrix<T>, T>,
48  public FunctionalMethods<DynMatrix<T>, T>
49 {
50  size_t n = 0;
51  size_t m = 0;
52 
53  DynArray<T> * array_ptr = nullptr;
54 
55  mutable T Zero = T();
56 
57  const T & read_array_entry(const size_t i) const noexcept
58  {
59  T * entry_ptr = array_ptr->test(i);
60 
61  return entry_ptr == nullptr ? Zero : *entry_ptr;
62  }
63 
64  T & write_array_entry(const size_t i, const T & data)
65  {
66  T & ref = array_ptr->touch(i) = data;
67 
68  return ref;
69  }
70 
71  size_t get_index(const size_t i, const size_t j) const noexcept
72  {
73  return i*m + j;
74  }
75 
76 public:
77 
79  void set_default_initial_value(const T & value)
80  {
81  array_ptr->set_default_initial_value(value);
82  }
83 
85  void swap(DynMatrix & mat)
86  {
87  std::swap(n, mat.n);
88  std::swap(m, mat.m);
89  std::swap(Zero, mat.Zero);
90  std::swap(array_ptr, mat.array_ptr);
91  }
92 
94  void set_dimension(const size_t __n, const size_t __m)
95  {
96  if (array_ptr != nullptr) // se verifica porque puede llamarse desde el ctor
97  delete array_ptr;
98 
99  n = __n;
100  m = __m;
101  size_t d, s, b;
102  const size_t N = n*m;
103  DynArray<T>::compute_sizes(N, d, s, b);
104 
105  array_ptr = new DynArray<T> (d, s, b);
106  array_ptr->set_default_initial_value(Zero);
107  }
108 
111  void allocate()
112  {
113  array_ptr->reserve(n*m);
114  }
115 
123  DynMatrix(const size_t __n, const size_t __m, const T & zero = T())
124  : n(__n), m(__m), array_ptr(nullptr), Zero(zero)
125  {
126  set_dimension(n, m);
127  }
128 
129  ~DynMatrix()
130  {
131  if (array_ptr)
132  delete array_ptr;
133  }
134 
136  DynMatrix(const DynMatrix<T> & mat)
137  : DynMatrix(mat.n, mat.m, mat.Zero)
138  {
139  *array_ptr = *mat.array_ptr;
140  }
141 
144  {
145  swap(mat);
146  }
147 
150  {
151  if (this == &mat)
152  return *this;
153 
154  n = mat.n;
155  m = mat.m;
156  array_ptr->copy_array(*mat.array_ptr);
157 
158  return *this;
159  }
160 
163  {
164  swap(mat);
165  return *this;
166  }
167 
169  bool operator == (const DynMatrix<T> & mat) const
170  {
171  const size_t N = n*m;
172  for (int i = 0; i < N; ++i)
173  if (read_array_entry(i) != mat.read_array_entry(i))
174  return false;
175 
176  return true;
177  }
178 
180  const size_t & rows() const noexcept { return n; }
181 
183  const size_t & cols() const noexcept { return m; }
184 
186  const T & read(const size_t i, const size_t j) const
187  {
188  assert(i < n and j < m);
189 
190  return read_array_entry(get_index(i, j));
191  }
192 
201  T & write(const size_t i, const size_t j, const T & data)
202  {
203  assert(i < n and j < m);
204 
205  return write_array_entry(get_index(i, j), data);
206  }
207 
209  T & access(const size_t i, const size_t j) const
210  {
211  return array_ptr->access(get_index(i, j));
212  }
213 
215  T & operator () (const size_t i, const size_t j) const
216  {
217  return access(i, j);
218  }
219 
225  template <class Operation>
226  bool traverse(Operation & operation) const
227  {
228  return array_ptr->traverse(operation);
229  }
230 
232  template <class Operation>
233  bool traverse(Operation & operation)
234  {
235  return array_ptr->traverse(operation);
236  }
237 
239  template <class Operation>
240  bool traverse(Operation && operation) const
241  {
242  return traverse<Operation>(operation);
243  }
244 
246  template <class Operation>
247  bool traverse(Operation && operation)
248  {
249  return traverse<Operation>(operation);
250  }
251 };
252 
253 } // end namespace Aleph
254 
255 #endif // TPL_DYNMAT_H
Definition: htlist.H:450
void reserve(const size_t l, const size_t r)
Definition: tpl_dynArray.H:998
Definition: htlist.H:133
bool operator==(const DynMatrix< T > &mat) const
Return true if this is equal to mat. It is based on T::operator ==
Definition: tpl_dynMat.H:169
bool traverse(Operation &operation) const
Definition: tpl_dynMat.H:226
const size_t & rows() const noexcept
Return the number of rows.
Definition: tpl_dynMat.H:180
T & access(const size_t i) const noexcept
Definition: tpl_dynArray.H:874
T & touch(const size_t i)
Definition: tpl_dynArray.H:958
DynMatrix(DynMatrix< T > &&mat)
Move constructor.
Definition: tpl_dynMat.H:143
void copy_array(const DynArray< T > &src_array)
Definition: tpl_dynArray.H:760
bool traverse(Operation &&operation) const
Definition: tpl_dynMat.H:240
DynMatrix(const DynMatrix< T > &mat)
Copy constructor.
Definition: tpl_dynMat.H:136
static void compute_sizes(const size_t n, size_t &d, size_t &s, size_t &b) noexcept
Definition: tpl_dynArray.H:224
void set_default_initial_value(const T &value)
Set the initial or zero value to value
Definition: tpl_dynMat.H:79
Definition: tpl_dynMat.H:46
T & operator()(const size_t i, const size_t j) const
Definition: tpl_dynMat.H:215
T & access(const size_t i, const size_t j) const
Fast access to i-th row j-th column.
Definition: tpl_dynMat.H:209
void swap(DynMatrix &mat)
Swap mat with this
Definition: tpl_dynMat.H:85
DynMatrix< T > & operator=(const DynMatrix< T > &mat)
Copy assign.
Definition: tpl_dynMat.H:149
DynMatrix(const size_t __n, const size_t __m, const T &zero=T())
Definition: tpl_dynMat.H:123
Definition: ah-comb.H:35
bool traverse(Operation &operation) const noexcept(noexcept(operation))
Definition: tpl_dynArray.H:1379
T * test(const size_t i) const noexcept
Definition: tpl_dynArray.H:927
bool traverse(Operation &&operation)
Definition: tpl_dynMat.H:247
const T & read(const size_t i, const size_t j) const
Read the entry at the row i and column j
Definition: tpl_dynMat.H:186
void set_dimension(const size_t __n, const size_t __m)
Change the matrix dimensions.
Definition: tpl_dynMat.H:94
Definition: tpl_dynArray.H:159
T & write(const size_t i, const size_t j, const T &data)
Definition: tpl_dynMat.H:201
void set_default_initial_value(const T &value) noexcept
Definition: tpl_dynArray.H:659
void allocate()
Definition: tpl_dynMat.H:111
const size_t & cols() const noexcept
Retorna la cantidad de columnas de la matriz.
Definition: tpl_dynMat.H:183
bool traverse(Operation &operation)
Definition: tpl_dynMat.H:233

Leandro Rabindranath León