Commit 1c4e32fb authored by Mark OLESEN's avatar Mark OLESEN Committed by Andrew Heather
Browse files

ENH: add iterators to VectorSpace (#1265)

- this adds support for various STL operations including

    * sorting, filling, find min/max element etc.
    * for-range iteration

STYLE: use constexpr for VectorSpace rank
parent f3670521
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -31,6 +31,7 @@ Description
#include "vector.H"
#include "IOstreams.H"
#include <algorithm>
using namespace Foam;
......@@ -74,6 +75,18 @@ void doTest(vector& vec1, vector& vec2)
}
template<class VecSpace>
void testIterator(const VecSpace& vs)
{
Info<< "size: " << vs.size() << " for:";
for (const auto& val : vs)
{
Info<< " " << val;
}
Info<< nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
......@@ -89,8 +102,21 @@ int main(int argc, char *argv[])
vector vec2(0.5, 0.51, -0.5);
doTest(vec1, vec2);
testIterator(vec1);
testIterator(vec2);
// Use STL algorithm(s)
std::sort(vec2.begin(), vec2.end());
Info<< "sorted: " << vec2 << nl;
std::random_shuffle(vec2.begin(), vec2.end());
Info<< "shuffled: " << vec2 << nl;
}
Info<< "\nEnd\n" << nl;
return 0;
}
......
......@@ -67,7 +67,7 @@ public:
// Member constants
//- Rank of BarycentricTensor is 2
static const direction rank = 2;
static constexpr direction rank = 2;
//- Component labeling enumeration
......
......@@ -67,7 +67,7 @@ public:
// Member constants
//- Rank of DiagTensor is 2
static const direction rank = 2;
static constexpr direction rank = 2;
//- Component labeling enumeration
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2016 OpenFOAM Foundation
......@@ -68,20 +68,20 @@ public:
// Member constants
static const direction mRows = Mrows;
static const direction nCols = Ncols;
static constexpr direction mRows = Mrows;
static constexpr direction nCols = Ncols;
// Static member functions
//- Return the number of rows
static direction m()
static direction m() noexcept
{
return Mrows;
}
//- Return the number of columns
static direction n()
static direction n() noexcept
{
return Ncols;
}
......
......@@ -345,7 +345,7 @@ inline const Cmpt& Foam::MatrixSpace<Form, Cmpt, Mrows, Ncols>::operator()
) const
{
#ifdef FULLDEBUG
if (i > Mrows-1 || j > Ncols-1)
if (i >= Mrows || j >= Ncols)
{
FatalErrorInFunction
<< "indices out of range"
......@@ -365,7 +365,7 @@ inline Cmpt& Foam::MatrixSpace<Form, Cmpt, Mrows, Ncols>::operator()
)
{
#ifdef FULLDEBUG
if (i > Mrows-1 || j > Ncols-1)
if (i >= Mrows || j >= Ncols)
{
FatalErrorInFunction
<< "indices out of range"
......
......@@ -65,7 +65,7 @@ public:
// Member constants
//- Rank of SphericalTensor is 2
static const direction rank = 2;
static constexpr direction rank = 2;
// Static data members
......
......@@ -61,7 +61,7 @@ public:
// Member constants
//- Rank of SphericalTensor2D is 2
static const direction rank = 2;
static constexpr direction rank = 2;
// Static data members
......
......@@ -67,7 +67,7 @@ public:
// Member constants
//- Rank of SymmTensor is 2
static const direction rank = 2;
static constexpr direction rank = 2;
// Static data members
......
......@@ -67,7 +67,7 @@ public:
// Member constants
//- Rank of SymmTensor2D is 2
static const direction rank = 2;
static constexpr direction rank = 2;
// Static data members
......
......@@ -53,8 +53,9 @@ See also
namespace Foam
{
template<class Cmpt>
class SymmTensor;
// Forward Declarations
template<class Cmpt> class SymmTensor;
/*---------------------------------------------------------------------------*\
Class Tensor Declaration
......@@ -75,7 +76,7 @@ public:
// Member constants
//- Rank of Tensor is 2
static const direction rank = 2;
static constexpr direction rank = 2;
// Static data members
......
......@@ -70,7 +70,7 @@ public:
// Member constants
//- Rank of Tensor2D is 2
static const direction rank = 2;
static constexpr direction rank = 2;
// Static data members
......
......@@ -72,7 +72,7 @@ public:
// Member constants
//- Rank of Vector is 1
static const direction rank = 1;
static constexpr direction rank = 1;
//- Component labeling enumeration
......
......@@ -65,7 +65,7 @@ public:
// Member constants
//- Rank of Vector2D is 1
static const direction rank = 1;
static constexpr direction rank = 1;
//- Component labeling enumeration
......
......@@ -38,18 +38,15 @@ Foam::VectorSpace<Form, Cmpt, Ncmpts>::VectorSpace
Istream& is
)
{
// Read beginning of VectorSpace<Cmpt>
is.readBegin("VectorSpace<Form, Cmpt, Ncmpts>");
is.readBegin("VectorSpace");
for (direction i=0; i<Ncmpts; i++)
{
is >> v_[i];
}
// Read end of VectorSpace<Cmpt>
is.readEnd("VectorSpace<Form, Cmpt, Ncmpts>");
is.readEnd("VectorSpace");
// Check state of Istream
is.check(FUNCTION_NAME);
}
......@@ -64,7 +61,7 @@ Foam::word Foam::name
buf << '(' << vs.v_[0];
for (direction i=1; i<Ncmpts; i++)
for (direction i=1; i<Ncmpts; ++i)
{
buf << ',' << vs.v_[i];
}
......@@ -84,18 +81,15 @@ Foam::Istream& Foam::operator>>
VectorSpace<Form, Cmpt, Ncmpts>& vs
)
{
// Read beginning of VectorSpace<Cmpt, Ncmpts>
is.readBegin("VectorSpace<Form, Cmpt, Ncmpts>");
is.readBegin("VectorSpace");
for (direction i=0; i<Ncmpts; i++)
{
is >> vs.v_[i];
}
// Read end of VectorSpace<Cmpt, Ncmpts>
is.readEnd("VectorSpace<Form, Cmpt, Ncmpts>");
is.readEnd("VectorSpace");
// Check state of Istream
is.check(FUNCTION_NAME);
return is;
......@@ -111,7 +105,7 @@ Foam::Ostream& Foam::operator<<
{
os << token::BEGIN_LIST << vs.v_[0];
for (direction i=1; i<Ncmpts; i++)
for (direction i=1; i<Ncmpts; ++i)
{
os << token::SPACE << vs.v_[i];
}
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -89,23 +89,23 @@ public:
typedef Cmpt cmptType;
// Static constants
// Static Constants
//- Dimensionality of space
static const direction dim = 3;
static constexpr direction dim = 3;
//- Number of components in this vector space
static const direction nComponents = Ncmpts;
static constexpr direction nComponents = Ncmpts;
// VectorSpace currently defaults to a column-vector
// This will be removed when column-vector is introduced
// as a specialization
static const direction mRows = Ncmpts;
static const direction nCols = 1;
static constexpr direction mRows = Ncmpts;
static constexpr direction nCols = 1;
// Static data members
// Static Data Members
static const char* const typeName;
static const char* const componentNames[];
......@@ -120,11 +120,7 @@ public:
// Sub-Block Classes
//- Const sub-block type
template
<
class SubVector,
direction BStart
>
template<class SubVector, direction BStart>
class ConstBlock
{
const vsType& vs_;
......@@ -158,12 +154,12 @@ public:
inline VectorSpace(const Foam::zero);
//- Construct from Istream
VectorSpace(Istream&);
VectorSpace(Istream& is);
//- Construct as copy
inline VectorSpace(const VectorSpace<Form, Cmpt, Ncmpts>&);
//- Copy construct
inline VectorSpace(const VectorSpace<Form, Cmpt, Ncmpts>& vs);
//- Construct as copy of a VectorSpace with the same size
//- Copy construct of a VectorSpace with the same size
template<class Form2, class Cmpt2>
inline explicit VectorSpace(const VectorSpace<Form2, Cmpt2, Ncmpts>&);
......@@ -171,7 +167,7 @@ public:
// Member Functions
//- Return the number of elements in the VectorSpace = Ncmpts.
inline static direction size();
inline static constexpr direction size();
inline const Cmpt& component(const direction) const;
inline Cmpt& component(const direction);
......@@ -200,6 +196,39 @@ public:
inline void operator/=(const scalar);
// Iterators
//- Random access iterator for traversing VectorSpace
typedef Cmpt* iterator;
//- Random access iterator for traversing VectorSpace
typedef const Cmpt* const_iterator;
// Random access iterator (non-const)
//- Return an iterator to begin of VectorSpace
inline iterator begin();
//- Return an iterator to end of UListVectorSpace
inline iterator end();
// Random access iterator (const)
//- Return const_iterator to begin of VectorSpace
inline const_iterator cbegin() const;
//- Return const_iterator to end of VectorSpace
inline const_iterator cend() const;
//- Return const_iterator to begin of VectorSpace
inline const_iterator begin() const;
//- Return const_iterator to end of VectorSpace
inline const_iterator end() const;
// IOstream Operators
friend Istream& operator>> <Form, Cmpt, Ncmpts>
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -31,27 +31,23 @@ License
#include "ops.H"
#include <type_traits>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Form, class Cmpt, direction Ncmpts>
inline VectorSpace<Form, Cmpt, Ncmpts>::VectorSpace()
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline Foam::VectorSpace<Form, Cmpt, Ncmpts>::VectorSpace()
{}
template<class Form, class Cmpt, direction Ncmpts>
inline VectorSpace<Form, Cmpt, Ncmpts>::VectorSpace(const Foam::zero)
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline Foam::VectorSpace<Form, Cmpt, Ncmpts>::VectorSpace(const Foam::zero)
{
VectorSpaceOps<Ncmpts,0>::eqOpS(*this, Zero, eqOp<Cmpt>());
}
template<class Form, class Cmpt, direction Ncmpts>
inline VectorSpace<Form, Cmpt, Ncmpts>::VectorSpace
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline Foam::VectorSpace<Form, Cmpt, Ncmpts>::VectorSpace
(
const VectorSpace<Form, Cmpt, Ncmpts>& vs
)
......@@ -60,9 +56,9 @@ inline VectorSpace<Form, Cmpt, Ncmpts>::VectorSpace
}
template<class Form, class Cmpt, direction Ncmpts>
template<class Form, class Cmpt, Foam::direction Ncmpts>
template<class Form2, class Cmpt2>
inline VectorSpace<Form, Cmpt, Ncmpts>::VectorSpace
inline Foam::VectorSpace<Form, Cmpt, Ncmpts>::VectorSpace
(
const VectorSpace<Form2, Cmpt2, Ncmpts>& vs
)
......@@ -71,10 +67,10 @@ inline VectorSpace<Form, Cmpt, Ncmpts>::VectorSpace
}
template<class Form, class Cmpt, direction Ncmpts>
template<class SubVector, direction BStart>
inline
VectorSpace<Form, Cmpt, Ncmpts>::ConstBlock<SubVector, BStart>::ConstBlock
template<class Form, class Cmpt, Foam::direction Ncmpts>
template<class SubVector, Foam::direction BStart>
inline Foam::VectorSpace<Form, Cmpt, Ncmpts>::ConstBlock<SubVector, BStart>
::ConstBlock
(
const vsType& vs
)
......@@ -91,15 +87,15 @@ VectorSpace<Form, Cmpt, Ncmpts>::ConstBlock<SubVector, BStart>::ConstBlock
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Form, class Cmpt, direction Ncmpts>
inline direction VectorSpace<Form, Cmpt, Ncmpts>::size()
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline constexpr Foam::direction Foam::VectorSpace<Form, Cmpt, Ncmpts>::size()
{
return Ncmpts;
}
template<class Form, class Cmpt, direction Ncmpts>
inline const Cmpt& VectorSpace<Form, Cmpt, Ncmpts>::component
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline const Cmpt& Foam::VectorSpace<Form, Cmpt, Ncmpts>::component
(
const direction d
) const
......@@ -117,8 +113,8 @@ inline const Cmpt& VectorSpace<Form, Cmpt, Ncmpts>::component
}
template<class Form, class Cmpt, direction Ncmpts>
inline Cmpt& VectorSpace<Form, Cmpt, Ncmpts>::component
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline Cmpt& Foam::VectorSpace<Form, Cmpt, Ncmpts>::component
(
const direction d
)
......@@ -136,8 +132,8 @@ inline Cmpt& VectorSpace<Form, Cmpt, Ncmpts>::component
}
template<class Form, class Cmpt, direction Ncmpts>
inline void VectorSpace<Form, Cmpt, Ncmpts>::component
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline void Foam::VectorSpace<Form, Cmpt, Ncmpts>::component
(
Cmpt& c,
const direction d
......@@ -156,8 +152,8 @@ inline void VectorSpace<Form, Cmpt, Ncmpts>::component
}
template<class Form, class Cmpt, direction Ncmpts>
inline void VectorSpace<Form, Cmpt, Ncmpts>::replace
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline void Foam::VectorSpace<Form, Cmpt, Ncmpts>::replace
(
const direction d,
const Cmpt& c
......@@ -176,8 +172,8 @@ inline void VectorSpace<Form, Cmpt, Ncmpts>::replace
}
template<class Form, class Cmpt, direction Ncmpts>
inline Form VectorSpace<Form, Cmpt, Ncmpts>::uniform(const Cmpt& s)
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline Form Foam::VectorSpace<Form, Cmpt, Ncmpts>::uniform(const Cmpt& s)
{
Form v;
VectorSpaceOps<Ncmpts,0>::eqOpS(v, s, eqOp<Cmpt>());
......@@ -185,20 +181,64 @@ inline Form VectorSpace<Form, Cmpt, Ncmpts>::uniform(const Cmpt& s)
}
template<class Form, class Cmpt, direction Ncmpts>
template<class SubVector, direction BStart>
inline const typename VectorSpace<Form, Cmpt, Ncmpts>::template
template<class Form, class Cmpt, Foam::direction Ncmpts>
template<class SubVector, Foam::direction BStart>
inline const typename Foam::VectorSpace<Form, Cmpt, Ncmpts>::template
ConstBlock<SubVector, BStart>
VectorSpace<Form, Cmpt, Ncmpts>::block() const
Foam::VectorSpace<Form, Cmpt, Ncmpts>::block() const
{
return *this;
}
// * * * * * * * * * * * * * * * * Iterator * * * * * * * * * * * * * * * * //
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline Cmpt* Foam::VectorSpace<Form, Cmpt, Ncmpts>::begin()
{
return v_;
}
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline Cmpt* Foam::VectorSpace<Form, Cmpt, Ncmpts>::end()
{
return (v_ + Ncmpts);
}
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline const Cmpt* Foam::VectorSpace<Form, Cmpt, Ncmpts>::cbegin() const
{
return v_;
}
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline const Cmpt* Foam::VectorSpace<Form, Cmpt, Ncmpts>::cend() const
{
return (v_ + Ncmpts);
}
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline const Cmpt* Foam::VectorSpace<Form, Cmpt, Ncmpts>::begin() const
{
return v_;
}
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline const Cmpt* Foam::VectorSpace<Form, Cmpt, Ncmpts>::end() const
{
return (v_ + Ncmpts);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Form, class Cmpt, direction Ncmpts>
inline const Cmpt& VectorSpace<Form, Cmpt, Ncmpts>::operator[]
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline const Cmpt& Foam::VectorSpace<Form, Cmpt, Ncmpts>::operator[]
(
const direction d
) const
......@@ -216,8 +256,8 @@ inline const Cmpt& VectorSpace<Form, Cmpt, Ncmpts>::operator[]
}
template<class Form, class Cmpt, direction Ncmpts>
inline Cmpt& VectorSpace<Form, Cmpt, Ncmpts>::operator[]
template<class Form, class Cmpt, Foam::direction Ncmpts>
inline Cmpt& Foam::VectorSpace<Form, Cmpt, Ncmpts>::operator[]
(
const direction d
)
......@@ -235,10 +275,10 @@ inline Cmpt& VectorSpace<Form, Cmpt, Ncmpts>::operator[]
}