diff --git a/applications/test/UIndirectList/Test-UIndirectList.C b/applications/test/UIndirectList/Test-UIndirectList.C index 9dbd0e8b0ef13ad7fd950c97c8995d342cfd6e0b..a9d9cabd7670f76c1d09f75693ca3e040bdf82e8 100644 --- a/applications/test/UIndirectList/Test-UIndirectList.C +++ b/applications/test/UIndirectList/Test-UIndirectList.C @@ -62,7 +62,9 @@ void testFind(const T& val, const ListType& lst) int main(int argc, char *argv[]) { - List<label> completeList(20); + labelList completeList(20); + labelList scratch(20); + scratch = -1; forAll(completeList, i) { @@ -71,12 +73,24 @@ int main(int argc, char *argv[]) Info<< "raw : " << flatOutput(completeList) << nl << endl; - List<label> addresses{1, 0, 3, 7, 4, 8, 5, 1, 0, 3, 7, 4, 8, 5, }; + List<label> addresses({ 1, 0, 3, 7, 4, 8, 5, 1, 0, 3, 7, 4, 8, 5 }); labelUIndList idl1(completeList, addresses); printInfo(idl1); + labelList::subList slice(scratch, idl1.size()); + slice = idl1; + + Info<< "sliced: " << flatOutput(slice) << nl; + Info<< "scratch: " << flatOutput(scratch) << nl; + + // Again, but offset and using intermediate only + scratch = -1; + labelList::subList(scratch, idl1.size(), 5) = idl1; + Info<< "offset: " << flatOutput(scratch) << nl; + + for (const label val : { 10, 30, 40, 50, 90, 80, 120 } ) { testFind(val, idl1); diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index 17f33459d4dbede358f145d5c1724d44166abe58..ebded60c266d09491256967a5bfaafb0adf5f05d 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -53,16 +53,12 @@ namespace Foam { // Forward Declarations -class Istream; -class Ostream; - template<class T> class List; template<class T, unsigned N> class FixedList; template<class T, int SizeMin> class DynamicList; template<class T> class PtrList; template<class T> class SortableList; -template<class T, class Addr> class IndirectListBase; template<class T> Istream& operator>>(Istream& is, List<T>& list); diff --git a/src/OpenFOAM/containers/Lists/SubList/SubList.H b/src/OpenFOAM/containers/Lists/SubList/SubList.H index 061028554c598dcfdc7214f55397d305c3e4538b..4eddc1abb35bf81477838592da5c064c70dcaadc 100644 --- a/src/OpenFOAM/containers/Lists/SubList/SubList.H +++ b/src/OpenFOAM/containers/Lists/SubList/SubList.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -125,12 +125,16 @@ public: //- Allow cast to a const List\<T\>& inline operator const Foam::List<T>&() const; - //- Copy assign entries from given sub-list + //- Copy assign entries from given sub-list. Sizes must match! inline void operator=(const SubList<T>& list); - //- Copy assign entries from given list + //- Copy assign entries from given list. Sizes must match! inline void operator=(const UList<T>& list); + //- Copy assign entries from given indirect list. Sizes must match! + template<class Addr> + inline void operator=(const IndirectListBase<T, Addr>& list); + //- Assign all entries to the given value inline void operator=(const T& val); diff --git a/src/OpenFOAM/containers/Lists/SubList/SubListI.H b/src/OpenFOAM/containers/Lists/SubList/SubListI.H index 3ec6aa5fa34d8fe07a55c2cf35e0056b3365e177..53408e3a762ee8882c8fa59d364becfebdbbdb7e 100644 --- a/src/OpenFOAM/containers/Lists/SubList/SubListI.H +++ b/src/OpenFOAM/containers/Lists/SubList/SubListI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -135,6 +135,14 @@ inline void Foam::SubList<T>::operator=(const UList<T>& list) } +template<class T> +template<class Addr> +inline void Foam::SubList<T>::operator=(const IndirectListBase<T, Addr>& list) +{ + UList<T>::deepCopy(list); +} + + template<class T> inline void Foam::SubList<T>::operator=(const T& val) { diff --git a/src/OpenFOAM/containers/Lists/UList/UList.C b/src/OpenFOAM/containers/Lists/UList/UList.C index 41dbb8f036de1fd2c55fd3af958183ff92ae9dac..3263d6f0c8adb0af77530747069571772de4b0c1 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.C +++ b/src/OpenFOAM/containers/Lists/UList/UList.C @@ -110,8 +110,8 @@ void Foam::UList<T>::deepCopy(const UList<T>& list) if (len != list.size_) { FatalErrorInFunction - << "ULists have different sizes: " - << len << " " << list.size_ + << "Lists have different sizes: " + << len << " != " << list.size() << nl << abort(FatalError); } else if (len) @@ -138,6 +138,30 @@ void Foam::UList<T>::deepCopy(const UList<T>& list) } +template<class T> +template<class Addr> +void Foam::UList<T>::deepCopy(const IndirectListBase<T, Addr>& list) +{ + const label len = this->size_; + + if (len != list.size()) + { + FatalErrorInFunction + << "Lists have different sizes: " + << len << " != " << list.size() << nl + << abort(FatalError); + } + else if (len) + { + List_ACCESS(T, (*this), lhs); + for (label i = 0; i < len; ++i) + { + lhs[i] = list[i]; + } + } +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template<class T> diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H index 1493a88637896861a2c985265eedeaa948462565..1fd97cb99ee407deae51dad8d6462d437da61032 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.H +++ b/src/OpenFOAM/containers/Lists/UList/UList.H @@ -68,10 +68,15 @@ namespace Foam { // Forward Declarations +class Istream; +class Ostream; class labelRange; + template<class T> class List; template<class T> class SubList; template<class T> class UList; +template<class T, class Addr> class IndirectListBase; + template<class T> Istream& operator>>(Istream&, UList<T>&); template<class T> Ostream& operator<<(Ostream&, const UList<T>&); @@ -342,6 +347,10 @@ public: //- Copy elements of the given UList void deepCopy(const UList<T>& list); + //- Copy elements of the given indirect list + template<class Addr> + void deepCopy(const IndirectListBase<T, Addr>& list); + // Member Operators @@ -493,7 +502,7 @@ public: typename std::enable_if<std::is_same<bool, TypeT>::value, bool>::type inline test(const label i) const { - return (i >= 0 && i < size() && v_[i]); + return (i >= 0 && i < size_ && v_[i]); } //- A bitSet::get() method for a list of bool diff --git a/src/OpenFOAM/fields/Fields/Field/Field.H b/src/OpenFOAM/fields/Fields/Field/Field.H index 916c0944a15c860895fe586e42d1baf4a7700202..056265f3b402bfd7076b9e541e603d1d2053e92c 100644 --- a/src/OpenFOAM/fields/Fields/Field/Field.H +++ b/src/OpenFOAM/fields/Fields/Field/Field.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2015-2020 OpenCFD Ltd. + Copyright (C) 2015-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -101,7 +101,7 @@ public: // Constructors - //- Construct null + //- Default construct // For temporary fields that are initialised after construction inline constexpr Field() noexcept; @@ -374,8 +374,13 @@ public: //- Copy assignment void operator=(const Field<Type>&); void operator=(const tmp<Field<Type>>&); - inline void operator=(const UList<Type>&); - inline void operator=(const SubField<Type>&); + + inline void operator=(const UList<Type>& rhs); + inline void operator=(const SubField<Type>& rhs); + + //- Copy assign from IndirectList + template<class Addr> + inline void operator=(const IndirectListBase<Type, Addr>& rhs); //- Move assignment inline void operator=(Field<Type>&& rhs); diff --git a/src/OpenFOAM/fields/Fields/Field/FieldI.H b/src/OpenFOAM/fields/Fields/Field/FieldI.H index 111c87115ece66797b4a3f7802cd8ee67aca6bd0..7cfd44d6f94216ec39a6c9b753e520042422b083 100644 --- a/src/OpenFOAM/fields/Fields/Field/FieldI.H +++ b/src/OpenFOAM/fields/Fields/Field/FieldI.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2020 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -172,6 +172,17 @@ inline void Foam::Field<Type>::operator=(const SubField<Type>& rhs) } +template<class Type> +template<class Addr> +inline void Foam::Field<Type>::operator= +( + const IndirectListBase<Type, Addr>& rhs +) +{ + List<Type>::operator=(rhs); +} + + template<class Type> inline void Foam::Field<Type>::operator=(Field<Type>&& rhs) {