Vector.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #ifndef __Vector_hpp__
23 #define __Vector_hpp__
24 
25 #include <vector>
26 #include <algorithm>
27 
28 #include "typeInfo.hpp"
29 #include "error.hpp"
30 #include "uniquePtr.hpp"
31 #include "stdAlgorithms.hpp"
32 #include "indexContainer.hpp"
33 #include "iOstream.hpp"
34 #include "iIstream.hpp"
35 
36 #ifndef __RESERVE__
37 #define __RESERVE__
38  struct RESERVE{};
39 #endif
40 
41 namespace pFlow
42 {
43 
44 
45 template<typename T, typename Allocator>
46 class Vector;
47 
48 #include "VectorFwd.hpp"
49 
50 
51 template <class T>
53  : public std::allocator<T>
54 {
55 public:
56  using std::allocator<T>::allocator;
57 
58  template <class U, class... Args> void construct(U*, Args&&...) {}
59 };
60 
61 template<typename T>
62 using vecAllocator = std::allocator<T>;
63 
64 template<typename T, typename Allocator = vecAllocator<T> >
65 class Vector
66 :
67  public std::vector<T, Allocator>
68 {
69 public:
70 
72 
73  typedef typename std::vector<T, Allocator> vectorType;
74 
75  typedef typename vectorType::iterator iterator;
76 
77  typedef typename vectorType::const_iterator constIterator;
78 
79  typedef typename vectorType::reference reference;
80 
81  typedef typename vectorType::const_reference constReference;
82 
83  typedef T valueType;
84 
85  typedef T* pointer;
86 
87  typedef const T* constPointer;
88 
89  typedef typename std::initializer_list<T> initList;
90 
91 protected:
92 
93  // - name of the vector
95 
96  static inline size_t getVectorStride(const size_t& len)
97  {
98  size_t stride = 1;
99  if( len < 6 ) stride = len;
100  else if( len <16 ) stride = 3;
101  else if( len < 31) stride = 2;
102  else stride = 1;
103 
104  return stride;
105  }
106 
107  static constexpr bool isHostAccessible_ = true;
108 
109  constexpr static inline const char* memoerySpaceName()
110  {
111  return "std";
112  }
113 
114 
115 public:
116 
117  // - Type info
118  TypeInfoTemplateNV2("Vector", T, memoerySpaceName());
119 
121 
122  // - empty Vector
123  inline Vector()
124  :
125  Vector("Vector")
126  {}
127 
128  inline Vector(const word& name)
129  :
130  name_(name)
131  {}
132  // - with sepcified length
133  inline Vector(const size_t len)
134  :
135  Vector("Vector",len)
136  {}
137 
138  // - with specified length and name
139  inline Vector(const word& name, size_t len)
140  :
141  vectorType(len),
142  name_(name)
143  {
144 
145  }
146 
147  // - with length and value
148  inline Vector(size_t len, const T& val)
149  :
150  Vector("Vector", len, val)
151  {}
152 
153  inline Vector(const word& name, size_t len, const T& val)
154  :
155  Vector(name, len)
156  {
157  this->assign(len, val);
158  }
159 
160  // - zero length with specified capacity, use Logical
161  // to make it different from previous constructor.
162  inline Vector(const size_t cap, RESERVE ):
163  Vector("Vector", cap, 0, RESERVE())
164  {
165  }
166 
167  inline Vector(const size_t cap, const size_t len, RESERVE )
168  :
169  Vector("Vector", cap, len, RESERVE())
170  {
171 
172  }
173 
174  Vector(const word& name, size_t cap, size_t len, RESERVE ):
175  name_(name)
176  {
177  this->reserve(cap);
178  this->resize(len);
179  }
180 
181  inline Vector(const size_t cap, const size_t len, const T& val, RESERVE )
182  {
183  name_ = "Vector";
184  reserve(cap);
185  this->assign(len, val);
186  }
187 
188 
189  // from initializer list
190  inline Vector(const initList &l)
191  :
192  vectorType(l)
193  {}
194 
195  // - from src and a new name
196  inline Vector(const word name, const Vector<T>& src):
197  vectorType(src),
198  name_(name)
199  {}
200 
201  // copy construct
202  inline Vector(const VectorType& src) = default;
203 
204  // move construct
205  inline Vector( VectorType && mv) = default;
206 
207  inline Vector(const vectorType& src)
208  :
209  vectorType(src),
210  name_("Vector")
211  {
212 
213  }
214 
215  // copy assignment
216  inline VectorType& operator=( const VectorType& rhs ) = default;
217 
218  inline VectorType& operator=(const vectorType& rhs)
219  {
220  Vector::assign(rhs.begin(), rhs.end());
221  return *this;
222  }
223 
224  // move assignment
225  inline VectorType& operator=( VectorType && mv) = default;
226 
227  // scalar assignment
228  inline void operator=(const T& val)
229  {
230  fill(val);
231  }
232 
233  inline ~Vector()
234  {
235  vectorType::clear();
236  }
237 
239  {
240  return makeUnique<VectorType>(*this);
241  }
242 
243  inline VectorType* clonePtr()const
244  {
245  return new VectorType(*this);
246  }
247 
248  inline auto clear()
249  {
250  return vectorType::clear();
251  }
252 
253  // access to this, mostly used by derived classes
254  const VectorType& VectorField() const
255  {
256  return *this;
257  }
258 
260  {
261  return *this;
262  }
263 
264  const vectorType& vectorField()const
265  {
266  return *this;
267  }
268 
270  {
271  return *this;
272  }
273 
275  {
276  return *this;
277  }
278 
279  const auto& deviceVectorAll()const
280  {
281  return *this;
282  }
283 
284  auto& deviceVector()
285  {
286  return *this;
287  }
288 
289  const auto& deviceVector()const
290  {
291  return *this;
292  }
293 
294  const word& name()const
295  {
296  return name_;
297  }
298 
299  inline auto size()const
300  {
301  return vectorType::size();
302  }
303 
304  inline auto capacity()const
305  {
306  return vectorType::capacity();
307  }
308 
309  inline auto reserve(label len)
310  {
311  return vectorType::reserve(len);
312  }
313 
314  // - delete elemens of vector based on sorted indices
315  // return false if out of range
316  bool deleteElement_sorted(const Vector<label>& indices );
317 
318  // - delete elemens of vector based on indices
319  // return false if out of range
320  bool deleteElement(const Vector<label>& indices );
321 
322  // - delete elment with index
323  // return false if out of range
324  bool deleteElement(label index);
325 
326  // - set or insert new elements into the vector
327  // return false if it fails
328  bool insertSetElement(const int32IndexContainer& indices, const T& val);
329 
330  // - set or insert new elements into the vector
331  // return false if it fails
332  bool insertSetElement(const int32IndexContainer& indices, const Vector<T>& vals);
333 
334  // - set or insert new elements into the vector
335  // return false if it fails
336  bool insertSetElement(const Vector<int32>& indices, const T& val);
337 
338  // - set or insert new elements into the vector
339  // return false if it fails
340  bool insertSetElement(const Vector<int32>& indices, const Vector<T>& vals);
341 
342  // - set or insert a new element into the vecor
343  // return false if it fails
344  inline bool insertSetElement(int32 idx, const T& val);
345 
346  // - fill the whole content of vector, [begin, end), with val
347  inline void fill( const T& val);
348 
349  static constexpr bool isHostAccessible()
350  {
351  return isHostAccessible_;
352  }
353 
354  inline void operator +=( const T& val);
355  inline void operator -=( const T& val);
356  inline void operator *=( const T& val);
357  inline void operator /=( const T& val);
358 
359  inline void operator +=( const VectorType& v );
360  inline void operator -=( const VectorType& v );
361  inline void operator /=( const VectorType& v );
362  inline void operator *=( const VectorType& v );
363 
364  inline VectorType operator -()const;
365 
366  // from iIstream and specified size
367  //Vector(iIstream & is, size_t len);
368 
369  // from iIstream and free size
370  Vector(iIstream& is);
371 
372  //bool readVector(iIstream & is, size_t len);
373 
374  bool readVector(iIstream& is);
375 
376  bool writeVector(iOstream& os) const;
377 
378  bool read(iIstream& is)
379  {
380  return readVector(is);
381  }
382 
383  bool write(iOstream& os)const
384  {
385  return writeVector(os);
386  }
387 
388 };
389 
390 
391 template<typename T, typename Allocator>
393 {
394  if( !ivec.readVector(is) )
395  {
396  ioErrorInFile (is.name(), is.lineNumber());
397  fatalExit;
398  }
399  return is;
400 }
401 
402 template<typename T, typename Allocator>
404 {
405 
406  if( !ovec.writeVector(os) )
407  {
408  ioErrorInFile(os.name(), os.lineNumber());
409  fatalExit;
410  }
411 
412  return os;
413 }
414 
415 
416 
417 } // pFlow
418 
419 
420 #include "VectorI.hpp"
421 #include "Vector.cpp"
422 #include "VectorMath.hpp"
423 #include "VectorAlgorithm.hpp"
424 
425 #endif
pFlow::Vector::read
bool read(iIstream &is)
Definition: Vector.hpp:378
pFlow::Vector::Vector
Vector(const vectorType &src)
Definition: Vector.hpp:207
pFlow::Vector::initList
std::initializer_list< T > initList
Definition: Vector.hpp:89
pFlow::Vector::operator-
VectorType operator-() const
Definition: VectorI.hpp:146
pFlow::Vector::constReference
vectorType::const_reference constReference
Definition: Vector.hpp:81
pFlow::Vector::VectorField
VectorType & VectorField()
Definition: Vector.hpp:259
pFlow::Vector::Vector
Vector(const size_t cap, RESERVE)
Definition: Vector.hpp:162
pFlow::Vector::operator=
VectorType & operator=(const VectorType &rhs)=default
pFlow::Vector::vectorField
const vectorType & vectorField() const
Definition: Vector.hpp:264
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::Vector::deviceVectorAll
const auto & deviceVectorAll() const
Definition: Vector.hpp:279
pFlow::Vector::clonePtr
VectorType * clonePtr() const
Definition: Vector.hpp:243
pFlow::Vector::operator=
VectorType & operator=(const vectorType &rhs)
Definition: Vector.hpp:218
iIstream.hpp
pFlow::Vector::~Vector
~Vector()
Definition: Vector.hpp:233
pFlow::Vector::operator*=
void operator*=(const T &val)
Definition: VectorI.hpp:49
pFlow::Vector::name_
word name_
Definition: Vector.hpp:94
pFlow::noConstructAllocator::construct
void construct(U *, Args &&...)
Definition: Vector.hpp:58
pFlow::Vector::operator/=
void operator/=(const T &val)
Definition: VectorI.hpp:59
pFlow::Vector::Vector
Vector(const word &name, size_t len)
Definition: Vector.hpp:139
pFlow::Vector::VectorField
const VectorType & VectorField() const
Definition: Vector.hpp:254
pFlow::Vector::constIterator
vectorType::const_iterator constIterator
Definition: Vector.hpp:77
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::Vector::Vector
Vector()
Definition: Vector.hpp:123
pFlow::Vector::Vector
Vector(const word &name, size_t cap, size_t len, RESERVE)
Definition: Vector.hpp:174
pFlow::Vector::Vector
Vector(size_t len, const T &val)
Definition: Vector.hpp:148
pFlow::Vector::size
auto size() const
Definition: Vector.hpp:299
pFlow::Vector::isHostAccessible
static constexpr bool isHostAccessible()
Definition: Vector.hpp:349
pFlow::Vector::operator=
void operator=(const T &val)
Definition: Vector.hpp:228
pFlow::Vector::deleteElement
bool deleteElement(const Vector< label > &indices)
Definition: Vector.cpp:166
pFlow::vecAllocator
std::allocator< T > vecAllocator
Definition: Vector.hpp:62
pFlow
Definition: demComponent.hpp:28
pFlow::Vector::writeVector
bool writeVector(iOstream &os) const
Definition: Vector.cpp:90
RESERVE
Definition: Vector.hpp:38
uniquePtr.hpp
pFlow::Vector::Vector
Vector(const size_t cap, const size_t len, RESERVE)
Definition: Vector.hpp:167
VectorMath.hpp
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::Vector::deleteElement_sorted
bool deleteElement_sorted(const Vector< label > &indices)
Definition: Vector.cpp:124
pFlow::Vector::getVectorStride
static size_t getVectorStride(const size_t &len)
Definition: Vector.hpp:96
pFlow::Vector::deviceVectorAll
auto & deviceVectorAll()
Definition: Vector.hpp:274
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::Vector::Vector
Vector(const word name, const Vector< T > &src)
Definition: Vector.hpp:196
pFlow::Vector::capacity
auto capacity() const
Definition: Vector.hpp:304
pFlow::Vector::Vector
Vector(const initList &l)
Definition: Vector.hpp:190
pFlow::Vector::valueType
T valueType
Definition: Vector.hpp:83
pFlow::Vector::reserve
auto reserve(label len)
Definition: Vector.hpp:309
pFlow::Vector::reference
vectorType::reference reference
Definition: Vector.hpp:79
pFlow::noConstructAllocator
Definition: Vector.hpp:52
VectorFwd.hpp
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::Vector::deviceVector
auto & deviceVector()
Definition: Vector.hpp:284
pFlow::Vector::operator+=
void operator+=(const T &val)
Definition: VectorI.hpp:29
VectorAlgorithm.hpp
pFlow::Vector::iterator
vectorType::iterator iterator
Definition: Vector.hpp:75
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::Vector::operator-=
void operator-=(const T &val)
Definition: VectorI.hpp:39
pFlow::Vector::constPointer
const typedef T * constPointer
Definition: Vector.hpp:87
pFlow::Vector::readVector
bool readVector(iIstream &is)
Definition: Vector.cpp:30
pFlow::Vector::memoerySpaceName
constexpr static const char * memoerySpaceName()
Definition: Vector.hpp:109
pFlow::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
VectorI.hpp
pFlow::Vector::clear
auto clear()
Definition: Vector.hpp:248
pFlow::Vector::isHostAccessible_
static constexpr bool isHostAccessible_
Definition: Vector.hpp:107
pFlow::Vector::Vector
Vector(const size_t len)
Definition: Vector.hpp:133
pFlow::Vector::VectorType
Vector< T, Allocator > VectorType
Definition: Vector.hpp:71
pFlow::Vector::Vector
Vector(const size_t cap, const size_t len, const T &val, RESERVE)
Definition: Vector.hpp:181
pFlow::Vector::write
bool write(iOstream &os) const
Definition: Vector.hpp:383
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
pFlow::Vector::TypeInfoTemplateNV2
TypeInfoTemplateNV2("Vector", T, memoerySpaceName())
pFlow::Vector::pointer
T * pointer
Definition: Vector.hpp:85
pFlow::Vector::name
const word & name() const
Definition: Vector.hpp:294
pFlow::Vector::deviceVector
const auto & deviceVector() const
Definition: Vector.hpp:289
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
pFlow::Vector::fill
void fill(const T &val)
Definition: VectorI.hpp:22
typeInfo.hpp
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::Vector::Vector
Vector(const word &name, size_t len, const T &val)
Definition: Vector.hpp:153
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
iOstream.hpp
pFlow::Vector
Definition: Vector.hpp:46
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::Vector::vectorType
std::vector< T, Allocator > vectorType
Definition: Vector.hpp:73
stdAlgorithms.hpp
indexContainer.hpp
pFlow::Vector::vectorField
vectorType & vectorField()
Definition: Vector.hpp:269
Vector.cpp
pFlow::indexContainer< int32 >
pFlow::Vector::clone
uniquePtr< VectorType > clone() const
Definition: Vector.hpp:238
pFlow::Vector::insertSetElement
bool insertSetElement(const int32IndexContainer &indices, const T &val)
Definition: Vector.cpp:201
error.hpp
pFlow::Vector::Vector
Vector(const word &name)
Definition: Vector.hpp:128