ENH: minor de-clutter of List, DynamicList, DynamicField (#2385)

- do not need contruct or move assign from SortableList.
  Rarely (never) used and can simply treat like a normal list
  by applying shrink beforehand.

- make append() methods return void instead of returning self, which
  makes it easier to derive from. Having them return self was a bit of
  an original design mistake.
  Chaining appends do not actually occur anywhere. Even if they were
  to be used, would not want to rely on them (fear of slicing on any
  derived classes).

BUG: IndirectList iterator comparison loses constness
This commit is contained in:
Mark Olesen
2022-03-07 10:46:25 +01:00
parent 47cd988296
commit 42fe95a6a9
11 changed files with 91 additions and 226 deletions

View File

@ -106,6 +106,18 @@ int main(int argc, char *argv[])
testFind(val, idl1);
}
{
auto iter = idl1.cbegin();
const auto endIter = idl1.cend();
while (iter != endIter)
{
// No post-fix increment:
Info<< *iter << nl;
++iter;
}
}
inplaceReverseList(addresses);
idl1.addressing() = std::move(addresses);

View File

@ -89,8 +89,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef PackedList_H
#define PackedList_H
#ifndef Foam_PackedList_H
#define Foam_PackedList_H
#include "BitOps.H"
#include "labelList.H"
@ -362,10 +362,9 @@ public:
//- Currently identical to resize. Subject to future change (Oct-2021)
inline void resize_nocopy(const label numElem);
//- Reserve allocation space for at least this size.
//- Reserve allocation space for at least this size
//- (uses a size doubling strategy).
// Never shrinks the allocated size.
// The list size is adjusted as per DynamicList with
// SizeInc=0, SizeMult=2, SizeDiv=1
inline void reserve(const label numElem);
//- Clear the list, i.e. set addressable size to zero.

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -39,8 +39,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef IndirectListBase_H
#define IndirectListBase_H
#ifndef Foam_IndirectListBase_H
#define Foam_IndirectListBase_H
#include "List.H"
@ -242,9 +242,11 @@ public:
// Iterators
//- A non-const iterator for an indirect list
// Only supports forward prefix increment, since the addressing
// may/may not support postfix or decrement.
class iterator
{
typename UList<T>::pointer data_;
T* begin_;
typename addressing_type::const_iterator iter_;
public:
@ -258,17 +260,14 @@ public:
iterator
(
UList<T>& list,
typename addressing_type::const_iterator baseIter
typename addressing_type::const_iterator addrIter
)
:
data_(list.begin()),
iter_(baseIter)
begin_(list.data()),
iter_(addrIter)
{}
reference operator*() const
{
return data_[*iter_];
}
reference operator*() const { return *(begin_ + *iter_); }
iterator& operator++()
{
@ -276,12 +275,12 @@ public:
return *this;
}
bool operator==(iterator& rhs) const
bool operator==(const iterator& rhs) const
{
return iter_ == rhs.iter_;
return (iter_ == rhs.iter_);
}
bool operator!=(iterator& rhs) const
bool operator!=(const iterator& rhs) const
{
return (iter_ != rhs.iter_);
}
@ -289,9 +288,11 @@ public:
//- A const iterator for an indirect list
// Only supports forward prefix increment, since the addressing
// may/may not support postfix or decrement.
class const_iterator
{
typename UList<T>::const_pointer data_;
const T* begin_;
typename addressing_type::const_iterator iter_;
public:
@ -305,17 +306,14 @@ public:
const_iterator
(
const UList<T>& list,
typename addressing_type::const_iterator baseIter
typename addressing_type::const_iterator addrIter
)
:
data_(list.begin()),
iter_(baseIter)
begin_(list.cdata()),
iter_(addrIter)
{}
reference operator*() const
{
return data_[*iter_];
}
reference operator*() const { return *(begin_ + *iter_); }
const_iterator& operator++()
{
@ -323,14 +321,14 @@ public:
return *this;
}
bool operator==(const_iterator& rhs) const
bool operator==(const const_iterator& rhs) const
{
return iter_ == rhs.iter_;
return (iter_ == rhs.iter_);
}
bool operator!=(const_iterator& rhs) const
bool operator!=(const const_iterator& rhs) const
{
return iter_ != rhs.iter_;
return (iter_ != rhs.iter_);
}
};
@ -364,17 +362,11 @@ public:
return const_iterator(values_, addr_.cend());
}
//- Return a const_iterator at end of list
inline const_iterator begin() const
{
return cbegin();
}
//- Return a const_iterator at begin of list
const_iterator begin() const { return cbegin(); }
//- Return a const_iterator at end of list
inline const_iterator end() const
{
return cend();
}
const_iterator end() const { return cend(); }
// Writing

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -42,8 +42,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef DynamicList_H
#define DynamicList_H
#ifndef Foam_DynamicList_H
#define Foam_DynamicList_H
#include "List.H"
@ -154,9 +154,6 @@ public:
//- Move construct from List
inline DynamicList(List<T>&& lst);
//- Move construct from SortableList
DynamicList(SortableList<T>&& lst);
//- Construct from Istream. Size set to size of list read.
explicit DynamicList(Istream& is);
@ -259,47 +256,32 @@ public:
template<int AnySizeMin>
inline void transfer(DynamicList<T, AnySizeMin>& list);
//- Transfer contents of the argument SortableList into this.
inline void transfer(SortableList<T>& list);
//- Append an element to the end of this list.
inline DynamicList<T, SizeMin>& append(const T& val);
//- Copy append an element to the end of this list.
inline void append(const T& val);
//- Move append an element
inline DynamicList<T, SizeMin>& append(T&& val);
inline void append(T&& val);
//- Append another list to the end of this list.
inline DynamicList<T, SizeMin>& append(const UList<T>& lst);
inline void append(const UList<T>& lst);
//- Append a FixedList to the end of this list.
template<unsigned N>
inline DynamicList<T, SizeMin>&
append(const FixedList<T, N>& lst);
inline void append(const FixedList<T, N>& lst);
//- Append an initializer list at the end of this list.
inline DynamicList<T, SizeMin>&
append(std::initializer_list<T> lst);
inline void append(std::initializer_list<T> lst);
//- Append a IndirectList at the end of this list
template<class Addr>
inline DynamicList<T, SizeMin>&
append(const IndirectListBase<T, Addr>& lst);
inline void append(const IndirectListBase<T, Addr>& lst);
//- Move append list
inline DynamicList<T, SizeMin>& append(List<T>&& lst);
//- Move append list
inline DynamicList<T, SizeMin>&
append(DynamicList<T, SizeMin>&& lst);
inline void append(List<T>&& lst);
//- Move append list
template<int AnySizeMin>
inline DynamicList<T, SizeMin>&
append(DynamicList<T, AnySizeMin>&& lst);
//- Move append list
inline DynamicList<T, SizeMin>&
append(SortableList<T>&& lst);
inline void append(DynamicList<T, AnySizeMin>&& list);
//- Append an element if not already in the list.
// \return the change in list length
@ -377,9 +359,6 @@ public:
template<int AnySizeMin>
inline void operator=(DynamicList<T, AnySizeMin>&& lst);
//- Move assignment
inline void operator=(SortableList<T>&& lst);
// Reading/writing

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -493,21 +493,7 @@ Foam::DynamicList<T, SizeMin>::transfer
template<class T, int SizeMin>
inline void
Foam::DynamicList<T, SizeMin>::transfer
(
SortableList<T>& list
)
{
list.shrink(); // Shrink away sort indices
capacity_ = list.size(); // Capacity after transfer == list size
List<T>::transfer(list);
}
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::append
inline void Foam::DynamicList<T, SizeMin>::append
(
const T& val
)
@ -516,13 +502,11 @@ Foam::DynamicList<T, SizeMin>::append
resize(idx + 1);
this->operator[](idx) = val; // copy element
return *this;
}
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::append
inline void Foam::DynamicList<T, SizeMin>::append
(
T&& val
)
@ -531,13 +515,11 @@ Foam::DynamicList<T, SizeMin>::append
resize(idx + 1);
this->operator[](idx) = std::move(val); // move assign element
return *this;
}
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::append
inline void Foam::DynamicList<T, SizeMin>::append
(
const UList<T>& lst
)
@ -556,14 +538,12 @@ Foam::DynamicList<T, SizeMin>::append
{
this->operator[](idx++) = val; // copy element
}
return *this;
}
template<class T, int SizeMin>
template<unsigned N>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::append
inline void Foam::DynamicList<T, SizeMin>::append
(
const FixedList<T, N>& lst
)
@ -575,13 +555,11 @@ Foam::DynamicList<T, SizeMin>::append
{
this->operator[](idx++) = val; // copy element
}
return *this;
}
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::append
inline void Foam::DynamicList<T, SizeMin>::append
(
std::initializer_list<T> lst
)
@ -593,14 +571,12 @@ Foam::DynamicList<T, SizeMin>::append
{
this->operator[](idx++) = val; // copy element
}
return *this;
}
template<class T, int SizeMin>
template<class Addr>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::append
inline void Foam::DynamicList<T, SizeMin>::append
(
const IndirectListBase<T, Addr>& lst
)
@ -614,13 +590,11 @@ Foam::DynamicList<T, SizeMin>::append
{
this->operator[](idx++) = lst[i]; // copy element
}
return *this;
}
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::append
inline void Foam::DynamicList<T, SizeMin>::append
(
List<T>&& list
)
@ -641,47 +615,18 @@ Foam::DynamicList<T, SizeMin>::append
}
list.clear();
return *this;
}
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::append
(
DynamicList<T, SizeMin>&& lst
)
{
append(std::move(static_cast<List<T>&>(lst)));
lst.clearStorage(); // Ensure capacity=0
return *this;
}
template<class T, int SizeMin>
template<int AnySizeMin>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::append
inline void Foam::DynamicList<T, SizeMin>::append
(
DynamicList<T, AnySizeMin>&& lst
DynamicList<T, AnySizeMin>&& list
)
{
append(std::move(static_cast<List<T>&>(lst)));
lst.clearStorage(); // Ensure capacity=0
return *this;
}
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::append
(
SortableList<T>&& lst
)
{
lst.shrink(); // Shrink away sort indices
append(std::move(static_cast<List<T>&>(lst)));
return *this;
append(std::move(static_cast<List<T>&>(list)));
list.clearStorage(); // Ensure capacity=0
}
@ -939,15 +884,4 @@ inline void Foam::DynamicList<T, SizeMin>::operator=
}
template<class T, int SizeMin>
inline void Foam::DynamicList<T, SizeMin>::operator=
(
SortableList<T>&& lst
)
{
clear();
transfer(lst);
}
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -405,15 +405,6 @@ Foam::List<T>::List(DynamicList<T, SizeMin>&& list)
}
template<class T>
Foam::List<T>::List(SortableList<T>&& list)
:
UList<T>()
{
transfer(list);
}
template<class T>
Foam::List<T>::List(SLList<T>&& list)
:
@ -483,15 +474,6 @@ void Foam::List<T>::transfer(DynamicList<T, SizeMin>& list)
}
template<class T>
void Foam::List<T>::transfer(SortableList<T>& list)
{
// Shrink away the sort indices
list.shrink();
transfer(static_cast<List<T>&>(list));
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T>
@ -638,13 +620,6 @@ void Foam::List<T>::operator=(DynamicList<T, SizeMin>&& list)
}
template<class T>
void Foam::List<T>::operator=(SortableList<T>&& list)
{
transfer(list);
}
template<class T>
void Foam::List<T>::operator=(SLList<T>&& list)
{

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -40,8 +40,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef List_H
#define List_H
#ifndef Foam_List_H
#define Foam_List_H
#include "autoPtr.H"
#include "UList.H"
@ -58,7 +58,6 @@ 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> Istream& operator>>(Istream& is, List<T>& list);
@ -182,9 +181,6 @@ public:
template<int SizeMin>
List(DynamicList<T, SizeMin>&& list);
//- Move construct from SortableList
List(SortableList<T>&& list);
//- Move construct from SLList
List(SLList<T>&& list);
@ -258,10 +254,6 @@ public:
template<int SizeMin>
void transfer(DynamicList<T, SizeMin>& list);
//- Transfer the contents of the argument List into this list
//- and annul the argument list
void transfer(SortableList<T>& list);
//- Return subscript-checked element of UList and resizing the list
//- if required.
inline T& newElmt(const label i);
@ -302,9 +294,6 @@ public:
template<int SizeMin>
void operator=(DynamicList<T, SizeMin>&& list);
//- Move assignment. Takes constant time.
void operator=(SortableList<T>&& list);
//- Move assignment. Takes constant time
void operator=(SLList<T>&& list);

View File

@ -105,7 +105,7 @@ inline Foam::SortableList<T>::SortableList(std::initializer_list<T> values)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
void Foam::SortableList<T>::clear()
inline void Foam::SortableList<T>::clear()
{
List<T>::clear();
indices_.clear();
@ -113,7 +113,7 @@ void Foam::SortableList<T>::clear()
template<class T>
Foam::List<T>& Foam::SortableList<T>::shrink()
inline Foam::List<T>& Foam::SortableList<T>::shrink()
{
indices_.clear();
return static_cast<List<T>&>(*this);

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -43,8 +43,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef SortableList_H
#define SortableList_H
#ifndef Foam_SortableList_H
#define Foam_SortableList_H
#include "labelList.H"
@ -106,22 +106,22 @@ public:
// Member Functions
//- Return the list of sorted indices. Updated every sort
const labelList& indices() const
const labelList& indices() const noexcept
{
return indices_;
}
//- Return non-const access to the sorted indices. Updated every sort
labelList& indices()
labelList& indices() noexcept
{
return indices_;
}
//- Clear the list and the indices
void clear();
inline void clear();
//- Clear the indices and return a reference to the underlying List
List<T>& shrink();
inline List<T>& shrink();
//- Forward (stable) sort the list (if changed after construction).
// Resizes the indices as required
@ -163,15 +163,7 @@ public:
};
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
// Exchange contents of lists - see SortableList::swap().
template<class T>
inline void Swap(SortableList<T>& a, SortableList<T>& b)
{
a.swap(b);
}
// Note: uses default Foam::Swap (move construct/assignment)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -35,8 +35,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef DynamicField_H
#define DynamicField_H
#ifndef Foam_DynamicField_H
#define Foam_DynamicField_H
#include "Field.H"
#include "DynamicList.H"
@ -283,14 +283,13 @@ public:
inline void transfer(DynamicField<T, AnySizeMin>& list);
//- Append an element at the end of the list
inline DynamicField<T, SizeMin>& append(const T& val);
inline void append(const T& val);
//- Move append an element
inline DynamicField<T, SizeMin>& append(T&& val);
inline void append(T&& val);
//- Append a List at the end of this list
inline DynamicField<T, SizeMin>&
append(const UList<T>& list);
inline void append(const UList<T>& list);
//- Remove and return the top element
inline T remove();

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -582,8 +582,7 @@ inline void Foam::DynamicField<T, SizeMin>::transfer
template<class T, int SizeMin>
inline Foam::DynamicField<T, SizeMin>&
Foam::DynamicField<T, SizeMin>::append
inline void Foam::DynamicField<T, SizeMin>::append
(
const T& val
)
@ -592,13 +591,11 @@ Foam::DynamicField<T, SizeMin>::append
resize(idx + 1);
this->operator[](idx) = val; // copy element
return *this;
}
template<class T, int SizeMin>
inline Foam::DynamicField<T, SizeMin>&
Foam::DynamicField<T, SizeMin>::append
inline void Foam::DynamicField<T, SizeMin>::append
(
T&& val
)
@ -607,13 +604,11 @@ Foam::DynamicField<T, SizeMin>::append
resize(idx + 1);
this->operator[](idx) = std::move(val); // move assign element
return *this;
}
template<class T, int SizeMin>
inline Foam::DynamicField<T, SizeMin>&
Foam::DynamicField<T, SizeMin>::append
inline void Foam::DynamicField<T, SizeMin>::append
(
const UList<T>& list
)
@ -632,7 +627,6 @@ Foam::DynamicField<T, SizeMin>::append
{
this->operator[](idx++) = val; // copy element
}
return *this;
}