VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
CbVector.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#ifndef CBVECTOR_H
35#define CBVECTOR_H
36
37#include <PointerDefinitions.h> //for memcopy
38#include <algorithm> //for std::swap
39#include <typeinfo> //for typeid
40#include <vector>
41
44
45template <typename T>
47template <typename T>
49
50//=========================================================================
63//=========================================================================
65template <typename T>
67{
68public:
69 using value_type = T;
71 using size_type = std::size_t;
72
73 friend class CbVectorAllocator<value_type>; // um auf ptrData und dataSize zugreifen zu koennen!
74
76
77public:
78 /*==========================================================*/
80 : ptrData(NULL), allocator(allocator)
81 {
82 this->allocator->alloc(*this, 0, value_type());
83 }
84 /*==========================================================*/
87 const value_type &value = value_type())
88 : ptrData(NULL), allocator(allocator)
89 {
90 this->allocator->alloc(*this, size, value);
91 }
92 /*==========================================================*/
93 virtual ~CbVector()
94 {
95 if (allocator) {
96 this->allocator->dealloc(*this);
97 delete allocator;
98 allocator = NULL;
99 }
100 }
101 /*=======================================================================*/
103 {
104 if (this == &src)
105 return *this;
106
107 // gespeicherte Datenelemente loeschen
108 // Laenge anpassen
109 this->allocator->resize(*this, src.size());
110
111 // gespeicherte Datenelemente kopieren
112 if (!src.empty()) {
113 memcpy((char *)ptrData, (char *)&src[0], src.size() * sizeof(value_type));
114 // for(size_type i=0; i<src.size(); i++)
115 // (*this)[i] = src[i];
116 }
117
118 return *this;
119 }
120 /*=======================================================================*/
121 CbVector &operator=(const std::vector<value_type> &src)
122 {
123 // gespeicherte Datenelemente loeschen
124 // Laenge anpassen
125 this->allocator->resize(*this, src.size());
126
127 // gespeicherte Datenelemente kopieren
128 if (!src.empty()) {
129 memcpy((char *)ptrData, (char *)&src[0], src.size() * sizeof(value_type));
130 // for(size_type i=0; i<src.size(); i++)
131 // (*this)[i] = src[i];
132 }
133
134 return *this;
135 }
136 /*=======================================================================*/
137 bool operator==(const CbVector &rhs) const
138 {
139 if (this == &rhs)
140 return true;
141 if (this->dataSize != rhs.dataSize)
142 return false;
143
144 for (size_type i = 0; i < rhs.size(); i++)
145 if (!isUbEqual(this->operator[](i), rhs.operator[](i)))
146 return false;
147
148 return true;
149 }
150 /*==========================================================*/
152 {
153 if (this->allocator) {
154 if (this->allocator == allocator)
155 return;
156 this->allocator->dealloc(*this);
157 delete this->allocator;
158 }
159 this->allocator = allocator;
160 this->allocator->alloc(*this, 0);
161 }
162 /*==========================================================*/
163 size_type size() const { return dataSize; }
164 /*==========================================================*/
165 bool empty() const { return dataSize == 0; }
166 /*==========================================================*/
167 bool resize(const size_type &dataSize) { return allocator->resize(*this, dataSize); }
168 /*==========================================================*/
169 bool resize(const size_type &dataSize, const value_type &value)
170 {
171 return allocator->resize(*this, dataSize, value);
172 }
173 /*==========================================================*/
175 {
176 if (this == &rhs)
177 return;
178
179 std::swap(this->ptrData, rhs.ptrData);
180 std::swap(this->dataSize, rhs.dataSize);
181 std::swap(this->allocator, rhs.allocator);
182 }
183 /*==========================================================*/
185 {
186#if !defined(VF_NO_RANGECHECK) && (defined(VF_DEBUG) || defined(VF_RANGECHECK))
187 if (i >= dataSize)
188 UB_THROW(UbException(UB_EXARGS, "T=" + (std::string) typeid(*this).name() + ub_system::toString(i) +
189 " out of range (size=" + ub_system::toString(dataSize) + ")"));
190#endif // VF_DEBUG
191
192 return ptrData[i];
193 }
194 /*==========================================================*/
195 const value_type &operator[](const size_type &i) const
196 {
197#if !defined(VF_NO_RANGECHECK) && (defined(VF_DEBUG) || defined(VF_RANGECHECK))
198 if (i >= dataSize)
199 UB_THROW(UbException(UB_EXARGS, "T=" + (std::string) typeid(*this).name() + ub_system::toString(i) +
200 " out of range (size=" + ub_system::toString(dataSize) + ")"));
201#endif // VF_DEBUG
202
203 return ptrData[i];
204 }
205 /*==========================================================*/
206 CbVectorAllocator<value_type> *getAllocator() const { return allocator; }
207 /*==========================================================*/
208
209private:
210 value_type *ptrData;
211 size_type dataSize{ 0 };
213 // CbVector<value_type>& operator=(const CbVector<value_type>& src);
214};
215
217// CbVectorAllocator-Interface
219template <typename T>
221{
222public:
225
226public:
227 CbVectorAllocator() = default;
228 virtual ~CbVectorAllocator() = default;
229
230 virtual bool alloc(CbVector<value_type> &vec, const size_type &dataSize,
231 const value_type &value = value_type()) = 0;
232 virtual bool resize(CbVector<value_type> &vec, const size_type &dataSize,
233 const value_type &value = value_type()) = 0;
234 virtual bool dealloc(CbVector<value_type> &vec) = 0;
235
236protected:
237 // folgende Methoden ersparen eine friend Deklaierung aller moeglichen Allocatoren
238 // denn durch diese beiden Methoden haben sie exklusive Zugriffsrechte!
239 //**********************************************************************************//
241 {
242 if (vec.getAllocator() != this)
243 UB_THROW(UbException(UB_EXARGS, "allocator is not member of vec!"));
244 return vec.ptrData;
245 }
246 //**********************************************************************************//
248 {
249 if (vec.getAllocator() != this)
250 UB_THROW(UbException(UB_EXARGS, "allocator is not member of vec!"));
251 return vec.dataSize;
252 }
253};
254
256// CbVectorAllocatorStd
258template <typename T>
260{
261public:
262 // typedefs wiederholen, da Basisklasse = template -> "Dependent-Base"-Problem
265
266public:
268 /*==========================================================*/
269 bool alloc(CbVector<value_type> &src, const size_type &dataSize, const value_type &value = value_type()) override
270 {
271 return this->resize(src, dataSize, value);
272 }
273 /*==========================================================*/
274 bool resize(CbVector<value_type> &vec, const size_type &dataSize, const value_type &value = value_type()) override
275 {
277 return false;
278
279 // new array
280 value_type *new_data = new value_type[dataSize];
281 // copy existing data to array
282 if (this->ptrDataOf(vec)) {
283 for (size_type i = 0; (i < vec.size() && i < dataSize); ++i)
285 delete[] this->ptrDataOf(vec);
286 }
287 this->ptrDataOf(vec) = new_data;
288 // new value for new items
289 for (size_type i = this->dataSizeOf(vec); i < dataSize; ++i)
290 this->ptrDataOf(vec)[i] = value;
291 // assign new dataSize
292 this->dataSizeOf(vec) = dataSize;
293
294 return true;
295 }
296 /*==========================================================*/
298 {
299 if (this->ptrDataOf(vec)) {
300 delete[] this->ptrDataOf(vec);
301 this->ptrDataOf(vec) = NULL;
302 }
303 this->dataSizeOf(vec) = 0;
304 return true;
305 }
306 /*==========================================================*/
307
308private:
309};
310
311#endif // CBVECTOR_H
312
value_type *& ptrDataOf(CbVector< value_type > &vec)
Definition CbVector.h:240
virtual bool alloc(CbVector< value_type > &vec, const size_type &dataSize, const value_type &value=value_type())=0
CbVectorAllocator()=default
virtual bool resize(CbVector< value_type > &vec, const size_type &dataSize, const value_type &value=value_type())=0
virtual bool dealloc(CbVector< value_type > &vec)=0
typename CbVector< T >::value_type value_type
Definition CbVector.h:223
virtual ~CbVectorAllocator()=default
typename CbVector< value_type >::size_type size_type
Definition CbVector.h:224
size_type & dataSizeOf(CbVector< value_type > &vec)
Definition CbVector.h:247
bool resize(CbVector< value_type > &vec, const size_type &dataSize, const value_type &value=value_type()) override
Definition CbVector.h:274
typename CbVector< value_type >::size_type size_type
Definition CbVector.h:264
bool dealloc(CbVector< value_type > &vec) override
Definition CbVector.h:297
typename CbVector< T >::value_type value_type
Definition CbVector.h:263
bool alloc(CbVector< value_type > &src, const size_type &dataSize, const value_type &value=value_type()) override
Definition CbVector.h:269
A class implements a container like a vector.
Definition CbVector.h:67
CbVector & operator=(const std::vector< value_type > &src)
Definition CbVector.h:121
value_type * pointer
Definition CbVector.h:70
void setAllocator(CbVectorAllocator< value_type > *const &allocator)
Definition CbVector.h:151
virtual ~CbVector()
Definition CbVector.h:93
value_type & operator[](const size_type &i)
Definition CbVector.h:184
CbVectorAllocator< value_type > * getAllocator() const
Definition CbVector.h:206
const value_type & operator[](const size_type &i) const
Definition CbVector.h:195
CbVector(const CbVector< value_type > &src)=delete
std::size_t size_type
Definition CbVector.h:71
CbVector(const size_type size, CbVectorAllocator< value_type > *const &allocator=new CbVectorAllocatorStd< value_type >, const value_type &value=value_type())
Definition CbVector.h:85
CbVector & operator=(const CbVector &src)
Definition CbVector.h:102
void swap(CbVector &rhs)
Definition CbVector.h:174
bool empty() const
Definition CbVector.h:165
CbVector(CbVectorAllocator< value_type > *const &allocator=new CbVectorAllocatorStd< value_type >)
Definition CbVector.h:79
bool resize(const size_type &dataSize)
Definition CbVector.h:167
bool resize(const size_type &dataSize, const value_type &value)
Definition CbVector.h:169
bool operator==(const CbVector &rhs) const
Definition CbVector.h:137
size_type size() const
Definition CbVector.h:163
T value_type
Definition CbVector.h:69
std::shared_ptr< T > SPtr
#define UB_THROW(e)
Definition UbException.h:78
#define UB_EXARGS
Definition UbException.h:73
bool isUbEqual(const T1 &a, const T2 &b)
Definition UbEqual.h:259
std::string toString(const T &x, int precision=15)
Definition UbSystem.h:199