Manual for OpenFLUID 2.1.11

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