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@supagro.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  { return m_Size; };
116 
117  /**
118  Returns the size of the vector
119  @return size of the vector
120  */
121  unsigned long size() const
122  { return getSize(); };
123 
124  /**
125  Returns a pointer to the content of the vector (like C arrays)
126  */
127  T* data() const { return m_Data; };
128 
129  /**
130  Sets data for the vector from Data and with size Size
131  */
132  void setData(T* Data, unsigned long Size);
133 
134  /**
135  Returns the element of the vector for index Index
136  */
137  T getElement(unsigned long Index) const;
138 
139  /**
140  Returns the element of the vector for index Index
141  */
142  inline T at(unsigned long Index) const { return getElement(Index); };
143 
144  /**
145  Returns the element of the vector for index Index
146  */
147  inline T get(unsigned long Index) const { return getElement(Index); };
148 
149 
150  /**
151  Sets a new value for element at the given index
152  */
153  void setElement(unsigned long Index, T Element);
154 
155  /**
156  Sets a new value for element at the given index
157  */
158  inline void set(unsigned long Index, T Element) { setElement(Index,Element); };
159 
160 
161  /**
162  Operator to set a new value for element given between []
163  */
164  T& operator[](unsigned long Index);
165 
166  /**
167  Allocation operator
168  */
169  Vector<T>& operator=(const Vector &A);
170 
171  /**
172  Fills the vector with given value
173  */
174  void fill(const T& Val);
175 
176 
177  /**
178  Clears the vector (empty and size is 0)
179  */
180  void clear();
181 
182  /**
183  Returns an iterator referring to the first element in the vector
184  @return an iterator to the first element in the vector
185  */
186  inline iterator begin()
187  { return &m_Data[0]; }
188 
189  /**
190  Returns a constant iterator referring to the first element in the vector
191  @return a constant iterator to the first element in the vector
192  */
193  inline const_iterator begin() const
194  { return &m_Data[0]; }
195 
196  /**
197  Returns an iterator referring to the past-the-end element in the vector
198  @return an iterator to the past-the-end element in the vector
199  */
200  inline iterator end()
201  { return &m_Data[m_Size]; }
202 
203  /**
204  Returns a constant iterator referring to the past-the-end element in the vector
205  @return a constant iterator to the past-the-end element in the vector
206  */
207  inline const_iterator end() const
208  { return &m_Data[m_Size]; }
209 
210 };
211 
212 
213 // =====================================================================
214 // =====================================================================
215 
216 
217 template <class T>
219 {
220  init();
221 }
222 
223 
224 // =====================================================================
225 // =====================================================================
226 
227 
228 template <class T>
230 {
231  init();
232 
233  if (!allocate(A.m_Size))
234  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
235 
236  std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
237 }
238 
239 
240 // =====================================================================
241 // =====================================================================
242 
243 
244 template <class T>
245 Vector<T>::Vector(unsigned long Size)
246 {
247  init();
248 
249  if (!allocate(Size))
250  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
251 }
252 
253 
254 // =====================================================================
255 // =====================================================================
256 
257 
258 template <class T>
259 Vector<T>::Vector(unsigned long Size, T InitValue)
260 {
261  init();
262 
263  if (!allocate(Size))
264  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
265 
266 
267  if (m_Data != nullptr)
268  {
269  unsigned long i;
270  for (i=0;i<m_Size;i++) m_Data[i] = InitValue;
271  }
272 }
273 
274 
275 // =====================================================================
276 // =====================================================================
277 
278 
279 template <class T>
280 Vector<T>::Vector(T* Data, unsigned long Size)
281 {
282  init();
283 
284  if (!allocate(Size))
285  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
286 
287  std::copy(Data, Data + Size, m_Data);
288 
289 }
290 
291 
292 // =====================================================================
293 // =====================================================================
294 
295 
296 template <class T>
298 {
299  clear();
300 }
301 
302 
303 // =====================================================================
304 // =====================================================================
305 
306 
307 template <class T>
308 bool Vector<T>::allocate(unsigned long Size)
309 {
310 
311  if (Size > 0)
312  {
313  m_Data = new T[Size];
314  if (m_Data != nullptr)
315  m_Size = Size;
316  else
317  return false;
318  }
319 
320  return true;
321 }
322 
323 
324 // =====================================================================
325 // =====================================================================
326 
327 
328 template <class T>
329 void Vector<T>::setData(T* Data, unsigned long Size)
330 {
331  clear();
332 
333  if (!allocate(Size))
334  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
335 
336  std::copy(Data, Data + Size, m_Data);
337 }
338 
339 
340 // =====================================================================
341 // =====================================================================
342 
343 
344 template <class T>
345 T Vector<T>::getElement(unsigned long Index) const
346 {
347  if (Index >= m_Size)
348  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
349 
350  return m_Data[Index];
351 }
352 
353 
354 // =====================================================================
355 // =====================================================================
356 
357 
358 template <class T>
359 void Vector<T>::setElement(unsigned long Index, T Element)
360 {
361  if (Index >= m_Size)
362  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
363  m_Data[Index] = Element;
364 }
365 
366 
367 // =====================================================================
368 // =====================================================================
369 
370 
371 template <class T>
372 T& Vector<T>::operator[](unsigned long Index)
373 {
374  if (Index >= m_Size)
375  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
376 
377  return m_Data[Index];
378 }
379 
380 
381 // =====================================================================
382 // =====================================================================
383 
384 
385 template <class T>
387 {
388  if (this == &A) // in case somebody tries assign array to itself
389  return *this;
390 
391  clear();
392 
393  allocate(A.m_Size);
394  std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
395 
396  return *this;
397 }
398 
399 
400 // =====================================================================
401 // =====================================================================
402 
403 
404 template <class T>
406 {
407  m_Data = nullptr;
408  m_Size = 0;
409 }
410 
411 
412 // =====================================================================
413 // =====================================================================
414 
415 
416 template <class T>
417 void Vector<T>::fill(const T& Val)
418 {
419  std::fill(m_Data, m_Data + m_Size,Val);
420 }
421 
422 
423 // =====================================================================
424 // =====================================================================
425 
426 
427 template <class T>
429 {
430  delete [] m_Data;
431  init();
432 }
433 
434 
435 // =====================================================================
436 // =====================================================================
437 
438 
439 template <class T>
440 void Vector<T>::copy(const Vector& Source, Vector& Dest)
441 {
442  Dest.clear();
443  Dest.allocate(Source.m_Size);
444  for (unsigned long i = 0; i < Source.m_Size;i++)
445  {
446  Dest.m_Data[i] = Source.m_Data[i];
447  }
448 }
449 
450 
451 } } // namespaces
452 
453 
454 #endif
const_iterator begin() const
Definition: Vector.hpp:193
Vector()
Definition: Vector.hpp:218
T at(unsigned long Index) const
Definition: Vector.hpp:142
void setData(T *Data, unsigned long Size)
Definition: Vector.hpp:329
Definition: FrameworkException.hpp:50
T * data() const
Definition: Vector.hpp:127
#define OPENFLUID_API
Definition: dllexport.hpp:87
iterator end()
Definition: Vector.hpp:200
T & operator[](unsigned long Index)
Definition: Vector.hpp:372
unsigned long size() const
Definition: Vector.hpp:121
virtual ~Vector()
Definition: Vector.hpp:297
unsigned long m_Size
Definition: Vector.hpp:69
void init()
Definition: Vector.hpp:405
Definition: ApplicationException.hpp:47
T getElement(unsigned long Index) const
Definition: Vector.hpp:345
const_iterator end() const
Definition: Vector.hpp:207
static void copy(const Vector &Source, Vector &Dest)
Definition: Vector.hpp:440
unsigned long getSize() const
Definition: Vector.hpp:114
T * iterator
Definition: Vector.hpp:60
void clear()
Definition: Vector.hpp:428
bool allocate(unsigned long Size)
Definition: Vector.hpp:308
void setElement(unsigned long Index, T Element)
Definition: Vector.hpp:359
T * m_Data
Definition: Vector.hpp:67
Vector< T > & operator=(const Vector &A)
Definition: Vector.hpp:386
void fill(const T &Val)
Definition: Vector.hpp:417
const T * const_iterator
Definition: Vector.hpp:62
Definition: Vector.hpp:56
iterator begin()
Definition: Vector.hpp:186