DynamicList: cosmetics

- using SizeInc to define the granularity in the SizeMult=0 needed?
This commit is contained in:
Mark Olesen
2008-11-29 13:11:52 +01:00
parent 8b02802696
commit dee34f2775
6 changed files with 88 additions and 82 deletions

View File

@ -126,15 +126,24 @@ int main(int argc, char *argv[])
<< " " << dlB.size() << "/" << dlB.capacity() << endl; << " " << dlB.size() << "/" << dlB.capacity() << endl;
// dlB.append(dlB); // Copy back and append a few time
// Info<< "appended to itself:" << endl; for (label i=0; i < 3; i++)
// Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: " {
// << " " << dlB.size() << "/" << dlB.capacity() << endl; dlB.append(lstA);
}
// dlB = dlB;
// Info<< "self assignment:" << endl; // check allocation granularity
// Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: " DynamicList<label, 6, 0> dlC;
// << " " << dlB.size() << "/" << dlB.capacity() << endl;
Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
<< " " << dlC.size() << "/" << dlC.capacity() << endl;
dlC.reserve(dlB.size());
dlC = dlB;
Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
<< " " << dlC.size() << "/" << dlC.capacity() << endl;
return 0; return 0;
} }

View File

@ -28,12 +28,12 @@ License
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
// Construct from Istream
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList(Istream& is) Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList(Istream& is)
: :
List<T>(is), List<T>(is),
allocSize_(List<T>::size()) capacity_(List<T>::size())
{} {}
@ -57,7 +57,7 @@ Foam::Istream& Foam::operator>>
) )
{ {
is >> static_cast<List<T>&>(lst); is >> static_cast<List<T>&>(lst);
lst.allocSize_ = lst.List<T>::size(); lst.capacity_ = lst.List<T>::size();
return is; return is;
} }

View File

@ -32,7 +32,7 @@ Description
Internal storage is a compact array and the list can be shrunk to compact Internal storage is a compact array and the list can be shrunk to compact
storage. The increase of list size is controlled by three template storage. The increase of list size is controlled by three template
parameters, which allows the list storage to either increase by the given parameters, which allows the list storage to either increase by the given
increment or the given multiplier and divider (allowing non-integer increment or by the given multiplier and divider (allowing non-integer
multiples). multiples).
SourceFiles SourceFiles
@ -81,8 +81,8 @@ class DynamicList
{ {
// Private data // Private data
//- Allocated size for underlying List. //- The capacity (allocated size) of the underlying list.
label allocSize_; label capacity_;
// Private Member Functions // Private Member Functions

View File

@ -30,7 +30,7 @@ template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList() inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList()
: :
List<T>(SizeInc), List<T>(SizeInc),
allocSize_(SizeInc) capacity_(SizeInc)
{ {
List<T>::size(0); List<T>::size(0);
} }
@ -39,11 +39,11 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList()
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
( (
const label s const label nElem
) )
: :
List<T>(s), List<T>(nElem),
allocSize_(s) capacity_(nElem)
{ {
List<T>::size(0); List<T>::size(0);
} }
@ -56,7 +56,7 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
) )
: :
List<T>(lst), List<T>(lst),
allocSize_(lst.size()) capacity_(lst.size())
{} {}
@ -66,26 +66,26 @@ template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::label Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::capacity() inline Foam::label Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::capacity()
const const
{ {
return allocSize_; return capacity_;
} }
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setCapacity inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setCapacity
( (
const label s const label nElem
) )
{ {
label nextFree = List<T>::size(); label nextFree = List<T>::size();
allocSize_ = s; capacity_ = nElem;
if (nextFree > allocSize_) if (nextFree > capacity_)
{ {
// truncate both allocated and addressed sizes // truncate addressed sizes too
nextFree = allocSize_; nextFree = capacity_;
} }
List<T>::setSize(allocSize_); List<T>::setSize(capacity_);
List<T>::size(nextFree); List<T>::size(nextFree);
} }
@ -93,20 +93,21 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setCapacity
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::reserve inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::reserve
( (
const label s const label nElem
) )
{ {
if (s > allocSize_) // allocate more capacity?
if (nElem > capacity_)
{ {
allocSize_ = max capacity_ = max
( (
s, nElem,
label(SizeInc + allocSize_ * SizeMult / SizeDiv) label(SizeInc + capacity_ * SizeMult / SizeDiv)
); );
// adjust allocated size, leave addressed size untouched // adjust allocated size, leave addressed size untouched
label nextFree = List<T>::size(); label nextFree = List<T>::size();
List<T>::setSize(allocSize_); List<T>::setSize(capacity_);
List<T>::size(nextFree); List<T>::size(nextFree);
} }
} }
@ -115,38 +116,38 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::reserve
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
( (
const label s const label nElem
) )
{ {
// allocate more space? // allocate more capacity?
if (s > allocSize_) if (nElem > capacity_)
{ {
allocSize_ = max capacity_ = max
( (
s, nElem,
label(SizeInc + allocSize_ * SizeMult / SizeDiv) label(SizeInc + capacity_ * SizeMult / SizeDiv)
); );
List<T>::setSize(allocSize_); List<T>::setSize(capacity_);
} }
// adjust addressed size // adjust addressed size
List<T>::size(s); List<T>::size(nElem);
} }
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
( (
const label s, const label nElem,
const T& t const T& t
) )
{ {
label nextFree = List<T>::size(); label nextFree = List<T>::size();
setSize(s); setSize(nElem);
// set new elements to constant value // set new elements to constant value
while (nextFree < s) while (nextFree < nElem)
{ {
this->operator[](nextFree++) = t; this->operator[](nextFree++) = t;
} }
@ -164,7 +165,7 @@ template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::clearStorage() inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::clearStorage()
{ {
List<T>::clear(); List<T>::clear();
allocSize_ = 0; capacity_ = 0;
} }
@ -173,13 +174,13 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>&
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::shrink() Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::shrink()
{ {
label nextFree = List<T>::size(); label nextFree = List<T>::size();
if (allocSize_ > nextFree) if (capacity_ > nextFree)
{ {
// use the full list when resizing // use the full list when resizing
List<T>::size(allocSize_); List<T>::size(capacity_);
// the new size // the new size
allocSize_ = nextFree; capacity_ = nextFree;
List<T>::setSize(allocSize_); List<T>::setSize(capacity_);
List<T>::size(nextFree); List<T>::size(nextFree);
} }
return *this; return *this;
@ -190,7 +191,7 @@ template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void inline void
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer(List<T>& lst) Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer(List<T>& lst)
{ {
allocSize_ = lst.size(); capacity_ = lst.size();
List<T>::transfer(lst); // take over storage, clear addressing for lst. List<T>::transfer(lst); // take over storage, clear addressing for lst.
} }
@ -203,8 +204,8 @@ Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer
) )
{ {
// take over storage as-is (without shrink), clear addressing for lst. // take over storage as-is (without shrink), clear addressing for lst.
allocSize_ = lst.allocSize_; capacity_ = lst.capacity_;
lst.allocSize_ = 0; lst.capacity_ = 0;
List<T>::transfer(static_cast<List<T>&>(lst)); List<T>::transfer(static_cast<List<T>&>(lst));
} }
@ -213,16 +214,13 @@ Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
( (
const T& e const T& t
) )
{ {
// Work on copy free index since the size gets overwritten by setSize label elemI = List<T>::size();
label nextFree = List<T>::size(); setSize(elemI + 1);
reserve(nextFree+1); this->operator[](elemI) = t;
List<T>::size(nextFree+1);
this->operator[](nextFree) = e;
} }
@ -238,12 +236,12 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
{ {
FatalErrorIn FatalErrorIn
( (
"DynamicList<T, SizeInc, SizeMult, SizeDiv>::append(UList<T>&)" "DynamicList<T, SizeInc, SizeMult, SizeDiv>::append"
"(const UList<T>&)"
) << "attempted appending to self" << abort(FatalError); ) << "attempted appending to self" << abort(FatalError);
} }
reserve(nextFree + lst.size()); setSize(nextFree + lst.size());
List<T>::size(nextFree + lst.size());
forAll(lst, elemI) forAll(lst, elemI)
{ {
@ -255,7 +253,9 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline T Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove() inline T Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove()
{ {
if (List<T>::size() == 0) label elemI = List<T>::size() - 1;
if (elemI < 0)
{ {
FatalErrorIn FatalErrorIn
( (
@ -263,11 +263,9 @@ inline T Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove()
) << "List is empty" << abort(FatalError); ) << "List is empty" << abort(FatalError);
} }
label nextFree = List<T>::size()-1; const T& val = List<T>::operator[](elemI);
const T& val = List<T>::operator[](nextFree); List<T>::size(elemI);
List<T>::size(nextFree);
return val; return val;
} }
@ -278,16 +276,15 @@ inline T Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove()
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv> template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline T& Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator() inline T& Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator()
( (
const label i const label elemI
) )
{ {
label nextFree = List<T>::size(); if (elemI >= List<T>::size())
nextFree = max(nextFree, i + 1); {
setSize(elemI + 1);
}
reserve(nextFree); return this->operator[](elemI);
List<T>::size(nextFree);
return this->operator[](i);
} }
@ -311,23 +308,24 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
{ {
FatalErrorIn FatalErrorIn
( (
"DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=(UList<T>&)" "DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator="
"(const UList<T>&)"
) << "attempted assignment to self" << abort(FatalError); ) << "attempted assignment to self" << abort(FatalError);
} }
if (allocSize_ >= lst.size()) if (capacity_ >= lst.size())
{ {
// can copy without reallocation, match sizes to avoid reallocation // can copy w/o reallocating, match initial size to avoid reallocation
List<T>::size(lst.size()); List<T>::size(lst.size());
List<T>::operator=(lst); List<T>::operator=(lst);
} }
else else
{ {
// make the entire storage available for the copy operation // make everything available for the copy operation
List<T>::size(allocSize_); List<T>::size(capacity_);
List<T>::operator=(lst); List<T>::operator=(lst);
allocSize_ = List<T>::size(); capacity_ = List<T>::size();
} }
} }

View File

@ -432,9 +432,8 @@ void Foam::List<T>::transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>& a)
{ {
// shrink the allocated space to the number of elements used // shrink the allocated space to the number of elements used
a.shrink(); a.shrink();
a.allocSize_ = 0;
transfer(static_cast<List<T>&>(a)); transfer(static_cast<List<T>&>(a));
a.clearStorage();
} }

View File

@ -298,7 +298,7 @@ public:
label triangles label triangles
( (
const pointField& points, const pointField& points,
DynamicList<face, SizeInc,SizeMult, SizeDiv>& triFaces DynamicList<face, SizeInc, SizeMult, SizeDiv>& triFaces
) const; ) const;
//- Number of triangles and quads after splitting //- Number of triangles and quads after splitting