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 "span.hpp"
33 #include "iOstream.hpp"
34 #include "iIstream.hpp"
35 
36 #include "stdVectorHelper.hpp"
37 
38 #ifndef __RESERVE__
39 #define __RESERVE__
40  struct RESERVE{};
41 #endif
42 
43 namespace pFlow
44 {
45 
46 
47 template<typename T, typename Allocator>
48 class Vector;
49 
50 #include "VectorFwd.hpp"
51 
52 
53 
54 
55 template<typename T, typename Allocator = vecAllocator<T> >
56 class Vector
57 :
58  public std::vector<T, Allocator>
59 {
60 public:
61 
63 
64  using vectorType = typename std::vector<T, Allocator>;
65 
66  using iterator = typename vectorType::iterator;
67 
68  using const_iterator = typename vectorType::const_iterator;
69 
70  using reference = typename vectorType::reference;
71 
72  using const_reference = typename vectorType::const_reference;
73 
74  using value_type = T;
75 
76  using pointer = T *;
77 
78  using const_pointer = const T *;
79 
80  using init_list = typename std::initializer_list<T>;
81 
82 private:
83 
84  // - name of the vector
86 
87 
88  static constexpr bool isHostAccessible_ = true;
89 
90 
91 
92 
93 public:
94 
95  // - Type info
97 
99 
101  inline Vector()
102  :
103  Vector("Vector")
104  {}
105 
106 
108  inline
109  explicit Vector(const word& name)
110  :
111  name_(name)
112  {}
113 
114 
116  inline
117  Vector(const word& name, size_t len)
118  :
119  vectorType(len),
120  name_(name)
121  {}
122 
124  inline
125  Vector(const word& name, size_t len, const T& val)
126  :
127  Vector(name, len)
128  {
129  this->assign(len, val);
130  }
131 
133  Vector(const word& name, size_t cap, size_t len, RESERVE ):
134  name_(name)
135  {
136  this->reserve(cap);
137  this->resize(len);
138  }
139 
140 
142  inline
143  Vector(const word& name, const init_list &l)
144  :
145  vectorType(l),
146  name_(name)
147  {}
148 
150  inline Vector(const word& name, const vectorType& src)
151  :
152  vectorType(src),
153  name_(name)
154  {}
155 
158  inline Vector(const word& name, const vectorType& src, size_t cap)
159  :
160  Vector(name, cap, src.size(), RESERVE())
161  {
162  this->assign(src.begin(), src.end());
163  }
164 
166  inline Vector(const VectorType& src) = default;
167 
169  inline Vector(const word& name, const Vector<T>& src):
170  vectorType(src),
171  name_(name)
172  {}
173 
175  inline VectorType& operator=( const VectorType& rhs ) = default;
176 
178  inline VectorType& operator=(const vectorType& rhs)
179  {
180  Vector::assign(rhs.begin(), rhs.end());
181  return *this;
182  }
183 
185  Vector( VectorType && mv) noexcept= default;
186 
188  VectorType& operator=( VectorType && mv)noexcept = default;
189 
191  inline void operator=(const T& val)
192  {
193  fill(val);
194  }
195 
197  ~Vector() = default;
198 
199 
202  {
203  return makeUnique<VectorType>(*this);
204  }
205 
207  inline VectorType* clonePtr()const
208  {
209  uniquePtr<VectorType> Ptr = makeUnique<VectorType>(*this);
210  return Ptr.release();
211  }
212 
213 
215 
217  const VectorType& VectorField() const
218  {
219  return *this;
220  }
221 
223  {
224  return *this;
225  }
226 
227  const vectorType& vectorField()const
228  {
229  return *this;
230  }
231 
233  {
234  return *this;
235  }
236 
238  {
239  return *this;
240  }
241 
242  const auto& deviceVectorAll()const
243  {
244  return *this;
245  }
246 
247  auto& deviceVector()
248  {
249  return *this;
250  }
251 
252  const auto& deviceVector()const
253  {
254  return *this;
255  }
256 
257 
259  const word& name()const
260  {
261  return name_;
262  }
263 
265  inline auto size()const
266  {
267  return vectorType::size();
268  }
269 
271  inline auto capacity()const
272  {
273  return vectorType::capacity();
274  }
275 
277  inline bool empty()const
278  {
279  return vectorType::empty();
280  }
281 
284  inline void reserve(size_t cap)
285  {
286  vectorType::reserve(cap);
287  }
288 
289  // - fill the whole content of vector, [begin, end), with val
290  void fill( const T& val);
291 
293  void fill( uint32 start, uint32 end, const T& val);
294 
295  inline
296  auto getSpan()
297  {
298  return span<T>(this->data(), this->size());
299  }
300 
301  inline
302  auto getSpan()const
303  {
304  return span<T>(const_cast<T*>(this->data()), this->size());
305  }
306 
307  static constexpr bool isHostAccessible()
308  {
309  return isHostAccessible_;
310  }
311 
312  void operator +=( const T& val);
313  void operator -=( const T& val);
314  void operator *=( const T& val);
315  void operator /=( const T& val);
316 
317  void operator +=( const VectorType& v );
318  void operator -=( const VectorType& v );
319  void operator /=( const VectorType& v );
320  void operator *=( const VectorType& v );
321 
322  VectorType operator -()const;
323 
324  bool read(iIstream& is)
325  {
326  return readStdVector(is, vectorField());
327  }
328  bool write(iOstream& os)const
329  {
330  return writeStdVector(os, vectorField());
331  }
332 
333  bool read(iIstream& is, const IOPattern& iop)
334  {
335  return readStdVector(is, vectorField(), iop);
336  }
337 
338  bool write(iOstream& os, const IOPattern& iop)const
339  {
340  return writeStdVector(os, vectorField(), iop);
341  }
342 
343  static
344  constexpr const char* memoerySpaceName()
345  {
346  return "std";
347  }
348 };
349 
350 
351 template<typename T, typename Allocator>
353 {
354  if( !ivec.read(is) )
355  {
356  ioErrorInFile (is.name(), is.lineNumber());
357  fatalExit;
358  }
359  return is;
360 }
361 
362 template<typename T, typename Allocator>
364 {
365  if( !ovec.write(os) )
366  {
367  ioErrorInFile(os.name(), os.lineNumber());
368  fatalExit;
369  }
370  return os;
371 }
372 
373 
374 } // pFlow
375 
376 
377 #include "VectorI.hpp"
378 #include "Vector.cpp"
379 #include "VectorMath.hpp"
380 #include "VectorAlgorithm.hpp"
381 
382 #endif
383 
384 
385 
386  /*// - delete elemens of vector based on sorted indices
387  // return false if out of range
388  bool deleteElement_sorted(const Vector<label>& indices );
389 
390  // - delete elemens of vector based on indices
391  // return false if out of range
392  bool deleteElement(const Vector<label>& indices );
393 
394  // - delete elment with index
395  // return false if out of range
396  bool deleteElement(label index);
397 
399  void sortItems(const int32IndexContainer& indices);
400 
401  // - set or insert new elements into the vector
402  // return false if it fails
403  bool insertSetElement(const int32IndexContainer& indices, const T& val);
404 
405  // - set or insert new elements into the vector
406  // return false if it fails
407  bool insertSetElement(const int32IndexContainer& indices, const Vector<T>& vals);
408 
409  // - set or insert new elements into the vector
410  // return false if it fails
411  bool insertSetElement(const Vector<int32>& indices, const T& val);
412 
413  // - set or insert new elements into the vector
414  // return false if it fails
415  bool insertSetElement(const Vector<int32>& indices, const Vector<T>& vals);
416 
417  // - set or insert a new element into the vecor
418  // return false if it fails
419  inline bool insertSetElement(int32 idx, const T& val);*/
pFlow::Vector::read
bool read(iIstream &is)
Definition: Vector.hpp:324
pFlow::span
Definition: span.hpp:31
pFlow::Vector::operator-
VectorType operator-() const
Definition: VectorI.hpp:155
pFlow::Vector::VectorField
VectorType & VectorField()
Definition: Vector.hpp:222
pFlow::Vector< word >::const_iterator
typename vectorType::const_iterator const_iterator
Definition: Vector.hpp:68
pFlow::Vector::operator=
VectorType & operator=(const VectorType &rhs)=default
Copy assignment.
pFlow::Vector::vectorField
const vectorType & vectorField() const
Definition: Vector.hpp:227
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::Vector::deviceVectorAll
const auto & deviceVectorAll() const
Definition: Vector.hpp:242
pFlow::readStdVector
bool readStdVector(iIstream &is, std::vector< T, Allocator > &vec)
Definition: stdVectorHelper.hpp:126
pFlow::Vector::clonePtr
VectorType * clonePtr() const
Clone as a pointer.
Definition: Vector.hpp:207
pFlow::Vector::operator=
VectorType & operator=(const vectorType &rhs)
Copy assignment from std::vector.
Definition: Vector.hpp:178
iIstream.hpp
pFlow::Vector::operator*=
void operator*=(const T &val)
Definition: VectorI.hpp:58
pFlow::Vector::read
bool read(iIstream &is, const IOPattern &iop)
Definition: Vector.hpp:333
pFlow::Vector::name_
word name_
Definition: Vector.hpp:85
pFlow::Vector::operator/=
void operator/=(const T &val)
Definition: VectorI.hpp:68
pFlow::Vector::Vector
Vector(const word &name, size_t len)
Vector with specified length and name.
Definition: Vector.hpp:117
pFlow::Vector::VectorField
const VectorType & VectorField() const
Access to this, mostly used by derived classes.
Definition: Vector.hpp:217
pFlow::Vector::reserve
void reserve(size_t cap)
Reserve capacity for vector Preserve the content.
Definition: Vector.hpp:284
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::Vector::VectorType
Vector< T, Allocator > VectorType
Definition: Vector.hpp:62
pFlow::Vector< word >::const_pointer
const word * const_pointer
Definition: Vector.hpp:78
pFlow::Vector::Vector
Vector()
Empty Vector.
Definition: Vector.hpp:101
pFlow::Vector::Vector
Vector(const word &name, size_t cap, size_t len, RESERVE)
Vector with name, size and reserved capacity.
Definition: Vector.hpp:133
pFlow::Vector::getSpan
auto getSpan()
Definition: Vector.hpp:296
pFlow::Vector::size
auto size() const
Size of the vector.
Definition: Vector.hpp:265
pFlow::Vector::write
bool write(iOstream &os, const IOPattern &iop) const
Definition: Vector.hpp:338
pFlow::Vector::isHostAccessible
static constexpr bool isHostAccessible()
Definition: Vector.hpp:307
pFlow::Vector::operator=
void operator=(const T &val)
Scalar assignment.
Definition: Vector.hpp:191
pFlow
Definition: demGeometry.hpp:27
pFlow::Vector::~Vector
~Vector()=default
Destructor.
RESERVE
Definition: Vector.hpp:40
uniquePtr.hpp
VectorMath.hpp
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::Vector::deviceVectorAll
auto & deviceVectorAll()
Definition: Vector.hpp:237
pFlow::Vector::getSpan
auto getSpan() const
Definition: Vector.hpp:302
pFlow::Vector< word >::pointer
word * pointer
Definition: Vector.hpp:76
pFlow::Vector::Vector
Vector(const word &name, const vectorType &src, size_t cap)
Construct with a name and form std::vector (host memory) and with a desired capacity.
Definition: Vector.hpp:158
pFlow::Vector::capacity
auto capacity() const
Capacity of the vector.
Definition: Vector.hpp:271
stdVectorHelper.hpp
pFlow::Vector< word >::value_type
word value_type
Definition: Vector.hpp:74
VectorFwd.hpp
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::IOPattern
Definition: IOPattern.hpp:32
pFlow::Vector::deviceVector
auto & deviceVector()
Definition: Vector.hpp:247
pFlow::Vector::operator+=
void operator+=(const T &val)
Definition: VectorI.hpp:38
VectorAlgorithm.hpp
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:48
pFlow::Vector< word >::init_list
typename std::initializer_list< word > init_list
Definition: Vector.hpp:80
pFlow::Vector< word >::reference
typename vectorType::reference reference
Definition: Vector.hpp:70
pFlow::Vector::empty
bool empty() const
If vector is empty.
Definition: Vector.hpp:277
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
VectorI.hpp
pFlow::Vector::Vector
Vector(const word &name, const Vector< T > &src)
Copy from src with a new name.
Definition: Vector.hpp:169
pFlow::Vector::isHostAccessible_
static constexpr bool isHostAccessible_
Definition: Vector.hpp:88
pFlow::Vector::write
bool write(iOstream &os) const
Definition: Vector.hpp:328
pFlow::Vector< word >::const_reference
typename vectorType::const_reference const_reference
Definition: Vector.hpp:72
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
pFlow::Vector::name
const word & name() const
Name of the vector.
Definition: Vector.hpp:259
pFlow::Vector::deviceVector
const auto & deviceVector() const
Definition: Vector.hpp:252
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Report an error in file operation with supplied fileName and lineNumber.
Definition: error.hpp:87
pFlow::Vector::fill
void fill(const T &val)
Definition: VectorI.hpp:23
typeInfo.hpp
pFlow::Vector::Vector
Vector(const word &name, size_t len, const T &val)
Vector with name, length and value.
Definition: Vector.hpp:125
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:223
pFlow::Vector< word >::iterator
typename vectorType::iterator iterator
Definition: Vector.hpp:66
iOstream.hpp
pFlow::Vector< word >::vectorType
typename std::vector< word, vecAllocator< word > > vectorType
Definition: Vector.hpp:64
pFlow::Vector::Vector
Vector(const word &name, const vectorType &src)
Construct with a name and form std::vector (host memory)
Definition: Vector.hpp:150
pFlow::Vector
Definition: Vector.hpp:48
pFlow::Vector::TypeInfoTemplateNV111
TypeInfoTemplateNV111("Vector", T, memoerySpaceName())
span.hpp
pFlow::Vector::memoerySpaceName
static constexpr const char * memoerySpaceName()
Definition: Vector.hpp:344
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::Vector::Vector
Vector(const word &name, const init_list &l)
Vector from name and initializer list.
Definition: Vector.hpp:143
stdAlgorithms.hpp
pFlow::Vector::vectorField
vectorType & vectorField()
Definition: Vector.hpp:232
Vector.cpp
pFlow::Vector::clone
uniquePtr< VectorType > clone() const
Clone as a uniquePtr.
Definition: Vector.hpp:201
pFlow::writeStdVector
bool writeStdVector(iOstream &os, const std::vector< T, Allocator > &vec)
Definition: stdVectorHelper.hpp:103
error.hpp
pFlow::Vector::Vector
Vector(const word &name)
Empty Vector with a name.
Definition: Vector.hpp:109