ENH: improve support for assignment from indirect list

- copy assignment from indirect list to SubList and Field
This commit is contained in:
Mark Olesen
2021-05-10 10:02:15 +02:00
parent 40567b844a
commit 17e6a14773
8 changed files with 89 additions and 18 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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)
{

View File

@ -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>

View File

@ -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

View File

@ -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);

View File

@ -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)
{