# PhasicFlow: src/phasicFlow/containers/Vector/Vector.hpp Source File

Vector.hpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#ifndef __Vector_hpp__
#define __Vector_hpp__
#include <vector>
#include <algorithm>
#include "typeInfo.hpp"
#include "error.hpp"
#include "uniquePtr.hpp"
#include "stdAlgorithms.hpp"
#include "indexContainer.hpp"
#include "iOstream.hpp"
#include "iIstream.hpp"
#ifndef __RESERVE__
#define __RESERVE__
struct RESERVE{};
#endif
namespace pFlow
{
template<typename T, typename Allocator>
class Vector;
#include "VectorFwd.hpp"
template <class T>
class noConstructAllocator
: public std::allocator<T>
{
public:
using std::allocator<T>::allocator;
template <class U, class... Args> void construct(U*, Args&&...) {}
};
template<typename T>
using vecAllocator = std::allocator<T>;
template<typename T, typename Allocator = vecAllocator<T> >
class Vector
:
public std::vector<T, Allocator>
{
public:
typedef Vector<T, Allocator> VectorType;
typedef typename std::vector<T, Allocator> vectorType;
typedef typename vectorType::iterator iterator;
typedef typename vectorType::const_iterator constIterator;
typedef typename vectorType::reference reference;
typedef typename vectorType::const_reference constReference;
typedef T valueType;
typedef T* pointer;
typedef const T* constPointer;
typedef typename std::initializer_list<T> initList;
protected:
// - name of the vector
word name_;
static inline size_t getVectorStride(const size_t& len)
{
size_t stride = 1;
if( len < 6 ) stride = len;
else if( len <16 ) stride = 3;
else if( len < 31) stride = 2;
else stride = 1;
return stride;
}
static constexpr bool isHostAccessible_ = true;
constexpr static inline const char* memoerySpaceName()
{
return "std";
}
public:
// - Type info
TypeInfoTemplateNV2("Vector", T, memoerySpaceName());
// - empty Vector
inline Vector()
:
Vector("Vector")
{}
inline Vector(const word& name)
:
name_(name)
{}
// - with sepcified length
inline Vector(const size_t len)
:
Vector("Vector",len)
{}
// - with specified length and name
inline Vector(const word& name, size_t len)
:
vectorType(len),
name_(name)
{
}
// - with length and value
inline Vector(size_t len, const T& val)
:
Vector("Vector", len, val)
{}
inline Vector(const word& name, size_t len, const T& val)
:
Vector(name, len)
{
this->assign(len, val);
}
// - zero length with specified capacity, use Logical
// to make it different from previous constructor.
inline Vector(const size_t cap, RESERVE ):
Vector("Vector", cap, 0, RESERVE())
{
}
inline Vector(const size_t cap, const size_t len, RESERVE )
:
Vector("Vector", cap, len, RESERVE())
{
}
Vector(const word& name, size_t cap, size_t len, RESERVE ):
name_(name)
{
this->reserve(cap);
this->resize(len);
}
inline Vector(const size_t cap, const size_t len, const T& val, RESERVE )
{
name_ = "Vector";
reserve(cap);
this->assign(len, val);
}
// from initializer list
inline Vector(const initList &l)
:
vectorType(l)
{}
// - from src and a new name
inline Vector(const word name, const Vector<T>& src):
vectorType(src),
name_(name)
{}
// copy construct
inline Vector(const VectorType& src) = default;
// move construct
inline Vector( VectorType && mv) = default;
inline Vector(const vectorType& src)
:
vectorType(src),
name_("Vector")
{
}
// copy assignment
inline VectorType& operator=( const VectorType& rhs ) = default;
inline VectorType& operator=(const vectorType& rhs)
{
Vector::assign(rhs.begin(), rhs.end());
return *this;
}
// move assignment
inline VectorType& operator=( VectorType && mv) = default;
// scalar assignment
inline void operator=(const T& val)
{
fill(val);
}
inline ~Vector()
{
vectorType::clear();
}
inline uniquePtr<VectorType> clone() const
{
return makeUnique<VectorType>(*this);
}
inline VectorType* clonePtr()const
{
return new VectorType(*this);
}
inline auto clear()
{
return vectorType::clear();
}
// access to this, mostly used by derived classes
const VectorType& VectorField() const
{
return *this;
}
VectorType& VectorField()
{
return *this;
}
const vectorType& vectorField()const
{
return *this;
}
vectorType& vectorField()
{
return *this;
}
auto& deviceVectorAll()
{
return *this;
}
const auto& deviceVectorAll()const
{
return *this;
}
auto& deviceVector()
{
return *this;
}
const auto& deviceVector()const
{
return *this;
}
const word& name()const
{
return name_;
}
inline auto size()const
{
return vectorType::size();
}
inline auto capacity()const
{
return vectorType::capacity();
}
inline auto reserve(label len)
{
return vectorType::reserve(len);
}
// - delete elemens of vector based on sorted indices
// return false if out of range
bool deleteElement_sorted(const Vector<label>& indices );
// - delete elemens of vector based on indices
// return false if out of range
bool deleteElement(const Vector<label>& indices );
// - delete elment with index
// return false if out of range
bool deleteElement(label index);
// - set or insert new elements into the vector
// return false if it fails
bool insertSetElement(const int32IndexContainer& indices, const T& val);
// - set or insert new elements into the vector
// return false if it fails
bool insertSetElement(const int32IndexContainer& indices, const Vector<T>& vals);
// - set or insert new elements into the vector
// return false if it fails
bool insertSetElement(const Vector<int32>& indices, const T& val);
// - set or insert new elements into the vector
// return false if it fails
bool insertSetElement(const Vector<int32>& indices, const Vector<T>& vals);
// - set or insert a new element into the vecor
// return false if it fails
inline bool insertSetElement(int32 idx, const T& val);
// - fill the whole content of vector, [begin, end), with val
inline void fill( const T& val);
static constexpr bool isHostAccessible()
{
return isHostAccessible_;
}
inline void operator +=( const T& val);
inline void operator -=( const T& val);
inline void operator *=( const T& val);
inline void operator /=( const T& val);
inline void operator +=( const VectorType& v );
inline void operator -=( const VectorType& v );
inline void operator /=( const VectorType& v );
inline void operator *=( const VectorType& v );
inline VectorType operator -()const;
// from iIstream and specified size
//Vector(iIstream & is, size_t len);
// from iIstream and free size
Vector(iIstream& is);
//bool readVector(iIstream & is, size_t len);
bool readVector(iIstream& is);
bool writeVector(iOstream& os) const;
bool read(iIstream& is)
{
return readVector(is);
}
bool write(iOstream& os)const
{
return writeVector(os);
}
};
template<typename T, typename Allocator>
inline iIstream& operator >> (iIstream & is, Vector<T, Allocator> & ivec )
{
if( !ivec.readVector(is) )
{
ioErrorInFile (is.name(), is.lineNumber());
fatalExit;
}
return is;
}
template<typename T, typename Allocator>
inline iOstream& operator << (iOstream& os, const Vector<T, Allocator>& ovec )
{
if( !ovec.writeVector(os) )
{
ioErrorInFile(os.name(), os.lineNumber());
fatalExit;
}
return os;
}
} // pFlow
#include "VectorI.hpp"
#include "Vector.cpp"
#include "VectorMath.hpp"
#include "VectorAlgorithm.hpp"
#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