Matrix.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 
34 /**
35  @file Matrix.hpp
36 
37  @author Jean-Christophe FABRE <jean-christophe.fabre@supagro.inra.fr>
38  */
39 
40 
41 #ifndef __OPENFLUID_CORE_MATRIX_HPP__
42 #define __OPENFLUID_CORE_MATRIX_HPP__
43 
44 
45 #include <openfluid/dllexport.hpp>
47 
48 
49 namespace openfluid { namespace core {
50 
51 
52 /**
53  Template class for matrix data
54 */
55 template <class T>
57 {
58  protected :
59 
60  T* m_Data;
61 
62  unsigned long m_ColsNbr;
63 
64  unsigned long m_RowsNbr;
65 
66  bool allocate(unsigned long ColsNbr, unsigned long RowsNbr);
67 
68  void init();
69 
70 
71  public :
72 
73  /**
74  Default constructor, creates an empty Matrix
75  */
76  Matrix();
77 
78  /**
79  Copy constructor
80  */
81  Matrix(const Matrix &Matrix);
82 
83  /**
84  Constructor, creates a Matrix containing Size elements
85  */
86  Matrix(unsigned long ColsNbr,unsigned long RowsNbr);
87 
88  /**
89  Constructor, creates a Matrix containing Size elements, initialized with value InitValue
90  */
91  Matrix(unsigned long ColsNbr,unsigned long RowsNbr, T InitValue);
92 
93  /**
94  Destructor
95  */
96  virtual ~Matrix();
97 
98  /**
99  Returns the number of columns of the Matrix
100  @return number of columns
101  */
102  inline unsigned long getColsNbr() const
103  { return m_ColsNbr; };
104 
105  /**
106  Returns the number of rows of the Matrix
107  @return number of rows
108  */
109  inline unsigned long getRowsNbr() const { return m_RowsNbr; };
110 
111  /**
112  Returns the full size of the Matrix (number of columns x number of rows)
113  @return size of the Matrix
114  */
115  inline unsigned long getSize() const
116  { return (m_ColsNbr * m_RowsNbr); };
117 
118  /**
119  Returns the full size of the Matrix
120  */
121  inline unsigned long size() const
122  { return (m_ColsNbr * m_RowsNbr); };
123 
124 
125  /**
126  Returns a pointer to the content of the Matrix (like C arrays)
127  */
128  T* data() const
129  { return static_cast<T*>(m_Data); };
130 
131  /**
132  Sets data from a pointer to a content (like C arrays)
133  */
134  void setData(T* Data, unsigned long ColsNbr, unsigned long RowsNbr);
135 
136  /**
137  Returns the element of the Matrix for index Index
138  */
139  T getElement(unsigned long ColIndex, unsigned long RowIndex) const;
140 
141  /**
142  Returns the element of the Matrix for index Index
143  */
144  inline T at(unsigned long ColIndex, unsigned long RowIndex) const
145  { return getElement(ColIndex,RowIndex); };
146 
147  /**
148  Returns the element of the Matrix for index Index
149  */
150  inline T get(unsigned long ColIndex, unsigned long RowIndex) const
151  { return getElement(ColIndex,RowIndex); };
152 
153 
154  /**
155  Sets a new value for element at the given index
156  */
157  void setElement(unsigned long ColIndex, unsigned long RowIndex, T Element);
158 
159  /**
160  Sets a new value for element at the given index
161  */
162  inline void set(unsigned long ColIndex, unsigned long RowIndex, T Element)
163  { setElement(ColIndex,RowIndex,Element); };
164 
165  /**
166  Allocation operator
167  */
168  Matrix<T>& operator=(const Matrix &A);
169 
170  /**
171  Fills the Matrix with given value
172  */
173  void fill(const T& Val);
174 
175  /**
176  Clears the Matrix (empty and size is 0)
177  */
178  void clear();
179 
180 };
181 
182 
183 // =====================================================================
184 // =====================================================================
185 
186 
187 template <class T>
189  m_ColsNbr(0),m_RowsNbr(0)
190 {
191  init();
192 }
193 
194 
195 // =====================================================================
196 // =====================================================================
197 
198 
199 template <class T>
201 {
202  init();
203 
204  if (!allocate(A.m_ColsNbr,A.m_RowsNbr))
205  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
206 
207  std::copy(A.m_Data, A.m_Data + (A.m_ColsNbr*A.m_RowsNbr), m_Data);
208 }
209 
210 
211 // =====================================================================
212 // =====================================================================
213 
214 
215 template <class T>
216 Matrix<T>::Matrix(unsigned long ColsNbr, unsigned long RowsNbr)
217 {
218  init();
219 
220  if (!allocate(ColsNbr,RowsNbr))
221  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
222 
223  fill(0);
224 }
225 
226 
227 // =====================================================================
228 // =====================================================================
229 
230 
231 template <class T>
232 Matrix<T>::Matrix(unsigned long ColsNbr, unsigned long RowsNbr, T InitValue)
233 {
234  init();
235 
236  if (!allocate(ColsNbr,RowsNbr))
237  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
238 
239  fill(InitValue);
240 }
241 
242 
243 // =====================================================================
244 // =====================================================================
245 
246 
247 template <class T>
249 {
250  clear();
251 }
252 
253 
254 // =====================================================================
255 // =====================================================================
256 
257 
258 template <class T>
260 {
261  m_Data = nullptr;
262  m_ColsNbr = 0;
263  m_RowsNbr = 0;
264 }
265 
266 
267 // =====================================================================
268 // =====================================================================
269 
270 
271 template <class T>
272 bool Matrix<T>::allocate(unsigned long ColsNbr, unsigned long RowsNbr)
273 {
274  if (ColsNbr > 0 && RowsNbr > 0)
275  {
276  m_Data = new T[ColsNbr*RowsNbr];
277  if (m_Data)
278  {
279  m_RowsNbr = RowsNbr;
280  m_ColsNbr = ColsNbr;
281  }
282  else
283  return false;
284  }
285 
286  return true;
287 }
288 
289 // =====================================================================
290 // =====================================================================
291 
292 
293 template <class T>
294 void Matrix<T>::setData(T* Data, unsigned long ColsNbr, unsigned long RowsNbr)
295 {
296  clear();
297 
298  if (!allocate(ColsNbr,RowsNbr))
299  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
300 
301  std::copy(Data, Data + (ColsNbr*RowsNbr), m_Data);
302 }
303 
304 
305 // =====================================================================
306 // =====================================================================
307 
308 
309 template <class T>
310 T Matrix<T>::getElement(unsigned long ColIndex, unsigned long RowIndex) const
311 {
312  if (ColIndex >= m_ColsNbr || RowIndex >= m_RowsNbr)
313  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
314 
315  return m_Data[ColIndex*m_RowsNbr+RowIndex];
316 }
317 
318 
319 // =====================================================================
320 // =====================================================================
321 
322 
323 template <class T>
324 void Matrix<T>::setElement(unsigned long ColIndex, unsigned long RowIndex, T Element)
325 {
326  if (ColIndex >= m_ColsNbr || RowIndex >= m_RowsNbr)
327  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
328 
329  m_Data[ColIndex*m_RowsNbr+RowIndex] = Element;
330 }
331 
332 
333 
334 // =====================================================================
335 // =====================================================================
336 
337 template <class T>
339 {
340 
341  if (this == &A) // in case somebody tries assign array to itself
342  return *this;
343 
344  clear();
345 
346  if (!allocate(A.m_ColsNbr,A.m_RowsNbr))
347  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
348 
349  std::copy(A.m_Data, A.m_Data + (A.m_ColsNbr*A.m_RowsNbr), m_Data);
350 
351  return *this;
352 }
353 
354 
355 // =====================================================================
356 // =====================================================================
357 
358 
359 template <class T>
360 void Matrix<T>::fill(const T& Val)
361 {
362  for (unsigned long i=0;i<m_RowsNbr;i++)
363  for (unsigned long j=0;j<m_ColsNbr;j++)
364  m_Data[i*m_ColsNbr+j] = Val;
365 }
366 
367 
368 // =====================================================================
369 // =====================================================================
370 
371 
372 template <class T>
374 {
375  delete [] m_Data;
376  init();
377 }
378 
379 
380 
381 } }
382 
383 #endif /* __OPENFLUID_CORE_MATRIX_HPP__ */
T at(unsigned long ColIndex, unsigned long RowIndex) const
Definition: Matrix.hpp:144
Definition: FrameworkException.hpp:50
#define OPENFLUID_API
Definition: dllexport.hpp:87
unsigned long m_RowsNbr
Definition: Matrix.hpp:64
T * data() const
Definition: Matrix.hpp:128
unsigned long m_ColsNbr
Definition: Matrix.hpp:62
Definition: ApplicationException.hpp:47
unsigned long getRowsNbr() const
Definition: Matrix.hpp:109
Matrix()
Definition: Matrix.hpp:188
unsigned long getSize() const
Definition: Matrix.hpp:115
Definition: Matrix.hpp:56
unsigned long size() const
Definition: Matrix.hpp:121
T * m_Data
Definition: Matrix.hpp:60
unsigned long getColsNbr() const
Definition: Matrix.hpp:102