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  ~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 template <class T>
297 {
298  if (m_Data != nullptr) clear();
299 }
300 
301 
302 // =====================================================================
303 // =====================================================================
304 
305 
306 template <class T>
307 bool Vector<T>::allocate(unsigned long Size)
308 {
309 
310  if (Size > 0)
311  {
312  m_Data = new T[Size];
313  if (m_Data != nullptr)
314  m_Size = Size;
315  else
316  return false;
317  }
318 
319  return true;
320 }
321 
322 
323 // =====================================================================
324 // =====================================================================
325 
326 
327 template <class T>
328 void Vector<T>::setData(T* Data, unsigned long Size)
329 {
330  clear();
331 
332  if (!allocate(Size))
333  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Cannot allocate memory");
334 
335  std::copy(Data, Data + Size, m_Data);
336 }
337 
338 
339 // =====================================================================
340 // =====================================================================
341 
342 
343 template <class T>
344 T Vector<T>::getElement(unsigned long Index) const
345 {
346  if (Index >= m_Size)
347  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
348 
349  return m_Data[Index];
350 }
351 
352 
353 // =====================================================================
354 // =====================================================================
355 
356 
357 template <class T>
358 void Vector<T>::setElement(unsigned long Index, T Element)
359 {
360  if (Index >= m_Size)
361  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
362  m_Data[Index] = Element;
363 }
364 
365 
366 // =====================================================================
367 // =====================================================================
368 
369 
370 template <class T>
371 T& Vector<T>::operator[](unsigned long Index)
372 {
373  if (Index >= m_Size)
374  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"element access range error");
375 
376  return m_Data[Index];
377 }
378 
379 
380 // =====================================================================
381 // =====================================================================
382 
383 
384 template <class T>
386 {
387  if (this == &A) return *this; // in case somebody tries assign array to itself
388 
389  clear();
390 
391  allocate(A.m_Size);
392  std::copy(A.m_Data, A.m_Data + A.m_Size, m_Data);
393 
394  return *this;
395 }
396 
397 
398 // =====================================================================
399 // =====================================================================
400 
401 
402 template <class T>
404 {
405  m_Data = nullptr;
406  m_Size = 0;
407 }
408 
409 
410 // =====================================================================
411 // =====================================================================
412 
413 
414 template <class T>
415 void Vector<T>::fill(const T& Val)
416 {
417  std::fill(m_Data, m_Data + m_Size,Val);
418 }
419 
420 
421 // =====================================================================
422 // =====================================================================
423 
424 
425 template <class T>
427 {
428  delete [] m_Data;
429  init();
430 }
431 
432 
433 // =====================================================================
434 // =====================================================================
435 
436 
437 template <class T>
438 void Vector<T>::copy(const Vector& Source, Vector& Dest)
439 {
440  Dest.clear();
441  Dest.allocate(Source.m_Size);
442  for (unsigned long i = 0; i < Source.m_Size;i++)
443  {
444  Dest.m_Data[i] = Source.m_Data[i];
445  }
446 }
447 
448 
449 } } // namespaces
450 
451 
452 #endif
T * iterator
Definition: Vector.hpp:60
T & operator[](unsigned long Index)
Definition: Vector.hpp:371
void setElement(unsigned long Index, T Element)
Definition: Vector.hpp:358
void init()
Definition: Vector.hpp:403
T getElement(unsigned long Index) const
Definition: Vector.hpp:344
iterator end()
Definition: Vector.hpp:200
void setData(T *Data, unsigned long Size)
Definition: Vector.hpp:328
bool allocate(unsigned long Size)
Definition: Vector.hpp:307
Vector()
Definition: Vector.hpp:218
T * m_Data
Definition: Vector.hpp:67
unsigned long m_Size
Definition: Vector.hpp:69
void clear()
Definition: Vector.hpp:426
Vector< T > & operator=(const Vector &A)
Definition: Vector.hpp:385
const T * const_iterator
Definition: Vector.hpp:62
const_iterator end() const
Definition: Vector.hpp:207
const_iterator begin() const
Definition: Vector.hpp:193
Definition: FrameworkException.hpp:50
~Vector()
Definition: Vector.hpp:296
Definition: ApplicationException.hpp:47
void fill(const T &Val)
Definition: Vector.hpp:415
#define OPENFLUID_API
Definition: dllexport.hpp:87
static void copy(const Vector &Source, Vector &Dest)
Definition: Vector.hpp:438
unsigned long getSize() const
Definition: Vector.hpp:114
T * data() const
Definition: Vector.hpp:127
Definition: Vector.hpp:56
iterator begin()
Definition: Vector.hpp:186
unsigned long size() const
Definition: Vector.hpp:121
T at(unsigned long Index) const
Definition: Vector.hpp:142