Commit b22ada09 authored by Mark Olesen's avatar Mark Olesen
Browse files

DynamicList re-visited:

 - shrink() should now correctly actually shrink
 - List::transfer(DynamicList&) invokes shrink() before transferring
   contents: otherwise the ununsed allocated space is never recovered
   until much, much later.
 - DynamicList::transfer(List&) no longer throws a FatalError when
   transferring in a smaller list. The original list contents are getting
   tossed away anyhow!
parent bb5bc164
......@@ -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<T> to the number of elements used
inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& shrink();
//- Shrink the allocated space to the number of elements used.
// Returns a reference to the DynamicList.
inline DynamicList<T, SizeInc, SizeMult, SizeDiv>& 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<T>&);
//- Transfer contents of the argument List into this DynamicList
inline void transfer(List<T>&);
//- Transfer the contents of the argument DynamicList into this
// DynamicList and annull the argument list.
inline void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>&);
//- Transfer contents of the argument DynamicList into this DynamicList
inline void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>&);
// 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
......
......@@ -26,7 +26,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
//- Construct null
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList()
:
......@@ -37,7 +36,6 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList()
}
//- Construct given size
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
(
......@@ -51,15 +49,14 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
}
//- Construct given size
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
(
const UList<T>& s
const UList<T>& lst
)
:
List<T>(s),
allocSize_(s.size())
List<T>(lst),
allocSize_(lst.size())
{}
......@@ -114,31 +111,24 @@ template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>&
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::shrink()
{
allocSize_ = List<T>::size();
List<T>::setSize(allocSize_);
if (allocSize_ > List<T>::size())
{
allocSize_ = List<T>::size();
// force re-allocation/copying in List<T>::setSize() by temporarily
// faking a larger list size that will be truncated
List<T>::size(allocSize_+1);
List<T>::setSize(allocSize_);
}
return *this;
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer(List<T>& l)
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer(List<T>& lst)
{
if (l.size() < List<T>::size())
{
FatalErrorIn
(
"void DynamicList<T, SizeInc, SizeMult"
", SizeDiv>::transfer(List<T>&)"
) << "Cannot replace the underlying storage of this DynamicList"
<< " of which " << List<T>::size() << " elements are used" << nl
<< "with a List of size " << l.size() << abort(FatalError);
}
else
{
allocSize_ = l.size();
List<T>::transfer(l); // take over storage
}
allocSize_ = lst.size();
List<T>::transfer(lst); // take over storage, null lst
}
......@@ -146,11 +136,11 @@ template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer
(
DynamicList<T, SizeInc, SizeMult, SizeDiv>& l
DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
)
{
allocSize_ = l.allocSize();
List<T>::transfer(l); // take over storage. Null l.
allocSize_ = lst.allocSize();
List<T>::transfer(lst); // take over storage, null lst.
}
......
......@@ -430,6 +430,9 @@ template<class T>
template<unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
void Foam::List<T>::transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>& 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_;
......
......@@ -45,7 +45,7 @@ void Foam::UList<T>::assign(const UList<T>& a)
{
if (a.size_ != this->size_)
{
FatalErrorIn("UList<T>::operator=(const UList<T>&)")
FatalErrorIn("UList<T>::assign(const UList<T>&)")
<< "ULists have different sizes: "
<< this->size_ << " " << a.size_
<< abort(FatalError);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment