www.cemf.ir
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 
295 
296  const word& name()const
297  {
298  return name_;
299  }
300 
301  inline auto size()const
302  {
303  return vectorType::size();
304  }
305 
306  inline auto capacity()const
307  {
308  return vectorType::capacity();
309  }
310 
311  inline auto reserve(label len)
312  {
313  return vectorType::reserve(len);
314  }
315 
316  // - delete elemens of vector based on sorted indices
317  // return false if out of range
318  bool deleteElement_sorted(const Vector<label>& indices );
319 
320  // - delete elemens of vector based on indices
321  // return false if out of range
322  bool deleteElement(const Vector<label>& indices );
323 
324  // - delete elment with index
325  // return false if out of range
326  bool deleteElement(label index);
327 
329  void sortItems(const int32IndexContainer& indices);
330 
331  // - set or insert new elements into the vector
332  // return false if it fails
333  bool insertSetElement(const int32IndexContainer& indices, const T& val);
334 
335  // - set or insert new elements into the vector
336  // return false if it fails
337  bool insertSetElement(const int32IndexContainer& indices, const Vector<T>& vals);
338 
339  // - set or insert new elements into the vector
340  // return false if it fails
341  bool insertSetElement(const Vector<int32>& indices, const T& val);
342 
343  // - set or insert new elements into the vector
344  // return false if it fails
345  bool insertSetElement(const Vector<int32>& indices, const Vector<T>& vals);
346 
347  // - set or insert a new element into the vecor
348  // return false if it fails
349  inline bool insertSetElement(int32 idx, const T& val);
350 
351  // - fill the whole content of vector, [begin, end), with val
352  inline void fill( const T& val);
353 
354  static constexpr bool isHostAccessible()
355  {
356  return isHostAccessible_;
357  }
358 
359  inline void operator +=( const T& val);
360  inline void operator -=( const T& val);
361  inline void operator *=( const T& val);
362  inline void operator /=( const T& val);
363 
364  inline void operator +=( const VectorType& v );
365  inline void operator -=( const VectorType& v );
366  inline void operator /=( const VectorType& v );
367  inline void operator *=( const VectorType& v );
368 
369  inline VectorType operator -()const;
370 
371  // from iIstream and specified size
372  //Vector(iIstream & is, size_t len);
373 
374  // from iIstream and free size
375  Vector(iIstream& is);
376 
377  bool readVector(iIstream& is, size_t len=0);
378 
379  bool writeVector(iOstream& os) const;
380 
381  bool read(iIstream& is)
382  {
383  return readVector(is);
384  }
385 
386  bool write(iOstream& os)const
387  {
388  return writeVector(os);
389  }
390 
391 };
392 
393 
394 template<typename T, typename Allocator>
396 {
397  if( !ivec.readVector(is) )
398  {
399  ioErrorInFile (is.name(), is.lineNumber());
400  fatalExit;
401  }
402  return is;
403 }
404 
405 template<typename T, typename Allocator>
407 {
408 
409  if( !ovec.writeVector(os) )
410  {
411  ioErrorInFile(os.name(), os.lineNumber());
412  fatalExit;
413  }
414 
415  return os;
416 }
417 
418 
419 
420 } // pFlow
421 
422 
423 #include "VectorI.hpp"
424 #include "Vector.cpp"
425 #include "VectorMath.hpp"
426 #include "VectorAlgorithm.hpp"
427 
428 #endif
pFlow::Vector::read
bool read(iIstream &is)
Definition: Vector.hpp:381
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::readVector
bool readVector(iIstream &is, size_t len=0)
Definition: Vector.cpp:30
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::sortItems
void sortItems(const int32IndexContainer &indices)
Sort elements based on the indices.
Definition: Vector.cpp:217
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:301
pFlow::Vector::isHostAccessible
static constexpr bool isHostAccessible()
Definition: Vector.hpp:354
pFlow::Vector::operator=
void operator=(const T &val)
Definition: Vector.hpp:228
pFlow::Vector::deleteElement
bool deleteElement(const Vector< label > &indices)
Definition: Vector.cpp:182
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:101
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:140
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:306
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:311
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::memoerySpaceName
constexpr static const char * memoerySpaceName()
Definition: Vector.hpp:109
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
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:386
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:296
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
Const access to the current stream line number.
Definition: IOstream.hpp:221
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:237
error.hpp
pFlow::Vector::Vector
Vector(const word &name)
Definition: Vector.hpp:128