From 2c7e95d2cbbc609b1129fc367894eab16144e3ec Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 13 Jan 2021 19:50:33 +0100 Subject: [PATCH] ENH: rename protected UList size(label) -> setAddressableSize(label) - modification/continuation of 8d63073b0802 and 5c1ec7ecb8ff (#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 --- .../containers/Bits/PackedList/PackedList.H | 5 +- .../containers/Bits/PackedList/PackedListI.H | 4 +- .../PtrListDictionary/PtrListDictionary.C | 2 +- .../Lists/CompactListList/CompactListList.C | 12 +- .../Lists/CompactListList/CompactListList.H | 10 +- .../Lists/DynamicList/DynamicList.C | 39 +-- .../Lists/DynamicList/DynamicList.H | 147 ++++----- .../Lists/DynamicList/DynamicListI.H | 295 ++++++++---------- .../containers/Lists/FixedList/FixedList.H | 11 +- .../containers/Lists/FixedList/FixedListI.H | 26 +- src/OpenFOAM/containers/Lists/List/List.H | 39 +-- src/OpenFOAM/containers/Lists/List/ListI.H | 24 +- .../Lists/SortableList/SortableList.C | 19 +- .../Lists/SortableList/SortableList.H | 16 +- src/OpenFOAM/containers/Lists/UList/UList.H | 17 +- src/OpenFOAM/containers/Lists/UList/UListI.H | 16 +- .../PtrLists/PtrDynList/PtrDynList.H | 40 +-- .../PtrLists/PtrDynList/PtrDynListI.H | 103 +++--- .../containers/PtrLists/PtrList/PtrList.H | 2 +- .../containers/PtrLists/PtrList/PtrListI.H | 7 - .../PtrLists/PtrListDetail/PtrListDetail.H | 6 +- .../PtrLists/PtrListDetail/PtrListDetailI.H | 9 +- .../containers/PtrLists/UPtrList/UPtrList.H | 22 +- .../containers/PtrLists/UPtrList/UPtrListI.H | 18 +- .../fields/Fields/DynamicField/DynamicField.C | 78 +---- .../fields/Fields/DynamicField/DynamicField.H | 115 +++---- .../Fields/DynamicField/DynamicFieldI.H | 268 ++++++++-------- src/OpenFOAM/memory/tmpNrc/tmpNrcI.H | 1 - 28 files changed, 594 insertions(+), 757 deletions(-) delete mode 100644 src/OpenFOAM/memory/tmpNrc/tmpNrcI.H diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H index 566dea80d8..7c2bf50a77 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H @@ -525,10 +525,7 @@ public: void assign(const PackedList& 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); } }; diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H index 5a8763d4d2..d52fc32eff 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.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. @@ -577,7 +577,7 @@ inline void Foam::PackedList::swap(PackedList& rhs) } blocks_.swap(rhs.blocks_); - Foam::Swap(size_, rhs.size_); + std::swap(size_, rhs.size_); } diff --git a/src/OpenFOAM/containers/Dictionaries/PtrListDictionary/PtrListDictionary.C b/src/OpenFOAM/containers/Dictionaries/PtrListDictionary/PtrListDictionary.C index 36a1f658ea..4cad8cb575 100644 --- a/src/OpenFOAM/containers/Dictionaries/PtrListDictionary/PtrListDictionary.C +++ b/src/OpenFOAM/containers/Dictionaries/PtrListDictionary/PtrListDictionary.C @@ -34,7 +34,7 @@ Foam::PtrListDictionary::PtrListDictionary(const label size) : DictionaryBase, T>(2*size) { - PtrList::setSize(size); + PtrList::resize(size); } diff --git a/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C b/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C index 2ecdb5c1f0..2f17099edd 100644 --- a/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C +++ b/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.C @@ -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::clear() template void Foam::CompactListList::swap ( - CompactListList& lst + CompactListList& 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_); } diff --git a/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.H b/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.H index 11a0712618..b9f80dda9b 100644 --- a/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.H +++ b/src/OpenFOAM/containers/Lists/CompactListList/CompactListList.H @@ -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> @@ -197,7 +197,7 @@ public: labelList sizes() const; //- Swap contents - void swap(CompactListList& lst); + void swap(CompactListList& other); //- Transfer contents into this and annul the argument void transfer(CompactListList& list); @@ -261,13 +261,15 @@ public: }; +// Note: uses default Foam::Swap (move construct/assignment) + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - #include "CompactListListI.H" +#include "CompactListListI.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.C b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.C index 2c6bc1630d..c6ccc61125 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.C +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.C @@ -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::removeElements { if (!slice.size()) { - // Noop + // No-op return 0; } else if (slice.after() >= this->size()) @@ -89,40 +88,4 @@ Foam::label Foam::DynamicList::subsetElements } -// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // - -template -Foam::DynamicList::DynamicList(Istream& is) -: - List(is), - capacity_(List::size()) -{} - - -template -Foam::Ostream& Foam::operator<< -( - Ostream& os, - const DynamicList& lst -) -{ - os << static_cast&>(lst); - return os; -} - - -template -Foam::Istream& Foam::operator>> -( - Istream& is, - DynamicList& lst -) -{ - is >> static_cast&>(lst); - lst.capacity_ = lst.List::size(); - - return is; -} - - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H index 9a81878b5d..6ba6fec7d0 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H @@ -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 DynamicList; template -Ostream& operator<< -( - Ostream& os, - const DynamicList& lst -); +inline Istream& operator>>(Istream&, DynamicList&); template -Istream& operator>> -( - Istream& is, - DynamicList& lst -); +inline Ostream& operator<<(Ostream&, const DynamicList&); /*---------------------------------------------------------------------------*\ @@ -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 - inline void assignDynList(const ListType& lst); + inline void assignDynList(const ListType& list); public: - // Related Types - - //- Declare friendship with the List class - friend class List; - - // 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& 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 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 newCapacity); - //- 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); + //- Reserve allocation space for at least this size. + // Never shrinks the allocated size, use setCapacity() for that. + inline void reserve(const label len); - //- Alter addressable list size and fill new space with constant. - inline void setSize(const label nElem, const T& val); + //- Alter addressable size. + // New space will be allocated if required. + inline void resize(const label newLen); - //- 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 size and fill new space with constant value + inline void resize(const label newLen, const T& val); - //- Alter addressable list size and fill new space with constant. - inline void resize(const label nElem, const T& val); + //- Alias for resize() + void setSize(const label n) { this->resize(n); } - //- Reserve allocation space for at least this size. - // Never shrinks the allocated size, use setCapacity() for that. - inline void reserve(const label nElem); + //- 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(); + //- Clear the addressed list, i.e. set the size to zero. + // Allocated size does not change + inline void clear() noexcept; - //- Clear the list and delete storage. - inline void clearStorage(); + //- 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(); + //- Expand the addressable size to fit the allocated capacity. + // Returns the previous addressable size. + inline label expandStorage() noexcept; - //- Shrink the allocated space to the number of elements used. - // Returns a reference to the DynamicList. - inline DynamicList& shrink(); + //- Shrink the allocated space to the number of elements used. + // Returns a reference to the DynamicList. + inline DynamicList& shrink(); - //- Swap content with any sized DynamicList + + // Edit + + //- Swap content, independent of sizing parameter template - inline void swap(DynamicList& lst); + inline void swap(DynamicList& other); //- Transfer contents of the argument List into this. - inline void transfer(List& lst); + inline void transfer(List& list); //- Transfer contents of any sized DynamicList into this. template - inline void transfer(DynamicList& lst); + inline void transfer(DynamicList& list); //- Transfer contents of the argument SortableList into this. - inline void transfer(SortableList& lst); + inline void transfer(SortableList& list); //- Append an element to the end of this list. inline DynamicList& 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&& lst); - // IOstream operators + // IOstream Operators - // Write DynamicList to Ostream. - friend Ostream& operator<< - ( - Ostream& os, - const DynamicList& lst - ); + //- Read from Istream, discarding existing contents + inline friend Istream& operator>> + ( + Istream& is, + DynamicList& rhs + ); - //- Read from Istream, discarding contents of existing DynamicList. - friend Istream& operator>> - ( - Istream& is, - DynamicList& lst - ); + //- Write to Ostream + inline friend Ostream& operator<< + ( + Ostream& os, + const DynamicList& rhs + ); }; -// Global Functions +// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // // Exchange contents of lists - see DynamicList::swap(). -template -inline void Swap(DynamicList& a, DynamicList& b); +template +inline void Swap(DynamicList& a, DynamicList& b) +{ + a.swap(b); +} // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index 3944c2c990..6263b781c7 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -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 template inline void Foam::DynamicList::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::size(newSize); - List::operator=(lst); + List::setAddressableSize(newLen); + List::operator=(list); } else { // Ensure list size consistency prior to copying. - List::size(capacity_); + List::setAddressableSize(capacity_); - List::operator=(lst); + List::operator=(list); capacity_ = List::size(); } } @@ -61,27 +61,29 @@ inline void Foam::DynamicList::assignDynList template inline constexpr Foam::DynamicList::DynamicList() noexcept : + List(), capacity_(0) {} template -inline Foam::DynamicList::DynamicList(const label nElem) +inline Foam::DynamicList::DynamicList(const label len) : + List(), capacity_(0) { - reserve(nElem); + reserve(len); } template inline Foam::DynamicList::DynamicList ( - const label nElem, + const label len, const T& val ) : - List(nElem, val), + List(len, val), capacity_(List::size()) {} @@ -89,11 +91,11 @@ inline Foam::DynamicList::DynamicList template inline Foam::DynamicList::DynamicList ( - const label nElem, + const label len, const Foam::zero ) : - List(nElem, Zero), + List(len, Zero), capacity_(List::size()) {} @@ -101,10 +103,10 @@ inline Foam::DynamicList::DynamicList template inline Foam::DynamicList::DynamicList ( - const DynamicList& lst + const DynamicList& list ) : - List(lst), + List(list), capacity_(List::size()) {} @@ -113,10 +115,10 @@ template template inline Foam::DynamicList::DynamicList ( - const DynamicList& lst + const DynamicList& list ) : - List(lst), + List(list), capacity_(List::size()) {} @@ -124,10 +126,10 @@ inline Foam::DynamicList::DynamicList template inline Foam::DynamicList::DynamicList ( - const UList& lst + const UList& list ) : - List(lst), + List(list), capacity_(List::size()) {} @@ -136,12 +138,12 @@ template template inline Foam::DynamicList::DynamicList ( - const FixedList& lst + const FixedList& list ) : capacity_(0) { - this->operator=(lst); + this->operator=(list); } @@ -218,6 +220,14 @@ inline Foam::DynamicList::DynamicList } +template +inline Foam::DynamicList::DynamicList(Istream& is) +: + List(is), + capacity_(List::size()) +{} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template @@ -230,124 +240,83 @@ inline Foam::label Foam::DynamicList::capacity() const noexcept template inline void Foam::DynamicList::setCapacity ( - const label nElem + const label newCapacity ) { - label nextFree = List::size(); - capacity_ = nElem; + label currLen = List::size(); + capacity_ = newCapacity; - if (nextFree > capacity_) + if (currLen > capacity_) { // Truncate addressed sizes too - nextFree = capacity_; + currLen = capacity_; } - // We could also enforce sizing granularity - - List::setSize(capacity_); - List::size(nextFree); + List::resize(capacity_); + List::setAddressableSize(currLen); } template inline void Foam::DynamicList::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::size(); - List::setSize(capacity_); - List::size(nextFree); + const label currLen = List::size(); + List::resize(capacity_); + List::setAddressableSize(currLen); } } template -inline void Foam::DynamicList::setSize +inline void Foam::DynamicList::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::setSize(capacity_); + List::resize(capacity_); } // Adjust addressed size - List::size(nElem); + List::setAddressableSize(newLen); } template -inline void Foam::DynamicList::setSize +inline void Foam::DynamicList::resize ( - const label nElem, + const label newLen, const T& val ) { - label nextFree = List::size(); - setSize(nElem); + label currLen = List::size(); + resize(newLen); - // Set new elements to constant value - while (nextFree < nElem) + // Fill newly exposed with constant value + while (currLen < newLen) { - this->operator[](nextFree++) = val; + this->operator[](currLen++) = val; } } template -inline void Foam::DynamicList::resize -( - const label nElem -) +inline void Foam::DynamicList::clear() noexcept { - this->setSize(nElem); -} - - -template -inline void Foam::DynamicList::resize -( - const label nElem, - const T& val -) -{ - this->setSize(nElem, val); -} - - -template -inline void Foam::DynamicList::clear() -{ - List::size(0); + List::setAddressableSize(0); } @@ -360,14 +329,14 @@ inline void Foam::DynamicList::clearStorage() template -inline Foam::label Foam::DynamicList::expandStorage() +inline Foam::label Foam::DynamicList::expandStorage() noexcept { - const label nextFree = List::size(); + const label currLen = List::size(); // Allow addressing into the entire list - List::size(capacity_); + List::setAddressableSize(capacity_); - return nextFree; + return currLen; } @@ -375,16 +344,16 @@ template inline Foam::DynamicList& Foam::DynamicList::shrink() { - const label nextFree = List::size(); - if (capacity_ > nextFree) + const label currLen = List::size(); + if (currLen < capacity_) { // Use the full list when resizing - List::size(capacity_); + List::setAddressableSize(capacity_); - // The new size - capacity_ = nextFree; - List::setSize(capacity_); - List::size(nextFree); + // Capacity and size are identical + capacity_ = currLen; + List::resize(currLen); + // Redundant: List::setAddressableSize(currLen); } return *this; } @@ -394,41 +363,30 @@ template template inline void Foam::DynamicList::swap ( - DynamicList& lst + DynamicList& 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& cur = *this; + // Swap storage and addressable size + UList::swap(other); - // Make addressable size identical to the allocated capacity - const label oldSize1 = cur.expandStorage(); - const label oldSize2 = lst.expandStorage(); - - // Swap storage - UList::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 inline void -Foam::DynamicList::transfer(List& lst) +Foam::DynamicList::transfer(List& list) { - // Take over storage, clear addressing for lst. - capacity_ = lst.size(); - List::transfer(lst); + // Take over storage, clear addressing for list + capacity_ = list.size(); + List::transfer(list); } @@ -437,21 +395,21 @@ template inline void Foam::DynamicList::transfer ( - DynamicList& lst + DynamicList& 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::transfer(static_cast&>(lst)); - lst.clearStorage(); // Ensure capacity=0 + List::transfer(static_cast&>(list)); + list.clearStorage(); // Ensure capacity=0 } @@ -459,12 +417,12 @@ template inline void Foam::DynamicList::transfer ( - SortableList& lst + SortableList& list ) { - lst.shrink(); // Shrink away sort indices - capacity_ = lst.size(); // Capacity after transfer == list size - List::transfer(lst); + list.shrink(); // Shrink away sort indices + capacity_ = list.size(); // Capacity after transfer == list size + List::transfer(list); } @@ -476,7 +434,7 @@ Foam::DynamicList::append ) { const label idx = List::size(); - setSize(idx + 1); + resize(idx + 1); this->operator[](idx) = val; // copy element return *this; @@ -491,7 +449,7 @@ Foam::DynamicList::append ) { const label idx = List::size(); - setSize(idx + 1); + resize(idx + 1); this->operator[](idx) = std::move(val); // move assign element return *this; @@ -508,12 +466,12 @@ Foam::DynamicList::append if (this == &lst) { FatalErrorInFunction - << "Attempted appending to self" << abort(FatalError); + << "Attempted appending to self" + << abort(FatalError); } label idx = List::size(); - - setSize(idx + lst.size()); + resize(idx + lst.size()); for (const T& val : lst) { @@ -532,7 +490,7 @@ Foam::DynamicList::append ) { label idx = List::size(); - setSize(idx + lst.size()); + resize(idx + lst.size()); for (const T& val : lst) { @@ -550,8 +508,7 @@ Foam::DynamicList::append ) { label idx = List::size(); - - setSize(idx + lst.size()); + resize(idx + lst.size()); for (const T& val : lst) { @@ -572,7 +529,7 @@ Foam::DynamicList::append label idx = List::size(); const label n = lst.size(); - setSize(idx + n); + resize(idx + n); for (label i=0; i inline Foam::DynamicList& Foam::DynamicList::append ( - List&& lst + List&& list ) { - if (this == &lst) + if (this == &list) { FatalErrorInFunction - << "Attempted appending to self" << abort(FatalError); + << "Attempted appending to self" + << abort(FatalError); } label idx = List::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::remove() const T& val = List::operator[](idx); - List::size(idx); + List::setAddressableSize(idx); return val; } @@ -742,7 +699,7 @@ inline T& Foam::DynamicList::operator() { if (i >= List::size()) { - setSize(i + 1); + resize(i + 1); } return this->operator[](i); @@ -788,7 +745,7 @@ inline void Foam::DynamicList::operator= { const label n = lst.size(); - setSize(n); + resize(n); for (label i=0; i::operator= } -// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // -template -inline void Foam::Swap +template +inline Foam::Istream& Foam::operator>> ( - DynamicList& a, - DynamicList& b + Istream& is, + DynamicList& rhs ) { - a.swap(b); + // Use entire storage - ie, resize(capacity()) + (void) rhs.expandStorage(); + + is >> static_cast&>(rhs); + rhs.capacity_ = rhs.List::size(); + + return is; +} + + +template +inline Foam::Ostream& Foam::operator<< +( + Ostream& os, + const DynamicList& rhs +) +{ + os << static_cast&>(rhs); + return os; } diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index 694b4f5c70..2378f152bc 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -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& list); + inline void swap(FixedList& other); // STL Member Operators @@ -491,9 +491,12 @@ struct is_contiguous_scalar> : is_contiguous_scalar {}; // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * // //- Swap FixedList contents - see FixedList::swap(). -// Internally this actually swaps the individual list elements +// Internally actually swaps the individual list elements template -inline void Swap(FixedList& lhs, FixedList& rhs); +inline void Swap(FixedList& a, FixedList& b) +{ + a.swap(b); +} //- Hashing for FixedList data, which uses Hasher for contiguous data and diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index 3984a43e53..86eea83432 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -327,15 +327,6 @@ inline void Foam::FixedList::resize(const label n) #endif } -template -inline void Foam::FixedList::setSize(const label n) -{ - #ifdef FULLDEBUG - checkSize(n); - #endif -} - - template inline void Foam::FixedList::fill(const T& val) { @@ -357,16 +348,16 @@ inline void Foam::FixedList::fill(const Foam::zero) template -inline void Foam::FixedList::swap(FixedList& list) +inline void Foam::FixedList::swap(FixedList& other) { - if (this == &list) + if (this == &other) { return; // Self-swap is a no-op } for (unsigned i=0; i::operator=(FixedList&& 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::crend() const } -// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // - -template -inline void Foam::Swap(FixedList& lhs, FixedList& rhs) -{ - lhs.swap(rhs); -} - - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index fb07dc4b95..759d387388 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -93,7 +93,7 @@ class List template 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 + // 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 - //- 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); - - //- Clear the list, i.e. set size to zero - inline void clear(); - //- Append an element at the end of the list inline void append(const T& val); diff --git a/src/OpenFOAM/containers/Lists/List/ListI.H b/src/OpenFOAM/containers/Lists/List/ListI.H index a7b0aca30c..4e499c0f20 100644 --- a/src/OpenFOAM/containers/Lists/List/ListI.H +++ b/src/OpenFOAM/containers/Lists/List/ListI.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. @@ -128,31 +128,17 @@ namespace Foam { // Template specialization for bool. Fills with false template<> - inline void List::resize(const label newSize) + inline void List::resize(const label newLen) { - this->resize(newSize, false); + this->resize(newLen, false); } } template -inline void Foam::List::resize(const label len) +inline void Foam::List::resize(const label newLen) { - this->doResize(len); -} - - -template -inline void Foam::List::setSize(const label len) -{ - this->resize(len); -} - - -template -inline void Foam::List::setSize(const label len, const T& val) -{ - this->resize(len, val); + this->doResize(newLen); } diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C index 5d006387ee..0b72294e7d 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.C +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.C @@ -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::partialReverseSort(label n, label start) template -void Foam::SortableList::swap(SortableList& lst) +void Foam::SortableList::swap(SortableList& other) { - if (this == &lst) + if (this == &other) { return; // Self-swap is a no-op } - List::swap(lst); - indices_.swap(lst.indices_); + List::swap(other); + indices_.swap(other.indices_); } @@ -267,13 +267,4 @@ inline void Foam::SortableList::operator=(std::initializer_list lst) } -// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // - -template -inline void Foam::Swap(SortableList& a, SortableList& b) -{ - a.swap(b); -} - - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/SortableList/SortableList.H b/src/OpenFOAM/containers/Lists/SortableList/SortableList.H index 51da600d8e..e911b27ef4 100644 --- a/src/OpenFOAM/containers/Lists/SortableList/SortableList.H +++ b/src/OpenFOAM/containers/Lists/SortableList/SortableList.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. @@ -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& lst); + inline void swap(SortableList& other); // Member Operators @@ -160,20 +160,22 @@ public: //- Move assignment, removing indices. Takes linear time inline void operator=(List&& lst); - //- Move operator. Takes linear time + //- Move operator (constant time) inline void operator=(SortableList&& lst); //- Assignment to an initializer list void operator=(std::initializer_list lst); - }; -// Global Functions +// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // // Exchange contents of lists - see SortableList::swap(). template -inline void Swap(SortableList& a, SortableList& b); +inline void Swap(SortableList& a, SortableList& b) +{ + a.swap(b); +} // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H index 0ace669d61..fcdbc3d75a 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.H +++ b/src/OpenFOAM/containers/Lists/UList/UList.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. @@ -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& list, const label n); template inline void reverse(UList& list); -// Exchange contents of lists - see UList::swap(). +//- Exchange contents of lists - see UList::swap(). template -inline void Swap(UList& a, UList& b); +inline void Swap(UList& a, UList& b) +{ + a.swap(b); +} //- Hashing for UList data, which uses Hasher for contiguous data and diff --git a/src/OpenFOAM/containers/Lists/UList/UListI.H b/src/OpenFOAM/containers/Lists/UList/UListI.H index 36e68c3983..9edb72fc6a 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListI.H +++ b/src/OpenFOAM/containers/Lists/UList/UListI.H @@ -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::crend() const template -inline void Foam::UList::size(const label n) noexcept +inline void Foam::UList::setAddressableSize(const label n) noexcept { size_ = n; } @@ -385,8 +384,8 @@ inline void Foam::UList::swap(UList& 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& list) } -template -inline void Foam::Swap(UList& a, UList& b) -{ - a.swap(b); -} - - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H index a868fcf3a4..3c0131a623 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.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. @@ -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::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::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 + inline void swap(PtrDynList& other); + //- Construct and append an element to the end of the list template inline void emplace_append(Args&&... args); @@ -234,7 +235,6 @@ public: //- Move assignment with different sizing parameters template inline void operator=(PtrDynList&& list); - }; diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H index c5de353b08..950b5ad627 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.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. @@ -29,15 +29,6 @@ License #include "refPtr.H" #include "tmp.H" -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // - -template -inline void Foam::PtrDynList::setAddressableSize(const label len) -{ - (this->ptrs_).setAddressableSize(len); -} - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template @@ -54,7 +45,7 @@ inline Foam::PtrDynList::PtrDynList(const label len) PtrList(len), capacity_(len) { - setAddressableSize(0); + PtrList::setAddressableSize(0); } @@ -107,35 +98,34 @@ inline const T* Foam::PtrDynList::get(const label i) const template -inline void Foam::PtrDynList::setCapacity(const label nElem) +inline void Foam::PtrDynList::setCapacity(const label newCapacity) { - label nextFree = PtrList::size(); - capacity_ = nElem; + label currLen = PtrList::size(); + capacity_ = newCapacity; - if (nextFree > capacity_) + if (currLen > capacity_) { // Truncate addressed sizes too - nextFree = capacity_; + currLen = capacity_; } PtrList::resize(capacity_); - setAddressableSize(nextFree); + PtrList::setAddressableSize(currLen); } template -inline void Foam::PtrDynList::reserve(const label nElem) +inline void Foam::PtrDynList::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::size(); + const label currLen = PtrList::size(); PtrList::resize(capacity_); - setAddressableSize(nextFree); + PtrList::setAddressableSize(currLen); } } @@ -147,31 +137,25 @@ inline void Foam::PtrDynList::resize(const label newLen) const label oldLen = ptrs.size(); - if (newLen > capacity_) + if (capacity_ < newLen) { - // Allocate more capacity if necessary - capacity_ = max(SizeMin, max(newLen, label(2*capacity_))); + // Increase capacity (doubling) + capacity_ = max(SizeMin, max(newLen, label(2 * capacity_))); PtrList::resize(capacity_); } else if (newLen != oldLen) { // Truncation frees old pointers - for (label i=newLen; i -inline void Foam::PtrDynList::setSize(const label newLen) -{ - this->resize(newLen); + // Adjust addressed size + PtrList::setAddressableSize(newLen); } @@ -179,7 +163,7 @@ template inline void Foam::PtrDynList::clear() { (this->ptrs_).free(); // free old pointers - setAddressableSize(0); + PtrList::setAddressableSize(0); } @@ -192,30 +176,30 @@ inline void Foam::PtrDynList::clearStorage() template -inline Foam::label Foam::PtrDynList::expandStorage() +inline Foam::label Foam::PtrDynList::expandStorage() noexcept { - const label nextFree = PtrList::size(); + const label currLen = PtrList::size(); // Allow addressing into the entire list - setAddressableSize(capacity_); + PtrList::setAddressableSize(capacity_); - return nextFree; + return currLen; } template inline void Foam::PtrDynList::shrink() { - label nextFree = PtrList::size(); - if (capacity_ > nextFree) + const label currLen = PtrList::size(); + if (currLen < capacity_) { // Use the full list when resizing - setAddressableSize(capacity_); + PtrList::setAddressableSize(capacity_); - // The new size - capacity_ = nextFree; - PtrList::resize(capacity_); - setAddressableSize(nextFree); + // Capacity and size are identical + capacity_ = currLen; + PtrList::resize(currLen); + // Redundant: PtrList::setAddressableSize(currLen); } } @@ -229,6 +213,27 @@ inline Foam::label Foam::PtrDynList::squeezeNull() } +template +template +inline void Foam::PtrDynList::swap +( + PtrDynList& 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::swap(other); + + // Swap capacity + std::swap(this->capacity_, other.capacity_); +} + + template template inline void Foam::PtrDynList::emplace_append(Args&&... args) @@ -332,7 +337,7 @@ inline Foam::autoPtr Foam::PtrDynList::remove() autoPtr old(this->ptrs_[idx]); this->ptrs_[idx] = nullptr; - setAddressableSize(idx); + PtrList::setAddressableSize(idx); return old; } diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H index e9bf294a90..e1d3019120 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H +++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H @@ -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 diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H index a8a84aa8b4..452bd85d2a 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H @@ -101,13 +101,6 @@ inline void Foam::PtrList::clear() } -template -inline void Foam::PtrList::setSize(const label newLen) -{ - this->resize(newLen); -} - - template template inline void Foam::PtrList::emplace_append(Args&&... args) diff --git a/src/OpenFOAM/containers/PtrLists/PtrListDetail/PtrListDetail.H b/src/OpenFOAM/containers/PtrLists/PtrListDetail/PtrListDetail.H index 92904a9e08..811ae9d18f 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrListDetail/PtrListDetail.H +++ b/src/OpenFOAM/containers/PtrLists/PtrListDetail/PtrListDetail.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. @@ -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; diff --git a/src/OpenFOAM/containers/PtrLists/PtrListDetail/PtrListDetailI.H b/src/OpenFOAM/containers/PtrLists/PtrListDetail/PtrListDetailI.H index 42fe47f886..a8777f39ad 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrListDetail/PtrListDetailI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrListDetail/PtrListDetailI.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. @@ -85,9 +85,12 @@ inline Foam::Detail::PtrListDetail::PtrListDetail // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -inline void Foam::Detail::PtrListDetail::setAddressableSize(const label n) +inline void Foam::Detail::PtrListDetail::setAddressableSize +( + const label n +) noexcept { - List::size(n); + List::setAddressableSize(n); } diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H index a25ffe3dc7..fbdf773734 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H @@ -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 ptrs_; + // Protected Member Functions + + //- Adjust addressable size + inline void setAddressableSize(const label n) noexcept; + + // Constructors //- Low-level move construct - inline UPtrList(Detail::PtrListDetail&& ptrs); + inline explicit UPtrList(Detail::PtrListDetail&& 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 diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H index 3ae12f35b2..296d439318 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H @@ -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 +inline void Foam::UPtrList::setAddressableSize(const label n) noexcept +{ + ptrs_.setAddressableSize(n); +} + + // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // template @@ -177,13 +186,6 @@ inline void Foam::UPtrList::resize(const label newLen) } -template -inline void Foam::UPtrList::setSize(const label newLen) -{ - ptrs_.resize(newLen); -} - - template inline void Foam::UPtrList::append(T* ptr) { diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.C b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.C index ff045973af..7cec1b4541 100644 --- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.C +++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.C @@ -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 . - -\*---------------------------------------------------------------------------*/ - -#include "DynamicField.H" - -// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // - -template -Foam::DynamicField::DynamicField(Istream& is) -: - Field(is), - capacity_(Field::size()) -{} - - -template -Foam::tmp> -Foam::DynamicField::clone() const -{ - return tmp>::New(*this); -} - - -// * * * * * * * * * * * * * * * IOstream Operator * * * * * * * * * * * * * // - -template -Foam::Ostream& Foam::operator<< -( - Ostream& os, - const DynamicField& lst -) -{ - os << static_cast&>(lst); - return os; -} - - -template -Foam::Istream& Foam::operator>> -( - Istream& is, - DynamicField& lst -) -{ - is >> static_cast&>(lst); - lst.capacity_ = lst.Field::size(); - - return is; -} - - -// ************************************************************************* // +#warning File removed - left for old dependency check only diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H index 75761896c6..ce84afae41 100644 --- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H +++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H @@ -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 DynamicField; template -Ostream& operator<< -( - Ostream& os, - const DynamicField& fld -); +inline Istream& operator>>(Istream&, DynamicField&); template -Istream& operator>> -( - Istream& is, - DynamicField& fld -); - +inline Ostream& operator<<(Ostream&, const DynamicField&); /*---------------------------------------------------------------------------*\ 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 - 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> clone() const; + inline tmp> 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& shrink(); - //- Swap content with any sized DynamicField + + // Edit + + //- Swap content, independent of sizing parameter template - inline void swap(DynamicField& list); + inline void swap(DynamicField& other); //- Transfer the parameter contents into this inline void transfer(List& list); @@ -237,10 +226,11 @@ public: template inline void transfer(DynamicField& list); - //- Append an element at the end of the list - inline DynamicField& - append(const T& val); + inline DynamicField& append(const T& val); + + //- Move append an element + inline DynamicField& append(T&& val); //- Append a List at the end of this list inline DynamicField& @@ -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& list); @@ -274,6 +267,22 @@ public: template inline void operator=(DynamicField&& list); + + // IOstream Operators + + //- Read from Istream, discarding existing contents + inline friend Istream& operator>> + ( + Istream& is, + DynamicField& rhs + ); + + //- Write to Ostream + inline friend Ostream& operator<< + ( + Ostream& os, + const DynamicField& rhs + ); }; @@ -287,12 +296,6 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -#ifdef NoRepository - #include "DynamicField.C" -#endif - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - #endif // ************************************************************************* // diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H index d726d5de9c..835841d8b1 100644 --- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H +++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H @@ -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 template -inline void Foam::DynamicField::assignDynField +inline void Foam::DynamicField::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::size(list.size()); - Field::operator=(list); + List::setAddressableSize(newLen); + List::operator=(list); } else { // Ensure list size consistency prior to copying. - Field::size(capacity_); + List::setAddressableSize(capacity_); - Field::operator=(list); - capacity_ = Field::size(); + List::operator=(list); + capacity_ = List::size(); } } @@ -67,10 +67,10 @@ inline constexpr Foam::DynamicField::DynamicField() noexcept template inline Foam::DynamicField::DynamicField(const label len) : - Field(len), - capacity_(Field::size()) + Field(), + capacity_(0) { - Field::size(0); + reserve(len); } @@ -207,7 +207,6 @@ inline Foam::DynamicField::DynamicField {} -//- Construct by mapping from the given field template inline Foam::DynamicField::DynamicField ( @@ -220,6 +219,22 @@ inline Foam::DynamicField::DynamicField {} +template +inline Foam::DynamicField::DynamicField(Istream& is) +: + Field(is), + capacity_(Field::size()) +{} + + +template +inline Foam::tmp> +Foam::DynamicField::clone() const +{ + return tmp>::New(*this); +} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template @@ -232,144 +247,103 @@ inline Foam::label Foam::DynamicField::capacity() const noexcept template inline void Foam::DynamicField::setCapacity ( - const label nElem + const label newCapacity ) { - label nextFree = Field::size(); - capacity_ = nElem; + label currLen = List::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::setSize(capacity_); - Field::size(nextFree); + List::resize(capacity_); + List::setAddressableSize(currLen); } template inline void Foam::DynamicField::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::size(); - Field::setSize(capacity_); - Field::size(nextFree); + const label currLen = List::size(); + List::resize(capacity_); + List::setAddressableSize(currLen); } } template -inline void Foam::DynamicField::setSize +inline void Foam::DynamicField::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::setSize(capacity_); + List::resize(capacity_); } // Adjust addressed size - Field::size(nElem); + List::setAddressableSize(newLen); } template -inline void Foam::DynamicField::setSize +inline void Foam::DynamicField::resize ( - const label nElem, + const label newLen, const T& val ) { - label nextFree = Field::size(); - setSize(nElem); + label currLen = List::size(); + resize(newLen); - // Set new elements to constant value - while (nextFree < nElem) + // Fill newly exposed with constant value + while (currLen < newLen) { - this->operator[](nextFree++) = val; + this->operator[](currLen++) = val; } } template -inline void Foam::DynamicField::resize -( - const label nElem -) +inline void Foam::DynamicField::clear() noexcept { - this->setSize(nElem); -} - - -template -inline void Foam::DynamicField::resize -( - const label nElem, - const T& val -) -{ - this->setSize(nElem, val); -} - - -template -inline void Foam::DynamicField::clear() -{ - Field::size(0); + List::setAddressableSize(0); } template inline void Foam::DynamicField::clearStorage() { - Field::clear(); + List::clear(); capacity_ = 0; } template -inline Foam::label Foam::DynamicField::expandStorage() +inline Foam::label Foam::DynamicField::expandStorage() noexcept { - const label nextFree = Field::size(); + const label currLen = List::size(); // Allow addressing into the entire list - Field::size(capacity_); + List::setAddressableSize(capacity_); - return nextFree; + return currLen; } @@ -377,16 +351,16 @@ template inline Foam::DynamicField& Foam::DynamicField::shrink() { - label nextFree = Field::size(); - if (capacity_ > nextFree) + const label currLen = Field::size(); + if (currLen < capacity_) { // Use the full list when resizing - Field::size(capacity_); + List::setAddressableSize(capacity_); - // The new size - capacity_ = nextFree; - Field::setSize(capacity_); - Field::size(nextFree); + // Capacity and size are identical + capacity_ = currLen; + List::resize(currLen); + // Redundant: List::setAddressableSize(currLen); } return *this; } @@ -396,38 +370,27 @@ template template inline void Foam::DynamicField::swap ( - DynamicField& lst + DynamicField& 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& cur = *this; + // Swap storage and addressable size + UList::swap(other); - // Make addressable size identical to the allocated capacity - const label oldSize1 = cur.expandStorage(); - const label oldSize2 = lst.expandStorage(); - - // Swap storage - Field::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 inline void Foam::DynamicField::transfer(List& list) { - // Take over storage, clear addressing for list. + // Take over storage, clear addressing for list capacity_ = list.size(); Field::transfer(list); } @@ -443,7 +406,7 @@ inline void Foam::DynamicField::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::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::append ) { const label idx = List::size(); - setSize(idx + 1); + resize(idx + 1); this->operator[](idx) = val; // copy element return *this; } +template +inline Foam::DynamicField& +Foam::DynamicField::append +( + T&& val +) +{ + const label idx = List::size(); + resize(idx + 1); + + this->operator[](idx) = std::move(val); // move assign element + return *this; +} + + template inline Foam::DynamicField& Foam::DynamicField::append @@ -502,11 +480,12 @@ Foam::DynamicField::append if (this == &list) { FatalErrorInFunction - << "Attempted appending to self" << abort(FatalError); + << "Attempted appending to self" + << abort(FatalError); } label idx = List::size(); - setSize(idx + list.size()); + resize(idx + list.size()); for (const T& val : list) { @@ -530,7 +509,7 @@ inline T Foam::DynamicField::remove() const T& val = List::operator[](idx); - List::size(idx); + List::setAddressableSize(idx); return val; } @@ -546,7 +525,7 @@ inline T& Foam::DynamicField::operator() { if (i >= Field::size()) { - setSize(i + 1); + resize(i + 1); } return this->operator[](i); @@ -563,13 +542,23 @@ inline void Foam::DynamicField::operator= } +template +inline void Foam::DynamicField::operator= +( + const Foam::zero +) +{ + UList::operator=(Zero); +} + + template inline void Foam::DynamicField::operator= ( const UList& list ) { - assignDynField(list); + assignDynList(list); } @@ -584,7 +573,7 @@ inline void Foam::DynamicField::operator= return; // Self-assignment is a no-op } - assignDynField(list); + assignDynList(list); } @@ -619,4 +608,35 @@ inline void Foam::DynamicField::operator= } +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + +template +inline Foam::Istream& Foam::operator>> +( + Istream& is, + DynamicField& rhs +) +{ + // Use entire storage - ie, resize(capacity()) + (void) rhs.expandStorage(); + + is >> static_cast&>(rhs); + rhs.capacity_ = rhs.Field::size(); + + return is; +} + + +template +inline Foam::Ostream& Foam::operator<< +( + Ostream& os, + const DynamicField& rhs +) +{ + os << static_cast&>(rhs); + return os; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/memory/tmpNrc/tmpNrcI.H b/src/OpenFOAM/memory/tmpNrc/tmpNrcI.H deleted file mode 100644 index 7cec1b4541..0000000000 --- a/src/OpenFOAM/memory/tmpNrc/tmpNrcI.H +++ /dev/null @@ -1 +0,0 @@ -#warning File removed - left for old dependency check only