From db9460f0bcba66a8e1e5630c3b9f170d5da69a76 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Thu, 15 Jul 2021 13:08:54 +0200 Subject: [PATCH] ENH: copy construct FixedList from fixed subset of input - remove construct from two iterators (#2083) --- applications/test/FixedList/Test-FixedList.C | 12 +++++-- .../containers/Lists/FixedList/FixedList.H | 23 +++++++------ .../containers/Lists/FixedList/FixedListI.H | 32 ++++++++----------- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/applications/test/FixedList/Test-FixedList.C b/applications/test/FixedList/Test-FixedList.C index f75d77e3c80..c8648ae05f5 100644 --- a/applications/test/FixedList/Test-FixedList.C +++ b/applications/test/FixedList/Test-FixedList.C @@ -280,16 +280,24 @@ int main(int argc, char *argv[]) List<label> list3{0, 1, 2, 3}; - FixedList<label, 4> list4(list3.begin(), list3.end()); + FixedList<label, 4> list4(list3); Info<< "list3: " << list3 << nl << "list4: " << list4 << nl; - list4 = {1, 2, 3, 5}; + list4 = {1, 20, 3, 40}; Info<< "list4: " << list4 << nl; FixedList<label, 5> list5{0, 1, 2, 3, 4}; Info<< "list5: " << list5 << nl; + { + const FixedList<label, 2> indices({3, 1}); + FixedList<label, 2> list4b(list4, indices); + + Info<< "subset " << list4 << " with " << indices << " -> " + << list4b << nl; + } + List<FixedList<label, 2>> list6{{0, 1}, {2, 3}}; Info<< "list6: " << list6 << nl; diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index 59588b2c626..a79a318d06a 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -152,7 +152,7 @@ public: //- Construct and initialize all entries to zero inline explicit FixedList(const Foam::zero); - //- Copy construct from C-array + //- Copy construct from C-array (deprecated) inline explicit FixedList(const T list[N]); //- Copy construct @@ -162,25 +162,28 @@ public: //- list elements inline FixedList(FixedList<T, N>&& list); - //- Construct given begin/end iterators - // Uses std::distance when verifying the size. - template<class InputIterator> - inline FixedList(InputIterator begIter, InputIterator endIter); - - //- Construct from an initializer list + //- Construct from an initializer list. Runtime size check inline FixedList(std::initializer_list<T> list); - //- Construct from UList + //- Construct from UList. Runtime size check inline explicit FixedList(const UList<T>& list); - //- Copy construct from a subset of the input + //- Copy construct from a subset of the input. No size check + template<unsigned AnyNum> + inline FixedList + ( + const FixedList<T, AnyNum>& list, + const FixedList<label, N>& indices + ); + + //- Copy construct from a subset of the input. No size check inline FixedList ( const UList<T>& list, const FixedList<label, N>& indices ); - //- Construct from SLList + //- Construct from SLList. Runtime size check inline explicit FixedList(const SLList<T>& list); //- Construct from Istream diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index 4f7d544e465..78611d47640 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -28,7 +28,6 @@ License #include "UList.H" #include "SLList.H" -#include <type_traits> #include <utility> // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // @@ -87,45 +86,42 @@ inline Foam::FixedList<T, N>::FixedList(FixedList<T, N>&& list) template<class T, unsigned N> -template<class InputIterator> -inline Foam::FixedList<T, N>::FixedList -( - InputIterator begIter, - InputIterator endIter -) +inline Foam::FixedList<T, N>::FixedList(std::initializer_list<T> list) { - checkSize(std::distance(begIter, endIter)); + checkSize(list.size()); + auto iter = list.begin(); for (unsigned i=0; i<N; ++i) { - v_[i] = *begIter; - ++begIter; + v_[i] = *iter; + ++iter; } } template<class T, unsigned N> -inline Foam::FixedList<T, N>::FixedList(std::initializer_list<T> list) +inline Foam::FixedList<T, N>::FixedList(const UList<T>& list) { checkSize(list.size()); - auto iter = list.begin(); for (unsigned i=0; i<N; ++i) { - v_[i] = *iter; - ++iter; + v_[i] = list[i]; } } template<class T, unsigned N> -inline Foam::FixedList<T, N>::FixedList(const UList<T>& list) +template<unsigned AnyNum> +inline Foam::FixedList<T, N>::FixedList +( + const FixedList<T, AnyNum>& list, + const FixedList<label, N>& indices +) { - checkSize(list.size()); - for (unsigned i=0; i<N; ++i) { - v_[i] = list[i]; + v_[i] = list[indices[i]]; } } -- GitLab