diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H index 3891ad0ea8..214c780620 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H @@ -115,39 +115,36 @@ public: // Access - //- Size of the underlying storage. - inline label allocSize() const; + //- Size of the underlying storage. + inline label allocSize() const; // Edit - //- Alter the list size. - // When the new size is greater than the addressed list size, the - // allocated list sizes is adjusted and the - // addressed size does not change. - // Otherwise the addressed list size is just reduced and the - // allocated size does not change. - inline void setSize(const label); + //- Alter the list size. + // When the new size is greater than the addressed list size, the + // allocated list sizes is adjusted and the + // addressed size does not change. + // Otherwise the addressed list size is just reduced and the + // allocated size does not change. + inline void setSize(const label); - //- Clear the list, i.e. set the size to zero. - // Allocated size does not change - inline void clear(); + //- Clear the list, i.e. set the size to zero. + // Allocated size does not change + inline void clear(); - //- Clear the list and delete storage. - inline void clearStorage(); + //- Clear the list and delete storage. + inline void clearStorage(); - //- Shrink the List to the number of elements used - inline DynamicList& shrink(); + //- Shrink the allocated space to the number of elements used. + // Returns a reference to the DynamicList. + inline DynamicList& shrink(); - //- Transfer the contents of the argument List into this List - // and annull the argument list. Is same as List::transfer except - // checks that you're not changing the underlying list to something - // smaller than allocSize_. - inline void transfer(List&); + //- Transfer contents of the argument List into this DynamicList + inline void transfer(List&); - //- Transfer the contents of the argument DynamicList into this - // DynamicList and annull the argument list. - inline void transfer(DynamicList&); + //- Transfer contents of the argument DynamicList into this DynamicList + inline void transfer(DynamicList&); // Member Operators @@ -158,8 +155,7 @@ public: //- Remove and return the top element inline T remove(); - //- Return non-const access to an element, - // resizing the list if necessary + //- Return non-const access to an element, resizing list if necessary inline T& operator()(const label); //- Assignment of all addressed entries to the given value diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index 434dfcb468..8bb2a2561c 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -26,7 +26,6 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -//- Construct null template inline Foam::DynamicList::DynamicList() : @@ -37,7 +36,6 @@ inline Foam::DynamicList::DynamicList() } -//- Construct given size template inline Foam::DynamicList::DynamicList ( @@ -51,15 +49,14 @@ inline Foam::DynamicList::DynamicList } -//- Construct given size template inline Foam::DynamicList::DynamicList ( - const UList& s + const UList& lst ) : - List(s), - allocSize_(s.size()) + List(lst), + allocSize_(lst.size()) {} @@ -114,31 +111,24 @@ template inline Foam::DynamicList& Foam::DynamicList::shrink() { - allocSize_ = List::size(); - List::setSize(allocSize_); + if (allocSize_ > List::size()) + { + allocSize_ = List::size(); + // force re-allocation/copying in List::setSize() by temporarily + // faking a larger list size that will be truncated + List::size(allocSize_+1); + List::setSize(allocSize_); + } return *this; } template inline void -Foam::DynamicList::transfer(List& l) +Foam::DynamicList::transfer(List& lst) { - if (l.size() < List::size()) - { - FatalErrorIn - ( - "void DynamicList::transfer(List&)" - ) << "Cannot replace the underlying storage of this DynamicList" - << " of which " << List::size() << " elements are used" << nl - << "with a List of size " << l.size() << abort(FatalError); - } - else - { - allocSize_ = l.size(); - List::transfer(l); // take over storage - } + allocSize_ = lst.size(); + List::transfer(lst); // take over storage, null lst } @@ -146,11 +136,11 @@ template inline void Foam::DynamicList::transfer ( - DynamicList& l + DynamicList& lst ) { - allocSize_ = l.allocSize(); - List::transfer(l); // take over storage. Null l. + allocSize_ = lst.allocSize(); + List::transfer(lst); // take over storage, null lst. } diff --git a/src/OpenFOAM/containers/Lists/List/List.C b/src/OpenFOAM/containers/Lists/List/List.C index d6c8280264..d9a929f1a9 100644 --- a/src/OpenFOAM/containers/Lists/List/List.C +++ b/src/OpenFOAM/containers/Lists/List/List.C @@ -430,6 +430,9 @@ template template void Foam::List::transfer(DynamicList& a) { + // shrink the allocated space to the number of elements used + a.shrink(); + if (this->v_) delete[] this->v_; this->size_ = a.size_; this->v_ = a.v_; diff --git a/src/OpenFOAM/containers/Lists/UList/UList.C b/src/OpenFOAM/containers/Lists/UList/UList.C index 85ee1bb4ad..c77e6bfb80 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.C +++ b/src/OpenFOAM/containers/Lists/UList/UList.C @@ -45,7 +45,7 @@ void Foam::UList::assign(const UList& a) { if (a.size_ != this->size_) { - FatalErrorIn("UList::operator=(const UList&)") + FatalErrorIn("UList::assign(const UList&)") << "ULists have different sizes: " << this->size_ << " " << a.size_ << abort(FatalError);