Vector.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 Vector.hpp
35 
36  @author Jean-Christophe Fabre <jean-christophe.fabre@inra.fr>
37 */
38 
39 
40 #ifndef __OPENFLUID_CORE_VECTOR_HPP__
41 #define __OPENFLUID_CORE_VECTOR_HPP__
42 
43 
44 #include <iostream>
45 #include <openfluid/dllexport.hpp>
47 
48 
49 namespace openfluid { namespace core {
50 
51 
52 /**
53  Template class for vector data
54 */
55 template <class T>
57 {
58  public:
59 
60  typedef T* iterator;
61 
62  typedef const T* const_iterator;
63 
64 
65  protected :
66 
67  T* m_Data;
68 
69  unsigned long m_Size;
70 
71  bool allocate(unsigned long Size);
72 
73  void init();
74 
75  static void copy(const Vector& Source, Vector& Dest);
76 
77 
78  public :
79 
80  /**
81  Default constructor, creates an empty vector
82  */
83  Vector();
84 
85  /**
86  Copy constructor
87  */
88  Vector(const Vector &Vector);
89 
90  /**
91  Constructor, creates a vector containing Size elements
92  */
93  Vector(unsigned long Size);
94 
95  /**
96  Constructor, creates a vector containing Size elements, initialized with value InitValue
97  */
98  Vector(unsigned long Size, T InitValue);
99 
100  /**
101  Constructor, creates a vector of size Size, containing Data
102  */
103  Vector(T* Data, unsigned long Size);
104 
105  /**
106  Destructor
107  */
108  virtual ~Vector();
109 
110  /**
111  Returns the size of the vector
112  @return size of the vector
113  */
114  unsigned long getSize() const
115  {
116  return m_Size;
117  }
118 
119  /**
120  Returns the size of the vector
121  @return size of the vector
122  */
123  unsigned long size() const
124  {
125  return getSize();
126  }
127 
128  /**
129  Returns a pointer to the content of the vector (like C arrays)
130  */
131  T* data() const
132  {
133  return m_Data;
134  }
135 
136  /**
137  Sets data for the vector from Data and with size Size
138  */
139  void setData(T* Data, unsigned long Size);
140 
141  /**
142  Returns the element of the vector for index Index
143  */
144  T getElement(unsigned long Index) const;
145 
146  /**
147  Returns the element of the vector for index Index
148  */
149  inline T at(unsigned long Index) const
150  {
151  return getElement(Index);
152  }
153 
154  /**
155  Returns the element of the vector for index Index
156  */
157  inline T get(unsigned long Index) const
158  {
159  return getElement(Index);
160  }
161 
162 
163  /**
164  Sets a new value for element at the given index
165  */
166  void setElement(unsigned long Index, T Element);
167 
168  /**
169  Sets a new value for element at the given index
170  */
171  inline void set(unsigned long Index, T Element)
172  {
173  setElement(Index,Element);
174  }
175 
176 
177  /**
178  Operator to set a new value for element given between []
179  */
180  T& operator[](unsigned long Index);
181 
182  /**
183  Allocation operator
184  */
185  Vector<T>& operator=(const Vector &A);
186 
187  /**
188  Fills the vector with given value
189  */
190  void fill(const T& Val);
191 
192 
193  /**
194  Clears the vector (empty and size is 0)
195  */
196  void clear();
197 
198  /**
199  Returns an iterator referring to the first element in the vector
200  @return an iterator to the first element in the vector
201  */
202  inline iterator begin()
203  {
204  return &m_Data[0];
205  }
206 
207  /**
208  Returns a constant iterator referring to the first element in the vector
209  @return a constant iterator to the first element in the vector
210  */
211  inline const_iterator begin() const
212  {
213  return &m_Data[0];
214  }
215 
216  /**
217  Returns an iterator referring to the past-the-end element in the vector
218  @return an iterator to the past-the-end element in the vector
219  */
220  inline iterator end()
221  {
222  return &m_Data[m_Size];
223  }
224 
225  /**
226  Returns a constant iterator referring to the past-the-end element in the vector
227  @return a constant iterator to the past-the-end element in the vector
228  */
229  inline const_iterator end() const
230  {
231  return &m_Data[m_Size];
232  }
233 
234 };
235 
236 
237 // =====================================================================
238 // =====================================================================
239 
240 
241 template <class T>
243 {
244  init();
245 }
246 
247 
248 // =====================================================================
249 // =====================================================================
250 
251 
252 template <class T>
254 {
255  init();
256 
257  if (!allocate(A.m_Size))
258  {
259  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
260  }
261 
262  std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
263 }
264 
265 
266 // =====================================================================
267 // =====================================================================
268 
269 
270 template <class T>
271 Vector<T>::Vector(unsigned long Size)
272 {
273  init();
274 
275  if (!allocate(Size))
276  {
277  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
278  }
279 
280 }
281 
282 
283 // =====================================================================
284 // =====================================================================
285 
286 
287 template <class T>
288 Vector<T>::Vector(unsigned long Size, T InitValue)
289 {
290  init();
291 
292  if (!allocate(Size))
293  {
294  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
295  }
296 
297  if (m_Data != nullptr)
298  {
299  unsigned long i;
300  for (i=0;i<m_Size;i++) m_Data[i] = InitValue;
301  }
302 }
303 
304 
305 // =====================================================================
306 // =====================================================================
307 
308 
309 template <class T>
310 Vector<T>::Vector(T* Data, unsigned long Size)
311 {
312  init();
313 
314  if (!allocate(Size))
315  {
316  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
317  }
318 
319  std::copy(Data, Data + Size, m_Data);
320 
321 }
322 
323 
324 // =====================================================================
325 // =====================================================================
326 
327 
328 template <class T>
330 {
331  clear();
332 }
333 
334 
335 // =====================================================================
336 // =====================================================================
337 
338 
339 template <class T>
340 bool Vector<T>::allocate(unsigned long Size)
341 {
342 
343  if (Size > 0)
344  {
345  m_Data = new T[Size];
346  if (m_Data != nullptr)
347  {
348  m_Size = Size;
349  }
350  else
351  {
352  return false;
353  }
354  }
355 
356  return true;
357 }
358 
359 
360 // =====================================================================
361 // =====================================================================
362 
363 
364 template <class T>
365 void Vector<T>::setData(T* Data, unsigned long Size)
366 {
367  clear();
368 
369  if (!allocate(Size))
370  {
371  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
372  }
373 
374  std::copy(Data, Data + Size, m_Data);
375 }
376 
377 
378 // =====================================================================
379 // =====================================================================
380 
381 
382 template <class T>
383 T Vector<T>::getElement(unsigned long Index) const
384 {
385  if (Index >= m_Size)
386  {
387  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
388  }
389 
390  return m_Data[Index];
391 }
392 
393 
394 // =====================================================================
395 // =====================================================================
396 
397 
398 template <class T>
399 void Vector<T>::setElement(unsigned long Index, T Element)
400 {
401  if (Index >= m_Size)
402  {
403  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
404  }
405 
406  m_Data[Index] = Element;
407 }
408 
409 
410 // =====================================================================
411 // =====================================================================
412 
413 
414 template <class T>
415 T& Vector<T>::operator[](unsigned long Index)
416 {
417  if (Index >= m_Size)
418  {
419  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
420  }
421 
422  return m_Data[Index];
423 }
424 
425 
426 // =====================================================================
427 // =====================================================================
428 
429 
430 template <class T>
432 {
433  if (this == &A) // in case somebody tries assign array to itself
434  {
435  return *this;
436  }
437 
438  clear();
439 
440  allocate(A.m_Size);
441  std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
442 
443  return *this;
444 }
445 
446 
447 // =====================================================================
448 // =====================================================================
449 
450 
451 template <class T>
453 {
454  m_Data = nullptr;
455  m_Size = 0;
456 }
457 
458 
459 // =====================================================================
460 // =====================================================================
461 
462 
463 template <class T>
464 void Vector<T>::fill(const T& Val)
465 {
466  std::fill(m_Data, m_Data + m_Size,Val);
467 }
468 
469 
470 // =====================================================================
471 // =====================================================================
472 
473 
474 template <class T>
476 {
477  delete [] m_Data;
478  init();
479 }
480 
481 
482 // =====================================================================
483 // =====================================================================
484 
485 
486 template <class T>
487 void Vector<T>::copy(const Vector& Source, Vector& Dest)
488 {
489  Dest.clear();
490  Dest.allocate(Source.m_Size);
491  for (unsigned long i = 0; i < Source.m_Size;i++)
492  {
493  Dest.m_Data[i] = Source.m_Data[i];
494  }
495 }
496 
497 
498 } } // namespaces
499 
500 
501 #endif
T getElement(unsigned long Index) const
Definition: Vector.hpp:383
T * m_Data
Definition: Vector.hpp:67
void clear()
Definition: Vector.hpp:475
void setElement(unsigned long Index, T Element)
Definition: Vector.hpp:399
Vector< T > & operator=(const Vector &A)
Definition: Vector.hpp:431
virtual ~Vector()
Definition: Vector.hpp:329
void fill(const T &Val)
Definition: Vector.hpp:464
T at(unsigned long Index) const
Definition: Vector.hpp:149
T & operator[](unsigned long Index)
Definition: Vector.hpp:415
T * data() const
Definition: Vector.hpp:131
unsigned long getSize() const
Definition: Vector.hpp:114
bool allocate(unsigned long Size)
Definition: Vector.hpp:340
const_iterator begin() const
Definition: Vector.hpp:211
Definition: FrameworkException.hpp:50
Definition: ApplicationException.hpp:47
iterator begin()
Definition: Vector.hpp:202
T * iterator
Definition: Vector.hpp:60
iterator end()
Definition: Vector.hpp:220
Definition: Vector.hpp:56
unsigned long size() const
Definition: Vector.hpp:123
const T * const_iterator
Definition: Vector.hpp:62
#define OPENFLUID_API
Definition: dllexport.hpp:86
unsigned long m_Size
Definition: Vector.hpp:69
const_iterator end() const
Definition: Vector.hpp:229
Vector()
Definition: Vector.hpp:242
static void copy(const Vector &Source, Vector &Dest)
Definition: Vector.hpp:487
void init()
Definition: Vector.hpp:452
void setData(T *Data, unsigned long Size)
Definition: Vector.hpp:365