diff --git a/applications/test/vector/Test-vector.C b/applications/test/vector/Test-vector.C index 497dea43d1..fc3e430882 100644 --- a/applications/test/vector/Test-vector.C +++ b/applications/test/vector/Test-vector.C @@ -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 using namespace Foam; @@ -74,6 +75,18 @@ void doTest(vector& vec1, vector& vec2) } +template +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; } diff --git a/src/OpenFOAM/primitives/Barycentric/BarycentricTensor.H b/src/OpenFOAM/primitives/Barycentric/BarycentricTensor.H index 83d5ac064a..e603dc5304 100644 --- a/src/OpenFOAM/primitives/Barycentric/BarycentricTensor.H +++ b/src/OpenFOAM/primitives/Barycentric/BarycentricTensor.H @@ -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 diff --git a/src/OpenFOAM/primitives/DiagTensor/DiagTensor.H b/src/OpenFOAM/primitives/DiagTensor/DiagTensor.H index 3792d35e06..797b7c68eb 100644 --- a/src/OpenFOAM/primitives/DiagTensor/DiagTensor.H +++ b/src/OpenFOAM/primitives/DiagTensor/DiagTensor.H @@ -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 diff --git a/src/OpenFOAM/primitives/MatrixSpace/MatrixSpace.H b/src/OpenFOAM/primitives/MatrixSpace/MatrixSpace.H index 8c45d5669c..f83297a056 100644 --- a/src/OpenFOAM/primitives/MatrixSpace/MatrixSpace.H +++ b/src/OpenFOAM/primitives/MatrixSpace/MatrixSpace.H @@ -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; } diff --git a/src/OpenFOAM/primitives/MatrixSpace/MatrixSpaceI.H b/src/OpenFOAM/primitives/MatrixSpace/MatrixSpaceI.H index d6ff7d796c..5e696035ae 100644 --- a/src/OpenFOAM/primitives/MatrixSpace/MatrixSpaceI.H +++ b/src/OpenFOAM/primitives/MatrixSpace/MatrixSpaceI.H @@ -345,7 +345,7 @@ inline const Cmpt& Foam::MatrixSpace::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::operator() ) { #ifdef FULLDEBUG - if (i > Mrows-1 || j > Ncols-1) + if (i >= Mrows || j >= Ncols) { FatalErrorInFunction << "indices out of range" diff --git a/src/OpenFOAM/primitives/SphericalTensor/SphericalTensor.H b/src/OpenFOAM/primitives/SphericalTensor/SphericalTensor.H index 722a81f23d..ff9f8ec959 100644 --- a/src/OpenFOAM/primitives/SphericalTensor/SphericalTensor.H +++ b/src/OpenFOAM/primitives/SphericalTensor/SphericalTensor.H @@ -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 diff --git a/src/OpenFOAM/primitives/SphericalTensor2D/SphericalTensor2D.H b/src/OpenFOAM/primitives/SphericalTensor2D/SphericalTensor2D.H index c801540998..7318623108 100644 --- a/src/OpenFOAM/primitives/SphericalTensor2D/SphericalTensor2D.H +++ b/src/OpenFOAM/primitives/SphericalTensor2D/SphericalTensor2D.H @@ -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 diff --git a/src/OpenFOAM/primitives/SymmTensor/SymmTensor.H b/src/OpenFOAM/primitives/SymmTensor/SymmTensor.H index 71a8fadb78..2fb50c93b9 100644 --- a/src/OpenFOAM/primitives/SymmTensor/SymmTensor.H +++ b/src/OpenFOAM/primitives/SymmTensor/SymmTensor.H @@ -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 diff --git a/src/OpenFOAM/primitives/SymmTensor2D/SymmTensor2D.H b/src/OpenFOAM/primitives/SymmTensor2D/SymmTensor2D.H index 7b026dc82f..c961f6203a 100644 --- a/src/OpenFOAM/primitives/SymmTensor2D/SymmTensor2D.H +++ b/src/OpenFOAM/primitives/SymmTensor2D/SymmTensor2D.H @@ -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 diff --git a/src/OpenFOAM/primitives/Tensor/Tensor.H b/src/OpenFOAM/primitives/Tensor/Tensor.H index c373a266ce..21fc9176d0 100644 --- a/src/OpenFOAM/primitives/Tensor/Tensor.H +++ b/src/OpenFOAM/primitives/Tensor/Tensor.H @@ -53,8 +53,9 @@ See also namespace Foam { -template -class SymmTensor; +// Forward Declarations +template 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 diff --git a/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H b/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H index 1606215fa6..332d74e960 100644 --- a/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H +++ b/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H @@ -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 diff --git a/src/OpenFOAM/primitives/Vector/Vector.H b/src/OpenFOAM/primitives/Vector/Vector.H index d46e29416f..bccc1bd25f 100644 --- a/src/OpenFOAM/primitives/Vector/Vector.H +++ b/src/OpenFOAM/primitives/Vector/Vector.H @@ -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 diff --git a/src/OpenFOAM/primitives/Vector2D/Vector2D.H b/src/OpenFOAM/primitives/Vector2D/Vector2D.H index d2d25c5f9f..ed69c95ed7 100644 --- a/src/OpenFOAM/primitives/Vector2D/Vector2D.H +++ b/src/OpenFOAM/primitives/Vector2D/Vector2D.H @@ -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 diff --git a/src/OpenFOAM/primitives/VectorSpace/VectorSpace.C b/src/OpenFOAM/primitives/VectorSpace/VectorSpace.C index c88bb1f8fb..f40e9caacb 100644 --- a/src/OpenFOAM/primitives/VectorSpace/VectorSpace.C +++ b/src/OpenFOAM/primitives/VectorSpace/VectorSpace.C @@ -38,18 +38,15 @@ Foam::VectorSpace::VectorSpace Istream& is ) { - // Read beginning of VectorSpace - is.readBegin("VectorSpace"); + is.readBegin("VectorSpace"); for (direction i=0; i> v_[i]; } - // Read end of VectorSpace - is.readEnd("VectorSpace"); + 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> VectorSpace& vs ) { - // Read beginning of VectorSpace - is.readBegin("VectorSpace"); + is.readBegin("VectorSpace"); for (direction i=0; i> vs.v_[i]; } - // Read end of VectorSpace - is.readEnd("VectorSpace"); + 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 + template 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&); + //- Copy construct + inline VectorSpace(const VectorSpace& vs); - //- Construct as copy of a VectorSpace with the same size + //- Copy construct of a VectorSpace with the same size template inline explicit VectorSpace(const VectorSpace&); @@ -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>> diff --git a/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H b/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H index 89af43b3ab..bba3f77740 100644 --- a/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H +++ b/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H @@ -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 -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -template -inline VectorSpace::VectorSpace() +template +inline Foam::VectorSpace::VectorSpace() {} -template -inline VectorSpace::VectorSpace(const Foam::zero) +template +inline Foam::VectorSpace::VectorSpace(const Foam::zero) { VectorSpaceOps::eqOpS(*this, Zero, eqOp()); } -template -inline VectorSpace::VectorSpace +template +inline Foam::VectorSpace::VectorSpace ( const VectorSpace& vs ) @@ -60,9 +56,9 @@ inline VectorSpace::VectorSpace } -template +template template -inline VectorSpace::VectorSpace +inline Foam::VectorSpace::VectorSpace ( const VectorSpace& vs ) @@ -71,10 +67,10 @@ inline VectorSpace::VectorSpace } -template -template -inline -VectorSpace::ConstBlock::ConstBlock +template +template +inline Foam::VectorSpace::ConstBlock + ::ConstBlock ( const vsType& vs ) @@ -91,15 +87,15 @@ VectorSpace::ConstBlock::ConstBlock // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template -inline direction VectorSpace::size() +template +inline constexpr Foam::direction Foam::VectorSpace::size() { return Ncmpts; } -template -inline const Cmpt& VectorSpace::component +template +inline const Cmpt& Foam::VectorSpace::component ( const direction d ) const @@ -117,8 +113,8 @@ inline const Cmpt& VectorSpace::component } -template -inline Cmpt& VectorSpace::component +template +inline Cmpt& Foam::VectorSpace::component ( const direction d ) @@ -136,8 +132,8 @@ inline Cmpt& VectorSpace::component } -template -inline void VectorSpace::component +template +inline void Foam::VectorSpace::component ( Cmpt& c, const direction d @@ -156,8 +152,8 @@ inline void VectorSpace::component } -template -inline void VectorSpace::replace +template +inline void Foam::VectorSpace::replace ( const direction d, const Cmpt& c @@ -176,8 +172,8 @@ inline void VectorSpace::replace } -template -inline Form VectorSpace::uniform(const Cmpt& s) +template +inline Form Foam::VectorSpace::uniform(const Cmpt& s) { Form v; VectorSpaceOps::eqOpS(v, s, eqOp()); @@ -185,20 +181,64 @@ inline Form VectorSpace::uniform(const Cmpt& s) } -template -template -inline const typename VectorSpace::template +template +template +inline const typename Foam::VectorSpace::template ConstBlock -VectorSpace::block() const +Foam::VectorSpace::block() const { return *this; } +// * * * * * * * * * * * * * * * * Iterator * * * * * * * * * * * * * * * * // + +template +inline Cmpt* Foam::VectorSpace::begin() +{ + return v_; +} + + +template +inline Cmpt* Foam::VectorSpace::end() +{ + return (v_ + Ncmpts); +} + + +template +inline const Cmpt* Foam::VectorSpace::cbegin() const +{ + return v_; +} + + +template +inline const Cmpt* Foam::VectorSpace::cend() const +{ + return (v_ + Ncmpts); +} + + +template +inline const Cmpt* Foam::VectorSpace::begin() const +{ + return v_; +} + + +template +inline const Cmpt* Foam::VectorSpace::end() const +{ + return (v_ + Ncmpts); +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -template -inline const Cmpt& VectorSpace::operator[] +template +inline const Cmpt& Foam::VectorSpace::operator[] ( const direction d ) const @@ -216,8 +256,8 @@ inline const Cmpt& VectorSpace::operator[] } -template -inline Cmpt& VectorSpace::operator[] +template +inline Cmpt& Foam::VectorSpace::operator[] ( const direction d ) @@ -235,10 +275,10 @@ inline Cmpt& VectorSpace::operator[] } -template -template +template +template inline const Cmpt& -VectorSpace:: +Foam::VectorSpace:: ConstBlock::operator[] ( const direction d @@ -257,10 +297,10 @@ ConstBlock::operator[] } -template -template +template +template inline const Cmpt& -VectorSpace:: +Foam::VectorSpace:: ConstBlock::operator() ( const direction i, @@ -275,7 +315,7 @@ ConstBlock::operator() << abort(FatalError); } - if (j != 0) + if (j) { FatalErrorInFunction << "index " << j << " != 0" @@ -287,8 +327,8 @@ ConstBlock::operator() } -template -inline void VectorSpace::operator= +template +inline void Foam::VectorSpace::operator= ( const VectorSpace& vs ) @@ -297,8 +337,8 @@ inline void VectorSpace::operator= } -template -inline void VectorSpace::operator+= +template +inline void Foam::VectorSpace::operator+= ( const VectorSpace& vs ) @@ -307,8 +347,8 @@ inline void VectorSpace::operator+= } -template -inline void VectorSpace::operator-= +template +inline void Foam::VectorSpace::operator-= ( const VectorSpace& vs ) @@ -317,15 +357,15 @@ inline void VectorSpace::operator-= } -template -inline void VectorSpace::operator=(const Foam::zero) +template +inline void Foam::VectorSpace::operator=(const Foam::zero) { VectorSpaceOps::eqOpS(*this, 0, eqOp()); } -template -inline void VectorSpace::operator*= +template +inline void Foam::VectorSpace::operator*= ( const scalar s ) @@ -334,8 +374,8 @@ inline void VectorSpace::operator*= } -template -inline void VectorSpace::operator/= +template +inline void Foam::VectorSpace::operator/= ( const scalar s ) @@ -344,6 +384,11 @@ inline void VectorSpace::operator/= } +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/primitives/spatialVectorAlgebra/SpatialTensor/SpatialTensor.H b/src/OpenFOAM/primitives/spatialVectorAlgebra/SpatialTensor/SpatialTensor.H index 5bd51f6f3f..923a05bd61 100644 --- a/src/OpenFOAM/primitives/spatialVectorAlgebra/SpatialTensor/SpatialTensor.H +++ b/src/OpenFOAM/primitives/spatialVectorAlgebra/SpatialTensor/SpatialTensor.H @@ -74,7 +74,7 @@ public: // Member constants //- Rank of Tensor is 2 - static const direction rank = 2; + static constexpr direction rank = 2; // Static data members