Documentation for OpenFLUID 2.2.2
Loading...
Searching...
No Matches
Vector.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of OpenFLUID software
4 Copyright(c) 2021-2026, INRAE
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
49
50
51namespace openfluid { namespace core {
52
53
54/**
55 Template class for vector data
56*/
57template <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 */
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 */
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 */
211 {
212 return m_Data.get();
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.get();
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.get()+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.get()+m_Size;
240 }
241
242};
243
244
245// =====================================================================
246// =====================================================================
247
248
249template <class T>
251{
252 init();
253}
254
255
256// =====================================================================
257// =====================================================================
258
259
260template <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
278template <class T>
279Vector<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
295template <class T>
296Vector<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
316template <class T>
317Vector<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
334template <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
352template <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
373template <class T>
374bool 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
399template <class T>
400void 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
417template <class T>
418T 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
433template <class T>
434void 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
449template <class T>
450T& 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
465template <class T>
467{
468 m_Data.reset();
469 m_Size = 0;
470}
471
472
473// =====================================================================
474// =====================================================================
475
476
477template <class T>
478void 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
488template <class T>
490{
491 init();
492}
493
494
495// =====================================================================
496// =====================================================================
497
498
499template <class T>
500void 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__ */
Definition FrameworkException.hpp:51
Definition Vector.hpp:59
T * iterator
Definition Vector.hpp:62
std::unique_ptr< T[]> m_Data
Definition Vector.hpp:69
unsigned long getSize() const
Definition Vector.hpp:126
const_iterator begin() const
Definition Vector.hpp:219
T at(unsigned long Index) const
Definition Vector.hpp:161
T getElement(unsigned long Index) const
Definition Vector.hpp:418
void fill(const T &Val)
Definition Vector.hpp:478
void clear()
Definition Vector.hpp:489
void setData(T *Data, unsigned long Size)
Definition Vector.hpp:400
T get(unsigned long Index) const
Definition Vector.hpp:169
Vector< T > & operator=(Vector &&Other)
Definition Vector.hpp:353
Vector(unsigned long Size)
Definition Vector.hpp:279
void init()
Definition Vector.hpp:466
bool allocate(unsigned long Size)
Definition Vector.hpp:374
Vector()
Definition Vector.hpp:250
T & operator[](unsigned long Index)
Definition Vector.hpp:450
iterator begin()
Definition Vector.hpp:210
static void copy(const Vector &Source, Vector &Dest)
Definition Vector.hpp:500
T * data() const
Definition Vector.hpp:143
const T * const_iterator
Definition Vector.hpp:64
Vector(const Vector &Other)
Definition Vector.hpp:261
void set(unsigned long Index, const T &Val)
Definition Vector.hpp:183
Vector(unsigned long Size, const T &InitValue)
Definition Vector.hpp:296
Vector(T *Data, unsigned long Size)
Definition Vector.hpp:317
iterator end()
Definition Vector.hpp:228
unsigned long size() const
Definition Vector.hpp:135
virtual ~Vector()=default
const_iterator end() const
Definition Vector.hpp:237
Vector< T > & operator=(const Vector &Other)
Definition Vector.hpp:335
unsigned long m_Size
Definition Vector.hpp:71
void setElement(unsigned long Index, const T &Val)
Definition Vector.hpp:434
#define OPENFLUID_API
Definition dllexport.hpp:86
Definition ApplicationException.hpp:47