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  @file Matrix.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  */
38 
39 
40 #ifndef __OPENFLUID_CORE_MATRIX_HPP__
41 #define __OPENFLUID_CORE_MATRIX_HPP__
42 
43 
44 #include <openfluid/dllexport.hpp>
46 
47 
48 namespace openfluid { namespace core {
49 
50 
51 /**
52  Template class for matrix data
53 */
54 template <class T>
56 {
57  protected :
58 
59  T* m_Data;
60 
61  unsigned long m_ColsNbr;
62 
63  unsigned long m_RowsNbr;
64 
65  bool allocate(unsigned long ColsNbr, unsigned long RowsNbr);
66 
67  void init();
68 
69 
70  public :
71 
72  /**
73  Default constructor, creates an empty Matrix
74  */
75  Matrix();
76 
77  /**
78  Copy constructor
79  */
80  Matrix(const Matrix &Matrix);
81 
82  /**
83  Constructor, creates a Matrix containing Size elements
84  */
85  Matrix(unsigned long ColsNbr,unsigned long RowsNbr);
86 
87  /**
88  Constructor, creates a Matrix containing Size elements, initialized with value InitValue
89  */
90  Matrix(unsigned long ColsNbr,unsigned long RowsNbr, T InitValue);
91 
92  /**
93  Destructor
94  */
95  virtual ~Matrix();
96 
97  /**
98  Returns the number of columns of the Matrix
99  @return number of columns
100  */
101  inline unsigned long getColsNbr() const
102  {
103  return m_ColsNbr;
104  };
105 
106  /**
107  Returns the number of rows of the Matrix
108  @return number of rows
109  */
110  inline unsigned long getRowsNbr() const
111  {
112  return m_RowsNbr;
113  };
114 
115  /**
116  Returns the full size of the Matrix (number of columns x number of rows)
117  @return size of the Matrix
118  */
119  inline unsigned long getSize() const
120  {
121  return (m_ColsNbr * m_RowsNbr);
122  };
123 
124  /**
125  Returns the full size of the Matrix
126  */
127  inline unsigned long size() const
128  {
129  return (m_ColsNbr * m_RowsNbr);
130  };
131 
132 
133  /**
134  Returns a pointer to the content of the Matrix (like C arrays)
135  */
136  T* data() const
137  {
138  return static_cast<T*>(m_Data);
139  };
140 
141  /**
142  Sets data from a pointer to a content (like C arrays)
143  */
144  void setData(T* Data, unsigned long ColsNbr, unsigned long RowsNbr);
145 
146  /**
147  Returns the element of the Matrix for index Index
148  */
149  T getElement(unsigned long ColIndex, unsigned long RowIndex) const;
150 
151  /**
152  Returns the element of the Matrix for index Index
153  */
154  inline T at(unsigned long ColIndex, unsigned long RowIndex) const
155  {
156  return getElement(ColIndex,RowIndex);
157  };
158 
159  /**
160  Returns the element of the Matrix for index Index
161  */
162  inline T get(unsigned long ColIndex, unsigned long RowIndex) const
163  { return getElement(ColIndex,RowIndex); };
164 
165 
166  /**
167  Sets a new value for element at the given index
168  */
169  void setElement(unsigned long ColIndex, unsigned long RowIndex, T Element);
170 
171  /**
172  Sets a new value for element at the given index
173  */
174  inline void set(unsigned long ColIndex, unsigned long RowIndex, T Element)
175  {
176  setElement(ColIndex,RowIndex,Element);
177  };
178 
179  /**
180  Allocation operator
181  */
182  Matrix<T>& operator=(const Matrix &A);
183 
184  /**
185  Fills the Matrix with given value
186  */
187  void fill(const T& Val);
188 
189  /**
190  Clears the Matrix (empty and size is 0)
191  */
192  void clear();
193 
194 };
195 
196 
197 // =====================================================================
198 // =====================================================================
199 
200 
201 template <class T>
203  m_ColsNbr(0),m_RowsNbr(0)
204 {
205  init();
206 }
207 
208 
209 // =====================================================================
210 // =====================================================================
211 
212 
213 template <class T>
215 {
216  init();
217 
218  if (!allocate(A.m_ColsNbr,A.m_RowsNbr))
219  {
220  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
221  }
222 
223  std::copy(A.m_Data, A.m_Data + (A.m_ColsNbr*A.m_RowsNbr), m_Data);
224 }
225 
226 
227 // =====================================================================
228 // =====================================================================
229 
230 
231 template <class T>
232 Matrix<T>::Matrix(unsigned long ColsNbr, unsigned long RowsNbr)
233 {
234  init();
235 
236  if (!allocate(ColsNbr,RowsNbr))
237  {
238  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
239  }
240 
241  fill(0);
242 }
243 
244 
245 // =====================================================================
246 // =====================================================================
247 
248 
249 template <class T>
250 Matrix<T>::Matrix(unsigned long ColsNbr, unsigned long RowsNbr, T InitValue)
251 {
252  init();
253 
254  if (!allocate(ColsNbr,RowsNbr))
255  {
256  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
257  }
258 
259  fill(InitValue);
260 }
261 
262 
263 // =====================================================================
264 // =====================================================================
265 
266 
267 template <class T>
269 {
270  clear();
271 }
272 
273 
274 // =====================================================================
275 // =====================================================================
276 
277 
278 template <class T>
280 {
281  m_Data = nullptr;
282  m_ColsNbr = 0;
283  m_RowsNbr = 0;
284 }
285 
286 
287 // =====================================================================
288 // =====================================================================
289 
290 
291 template <class T>
292 bool Matrix<T>::allocate(unsigned long ColsNbr, unsigned long RowsNbr)
293 {
294  if (ColsNbr > 0 && RowsNbr > 0)
295  {
296  m_Data = new T[ColsNbr*RowsNbr];
297  if (m_Data)
298  {
299  m_RowsNbr = RowsNbr;
300  m_ColsNbr = ColsNbr;
301  }
302  else
303  {
304  return false;
305  }
306  }
307 
308  return true;
309 }
310 
311 
312 // =====================================================================
313 // =====================================================================
314 
315 
316 template <class T>
317 void Matrix<T>::setData(T* Data, unsigned long ColsNbr, unsigned long RowsNbr)
318 {
319  clear();
320 
321  if (!allocate(ColsNbr,RowsNbr))
322  {
323  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
324  }
325 
326  std::copy(Data, Data + (ColsNbr*RowsNbr), m_Data);
327 }
328 
329 
330 // =====================================================================
331 // =====================================================================
332 
333 
334 template <class T>
335 T Matrix<T>::getElement(unsigned long ColIndex, unsigned long RowIndex) const
336 {
337  if (ColIndex >= m_ColsNbr || RowIndex >= m_RowsNbr)
338  {
339  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
340  }
341 
342  return m_Data[ColIndex*m_RowsNbr+RowIndex];
343 }
344 
345 
346 // =====================================================================
347 // =====================================================================
348 
349 
350 template <class T>
351 void Matrix<T>::setElement(unsigned long ColIndex, unsigned long RowIndex, T Element)
352 {
353  if (ColIndex >= m_ColsNbr || RowIndex >= m_RowsNbr)
354  {
355  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
356  }
357 
358  m_Data[ColIndex*m_RowsNbr+RowIndex] = Element;
359 }
360 
361 
362 // =====================================================================
363 // =====================================================================
364 
365 
366 template <class T>
368 {
369 
370  if (this == &A) // in case of trying to assign array to itself
371  {
372  return *this;
373  }
374 
375  clear();
376 
377  if (!allocate(A.m_ColsNbr,A.m_RowsNbr))
378  {
379  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
380  }
381 
382  std::copy(A.m_Data, A.m_Data + (A.m_ColsNbr*A.m_RowsNbr), m_Data);
383 
384  return *this;
385 }
386 
387 
388 // =====================================================================
389 // =====================================================================
390 
391 
392 template <class T>
393 void Matrix<T>::fill(const T& Val)
394 {
395  for (unsigned long i=0;i<m_RowsNbr;i++)
396  {
397  for (unsigned long j=0;j<m_ColsNbr;j++)
398  {
399  m_Data[i*m_ColsNbr+j] = Val;
400  }
401  }
402 }
403 
404 
405 // =====================================================================
406 // =====================================================================
407 
408 
409 template <class T>
411 {
412  delete [] m_Data;
413  init();
414 }
415 
416 
417 } }
418 
419 #endif /* __OPENFLUID_CORE_MATRIX_HPP__ */
unsigned long size() const
Definition: Matrix.hpp:127
unsigned long getColsNbr() const
Definition: Matrix.hpp:101
unsigned long getRowsNbr() const
Definition: Matrix.hpp:110
Definition: FrameworkException.hpp:50
Definition: Matrix.hpp:55
Matrix()
Definition: Matrix.hpp:202
unsigned long getSize() const
Definition: Matrix.hpp:119
T at(unsigned long ColIndex, unsigned long RowIndex) const
Definition: Matrix.hpp:154
Definition: ApplicationException.hpp:47
unsigned long m_RowsNbr
Definition: Matrix.hpp:63
T * m_Data
Definition: Matrix.hpp:59
#define OPENFLUID_API
Definition: dllexport.hpp:86
unsigned long m_ColsNbr
Definition: Matrix.hpp:61
T * data() const
Definition: Matrix.hpp:136