# PhasicFlow: src/phasicFlow/containers/Vector/Vector.hpp Source File
PhasicFlow is an open-source parallel DEM (discrete element method) package for simulating granular flow. It is developed in C++ and can be exectued on both GPU (like CUDA) and CPU.

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 "span.hpp"
#include "iOstream.hpp"
#include "iIstream.hpp"
#include "stdVectorHelper.hpp"
#ifndef __RESERVE__
#define __RESERVE__
struct RESERVE{};
#endif
namespace pFlow
{
template<typename T, typename Allocator>
class Vector;
#include "VectorFwd.hpp"
template<typename T, typename Allocator = vecAllocator<T> >
class Vector
:
public std::vector<T, Allocator>
{
public:
using VectorType = Vector<T, Allocator>;
using vectorType = typename std::vector<T, Allocator>;
using iterator = typename vectorType::iterator;
using const_iterator = typename vectorType::const_iterator;
using reference = typename vectorType::reference;
using const_reference = typename vectorType::const_reference;
using value_type = T;
using pointer = T *;
using const_pointer = const T *;
using init_list = typename std::initializer_list<T>;
private:
// - name of the vector
word name_;
static constexpr bool isHostAccessible_ = true;
public:
// - Type info
TypeInfoTemplateNV111("Vector", T, memoerySpaceName());
inline Vector()
:
Vector("Vector")
{}
inline
explicit Vector(const word& name)
:
name_(name)
{}
inline
Vector(const word& name, size_t len)
:
vectorType(len),
name_(name)
{}
inline
Vector(const word& name, size_t len, const T& val)
:
Vector(name, len)
{
this->assign(len, val);
}
Vector(const word& name, size_t cap, size_t len, RESERVE ):
name_(name)
{
this->reserve(cap);
this->resize(len);
}
inline
Vector(const word& name, const init_list &l)
:
vectorType(l),
name_(name)
{}
inline Vector(const word& name, const vectorType& src)
:
vectorType(src),
name_(name)
{}
inline Vector(const word& name, const vectorType& src, size_t cap)
:
Vector(name, cap, src.size(), RESERVE())
{
this->assign(src.begin(), src.end());
}
inline Vector(const VectorType& src) = default;
inline Vector(const word& name, const Vector<T>& src):
vectorType(src),
name_(name)
{}
inline VectorType& operator=( const VectorType& rhs ) = default;
inline VectorType& operator=(const vectorType& rhs)
{
Vector::assign(rhs.begin(), rhs.end());
return *this;
}
Vector( VectorType && mv) noexcept= default;
VectorType& operator=( VectorType && mv)noexcept = default;
inline void operator=(const T& val)
{
fill(val);
}
~Vector() = default;
inline uniquePtr<VectorType> clone() const
{
return makeUnique<VectorType>(*this);
}
inline VectorType* clonePtr()const
{
uniquePtr<VectorType> Ptr = makeUnique<VectorType>(*this);
return Ptr.release();
}
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 bool empty()const
{
return vectorType::empty();
}
inline void reserve(size_t cap)
{
vectorType::reserve(cap);
}
// - fill the whole content of vector, [begin, end), with val
void fill( const T& val);
void fill( uint32 start, uint32 end, const T& val);
inline
auto getSpan()
{
return span<T>(this->data(), this->size());
}
inline
auto getSpan()const
{
return span<T>(const_cast<T*>(this->data()), this->size());
}
static constexpr bool isHostAccessible()
{
return isHostAccessible_;
}
void operator +=( const T& val);
void operator -=( const T& val);
void operator *=( const T& val);
void operator /=( const T& val);
void operator +=( const VectorType& v );
void operator -=( const VectorType& v );
void operator /=( const VectorType& v );
void operator *=( const VectorType& v );
VectorType operator -()const;
bool read(iIstream& is)
{
return readStdVector(is, vectorField());
}
bool write(iOstream& os)const
{
return writeStdVector(os, vectorField());
}
bool read(iIstream& is, const IOPattern& iop)
{
return readStdVector(is, vectorField(), iop);
}
bool write(iOstream& os, const IOPattern& iop)const
{
return writeStdVector(os, vectorField(), iop);
}
static
constexpr const char* memoerySpaceName()
{
return "std";
}
};
template<typename T, typename Allocator>
inline iIstream& operator >> (iIstream & is, Vector<T, Allocator> & ivec )
{
if( !ivec.read(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.write(os) )
{
ioErrorInFile(os.name(), os.lineNumber());
fatalExit;
}
return os;
}
} // pFlow
#include "VectorI.hpp"
#include "Vector.cpp"
#include "VectorMath.hpp"
#include "VectorAlgorithm.hpp"
#endif
/*// - 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);
void sortItems(const int32IndexContainer& indices);
// - 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);*/
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