diff --git a/applications/test/vector/Make/files b/applications/test/vector/Make/files index 62f06825ea..68db4be0f7 100644 --- a/applications/test/vector/Make/files +++ b/applications/test/vector/Make/files @@ -1,3 +1,3 @@ -Test-vector.C +Test-vector.cxx EXE = $(FOAM_USER_APPBIN)/Test-vector diff --git a/applications/test/vector/Test-vector.C b/applications/test/vector/Test-vector.cxx similarity index 84% rename from applications/test/vector/Test-vector.C rename to applications/test/vector/Test-vector.cxx index 582f8bf504..cc2ad57d36 100644 --- a/applications/test/vector/Test-vector.C +++ b/applications/test/vector/Test-vector.cxx @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2023 OpenCFD Ltd. + Copyright (C) 2018-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,7 +32,10 @@ Description \*---------------------------------------------------------------------------*/ #include "vectorField.H" +#include "boolVector.H" +#include "labelVector.H" #include "IOstreams.H" +#include "FixedList.H" #include "Random.H" #include #include @@ -125,6 +128,42 @@ void testNormalise(Field& fld) } +// Transcribe vectorspace information into a FixedList +template +void testTranscribe(Type& input) +{ + if constexpr + ( + is_vectorspace_v + && std::is_floating_point_v::type> + ) + { + constexpr auto nCmpts = pTraits_nComponents::value; + using cmpt = typename pTraits_cmptType::type; + + FixedList values; + values.back() = 100; // some additional data + + VectorSpaceOps::copy_n(input.cdata(), values.data()); + + Info<< "Transcribed " << input << " => " << values << nl; + + for (auto& val : values) + { + val *= -1; + } + + VectorSpaceOps::copy_n(values.cdata(), input.data()); + Info<< " copied back (-1) as " << input + << " from " << values << nl; + } + else + { + Info<< "Did not transcribe " << input << nl; + } +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: @@ -240,6 +279,16 @@ int main(int argc, char *argv[]) testNormalise(vfld2); } + Info<< nl + << "Test transcribing components" << nl; + { + vector vec1(1.1, 2.2, 3.3); + testTranscribe(vec1); + + labelVector vec2(10, 20, 30); + testTranscribe(vec2); + } + Info<< "\nEnd\n" << nl; return 0; diff --git a/applications/test/vectorTools/Make/files b/applications/test/vectorTools/Make/files index 0b30b98f8f..6c1e81deb8 100644 --- a/applications/test/vectorTools/Make/files +++ b/applications/test/vectorTools/Make/files @@ -1,3 +1,3 @@ -Test-vectorTools.C +Test-vectorTools.cxx EXE = $(FOAM_USER_APPBIN)/Test-vectorTools diff --git a/applications/test/vectorTools/Test-vectorTools.C b/applications/test/vectorTools/Test-vectorTools.cxx similarity index 100% rename from applications/test/vectorTools/Test-vectorTools.C rename to applications/test/vectorTools/Test-vectorTools.cxx diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index 5344e5b364..8a6abaa91f 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -384,6 +384,10 @@ public: //- Return an iterator to end traversing the FixedList inline iterator end() noexcept; + //- Return iterator at offset i from begin, + //- clamped to [0,N] range + inline iterator begin(const int i) noexcept; + // Random access iterator (const) @@ -399,6 +403,14 @@ public: //- Return const_iterator to end traversing the constant FixedList inline const_iterator end() const noexcept; + //- Return const_iterator at offset i from begin, + //- clamped to [0,N] range + inline const_iterator cbegin(const int i) const noexcept; + + //- Return const_iterator at offset i from begin, + //- clamped to [0,N] range + inline const_iterator begin(const int i) const noexcept; + // Reverse iterator (non-const) diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index 7d590c3799..61fe38572f 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -499,6 +499,30 @@ Foam::FixedList::cbegin() const noexcept } +template +inline typename Foam::FixedList::iterator +Foam::FixedList::begin(const int i) noexcept +{ + return (v_ + (i < 0 ? 0 : int(N) < i ? int(N) : i)); +} + + +template +inline typename Foam::FixedList::const_iterator +Foam::FixedList::begin(const int i) const noexcept +{ + return (v_ + (i < 0 ? 0 : int(N) < i ? int(N) : i)); +} + + +template +inline typename Foam::FixedList::const_iterator +Foam::FixedList::cbegin(const int i) const noexcept +{ + return (v_ + (i < 0 ? 0 : int(N) < i ? int(N) : i)); +} + + template inline typename Foam::FixedList::iterator Foam::FixedList::end() noexcept diff --git a/src/OpenFOAM/primitives/VectorSpace/VectorSpace.H b/src/OpenFOAM/primitives/VectorSpace/VectorSpace.H index 048f49bb40..c3f7163c4b 100644 --- a/src/OpenFOAM/primitives/VectorSpace/VectorSpace.H +++ b/src/OpenFOAM/primitives/VectorSpace/VectorSpace.H @@ -158,7 +158,7 @@ public: // Constructors //- Construct initialized to zero - inline VectorSpace(const Foam::zero); + inline VectorSpace(Foam::zero); //- Copy construct inline VectorSpace(const VectorSpace& vs); @@ -174,10 +174,7 @@ public: // Member Functions //- The number of elements in the VectorSpace = Ncmpts. - static constexpr direction size() noexcept - { - return Ncmpts; - } + static constexpr direction size() noexcept { return Ncmpts; } inline const Cmpt& component(const direction) const; inline Cmpt& component(const direction); @@ -186,10 +183,10 @@ public: inline void replace(const direction, const Cmpt&); //- Return const pointer to the first data element - inline const Cmpt* cdata() const noexcept; + const Cmpt* cdata() const noexcept { return v_; } //- Return pointer to the first data element - inline Cmpt* data() noexcept; + Cmpt* data() noexcept { return v_; } //- Assign all components to given value inline void fill(const Cmpt& s); @@ -210,7 +207,7 @@ public: inline void operator+=(const VectorSpace&); inline void operator-=(const VectorSpace&); - inline void operator=(const Foam::zero); + inline void operator=(Foam::zero); inline void operator*=(const scalar); inline void operator/=(const scalar); @@ -224,28 +221,25 @@ public: typedef const Cmpt* const_iterator; - // Random access iterator (non-const) + // Random access iterators (const and non-const) - //- Return an iterator to begin of VectorSpace - inline iterator begin() noexcept; + //- Return an iterator (pointer) to begin of VectorSpace + iterator begin() noexcept { return v_; } - //- Return an iterator to end of VectorSpace - inline iterator end() noexcept; + //- Return const_iterator (const pointer) to begin of VectorSpace + const_iterator begin() const noexcept { return v_; } + //- Return const_iterator (const pointer) to begin of VectorSpace + const_iterator cbegin() const noexcept { return v_; } - // Random access iterator (const) + //- Return an iterator (pointer) to end of VectorSpace + iterator end() noexcept { return (v_ + Ncmpts); } - //- Return const_iterator to begin of VectorSpace - inline const_iterator cbegin() const noexcept; + //- Return const_iterator (const pointer) to end of VectorSpace + const_iterator end() const noexcept { return (v_ + Ncmpts); } - //- Return const_iterator to end of VectorSpace - inline const_iterator cend() const noexcept; - - //- Return const_iterator to begin of VectorSpace - inline const_iterator begin() const noexcept; - - //- Return const_iterator to end of VectorSpace - inline const_iterator end() const noexcept; + //- Return const_iterator (const pointer) to end of VectorSpace + const_iterator cend() const noexcept { return (v_ + Ncmpts); } // IOstream Operators diff --git a/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H b/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H index 7f8bbd46dc..772baa7d4f 100644 --- a/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H +++ b/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H @@ -35,9 +35,9 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template -inline Foam::VectorSpace::VectorSpace(const Foam::zero) +inline Foam::VectorSpace::VectorSpace(Foam::zero) { - VectorSpaceOps::eqOpS(*this, Zero, eqOp()); + VectorSpaceOps::fill_n(this->begin(), Cmpt(Foam::zero{})); } @@ -47,7 +47,7 @@ inline Foam::VectorSpace::VectorSpace const VectorSpace& vs ) { - VectorSpaceOps::eqOp(*this, vs, eqOp()); + VectorSpaceOps::copy_n(vs.cbegin(), this->begin()); } @@ -58,7 +58,7 @@ inline Foam::VectorSpace::VectorSpace const VectorSpace& vs ) { - VectorSpaceOps::eqOp(*this, vs, eqOp()); + VectorSpaceOps::copy_n(vs.cbegin(), this->begin()); } @@ -163,7 +163,7 @@ inline void Foam::VectorSpace::replace template inline void Foam::VectorSpace::fill(const Cmpt& s) { - VectorSpaceOps::eqOpS(*this, s, eqOp()); + VectorSpaceOps::fill_n(this->begin(), s); } @@ -171,7 +171,7 @@ template inline Form Foam::VectorSpace::uniform(const Cmpt& s) { Form v; - VectorSpaceOps::eqOpS(v, s, eqOp()); + v.fill(s); return v; } @@ -186,68 +186,6 @@ Foam::VectorSpace::block() const } -// * * * * * * * * * * * * * * * * Iterator * * * * * * * * * * * * * * * * // - -template -inline Cmpt* Foam::VectorSpace::data() noexcept -{ - return v_; -} - - -template -inline const Cmpt* Foam::VectorSpace::cdata() const noexcept -{ - return v_; -} - - -template -inline Cmpt* Foam::VectorSpace::begin() noexcept -{ - return v_; -} - - -template -inline Cmpt* Foam::VectorSpace::end() noexcept -{ - return (v_ + Ncmpts); -} - - -template -inline const Cmpt* Foam::VectorSpace::cbegin() -const noexcept -{ - return v_; -} - - -template -inline const Cmpt* Foam::VectorSpace::cend() -const noexcept -{ - return (v_ + Ncmpts); -} - - -template -inline const Cmpt* Foam::VectorSpace::begin() -const noexcept -{ - return v_; -} - - -template -inline const Cmpt* Foam::VectorSpace::end() -const noexcept -{ - return (v_ + Ncmpts); -} - - // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template @@ -346,7 +284,7 @@ inline void Foam::VectorSpace::operator= const VectorSpace& vs ) { - VectorSpaceOps::eqOp(*this, vs, eqOp()); + VectorSpaceOps::copy_n(vs.cbegin(), this->begin()); } @@ -371,9 +309,9 @@ inline void Foam::VectorSpace::operator-= template -inline void Foam::VectorSpace::operator=(const Foam::zero) +inline void Foam::VectorSpace::operator=(Foam::zero) { - VectorSpaceOps::eqOpS(*this, 0, eqOp()); + VectorSpaceOps::fill_n(this->begin(), Cmpt(Foam::zero{})); } diff --git a/src/OpenFOAM/primitives/VectorSpace/VectorSpaceOps.H b/src/OpenFOAM/primitives/VectorSpace/VectorSpaceOps.H index c608971b26..bc1e736688 100644 --- a/src/OpenFOAM/primitives/VectorSpace/VectorSpaceOps.H +++ b/src/OpenFOAM/primitives/VectorSpace/VectorSpaceOps.H @@ -43,9 +43,40 @@ namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // //- Recursive execution. Terminating at \, starting at index \ -template +template struct VectorSpaceOps { + //- Somewhat equivalent to std::copy_n() but with templated loops. + // \param [in] input indexable input data + // \param [out] result indexable output data + template + static inline void copy_n(Input input, Output result) + { + // if constexpr (I < N) + { + result[I] = input[I]; + VectorSpaceOps::copy_n(input, result); + } + } + + //- Somewhat equivalent to std::fill_n() but with templated loops + // \param [out] result indexable output data + // \param val the value to assign for each entry + template + static inline void fill_n(Output result, const T& val) + { + // if constexpr (I < N) + { + result[I] = val; + VectorSpaceOps::fill_n(result, val); + } + } + + //- Apply the binary assignment operation to each vector-space + //- component. + // \param [in,out] vs vector-space (indexed) data + // \param s scalar/component data (non-indexed) + // \param eo binary combine/assign operation template static inline void eqOpS(V& vs, const S& s, EqOp eo) { @@ -56,6 +87,10 @@ struct VectorSpaceOps } } + //- Apply the inplace binary reduction operation. + // \param [in,out] s scalar or component data (non-indexed) + // \param [in] vs input vector-space (indexed) data + // \param eo binary combine/assign operation template static inline void SeqOp(S& s, const V& vs, EqOp eo) { @@ -66,6 +101,10 @@ struct VectorSpaceOps } } + //- Apply the inplace binary assignment operation to the components. + // \param [in,out] vs1 vector-space (indexed) data + // \param [in] vs2 second vector-space (indexed) data + // \param eo binary combine/assign operation template static inline void eqOp(V1& vs1, const V2& vs2, EqOp eo) { @@ -76,34 +115,51 @@ struct VectorSpaceOps } } - - template - static inline void opVS(V& vs, const V1& vs1, const S& s, Op o) + //- Apply the binary operation between vector-space and scalar data + //- and assign the result. + // \param [out] vs vector-space (indexed) data + // \param [in] vs1 vector-space (indexed) data operand + // \param [in] s scalar operand + // \param bop binary operation + template + static inline void opVS(V& vs, const V1& vs1, const S& s, BinaryOp bop) { // if constexpr (I < N) { - vs.v_[I] = o(vs1.v_[I], s); - VectorSpaceOps::opVS(vs, vs1, s, o); + vs.v_[I] = bop(vs1.v_[I], s); + VectorSpaceOps::opVS(vs, vs1, s, bop); } } - template - static inline void opSV(V& vs, const S& s, const V1& vs1, Op o) + //- Apply the binary operation between scalar and vector-space data + //- and assign the result. + // \param [out] vs vector-space (indexed) data + // \param [in] s scalar operand + // \param [in] vs1 vector-space (indexed) data operand + // \param bop binary operation + template + static inline void opSV(V& vs, const S& s, const V1& vs1, BinaryOp bop) { // if constexpr (I < N) { - vs.v_[I] = o(s, vs1.v_[I]); - VectorSpaceOps::opSV(vs, s, vs1, o); + vs.v_[I] = bop(s, vs1.v_[I]); + VectorSpaceOps::opSV(vs, s, vs1, bop); } } - template - static inline void op(V& vs, const V1& vs1, const V1& vs2, Op o) + //- Apply the binary operation between two vector-space data + //- and assign the result. + // \param [out] vs vector-space (indexed) data + // \param [in] vs1 vector-space (indexed) data operand + // \param [in] vs2 vector-space (indexed) data operand + // \param bop binary operation + template + static inline void op(V& vs, const V1& vs1, const V1& vs2, BinaryOp bop) { // if constexpr (I < N) { - vs.v_[I] = o(vs1.v_[I], vs2.v_[I]); - VectorSpaceOps::op(vs, vs1, vs2, o); + vs.v_[I] = bop(vs1.v_[I], vs2.v_[I]); + VectorSpaceOps::op(vs, vs1, vs2, bop); } } }; @@ -115,6 +171,12 @@ struct VectorSpaceOps template struct VectorSpaceOps { + template + static inline void copy_n(Input, Output) {} + + template + static inline void fill_n(Output, const T&) {} + template static inline void eqOpS(V&, const S&, EqOp) {} @@ -124,14 +186,14 @@ struct VectorSpaceOps template static inline void eqOp(V1&, const V2&, EqOp) {} - template - static inline void opVS(V& vs, const V1&, const S&, Op) {} + template + static inline void opVS(V&, const V1&, const S&, BinaryOp) {} - template - static inline void opSV(V& vs, const S&, const V1&, Op) {} + template + static inline void opSV(V&, const S&, const V1&, BinaryOp) {} - template - static inline void op(V& vs, const V1&, const V1&, Op) {} + template + static inline void op(V&, const V1&, const V1&, BinaryOp) {} };