diff --git a/applications/test/DynamicList/DynamicListTest.C b/applications/test/DynamicList/DynamicListTest.C index 2d4efa5e1f..28960444a2 100644 --- a/applications/test/DynamicList/DynamicListTest.C +++ b/applications/test/DynamicList/DynamicListTest.C @@ -126,15 +126,24 @@ int main(int argc, char *argv[]) << " " << dlB.size() << "/" << dlB.capacity() << endl; -// dlB.append(dlB); -// Info<< "appended to itself:" << endl; -// Info<< "" << dlB << "" << nl << "sizes: " -// << " " << dlB.size() << "/" << dlB.capacity() << endl; + // Copy back and append a few time + for (label i=0; i < 3; i++) + { + dlB.append(lstA); + } -// dlB = dlB; -// Info<< "self assignment:" << endl; -// Info<< "" << dlB << "" << nl << "sizes: " -// << " " << dlB.size() << "/" << dlB.capacity() << endl; + + // check allocation granularity + DynamicList dlC; + + Info<< "" << dlC << "" << nl << "sizes: " + << " " << dlC.size() << "/" << dlC.capacity() << endl; + + dlC.reserve(dlB.size()); + dlC = dlB; + + Info<< "" << dlC << "" << nl << "sizes: " + << " " << dlC.size() << "/" << dlC.capacity() << endl; return 0; } diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.C b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.C index 18548889c1..96159327c1 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.C +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.C @@ -28,12 +28,12 @@ License // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // -// Construct from Istream + template Foam::DynamicList::DynamicList(Istream& is) : List(is), - allocSize_(List::size()) + capacity_(List::size()) {} @@ -57,7 +57,7 @@ Foam::Istream& Foam::operator>> ) { is >> static_cast&>(lst); - lst.allocSize_ = lst.List::size(); + 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 1ce564de4d..847b94f647 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H @@ -32,7 +32,7 @@ Description 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 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). SourceFiles @@ -81,8 +81,8 @@ class DynamicList { // Private data - //- Allocated size for underlying List. - label allocSize_; + //- The capacity (allocated size) of the underlying list. + label capacity_; // Private Member Functions diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index 5ee12eabe9..1f7b8d4043 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -30,7 +30,7 @@ template inline Foam::DynamicList::DynamicList() : List(SizeInc), - allocSize_(SizeInc) + capacity_(SizeInc) { List::size(0); } @@ -39,11 +39,11 @@ inline Foam::DynamicList::DynamicList() template inline Foam::DynamicList::DynamicList ( - const label s + const label nElem ) : - List(s), - allocSize_(s) + List(nElem), + capacity_(nElem) { List::size(0); } @@ -56,7 +56,7 @@ inline Foam::DynamicList::DynamicList ) : List(lst), - allocSize_(lst.size()) + capacity_(lst.size()) {} @@ -66,26 +66,26 @@ template inline Foam::label Foam::DynamicList::capacity() const { - return allocSize_; + return capacity_; } template inline void Foam::DynamicList::setCapacity ( - const label s + const label nElem ) { label nextFree = List::size(); - allocSize_ = s; + capacity_ = nElem; - if (nextFree > allocSize_) + if (nextFree > capacity_) { - // truncate both allocated and addressed sizes - nextFree = allocSize_; + // truncate addressed sizes too + nextFree = capacity_; } - List::setSize(allocSize_); + List::setSize(capacity_); List::size(nextFree); } @@ -93,20 +93,21 @@ inline void Foam::DynamicList::setCapacity template inline void Foam::DynamicList::reserve ( - const label s + const label nElem ) { - if (s > allocSize_) + // allocate more capacity? + if (nElem > capacity_) { - allocSize_ = max + capacity_ = max ( - s, - label(SizeInc + allocSize_ * SizeMult / SizeDiv) + nElem, + label(SizeInc + capacity_ * SizeMult / SizeDiv) ); // adjust allocated size, leave addressed size untouched label nextFree = List::size(); - List::setSize(allocSize_); + List::setSize(capacity_); List::size(nextFree); } } @@ -115,38 +116,38 @@ inline void Foam::DynamicList::reserve template inline void Foam::DynamicList::setSize ( - const label s + const label nElem ) { - // allocate more space? - if (s > allocSize_) + // allocate more capacity? + if (nElem > capacity_) { - allocSize_ = max + capacity_ = max ( - s, - label(SizeInc + allocSize_ * SizeMult / SizeDiv) + nElem, + label(SizeInc + capacity_ * SizeMult / SizeDiv) ); - List::setSize(allocSize_); + List::setSize(capacity_); } // adjust addressed size - List::size(s); + List::size(nElem); } template inline void Foam::DynamicList::setSize ( - const label s, + const label nElem, const T& t ) { label nextFree = List::size(); - setSize(s); + setSize(nElem); // set new elements to constant value - while (nextFree < s) + while (nextFree < nElem) { this->operator[](nextFree++) = t; } @@ -164,7 +165,7 @@ template inline void Foam::DynamicList::clearStorage() { List::clear(); - allocSize_ = 0; + capacity_ = 0; } @@ -173,13 +174,13 @@ inline Foam::DynamicList& Foam::DynamicList::shrink() { label nextFree = List::size(); - if (allocSize_ > nextFree) + if (capacity_ > nextFree) { // use the full list when resizing - List::size(allocSize_); + List::size(capacity_); // the new size - allocSize_ = nextFree; - List::setSize(allocSize_); + capacity_ = nextFree; + List::setSize(capacity_); List::size(nextFree); } return *this; @@ -190,7 +191,7 @@ template inline void Foam::DynamicList::transfer(List& lst) { - allocSize_ = lst.size(); + capacity_ = lst.size(); List::transfer(lst); // take over storage, clear addressing for lst. } @@ -203,8 +204,8 @@ Foam::DynamicList::transfer ) { // take over storage as-is (without shrink), clear addressing for lst. - allocSize_ = lst.allocSize_; - lst.allocSize_ = 0; + capacity_ = lst.capacity_; + lst.capacity_ = 0; List::transfer(static_cast&>(lst)); } @@ -213,16 +214,13 @@ Foam::DynamicList::transfer template inline void Foam::DynamicList::append ( - const T& e + const T& t ) { - // Work on copy free index since the size gets overwritten by setSize - label nextFree = List::size(); + label elemI = List::size(); + setSize(elemI + 1); - reserve(nextFree+1); - List::size(nextFree+1); - - this->operator[](nextFree) = e; + this->operator[](elemI) = t; } @@ -238,12 +236,12 @@ inline void Foam::DynamicList::append { FatalErrorIn ( - "DynamicList::append(UList&)" + "DynamicList::append" + "(const UList&)" ) << "attempted appending to self" << abort(FatalError); } - reserve(nextFree + lst.size()); - List::size(nextFree + lst.size()); + setSize(nextFree + lst.size()); forAll(lst, elemI) { @@ -255,7 +253,9 @@ inline void Foam::DynamicList::append template inline T Foam::DynamicList::remove() { - if (List::size() == 0) + label elemI = List::size() - 1; + + if (elemI < 0) { FatalErrorIn ( @@ -263,11 +263,9 @@ inline T Foam::DynamicList::remove() ) << "List is empty" << abort(FatalError); } - label nextFree = List::size()-1; + const T& val = List::operator[](elemI); - const T& val = List::operator[](nextFree); - - List::size(nextFree); + List::size(elemI); return val; } @@ -278,16 +276,15 @@ inline T Foam::DynamicList::remove() template inline T& Foam::DynamicList::operator() ( - const label i + const label elemI ) { - label nextFree = List::size(); - nextFree = max(nextFree, i + 1); + if (elemI >= List::size()) + { + setSize(elemI + 1); + } - reserve(nextFree); - List::size(nextFree); - - return this->operator[](i); + return this->operator[](elemI); } @@ -311,23 +308,24 @@ inline void Foam::DynamicList::operator= { FatalErrorIn ( - "DynamicList::operator=(UList&)" + "DynamicList::operator=" + "(const UList&)" ) << "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::size(lst.size()); List::operator=(lst); } else { - // make the entire storage available for the copy operation - List::size(allocSize_); + // make everything available for the copy operation + List::size(capacity_); List::operator=(lst); - allocSize_ = List::size(); + capacity_ = List::size(); } } diff --git a/src/OpenFOAM/containers/Lists/List/List.C b/src/OpenFOAM/containers/Lists/List/List.C index 4bc73c590f..83f3464cca 100644 --- a/src/OpenFOAM/containers/Lists/List/List.C +++ b/src/OpenFOAM/containers/Lists/List/List.C @@ -432,9 +432,8 @@ void Foam::List::transfer(DynamicList& a) { // shrink the allocated space to the number of elements used a.shrink(); - a.allocSize_ = 0; - transfer(static_cast&>(a)); + a.clearStorage(); } diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.H b/src/OpenFOAM/meshes/meshShapes/face/face.H index 8032c47ac5..c9efe19cb3 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/face.H +++ b/src/OpenFOAM/meshes/meshShapes/face/face.H @@ -298,7 +298,7 @@ public: label triangles ( const pointField& points, - DynamicList& triFaces + DynamicList& triFaces ) const; //- Number of triangles and quads after splitting