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++)
301  {
302  m_Data[i] = InitValue;
303  }
304  }
305 }
306 
307 
308 // =====================================================================
309 // =====================================================================
310 
311 
312 template <class T>
313 Vector<T>::Vector(T* Data, unsigned long Size)
314 {
315  init();
316 
317  if (!allocate(Size))
318  {
319  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
320  }
321 
322  std::copy(Data, Data + Size, m_Data);
323 
324 }
325 
326 
327 // =====================================================================
328 // =====================================================================
329 
330 
331 template <class T>
333 {
334  clear();
335 }
336 
337 
338 // =====================================================================
339 // =====================================================================
340 
341 
342 template <class T>
343 bool Vector<T>::allocate(unsigned long Size)
344 {
345 
346  if (Size > 0)
347  {
348  m_Data = new T[Size];
349  if (m_Data != nullptr)
350  {
351  m_Size = Size;
352  }
353  else
354  {
355  return false;
356  }
357  }
358 
359  return true;
360 }
361 
362 
363 // =====================================================================
364 // =====================================================================
365 
366 
367 template <class T>
368 void Vector<T>::setData(T* Data, unsigned long Size)
369 {
370  clear();
371 
372  if (!allocate(Size))
373  {
374  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
375  }
376 
377  std::copy(Data, Data + Size, m_Data);
378 }
379 
380 
381 // =====================================================================
382 // =====================================================================
383 
384 
385 template <class T>
386 T Vector<T>::getElement(unsigned long Index) const
387 {
388  if (Index >= m_Size)
389  {
390  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
391  }
392 
393  return m_Data[Index];
394 }
395 
396 
397 // =====================================================================
398 // =====================================================================
399 
400 
401 template <class T>
402 void Vector<T>::setElement(unsigned long Index, T Element)
403 {
404  if (Index >= m_Size)
405  {
406  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
407  }
408 
409  m_Data[Index] = Element;
410 }
411 
412 
413 // =====================================================================
414 // =====================================================================
415 
416 
417 template <class T>
418 T& Vector<T>::operator[](unsigned long Index)
419 {
420  if (Index >= m_Size)
421  {
422  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
423  }
424 
425  return m_Data[Index];
426 }
427 
428 
429 // =====================================================================
430 // =====================================================================
431 
432 
433 template <class T>
435 {
436  if (this == &A) // in case somebody tries assign array to itself
437  {
438  return *this;
439  }
440 
441  clear();
442 
443  allocate(A.m_Size);
444  std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
445 
446  return *this;
447 }
448 
449 
450 // =====================================================================
451 // =====================================================================
452 
453 
454 template <class T>
456 {
457  m_Data = nullptr;
458  m_Size = 0;
459 }
460 
461 
462 // =====================================================================
463 // =====================================================================
464 
465 
466 template <class T>
467 void Vector<T>::fill(const T& Val)
468 {
469  std::fill(m_Data, m_Data + m_Size,Val);
470 }
471 
472 
473 // =====================================================================
474 // =====================================================================
475 
476 
477 template <class T>
479 {
480  delete [] m_Data;
481  init();
482 }
483 
484 
485 // =====================================================================
486 // =====================================================================
487 
488 
489 template <class T>
490 void Vector<T>::copy(const Vector& Source, Vector& Dest)
491 {
492  Dest.clear();
493  Dest.allocate(Source.m_Size);
494  for (unsigned long i = 0; i < Source.m_Size;i++)
495  {
496  Dest.m_Data[i] = Source.m_Data[i];
497  }
498 }
499 
500 
501 } } // namespaces
502 
503 
504 #endif
T * iterator
Definition: Vector.hpp:60
const T * const_iterator
Definition: Vector.hpp:62
iterator begin()
Definition: Vector.hpp:202
T & operator[](unsigned long Index)
Definition: Vector.hpp:418
unsigned long size() const
Definition: Vector.hpp:123
T * m_Data
Definition: Vector.hpp:67
const_iterator end() const
Definition: Vector.hpp:229
Vector()
Definition: Vector.hpp:242
Definition: FrameworkException.hpp:50
void setData(T *Data, unsigned long Size)
Definition: Vector.hpp:368
T * data() const
Definition: Vector.hpp:131
Definition: Vector.hpp:56
void fill(const T &Val)
Definition: Vector.hpp:467
T at(unsigned long Index) const
Definition: Vector.hpp:149
Definition: ApplicationException.hpp:47
static void copy(const Vector &Source, Vector &Dest)
Definition: Vector.hpp:490
void setElement(unsigned long Index, T Element)
Definition: Vector.hpp:402
const_iterator begin() const
Definition: Vector.hpp:211
unsigned long m_Size
Definition: Vector.hpp:69
unsigned long getSize() const
Definition: Vector.hpp:114
void clear()
Definition: Vector.hpp:478
T getElement(unsigned long Index) const
Definition: Vector.hpp:386
Vector< T > & operator=(const Vector &A)
Definition: Vector.hpp:434
iterator end()
Definition: Vector.hpp:220
void init()
Definition: Vector.hpp:455
virtual ~Vector()
Definition: Vector.hpp:332
#define OPENFLUID_API
Definition: dllexport.hpp:86
bool allocate(unsigned long Size)
Definition: Vector.hpp:343