ENH: rename protected UList size(label) -> setAddressableSize(label)

- modification/continuation of 8d63073b08 and 5c1ec7ecb8 (#595).

  Although this protected function is only used internally, the name
  `size(label)` is too easily confused with `resize(label)` and
  `setSize(label)`. The longer method name eliminates some ambiguity.
  Name consistent with PtrListDetail.

- leave size(label) method (for possible compatibility),
  but mark as deprecated

- improve sizing consistency for (Istream >> DynamicList)

STYLE: more consistent use of resize vs setSize in DynamicList

- more consistency between DynamicList and DynamicField.
  There were some inconsistencies in how construct with a size was
  interpreted.

STYLE: more consistent declaration/use of Swap
This commit is contained in:
Mark Olesen
2021-01-13 19:50:33 +01:00
parent 9b56c6b1ac
commit 2c7e95d2cb
28 changed files with 594 additions and 757 deletions

View File

@ -525,10 +525,7 @@ public:
void assign(const PackedList<Width>& rhs) { (*this) = rhs; }
//- Alias for resize()
void setSize(const label len, unsigned int val = 0u)
{
resize(len, val);
}
void setSize(const label n, unsigned int val = 0u) { resize(n, 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.
@ -577,7 +577,7 @@ inline void Foam::PackedList<Width>::swap(PackedList<Width>& rhs)
}
blocks_.swap(rhs.blocks_);
Foam::Swap(size_, rhs.size_);
std::swap(size_, rhs.size_);
}

View File

@ -34,7 +34,7 @@ Foam::PtrListDictionary<T>::PtrListDictionary(const label size)
:
DictionaryBase<PtrList<T>, T>(2*size)
{
PtrList<T>::setSize(size);
PtrList<T>::resize(size);
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019-2020 OpenCFD Ltd.
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -201,17 +201,17 @@ void Foam::CompactListList<T, Container>::clear()
template<class T, class Container>
void Foam::CompactListList<T, Container>::swap
(
CompactListList<T, Container>& lst
CompactListList<T, Container>& other
)
{
if (this == &lst)
if (this == &other)
{
return; // Self-swap is a no-op
}
Foam::Swap(size_, lst.size_);
offsets_.swap(lst.offsets_);
m_.swap(lst.m_);
std::swap(size_, other.size_);
offsets_.swap(other.offsets_);
m_.swap(other.m_);
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019-2020 OpenCFD Ltd.
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -103,7 +103,7 @@ public:
// Constructors
//- Null constructor.
//- Default construct
inline CompactListList();
//- Construct by converting given List<List<T>>
@ -197,7 +197,7 @@ public:
labelList sizes() const;
//- Swap contents
void swap(CompactListList<T, Container>& lst);
void swap(CompactListList<T, Container>& other);
//- Transfer contents into this and annul the argument
void transfer(CompactListList<T, Container>& list);
@ -261,6 +261,8 @@ public:
};
// Note: uses default Foam::Swap (move construct/assignment)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -5,7 +5,6 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -39,7 +38,7 @@ Foam::label Foam::DynamicList<T, SizeMin>::removeElements
{
if (!slice.size())
{
// Noop
// No-op
return 0;
}
else if (slice.after() >= this->size())
@ -89,40 +88,4 @@ Foam::label Foam::DynamicList<T, SizeMin>::subsetElements
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, int SizeMin>
Foam::DynamicList<T, SizeMin>::DynamicList(Istream& is)
:
List<T>(is),
capacity_(List<T>::size())
{}
template<class T, int SizeMin>
Foam::Ostream& Foam::operator<<
(
Ostream& os,
const DynamicList<T, SizeMin>& lst
)
{
os << static_cast<const List<T>&>(lst);
return os;
}
template<class T, int SizeMin>
Foam::Istream& Foam::operator>>
(
Istream& is,
DynamicList<T, SizeMin>& lst
)
{
is >> static_cast<List<T>&>(lst);
lst.capacity_ = lst.List<T>::size();
return is;
}
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd.
Copyright (C) 2016-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -56,18 +56,10 @@ namespace Foam
template<class T, int SizeMin> class DynamicList;
template<class T, int SizeMin>
Ostream& operator<<
(
Ostream& os,
const DynamicList<T, SizeMin>& lst
);
inline Istream& operator>>(Istream&, DynamicList<T, SizeMin>&);
template<class T, int SizeMin>
Istream& operator>>
(
Istream& is,
DynamicList<T, SizeMin>& lst
);
inline Ostream& operator<<(Ostream&, const DynamicList<T, SizeMin>&);
/*---------------------------------------------------------------------------*\
@ -95,36 +87,25 @@ class DynamicList
//- Subset elements in range
label subsetElements(const labelRange& slice);
protected:
// Protected Member Functions
//- Copy assignment from another list
template<class ListType>
inline void assignDynList(const ListType& lst);
inline void assignDynList(const ListType& list);
public:
// Related Types
//- Declare friendship with the List class
friend class List<T>;
// Constructors
//- Default construct, an empty list without allocation.
inline constexpr DynamicList() noexcept;
//- Construct an empty list with given reserve size.
inline explicit DynamicList(const label nElem);
inline explicit DynamicList(const label len);
//- Construct with given size and value for all elements.
inline DynamicList(const label nElem, const T& val);
inline DynamicList(const label len, const T& val);
//- Construct with given size initializing all elements to zero
inline DynamicList(const label nElem, const Foam::zero);
inline DynamicList(const label len, const Foam::zero);
//- Copy construct.
inline DynamicList(const DynamicList<T, SizeMin>& lst);
@ -181,64 +162,62 @@ public:
inline label capacity() const noexcept;
// Edit
// Sizing
//- Alter the size of the underlying storage.
// The addressed size will be truncated if needed to fit, but will
// remain otherwise untouched.
// Use this or reserve() in combination with append().
inline void setCapacity(const label nElem);
//- Alter addressable list size.
// New space will be allocated if required.
// Use this to resize the list prior to using the operator[] for
// setting values (as per List usage).
inline void setSize(const label nElem);
//- Alter addressable list size and fill new space with constant.
inline void setSize(const label nElem, const T& val);
//- Alter addressable list size.
// New space will be allocated if required.
// Use this to resize the list prior to using the operator[] for
// setting values (as per List usage).
inline void resize(const label nElem);
//- Alter addressable list size and fill new space with constant.
inline void resize(const label nElem, const T& val);
inline void setCapacity(const label newCapacity);
//- Reserve allocation space for at least this size.
// Never shrinks the allocated size, use setCapacity() for that.
inline void reserve(const label nElem);
inline void reserve(const label len);
//- Alter addressable size.
// New space will be allocated if required.
inline void resize(const label newLen);
//- Alter addressable size and fill new space with constant value
inline void resize(const label newLen, const T& val);
//- Alias for resize()
void setSize(const label n) { this->resize(n); }
//- Alias for resize()
void setSize(const label n, const T& val) { this->resize(n, val); }
//- Clear the addressed list, i.e. set the size to zero.
// Allocated size does not change
inline void clear();
inline void clear() noexcept;
//- Clear the list and delete storage.
inline void clearStorage();
//- Expand the addressable size to fit the allocated capacity.
// Returns the previous addressable size.
inline label expandStorage();
inline label expandStorage() noexcept;
//- Shrink the allocated space to the number of elements used.
// Returns a reference to the DynamicList.
inline DynamicList<T, SizeMin>& shrink();
//- Swap content with any sized DynamicList
// Edit
//- Swap content, independent of sizing parameter
template<int AnySizeMin>
inline void swap(DynamicList<T, AnySizeMin>& lst);
inline void swap(DynamicList<T, AnySizeMin>& other);
//- Transfer contents of the argument List into this.
inline void transfer(List<T>& lst);
inline void transfer(List<T>& list);
//- Transfer contents of any sized DynamicList into this.
template<int AnySizeMin>
inline void transfer(DynamicList<T, AnySizeMin>& lst);
inline void transfer(DynamicList<T, AnySizeMin>& list);
//- Transfer contents of the argument SortableList into this.
inline void transfer(SortableList<T>& lst);
inline void transfer(SortableList<T>& list);
//- Append an element to the end of this list.
inline DynamicList<T, SizeMin>& append(const T& val);
@ -311,8 +290,7 @@ public:
// Member Operators
//- Return non-const access to an element,
//- resizing list if necessary
//- Return non-const access to an element, resizing list if needed
inline T& operator()(const label i);
//- Assignment of all addressed entries to the given value
@ -356,29 +334,32 @@ public:
inline void operator=(SortableList<T>&& lst);
// IOstream operators
// IOstream Operators
// Write DynamicList to Ostream.
friend Ostream& operator<< <T, SizeMin>
(
Ostream& os,
const DynamicList<T, SizeMin>& lst
);
//- Read from Istream, discarding contents of existing DynamicList.
friend Istream& operator>> <T, SizeMin>
//- Read from Istream, discarding existing contents
inline friend Istream& operator>> <T, SizeMin>
(
Istream& is,
DynamicList<T, SizeMin>& lst
DynamicList<T, SizeMin>& rhs
);
//- Write to Ostream
inline friend Ostream& operator<< <T, SizeMin>
(
Ostream& os,
const DynamicList<T, SizeMin>& rhs
);
};
// Global Functions
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
// Exchange contents of lists - see DynamicList::swap().
template<class T, int SizeMin1, int SizeMin2>
inline void Swap(DynamicList<T, SizeMin1>& a, DynamicList<T, SizeMin2>& b);
template<class T, int SizeMinA, int SizeMinB>
inline void Swap(DynamicList<T, SizeMinA>& a, DynamicList<T, SizeMinB>& b)
{
a.swap(b);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,29 +28,29 @@ License
#include "FixedList.H"
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class T, int SizeMin>
template<class ListType>
inline void Foam::DynamicList<T, SizeMin>::assignDynList
(
const ListType& lst
const ListType& list
)
{
const label newSize = lst.size();
const label newLen = list.size();
if (capacity_ >= newSize)
if (newLen <= capacity_)
{
// Can copy w/o reallocating - adjust addressable size accordingly.
List<T>::size(newSize);
List<T>::operator=(lst);
List<T>::setAddressableSize(newLen);
List<T>::operator=(list);
}
else
{
// Ensure list size consistency prior to copying.
List<T>::size(capacity_);
List<T>::setAddressableSize(capacity_);
List<T>::operator=(lst);
List<T>::operator=(list);
capacity_ = List<T>::size();
}
}
@ -61,27 +61,29 @@ inline void Foam::DynamicList<T, SizeMin>::assignDynList
template<class T, int SizeMin>
inline constexpr Foam::DynamicList<T, SizeMin>::DynamicList() noexcept
:
List<T>(),
capacity_(0)
{}
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>::DynamicList(const label nElem)
inline Foam::DynamicList<T, SizeMin>::DynamicList(const label len)
:
List<T>(),
capacity_(0)
{
reserve(nElem);
reserve(len);
}
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>::DynamicList
(
const label nElem,
const label len,
const T& val
)
:
List<T>(nElem, val),
List<T>(len, val),
capacity_(List<T>::size())
{}
@ -89,11 +91,11 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>::DynamicList
(
const label nElem,
const label len,
const Foam::zero
)
:
List<T>(nElem, Zero),
List<T>(len, Zero),
capacity_(List<T>::size())
{}
@ -101,10 +103,10 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>::DynamicList
(
const DynamicList<T, SizeMin>& lst
const DynamicList<T, SizeMin>& list
)
:
List<T>(lst),
List<T>(list),
capacity_(List<T>::size())
{}
@ -113,10 +115,10 @@ template<class T, int SizeMin>
template<int AnySizeMin>
inline Foam::DynamicList<T, SizeMin>::DynamicList
(
const DynamicList<T, AnySizeMin>& lst
const DynamicList<T, AnySizeMin>& list
)
:
List<T>(lst),
List<T>(list),
capacity_(List<T>::size())
{}
@ -124,10 +126,10 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>::DynamicList
(
const UList<T>& lst
const UList<T>& list
)
:
List<T>(lst),
List<T>(list),
capacity_(List<T>::size())
{}
@ -136,12 +138,12 @@ template<class T, int SizeMin>
template<unsigned N>
inline Foam::DynamicList<T, SizeMin>::DynamicList
(
const FixedList<T, N>& lst
const FixedList<T, N>& list
)
:
capacity_(0)
{
this->operator=(lst);
this->operator=(list);
}
@ -218,6 +220,14 @@ inline Foam::DynamicList<T, SizeMin>::DynamicList
}
template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>::DynamicList(Istream& is)
:
List<T>(is),
capacity_(List<T>::size())
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T, int SizeMin>
@ -230,124 +240,83 @@ inline Foam::label Foam::DynamicList<T, SizeMin>::capacity() const noexcept
template<class T, int SizeMin>
inline void Foam::DynamicList<T, SizeMin>::setCapacity
(
const label nElem
const label newCapacity
)
{
label nextFree = List<T>::size();
capacity_ = nElem;
label currLen = List<T>::size();
capacity_ = newCapacity;
if (nextFree > capacity_)
if (currLen > capacity_)
{
// Truncate addressed sizes too
nextFree = capacity_;
currLen = capacity_;
}
// We could also enforce sizing granularity
List<T>::setSize(capacity_);
List<T>::size(nextFree);
List<T>::resize(capacity_);
List<T>::setAddressableSize(currLen);
}
template<class T, int SizeMin>
inline void Foam::DynamicList<T, SizeMin>::reserve
(
const label nElem
const label len
)
{
// Allocate more capacity if necessary
if (nElem > capacity_)
if (capacity_ < len)
{
capacity_ = max
(
SizeMin,
max
(
nElem,
// label(SizeInc + capacity_ * SizeMult / SizeDiv)
label(2 * capacity_)
)
);
// Increase capacity (doubling)
capacity_ = max(SizeMin, max(len, label(2 * capacity_)));
// Adjust allocated size, leave addressed size untouched
const label nextFree = List<T>::size();
List<T>::setSize(capacity_);
List<T>::size(nextFree);
const label currLen = List<T>::size();
List<T>::resize(capacity_);
List<T>::setAddressableSize(currLen);
}
}
template<class T, int SizeMin>
inline void Foam::DynamicList<T, SizeMin>::setSize
inline void Foam::DynamicList<T, SizeMin>::resize
(
const label nElem
const label newLen
)
{
// Allocate more capacity if necessary
if (nElem > capacity_)
if (capacity_ < newLen)
{
capacity_ = max
(
SizeMin,
max
(
nElem,
// label(SizeInc + capacity_ * SizeMult / SizeDiv)
label(2 * capacity_)
)
);
// Increase capacity (doubling)
capacity_ = max(SizeMin, max(newLen, label(2 * capacity_)));
List<T>::setSize(capacity_);
List<T>::resize(capacity_);
}
// Adjust addressed size
List<T>::size(nElem);
}
template<class T, int SizeMin>
inline void Foam::DynamicList<T, SizeMin>::setSize
(
const label nElem,
const T& val
)
{
label nextFree = List<T>::size();
setSize(nElem);
// Set new elements to constant value
while (nextFree < nElem)
{
this->operator[](nextFree++) = val;
}
List<T>::setAddressableSize(newLen);
}
template<class T, int SizeMin>
inline void Foam::DynamicList<T, SizeMin>::resize
(
const label nElem
)
{
this->setSize(nElem);
}
template<class T, int SizeMin>
inline void Foam::DynamicList<T, SizeMin>::resize
(
const label nElem,
const label newLen,
const T& val
)
{
this->setSize(nElem, val);
label currLen = List<T>::size();
resize(newLen);
// Fill newly exposed with constant value
while (currLen < newLen)
{
this->operator[](currLen++) = val;
}
}
template<class T, int SizeMin>
inline void Foam::DynamicList<T, SizeMin>::clear()
inline void Foam::DynamicList<T, SizeMin>::clear() noexcept
{
List<T>::size(0);
List<T>::setAddressableSize(0);
}
@ -360,14 +329,14 @@ inline void Foam::DynamicList<T, SizeMin>::clearStorage()
template<class T, int SizeMin>
inline Foam::label Foam::DynamicList<T, SizeMin>::expandStorage()
inline Foam::label Foam::DynamicList<T, SizeMin>::expandStorage() noexcept
{
const label nextFree = List<T>::size();
const label currLen = List<T>::size();
// Allow addressing into the entire list
List<T>::size(capacity_);
List<T>::setAddressableSize(capacity_);
return nextFree;
return currLen;
}
@ -375,16 +344,16 @@ template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::shrink()
{
const label nextFree = List<T>::size();
if (capacity_ > nextFree)
const label currLen = List<T>::size();
if (currLen < capacity_)
{
// Use the full list when resizing
List<T>::size(capacity_);
List<T>::setAddressableSize(capacity_);
// The new size
capacity_ = nextFree;
List<T>::setSize(capacity_);
List<T>::size(nextFree);
// Capacity and size are identical
capacity_ = currLen;
List<T>::resize(currLen);
// Redundant: List<T>::setAddressableSize(currLen);
}
return *this;
}
@ -394,41 +363,30 @@ template<class T, int SizeMin>
template<int AnySizeMin>
inline void Foam::DynamicList<T, SizeMin>::swap
(
DynamicList<T, AnySizeMin>& lst
DynamicList<T, AnySizeMin>& other
)
{
// Cannot compare 'this' for different types, so use cdata()
if (this->cdata() == lst.cdata())
if (this->cdata() == other.cdata())
{
return; // Self-swap is a no-op
}
DynamicList<T, SizeMin>& cur = *this;
// Swap storage and addressable size
UList<T>::swap(other);
// Make addressable size identical to the allocated capacity
const label oldSize1 = cur.expandStorage();
const label oldSize2 = lst.expandStorage();
// Swap storage
UList<T>::swap(lst);
// Match capacity to the underlying allocated list size
cur.setCapacity(cur.size());
lst.setCapacity(lst.size());
// Set addressable size
cur.setSize(oldSize2);
lst.setSize(oldSize1);
// Swap capacity
std::swap(this->capacity_, other.capacity_);
}
template<class T, int SizeMin>
inline void
Foam::DynamicList<T, SizeMin>::transfer(List<T>& lst)
Foam::DynamicList<T, SizeMin>::transfer(List<T>& list)
{
// Take over storage, clear addressing for lst.
capacity_ = lst.size();
List<T>::transfer(lst);
// Take over storage, clear addressing for list
capacity_ = list.size();
List<T>::transfer(list);
}
@ -437,21 +395,21 @@ template<int AnySizeMin>
inline void
Foam::DynamicList<T, SizeMin>::transfer
(
DynamicList<T, AnySizeMin>& lst
DynamicList<T, AnySizeMin>& list
)
{
// Cannot compare 'this' for different types, so use cdata()
if (this->cdata() == lst.cdata())
if (this->cdata() == list.cdata())
{
return; // Self-assignment is a no-op
}
// Take over storage as-is (without shrink, without using SizeMin)
// clear addressing and storage for old lst.
capacity_ = lst.capacity();
capacity_ = list.capacity();
List<T>::transfer(static_cast<List<T>&>(lst));
lst.clearStorage(); // Ensure capacity=0
List<T>::transfer(static_cast<List<T>&>(list));
list.clearStorage(); // Ensure capacity=0
}
@ -459,12 +417,12 @@ template<class T, int SizeMin>
inline void
Foam::DynamicList<T, SizeMin>::transfer
(
SortableList<T>& lst
SortableList<T>& list
)
{
lst.shrink(); // Shrink away sort indices
capacity_ = lst.size(); // Capacity after transfer == list size
List<T>::transfer(lst);
list.shrink(); // Shrink away sort indices
capacity_ = list.size(); // Capacity after transfer == list size
List<T>::transfer(list);
}
@ -476,7 +434,7 @@ Foam::DynamicList<T, SizeMin>::append
)
{
const label idx = List<T>::size();
setSize(idx + 1);
resize(idx + 1);
this->operator[](idx) = val; // copy element
return *this;
@ -491,7 +449,7 @@ Foam::DynamicList<T, SizeMin>::append
)
{
const label idx = List<T>::size();
setSize(idx + 1);
resize(idx + 1);
this->operator[](idx) = std::move(val); // move assign element
return *this;
@ -508,12 +466,12 @@ Foam::DynamicList<T, SizeMin>::append
if (this == &lst)
{
FatalErrorInFunction
<< "Attempted appending to self" << abort(FatalError);
<< "Attempted appending to self"
<< abort(FatalError);
}
label idx = List<T>::size();
setSize(idx + lst.size());
resize(idx + lst.size());
for (const T& val : lst)
{
@ -532,7 +490,7 @@ Foam::DynamicList<T, SizeMin>::append
)
{
label idx = List<T>::size();
setSize(idx + lst.size());
resize(idx + lst.size());
for (const T& val : lst)
{
@ -550,8 +508,7 @@ Foam::DynamicList<T, SizeMin>::append
)
{
label idx = List<T>::size();
setSize(idx + lst.size());
resize(idx + lst.size());
for (const T& val : lst)
{
@ -572,7 +529,7 @@ Foam::DynamicList<T, SizeMin>::append
label idx = List<T>::size();
const label n = lst.size();
setSize(idx + n);
resize(idx + n);
for (label i=0; i<n; ++i)
{
@ -586,25 +543,25 @@ template<class T, int SizeMin>
inline Foam::DynamicList<T, SizeMin>&
Foam::DynamicList<T, SizeMin>::append
(
List<T>&& lst
List<T>&& list
)
{
if (this == &lst)
if (this == &list)
{
FatalErrorInFunction
<< "Attempted appending to self" << abort(FatalError);
<< "Attempted appending to self"
<< abort(FatalError);
}
label idx = List<T>::size();
resize(idx + list.size());
setSize(idx + lst.size());
for (T& val : lst)
for (T& val : list)
{
Foam::Swap(this->operator[](idx++), val); // moved content
}
lst.clear();
list.clear();
return *this;
}
@ -663,7 +620,7 @@ inline T Foam::DynamicList<T, SizeMin>::remove()
const T& val = List<T>::operator[](idx);
List<T>::size(idx);
List<T>::setAddressableSize(idx);
return val;
}
@ -742,7 +699,7 @@ inline T& Foam::DynamicList<T, SizeMin>::operator()
{
if (i >= List<T>::size())
{
setSize(i + 1);
resize(i + 1);
}
return this->operator[](i);
@ -788,7 +745,7 @@ inline void Foam::DynamicList<T, SizeMin>::operator=
{
const label n = lst.size();
setSize(n);
resize(n);
for (label i=0; i<n; ++i)
{
@ -906,16 +863,34 @@ inline void Foam::DynamicList<T, SizeMin>::operator=
}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, int SizeMin1, int SizeMin2>
inline void Foam::Swap
template<class T, int SizeMin>
inline Foam::Istream& Foam::operator>>
(
DynamicList<T, SizeMin1>& a,
DynamicList<T, SizeMin2>& b
Istream& is,
DynamicList<T, SizeMin>& rhs
)
{
a.swap(b);
// Use entire storage - ie, resize(capacity())
(void) rhs.expandStorage();
is >> static_cast<List<T>&>(rhs);
rhs.capacity_ = rhs.List<T>::size();
return is;
}
template<class T, int SizeMin>
inline Foam::Ostream& Foam::operator<<
(
Ostream& os,
const DynamicList<T, SizeMin>& rhs
)
{
os << static_cast<const List<T>&>(rhs);
return os;
}

View File

@ -281,7 +281,7 @@ public:
inline void resize(const label n);
//- Dummy function, to make FixedList consistent with List
inline void setSize(const label n);
void setSize(const label n) { this->resize(n); }
//- Assign all entries to the given value
inline void fill(const T& val);
@ -399,7 +399,7 @@ public:
static constexpr unsigned max_size() noexcept { return N; }
//- Swap lists by swapping the content of the individual list elements
inline void swap(FixedList<T, N>& list);
inline void swap(FixedList<T, N>& other);
// STL Member Operators
@ -491,9 +491,12 @@ struct is_contiguous_scalar<FixedList<T, N>> : is_contiguous_scalar<T> {};
// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
//- Swap FixedList contents - see FixedList::swap().
// Internally this actually swaps the individual list elements
// Internally actually swaps the individual list elements
template<class T, unsigned N>
inline void Swap(FixedList<T, N>& lhs, FixedList<T, N>& rhs);
inline void Swap(FixedList<T, N>& a, FixedList<T, N>& b)
{
a.swap(b);
}
//- Hashing for FixedList data, which uses Hasher for contiguous data and

View File

@ -327,15 +327,6 @@ inline void Foam::FixedList<T, N>::resize(const label n)
#endif
}
template<class T, unsigned N>
inline void Foam::FixedList<T, N>::setSize(const label n)
{
#ifdef FULLDEBUG
checkSize(n);
#endif
}
template<class T, unsigned N>
inline void Foam::FixedList<T, N>::fill(const T& val)
{
@ -357,16 +348,16 @@ inline void Foam::FixedList<T, N>::fill(const Foam::zero)
template<class T, unsigned N>
inline void Foam::FixedList<T, N>::swap(FixedList<T, N>& list)
inline void Foam::FixedList<T, N>::swap(FixedList<T, N>& other)
{
if (this == &list)
if (this == &other)
{
return; // Self-swap is a no-op
}
for (unsigned i=0; i<N; ++i)
{
Foam::Swap(v_[i], list.v_[i]);
Foam::Swap(v_[i], other.v_[i]);
}
}
@ -490,8 +481,6 @@ inline void Foam::FixedList<T, N>::operator=(FixedList<T, N>&& list)
return; // Self-assignment is a no-op
}
// No significant speedup observed for copy assignment on simple types,
// use move assignment for generality with more complex types
for (unsigned i=0; i<N; ++i)
{
v_[i] = std::move(list.v_[i]);
@ -597,13 +586,4 @@ Foam::FixedList<T, N>::crend() const
}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
template<class T, unsigned N>
inline void Foam::Swap(FixedList<T, N>& lhs, FixedList<T, N>& rhs)
{
lhs.swap(rhs);
}
// ************************************************************************* //

View File

@ -93,7 +93,7 @@ class List
template<class List2>
inline void copyList(const List2& list);
//- Change allocation size of List. Backend for resize/setSize.
//- Change allocation size - backend for resize.
void doResize(const label newSize);
//- Construct given begin/end iterators and number of elements
@ -123,7 +123,7 @@ public:
// Constructors
//- Null constructor
//- Default construct
inline constexpr List() noexcept;
//- Construct with given size
@ -208,24 +208,27 @@ public:
// Member Functions
// Edit
//- Adjust allocated size of list.
// The boolList version fills new memory with false.
inline void resize(const label newSize);
//- Adjust allocated size of list and set val for new elements
void resize(const label newSize, const T& val);
//- Alias for resize(const label)
inline void setSize(const label newSize);
//- Alias for resize(const label, const T&)
inline void setSize(const label newSize, const T& val);
// Sizing
//- Clear the list, i.e. set size to zero
inline void clear();
//- Adjust allocated size of list.
// The boolList version fills new memory with false.
inline void resize(const label newLen);
//- Adjust allocated size of list and set val for new elements
void resize(const label newLen, const T& val);
//- Alias for resize()
void setSize(const label n) { this->resize(n); }
//- Alias for resize()
void setSize(const label n, const T& val) { this->resize(n, val); }
// Edit
//- Append an element at the end of the list
inline void append(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.
@ -128,31 +128,17 @@ namespace Foam
{
// Template specialization for bool. Fills with false
template<>
inline void List<bool>::resize(const label newSize)
inline void List<bool>::resize(const label newLen)
{
this->resize(newSize, false);
this->resize(newLen, false);
}
}
template<class T>
inline void Foam::List<T>::resize(const label len)
inline void Foam::List<T>::resize(const label newLen)
{
this->doResize(len);
}
template<class T>
inline void Foam::List<T>::setSize(const label len)
{
this->resize(len);
}
template<class T>
inline void Foam::List<T>::setSize(const label len, const T& val)
{
this->resize(len, val);
this->doResize(newLen);
}

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.
@ -195,15 +195,15 @@ void Foam::SortableList<T>::partialReverseSort(label n, label start)
template<class T>
void Foam::SortableList<T>::swap(SortableList<T>& lst)
void Foam::SortableList<T>::swap(SortableList<T>& other)
{
if (this == &lst)
if (this == &other)
{
return; // Self-swap is a no-op
}
List<T>::swap(lst);
indices_.swap(lst.indices_);
List<T>::swap(other);
indices_.swap(other.indices_);
}
@ -267,13 +267,4 @@ inline void Foam::SortableList<T>::operator=(std::initializer_list<T> lst)
}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
template<class T>
inline void Foam::Swap(SortableList<T>& a, SortableList<T>& b)
{
a.swap(b);
}
// ************************************************************************* //

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.
@ -72,7 +72,7 @@ public:
// Constructors
//- Null constructor, sort later (eg, after assignment or transfer)
//- Default construct
inline constexpr SortableList() noexcept;
//- Construct given size, sort later.
@ -143,7 +143,7 @@ public:
void partialReverseSort(label n, label start=0);
//- Swap content with another SortableList in constant time
inline void swap(SortableList<T>& lst);
inline void swap(SortableList<T>& other);
// Member Operators
@ -160,20 +160,22 @@ public:
//- Move assignment, removing indices. Takes linear time
inline void operator=(List<T>&& lst);
//- Move operator. Takes linear time
//- Move operator (constant time)
inline void operator=(SortableList<T>&& lst);
//- Assignment to an initializer list
void operator=(std::initializer_list<T> lst);
};
// Global Functions
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
// Exchange contents of lists - see SortableList::swap().
template<class T>
inline void Swap(SortableList<T>& a, SortableList<T>& b);
inline void Swap(SortableList<T>& a, SortableList<T>& b)
{
a.swap(b);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

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.
@ -101,9 +101,13 @@ protected:
// Protected Member Functions
//- Override size to be inconsistent with allocated storage.
//- Set addressed size to be inconsistent with allocated storage.
// Use with care
inline void size(const label n) noexcept;
inline void setAddressableSize(const label n) noexcept;
//- Older name for setAddressableSize
FOAM_DEPRECATED_FOR(2021-01, "setAddressableSize(label) method")
void size(const label n) { this->setAddressableSize(n); }
//- Write the UList with its compound type
void writeEntry(Ostream& os) const;
@ -579,9 +583,12 @@ inline void reverse(UList<T>& list, const label n);
template<class T>
inline void reverse(UList<T>& list);
// Exchange contents of lists - see UList::swap().
//- Exchange contents of lists - see UList::swap().
template<class T>
inline void Swap(UList<T>& a, UList<T>& b);
inline void Swap(UList<T>& a, UList<T>& b)
{
a.swap(b);
}
//- Hashing for UList data, which uses Hasher for contiguous data and

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2015-2019 OpenCFD Ltd.
Copyright (C) 2015-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,7 +28,6 @@ License
#include "error.H"
#include "pTraits.H"
#include "Swap.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
@ -357,7 +356,7 @@ Foam::UList<T>::crend() const
template<class T>
inline void Foam::UList<T>::size(const label n) noexcept
inline void Foam::UList<T>::setAddressableSize(const label n) noexcept
{
size_ = n;
}
@ -385,8 +384,8 @@ inline void Foam::UList<T>::swap(UList<T>& list)
return; // Self-swap is a no-op
}
Foam::Swap(size_, list.size_);
Foam::Swap(v_, list.v_);
std::swap(size_, list.size_);
std::swap(v_, list.v_);
}
@ -409,11 +408,4 @@ inline void Foam::reverse(UList<T>& list)
}
template<class T>
inline void Foam::Swap(UList<T>& a, UList<T>& b)
{
a.swap(b);
}
// ************************************************************************* //

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.
@ -71,17 +71,11 @@ class PtrDynList
label capacity_;
// Private Member Functions
//- Adjust addressable size
void setAddressableSize(const label len);
public:
// Constructors
//- Construct null
//- Default construct
inline constexpr PtrDynList() noexcept;
//- Construct with given capacity.
@ -118,23 +112,20 @@ public:
const T* set(const label i) const { return this->get(i); }
// Edit
//- Delete the allocated entries, but retain the list size.
using PtrList<T>::free;
// Sizing
//- Alter the size of the underlying storage.
inline void setCapacity(const label nElem);
inline void setCapacity(const label newCapacity);
//- Reserve allocation space for at least this size.
// Never shrinks the allocated size, use setCapacity() for that.
inline void reserve(const label len);
//- Alter the addressed list size.
inline void resize(const label newLen);
//- Same as resize()
inline void setSize(const label newLen);
//- Reserve allocation space for at least this size.
// Never shrinks the allocated size, use setCapacity() for that.
inline void reserve(const label nElem);
void setSize(const label n) { this->resize(n); }
//- Clear the addressed list, i.e. set the size to zero.
// Allocated size does not change
@ -145,16 +136,26 @@ public:
//- Expand the addressable size to fit the allocated capacity.
// Returns the previous addressable size.
inline label expandStorage();
inline label expandStorage() noexcept;
//- Shrink the allocated space to the number of elements used.
inline void shrink();
// Edit
//- Delete the allocated entries, but retain the list size.
using PtrList<T>::free;
//- Squeeze out intermediate nullptr entries in the list of pointers
//- and adjust the addressable size accordingly.
// \return the number of non-null entries
inline label squeezeNull();
//- Swap content, independent of sizing parameter
template<int AnySizeMin>
inline void swap(PtrDynList<T, AnySizeMin>& other);
//- Construct and append an element to the end of the list
template<class... Args>
inline void emplace_append(Args&&... args);
@ -234,7 +235,6 @@ public:
//- Move assignment with different sizing parameters
template<int AnySizeMin>
inline void operator=(PtrDynList<T, AnySizeMin>&& list);
};

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.
@ -29,15 +29,6 @@ License
#include "refPtr.H"
#include "tmp.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::setAddressableSize(const label len)
{
(this->ptrs_).setAddressableSize(len);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T, int SizeMin>
@ -54,7 +45,7 @@ inline Foam::PtrDynList<T, SizeMin>::PtrDynList(const label len)
PtrList<T>(len),
capacity_(len)
{
setAddressableSize(0);
PtrList<T>::setAddressableSize(0);
}
@ -107,35 +98,34 @@ inline const T* Foam::PtrDynList<T, SizeMin>::get(const label i) const
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::setCapacity(const label nElem)
inline void Foam::PtrDynList<T, SizeMin>::setCapacity(const label newCapacity)
{
label nextFree = PtrList<T>::size();
capacity_ = nElem;
label currLen = PtrList<T>::size();
capacity_ = newCapacity;
if (nextFree > capacity_)
if (currLen > capacity_)
{
// Truncate addressed sizes too
nextFree = capacity_;
currLen = capacity_;
}
PtrList<T>::resize(capacity_);
setAddressableSize(nextFree);
PtrList<T>::setAddressableSize(currLen);
}
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::reserve(const label nElem)
inline void Foam::PtrDynList<T, SizeMin>::reserve(const label len)
{
if (nElem > capacity_)
if (capacity_ < len)
{
// Allocate more capacity if necessary
capacity_ = max(SizeMin, max(nElem, label(2*capacity_)));
// Increase capacity (doubling)
capacity_ = max(SizeMin, max(len, label(2 * capacity_)));
// Adjust allocated size, leave addressed size untouched
const label nextFree = PtrList<T>::size();
const label currLen = PtrList<T>::size();
PtrList<T>::resize(capacity_);
setAddressableSize(nextFree);
PtrList<T>::setAddressableSize(currLen);
}
}
@ -147,9 +137,9 @@ inline void Foam::PtrDynList<T, SizeMin>::resize(const label newLen)
const label oldLen = ptrs.size();
if (newLen > capacity_)
if (capacity_ < newLen)
{
// Allocate more capacity if necessary
// Increase capacity (doubling)
capacity_ = max(SizeMin, max(newLen, label(2 * capacity_)));
PtrList<T>::resize(capacity_);
@ -164,14 +154,8 @@ inline void Foam::PtrDynList<T, SizeMin>::resize(const label newLen)
}
}
setAddressableSize(newLen);
}
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::setSize(const label newLen)
{
this->resize(newLen);
// Adjust addressed size
PtrList<T>::setAddressableSize(newLen);
}
@ -179,7 +163,7 @@ template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::clear()
{
(this->ptrs_).free(); // free old pointers
setAddressableSize(0);
PtrList<T>::setAddressableSize(0);
}
@ -192,30 +176,30 @@ inline void Foam::PtrDynList<T, SizeMin>::clearStorage()
template<class T, int SizeMin>
inline Foam::label Foam::PtrDynList<T, SizeMin>::expandStorage()
inline Foam::label Foam::PtrDynList<T, SizeMin>::expandStorage() noexcept
{
const label nextFree = PtrList<T>::size();
const label currLen = PtrList<T>::size();
// Allow addressing into the entire list
setAddressableSize(capacity_);
PtrList<T>::setAddressableSize(capacity_);
return nextFree;
return currLen;
}
template<class T, int SizeMin>
inline void Foam::PtrDynList<T, SizeMin>::shrink()
{
label nextFree = PtrList<T>::size();
if (capacity_ > nextFree)
const label currLen = PtrList<T>::size();
if (currLen < capacity_)
{
// Use the full list when resizing
setAddressableSize(capacity_);
PtrList<T>::setAddressableSize(capacity_);
// The new size
capacity_ = nextFree;
PtrList<T>::resize(capacity_);
setAddressableSize(nextFree);
// Capacity and size are identical
capacity_ = currLen;
PtrList<T>::resize(currLen);
// Redundant: PtrList<T>::setAddressableSize(currLen);
}
}
@ -229,6 +213,27 @@ inline Foam::label Foam::PtrDynList<T, SizeMin>::squeezeNull()
}
template<class T, int SizeMin>
template<int AnySizeMin>
inline void Foam::PtrDynList<T, SizeMin>::swap
(
PtrDynList<T, AnySizeMin>& other
)
{
// Cannot compare 'this' for different types, so use cdata()
if (this->cdata() == other.cdata())
{
return; // Self-swap is a no-op
}
// Swap storage and addressable size
UPtrList<T>::swap(other);
// Swap capacity
std::swap(this->capacity_, other.capacity_);
}
template<class T, int SizeMin>
template<class... Args>
inline void Foam::PtrDynList<T, SizeMin>::emplace_append(Args&&... args)
@ -332,7 +337,7 @@ inline Foam::autoPtr<T> Foam::PtrDynList<T, SizeMin>::remove()
autoPtr<T> old(this->ptrs_[idx]);
this->ptrs_[idx] = nullptr;
setAddressableSize(idx);
PtrList<T>::setAddressableSize(idx);
return old;
}

View File

@ -149,7 +149,7 @@ public:
void resize(const label newLen);
//- Same as resize()
inline void setSize(const label newLen);
void setSize(const label newLen) { this->resize(newLen); }
//- Construct and append an element to the end of the list
template<class... Args>

View File

@ -101,13 +101,6 @@ inline void Foam::PtrList<T>::clear()
}
template<class T>
inline void Foam::PtrList<T>::setSize(const label newLen)
{
this->resize(newLen);
}
template<class T>
template<class... Args>
inline void Foam::PtrList<T>::emplace_append(Args&&... args)

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.
@ -106,9 +106,9 @@ public:
// New entries are initialized to nullptr.
inline void resize(const label newLen);
//- Override size to be inconsistent with allocated storage.
//- Set addressed size to be inconsistent with allocated storage.
// Use with care
inline void setAddressableSize(const label n);
inline void setAddressableSize(const label n) noexcept;
//- Write output, optionally silently trimming nullptrs
Ostream& write(Ostream& os, const bool trimNull=false) const;

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.
@ -85,9 +85,12 @@ inline Foam::Detail::PtrListDetail<T>::PtrListDetail
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
inline void Foam::Detail::PtrListDetail<T>::setAddressableSize(const label n)
inline void Foam::Detail::PtrListDetail<T>::setAddressableSize
(
const label n
) noexcept
{
List<T*>::size(n);
List<T*>::setAddressableSize(n);
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -78,10 +78,16 @@ protected:
Detail::PtrListDetail<T> ptrs_;
// Protected Member Functions
//- Adjust addressable size
inline void setAddressableSize(const label n) noexcept;
// Constructors
//- Low-level move construct
inline UPtrList(Detail::PtrListDetail<T>&& ptrs);
inline explicit UPtrList(Detail::PtrListDetail<T>&& ptrs);
public:
@ -158,15 +164,15 @@ public:
inline const T& last() const;
//- Return pointer to element (can be nullptr),
// without bounds checking.
//- without bounds checking.
inline T* get(const label i);
//- Return const pointer to element (can be nullptr),
// without bounds checking.
//- without bounds checking.
inline const T* get(const label i) const;
//- Return const pointer to element (can be nullptr),
// without bounds checking - same as get().
//- without bounds checking - same as get().
// The return value can also be tested as a bool.
const T* set(const label i) const { return this->get(i); }
@ -176,12 +182,12 @@ public:
//- Set list size to zero.
inline void clear();
//- Reset size of list.
//- Change the size of the list.
// New entries are initialized to nullptr.
inline void resize(const label newLen);
//- Same as resize()
inline void setSize(const label newLen);
//- Alias for resize()
void setSize(const label n) { this->resize(n); }
//- Squeeze out intermediate nullptr entries in the list of pointers
// \return the number of non-null entries

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -26,6 +26,15 @@ License
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
template<class T>
inline void Foam::UPtrList<T>::setAddressableSize(const label n) noexcept
{
ptrs_.setAddressableSize(n);
}
// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
template<class T>
@ -177,13 +186,6 @@ inline void Foam::UPtrList<T>::resize(const label newLen)
}
template<class T>
inline void Foam::UPtrList<T>::setSize(const label newLen)
{
ptrs_.resize(newLen);
}
template<class T>
inline void Foam::UPtrList<T>::append(T* ptr)
{

View File

@ -1,77 +1 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "DynamicField.H"
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, int SizeMin>
Foam::DynamicField<T, SizeMin>::DynamicField(Istream& is)
:
Field<T>(is),
capacity_(Field<T>::size())
{}
template<class T, int SizeMin>
Foam::tmp<Foam::DynamicField<T, SizeMin>>
Foam::DynamicField<T, SizeMin>::clone() const
{
return tmp<DynamicField<T, SizeMin>>::New(*this);
}
// * * * * * * * * * * * * * * * IOstream Operator * * * * * * * * * * * * * //
template<class T, int SizeMin>
Foam::Ostream& Foam::operator<<
(
Ostream& os,
const DynamicField<T, SizeMin>& lst
)
{
os << static_cast<const Field<T>&>(lst);
return os;
}
template<class T, int SizeMin>
Foam::Istream& Foam::operator>>
(
Istream& is,
DynamicField<T, SizeMin>& lst
)
{
is >> static_cast<Field<T>&>(lst);
lst.capacity_ = lst.Field<T>::size();
return is;
}
// ************************************************************************* //
#warning File removed - left for old dependency check only

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd.
Copyright (C) 2016-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -32,7 +32,6 @@ Description
SourceFiles
DynamicFieldI.H
DynamicField.C
\*---------------------------------------------------------------------------*/
@ -47,24 +46,14 @@ SourceFiles
namespace Foam
{
// Forward declarations
// Forward Declarations
template<class T, int SizeMin> class DynamicField;
template<class T, int SizeMin>
Ostream& operator<<
(
Ostream& os,
const DynamicField<T, SizeMin>& fld
);
inline Istream& operator>>(Istream&, DynamicField<T, SizeMin>&);
template<class T, int SizeMin>
Istream& operator>>
(
Istream& is,
DynamicField<T, SizeMin>& fld
);
inline Ostream& operator<<(Ostream&, const DynamicField<T, SizeMin>&);
/*---------------------------------------------------------------------------*\
Class DynamicField Declaration
@ -77,7 +66,7 @@ class DynamicField
{
static_assert(SizeMin > 0, "Invalid min size parameter");
// Private data
// Private Data
//- The capacity (allocated size) of the underlying field.
label capacity_;
@ -87,7 +76,7 @@ class DynamicField
//- Copy assignment from another list
template<class ListType>
inline void assignDynField(const ListType& list);
inline void assignDynList(const ListType& list);
public:
@ -102,7 +91,7 @@ public:
// Constructors
//- Construct null
//- Default construct, an empty field without allocation.
inline constexpr DynamicField() noexcept;
//- Construct empty field with given reserve size.
@ -162,69 +151,69 @@ public:
);
//- Construct from Istream. Size set to size of list read.
explicit DynamicField(Istream& is);
inline explicit DynamicField(Istream& is);
//- Clone
tmp<DynamicField<T, SizeMin>> clone() const;
inline tmp<DynamicField<T, SizeMin>> clone() const;
// Member Functions
// Access
//- Normal lower capacity limit - the SizeMin template parameter
static constexpr label min_size() noexcept { return SizeMin; }
//- Size of the underlying storage.
inline label capacity() const noexcept;
// Edit
// Sizing
//- Alter the size of the underlying storage.
// The addressed size will be truncated if needed to fit, but will
// remain otherwise untouched.
// Use this or reserve() in combination with append().
inline void setCapacity(const label nElem);
//- Alter the addressed list size.
// New space will be allocated if required.
// Use this to resize the list prior to using the operator[] for
// setting values (as per List usage).
inline void setSize(const label nElem);
//- Alter the addressed list size and fill new space with a constant.
inline void setSize(const label nElem, const T& val);
//- Alter the addressed list size.
// New space will be allocated if required.
// Use this to resize the list prior to using the operator[] for
// setting values (as per List usage).
inline void resize(const label nElem);
//- Alter the addressed list size and fill new space with a
// constant.
inline void resize(const label nElem, const T& val);
inline void setCapacity(const label newCapacity);
//- Reserve allocation space for at least this size.
// Never shrinks the allocated size, use setCapacity() for that.
inline void reserve(const label nElem);
inline void reserve(const label len);
//- Alter addressable size.
// New space will be allocated if required.
inline void resize(const label newLen);
//- Alter addressable size and fill new space with constant value
inline void resize(const label newLen, const T& val);
//- Alias for resize()
void setSize(const label n) { this->resize(n); }
//- Alias for resize()
void setSize(const label n, const T& val) { this->resize(n, val); }
//- Clear the addressed list, i.e. set the size to zero.
// Allocated size does not change
inline void clear();
inline void clear() noexcept;
//- Clear the list and delete storage.
inline void clearStorage();
//- Expand the addressable size to fit the allocated capacity.
// Returns the previous addressable size.
inline label expandStorage();
inline label expandStorage() noexcept;
//- Shrink the allocated space to the number of elements used.
// Returns a reference to the DynamicField.
inline DynamicField<T, SizeMin>& shrink();
//- Swap content with any sized DynamicField
// Edit
//- Swap content, independent of sizing parameter
template<int AnySizeMin>
inline void swap(DynamicField<T, AnySizeMin>& list);
inline void swap(DynamicField<T, AnySizeMin>& other);
//- Transfer the parameter contents into this
inline void transfer(List<T>& list);
@ -237,10 +226,11 @@ public:
template<int AnySizeMin>
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 DynamicField<T, SizeMin>& append(const T& val);
//- Move append an element
inline DynamicField<T, SizeMin>& append(T&& val);
//- Append a List at the end of this list
inline DynamicField<T, SizeMin>&
@ -252,12 +242,15 @@ public:
// Member Operators
//- Return non-const access to an element, resizing list if necessary
//- Return non-const access to an element, resizing list if needed
inline T& operator()(const label i);
//- Assign addressed entries to the given value
inline void operator=(const T& val);
//- Assign addressed entries to zero
inline void operator=(const Foam::zero);
//- Copy assignment
inline void operator=(const UList<T>& list);
@ -274,6 +267,22 @@ public:
template<int AnySizeMin>
inline void operator=(DynamicField<T, AnySizeMin>&& list);
// IOstream Operators
//- Read from Istream, discarding existing contents
inline friend Istream& operator>> <T, SizeMin>
(
Istream& is,
DynamicField<T, SizeMin>& rhs
);
//- Write to Ostream
inline friend Ostream& operator<< <T, SizeMin>
(
Ostream& os,
const DynamicField<T, SizeMin>& rhs
);
};
@ -287,12 +296,6 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "DynamicField.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -30,26 +30,26 @@ License
template<class T, int SizeMin>
template<class ListType>
inline void Foam::DynamicField<T, SizeMin>::assignDynField
inline void Foam::DynamicField<T, SizeMin>::assignDynList
(
const ListType& list
)
{
const label newSize = list.size();
const label newLen = list.size();
if (capacity_ >= newSize)
if (newLen <= capacity_)
{
// Can copy w/o reallocating - adjust addressable size accordingly.
Field<T>::size(list.size());
Field<T>::operator=(list);
List<T>::setAddressableSize(newLen);
List<T>::operator=(list);
}
else
{
// Ensure list size consistency prior to copying.
Field<T>::size(capacity_);
List<T>::setAddressableSize(capacity_);
Field<T>::operator=(list);
capacity_ = Field<T>::size();
List<T>::operator=(list);
capacity_ = List<T>::size();
}
}
@ -67,10 +67,10 @@ inline constexpr Foam::DynamicField<T, SizeMin>::DynamicField() noexcept
template<class T, int SizeMin>
inline Foam::DynamicField<T, SizeMin>::DynamicField(const label len)
:
Field<T>(len),
capacity_(Field<T>::size())
Field<T>(),
capacity_(0)
{
Field<T>::size(0);
reserve(len);
}
@ -207,7 +207,6 @@ inline Foam::DynamicField<T, SizeMin>::DynamicField
{}
//- Construct by mapping from the given field
template<class T, int SizeMin>
inline Foam::DynamicField<T, SizeMin>::DynamicField
(
@ -220,6 +219,22 @@ inline Foam::DynamicField<T, SizeMin>::DynamicField
{}
template<class T, int SizeMin>
inline Foam::DynamicField<T, SizeMin>::DynamicField(Istream& is)
:
Field<T>(is),
capacity_(Field<T>::size())
{}
template<class T, int SizeMin>
inline Foam::tmp<Foam::DynamicField<T, SizeMin>>
Foam::DynamicField<T, SizeMin>::clone() const
{
return tmp<DynamicField<T, SizeMin>>::New(*this);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T, int SizeMin>
@ -232,144 +247,103 @@ inline Foam::label Foam::DynamicField<T, SizeMin>::capacity() const noexcept
template<class T, int SizeMin>
inline void Foam::DynamicField<T, SizeMin>::setCapacity
(
const label nElem
const label newCapacity
)
{
label nextFree = Field<T>::size();
capacity_ = nElem;
label currLen = List<T>::size();
capacity_ = newCapacity;
if (nextFree > capacity_)
if (currLen > capacity_)
{
// truncate addressed sizes too
nextFree = capacity_;
// Truncate addressed sizes too
currLen = capacity_;
}
// We could also enforce sizing granularity
Field<T>::setSize(capacity_);
Field<T>::size(nextFree);
List<T>::resize(capacity_);
List<T>::setAddressableSize(currLen);
}
template<class T, int SizeMin>
inline void Foam::DynamicField<T, SizeMin>::reserve
(
const label nElem
const label len
)
{
// Allocate more capacity if necessary
if (nElem > capacity_)
if (capacity_ < len)
{
capacity_ = max
(
SizeMin,
max
(
nElem,
// label(SizeInc + capacity_ * SizeMult / SizeDiv)
label(2*capacity_)
)
);
// Increase capacity (doubling)
capacity_ = max(SizeMin, max(len, label(2 * capacity_)));
// Adjust allocated size, leave addressed size untouched
const label nextFree = Field<T>::size();
Field<T>::setSize(capacity_);
Field<T>::size(nextFree);
const label currLen = List<T>::size();
List<T>::resize(capacity_);
List<T>::setAddressableSize(currLen);
}
}
template<class T, int SizeMin>
inline void Foam::DynamicField<T, SizeMin>::setSize
inline void Foam::DynamicField<T, SizeMin>::resize
(
const label nElem
const label newLen
)
{
// Allocate more capacity if necessary
if (nElem > capacity_)
if (capacity_ < newLen)
{
capacity_ = max
(
SizeMin,
max
(
nElem,
// label(SizeInc + capacity_ * SizeMult / SizeDiv)
label(2*capacity_)
)
);
// Increase capacity (doubling)
capacity_ = max(SizeMin, max(newLen, label(2 * capacity_)));
Field<T>::setSize(capacity_);
List<T>::resize(capacity_);
}
// Adjust addressed size
Field<T>::size(nElem);
}
template<class T, int SizeMin>
inline void Foam::DynamicField<T, SizeMin>::setSize
(
const label nElem,
const T& val
)
{
label nextFree = Field<T>::size();
setSize(nElem);
// Set new elements to constant value
while (nextFree < nElem)
{
this->operator[](nextFree++) = val;
}
List<T>::setAddressableSize(newLen);
}
template<class T, int SizeMin>
inline void Foam::DynamicField<T, SizeMin>::resize
(
const label nElem
)
{
this->setSize(nElem);
}
template<class T, int SizeMin>
inline void Foam::DynamicField<T, SizeMin>::resize
(
const label nElem,
const label newLen,
const T& val
)
{
this->setSize(nElem, val);
label currLen = List<T>::size();
resize(newLen);
// Fill newly exposed with constant value
while (currLen < newLen)
{
this->operator[](currLen++) = val;
}
}
template<class T, int SizeMin>
inline void Foam::DynamicField<T, SizeMin>::clear()
inline void Foam::DynamicField<T, SizeMin>::clear() noexcept
{
Field<T>::size(0);
List<T>::setAddressableSize(0);
}
template<class T, int SizeMin>
inline void Foam::DynamicField<T, SizeMin>::clearStorage()
{
Field<T>::clear();
List<T>::clear();
capacity_ = 0;
}
template<class T, int SizeMin>
inline Foam::label Foam::DynamicField<T, SizeMin>::expandStorage()
inline Foam::label Foam::DynamicField<T, SizeMin>::expandStorage() noexcept
{
const label nextFree = Field<T>::size();
const label currLen = List<T>::size();
// Allow addressing into the entire list
Field<T>::size(capacity_);
List<T>::setAddressableSize(capacity_);
return nextFree;
return currLen;
}
@ -377,16 +351,16 @@ template<class T, int SizeMin>
inline Foam::DynamicField<T, SizeMin>&
Foam::DynamicField<T, SizeMin>::shrink()
{
label nextFree = Field<T>::size();
if (capacity_ > nextFree)
const label currLen = Field<T>::size();
if (currLen < capacity_)
{
// Use the full list when resizing
Field<T>::size(capacity_);
List<T>::setAddressableSize(capacity_);
// The new size
capacity_ = nextFree;
Field<T>::setSize(capacity_);
Field<T>::size(nextFree);
// Capacity and size are identical
capacity_ = currLen;
List<T>::resize(currLen);
// Redundant: List<T>::setAddressableSize(currLen);
}
return *this;
}
@ -396,38 +370,27 @@ template<class T, int SizeMin>
template<int AnySizeMin>
inline void Foam::DynamicField<T, SizeMin>::swap
(
DynamicField<T, AnySizeMin>& lst
DynamicField<T, AnySizeMin>& other
)
{
// Cannot compare 'this' for different types, so use cdata()
if (this->cdata() == lst.cdata())
if (this->cdata() == other.cdata())
{
return; // Self-swap is a no-op
}
DynamicField<T, SizeMin>& cur = *this;
// Swap storage and addressable size
UList<T>::swap(other);
// Make addressable size identical to the allocated capacity
const label oldSize1 = cur.expandStorage();
const label oldSize2 = lst.expandStorage();
// Swap storage
Field<T>::swap(lst);
// Match capacity to the underlying allocated list size
cur.setCapacity(cur.size());
lst.setCapacity(lst.size());
// Set addressable size
cur.setSize(oldSize2);
lst.setSize(oldSize1);
// Swap capacity
std::swap(this->capacity_, other.capacity_);
}
template<class T, int SizeMin>
inline void Foam::DynamicField<T, SizeMin>::transfer(List<T>& list)
{
// Take over storage, clear addressing for list.
// Take over storage, clear addressing for list
capacity_ = list.size();
Field<T>::transfer(list);
}
@ -443,7 +406,7 @@ inline void Foam::DynamicField<T, SizeMin>::transfer
// Cannot compare 'this' for different types, so use cdata()
if (this->cdata() == list.cdata())
{
return; // Self-swap is a no-op
return; // Self-assignment is a no-op
}
// Take over storage as-is (without shrink, without using SizeMin)
@ -465,7 +428,7 @@ inline void Foam::DynamicField<T, SizeMin>::transfer
// Cannot compare 'this' for different types, so use cdata()
if (this->cdata() == list.cdata())
{
return; // Self-swap is a no-op
return; // Self-assignment is a no-op
}
// Take over storage as-is (without shrink, without using SizeMin)
@ -485,13 +448,28 @@ Foam::DynamicField<T, SizeMin>::append
)
{
const label idx = List<T>::size();
setSize(idx + 1);
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
(
T&& val
)
{
const label idx = List<T>::size();
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
@ -502,11 +480,12 @@ Foam::DynamicField<T, SizeMin>::append
if (this == &list)
{
FatalErrorInFunction
<< "Attempted appending to self" << abort(FatalError);
<< "Attempted appending to self"
<< abort(FatalError);
}
label idx = List<T>::size();
setSize(idx + list.size());
resize(idx + list.size());
for (const T& val : list)
{
@ -530,7 +509,7 @@ inline T Foam::DynamicField<T, SizeMin>::remove()
const T& val = List<T>::operator[](idx);
List<T>::size(idx);
List<T>::setAddressableSize(idx);
return val;
}
@ -546,7 +525,7 @@ inline T& Foam::DynamicField<T, SizeMin>::operator()
{
if (i >= Field<T>::size())
{
setSize(i + 1);
resize(i + 1);
}
return this->operator[](i);
@ -563,13 +542,23 @@ inline void Foam::DynamicField<T, SizeMin>::operator=
}
template<class T, int SizeMin>
inline void Foam::DynamicField<T, SizeMin>::operator=
(
const Foam::zero
)
{
UList<T>::operator=(Zero);
}
template<class T, int SizeMin>
inline void Foam::DynamicField<T, SizeMin>::operator=
(
const UList<T>& list
)
{
assignDynField(list);
assignDynList(list);
}
@ -584,7 +573,7 @@ inline void Foam::DynamicField<T, SizeMin>::operator=
return; // Self-assignment is a no-op
}
assignDynField(list);
assignDynList(list);
}
@ -619,4 +608,35 @@ inline void Foam::DynamicField<T, SizeMin>::operator=
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, int SizeMin>
inline Foam::Istream& Foam::operator>>
(
Istream& is,
DynamicField<T, SizeMin>& rhs
)
{
// Use entire storage - ie, resize(capacity())
(void) rhs.expandStorage();
is >> static_cast<Field<T>&>(rhs);
rhs.capacity_ = rhs.Field<T>::size();
return is;
}
template<class T, int SizeMin>
inline Foam::Ostream& Foam::operator<<
(
Ostream& os,
const DynamicField<T, SizeMin>& rhs
)
{
os << static_cast<const Field<T>&>(rhs);
return os;
}
// ************************************************************************* //

View File

@ -1 +0,0 @@
#warning File removed - left for old dependency check only