Commit 41bbcb63 authored by Mark Olesen's avatar Mark Olesen
Browse files

DynamicList changes.

- setSize() adjusts the addressable length only.
  Changed setSize(label) usage to setCapacity(label) or reserve(label)
  throughout. The final name (capacity vs. storageSize() vs. whatever) can
  easily be decided at a later date.
- added setSize(label, const T&), which may still not be really useful, but
  is at least now meaningful
- made shrink() a bit more legible.
- added append(UList<T>&)
- copying from a UList avoids reallocations where possible

The following bits of code continue to use the DynamicList::setSize(), but
appear to be legitimate (or the corresponding code itself needs rethinking).

  src/OpenFOAM/meshes/primitiveMesh/primitiveMeshPointCells.C:167: error: within this context
  src/OpenFOAM/lnInclude/faceTemplates.C:44: error: within this context
  src/surfMesh/surfaceFormats/tri/TRIsurfaceFormatCore.C:178: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:737: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:741: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:745: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:749: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:754: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:935: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:940: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:1041: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:1046: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:2161: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:2162: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:2201: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:2205: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:2261: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:2262: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:2263: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:2264: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:2265: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:3011: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:3076: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:3244: error: within this context
  src/dynamicMesh/polyTopoChange/polyTopoChange/polyTopoChange.C:3371: error: within this context
  src/dynamicMesh/meshCut/cellLooper/topoCellLooper.C:73: error: within this context
  src/dynamicMesh/meshCut/cellLooper/topoCellLooper.C:91: error: within this context
  src/dynamicMesh/meshCut/cellLooper/topoCellLooper.C:73: error: within this context
  src/dynamicMesh/meshCut/cellLooper/topoCellLooper.C:91: error: within this context
parent b7e349a7
......@@ -43,8 +43,8 @@ int main(int argc, char *argv[])
ldl[0](3) = 3;
ldl[0](1) = 1;
ldl[0].allocSize(5); // increase allocated size
ldl[1].allocSize(10); // increase allocated size
ldl[0].setCapacity(5); // increase allocated size
ldl[1].setCapacity(10); // increase allocated size
ldl[0].reserve(15); // should increase allocated size
ldl[1].reserve(5); // should not decrease allocated size
ldl[1](3) = 2; // allocates space and sets value
......@@ -57,7 +57,7 @@ int main(int argc, char *argv[])
Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
forAll(ldl, i)
{
Info<< " " << ldl[i].size() << "/" << ldl[i].allocSize();
Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
}
Info<< endl;
......@@ -68,7 +68,7 @@ int main(int argc, char *argv[])
Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
forAll(ldl, i)
{
Info<< " " << ldl[i].size() << "/" << ldl[i].allocSize();
Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
}
Info<< endl;
......@@ -83,10 +83,10 @@ int main(int argc, char *argv[])
{
dlA.append(i);
}
dlA.allocSize(10);
dlA.setCapacity(10);
Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
<< " " << dlA.size() << "/" << dlA.allocSize() << endl;
<< " " << dlA.size() << "/" << dlA.capacity() << endl;
dlB.transfer(dlA);
......@@ -96,9 +96,9 @@ int main(int argc, char *argv[])
Info<< "Transferred to dlB" << endl;
Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
<< " " << dlA.size() << "/" << dlA.allocSize() << endl;
<< " " << dlA.size() << "/" << dlA.capacity() << endl;
Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
<< " " << dlB.size() << "/" << dlB.allocSize() << endl;
<< " " << dlB.size() << "/" << dlB.capacity() << endl;
// try with a normal list:
List<label> lstA;
......@@ -107,7 +107,34 @@ int main(int argc, char *argv[])
Info<< "<lstA>" << lstA << "</lstA>" << nl << "sizes: "
<< " " << lstA.size() << endl;
Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
<< " " << dlB.size() << "/" << dlB.allocSize() << endl;
<< " " << dlB.size() << "/" << dlB.capacity() << endl;
// Copy back and append a few time
for (label i=0; i < 3; i++)
{
dlB.append(lstA);
}
Info<< "appended list a few times" << endl;
Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
<< " " << dlB.size() << "/" << dlB.capacity() << endl;
// assign the list (should maintain allocated space)
dlB = lstA;
Info<< "assigned list" << endl;
Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
<< " " << dlB.size() << "/" << dlB.capacity() << endl;
// dlB.append(dlB);
// Info<< "appended to itself:" << endl;
// Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
// << " " << dlB.size() << "/" << dlB.capacity() << endl;
// dlB = dlB;
// Info<< "self assignment:" << endl;
// Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
// << " " << dlB.size() << "/" << dlB.capacity() << endl;
return 0;
}
......
......@@ -108,13 +108,13 @@ int main(int argc, char *argv[])
face f1(dl);
face f2(xferCopy<labelList>(dl));
Info<< "dl[" << dl.size() << "/" << dl.allocSize() << "] " << dl << endl;
Info<< "dl[" << dl.size() << "/" << dl.capacity() << "] " << dl << endl;
Info<< "f1: " << f1 << endl;
Info<< "f2: " << f2 << endl;
// note: using xferMoveTo to ensure the correct transfer() method is called
face f3( xferMoveTo<labelList>(dl) );
Info<< "dl[" << dl.size() << "/" << dl.allocSize() << "] " << dl << endl;
Info<< "dl[" << dl.size() << "/" << dl.capacity() << "] " << dl << endl;
Info<< "f3: " << f3 << endl;
return 0;
......
......@@ -144,7 +144,7 @@ Foam::mergePolyMesh::mergePolyMesh(const IOobject& io)
wordList curPointZoneNames = pointZones().names();
if (curPointZoneNames.size() > 0)
{
pointZoneNames_.setSize(2*curPointZoneNames.size());
pointZoneNames_.setCapacity(2*curPointZoneNames.size());
}
forAll (curPointZoneNames, zoneI)
......@@ -157,7 +157,7 @@ Foam::mergePolyMesh::mergePolyMesh(const IOobject& io)
if (curFaceZoneNames.size() > 0)
{
faceZoneNames_.setSize(2*curFaceZoneNames.size());
faceZoneNames_.setCapacity(2*curFaceZoneNames.size());
}
forAll (curFaceZoneNames, zoneI)
{
......@@ -169,7 +169,7 @@ Foam::mergePolyMesh::mergePolyMesh(const IOobject& io)
if (curCellZoneNames.size() > 0)
{
cellZoneNames_.setSize(2*curCellZoneNames.size());
cellZoneNames_.setCapacity(2*curCellZoneNames.size());
}
forAll (curCellZoneNames, zoneI)
{
......
......@@ -354,7 +354,7 @@ bool domainDecomposition::writeDecomposition()
// Estimate size
forAll(zonePoints, zoneI)
{
zonePoints[zoneI].setSize(pz[zoneI].size() / nProcs_);
zonePoints[zoneI].setCapacity(pz[zoneI].size() / nProcs_);
}
// Use the pointToZone map to find out the single zone (if any),
......@@ -423,8 +423,8 @@ bool domainDecomposition::writeDecomposition()
{
label procSize = fz[zoneI].size() / nProcs_;
zoneFaces[zoneI].setSize(procSize);
zoneFaceFlips[zoneI].setSize(procSize);
zoneFaces[zoneI].setCapacity(procSize);
zoneFaceFlips[zoneI].setCapacity(procSize);
}
// Go through all the zoned faces and find out if they
......@@ -514,7 +514,7 @@ bool domainDecomposition::writeDecomposition()
// Estimate size
forAll(zoneCells, zoneI)
{
zoneCells[zoneI].setSize(cz[zoneI].size() / nProcs_);
zoneCells[zoneI].setCapacity(cz[zoneI].size() / nProcs_);
}
forAll (curCellLabels, celli)
......
......@@ -40,11 +40,11 @@ Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList(Istream& is)
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
Foam::Ostream& Foam::operator<<
(
Foam::Ostream& os,
const Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>& DL
Ostream& os,
const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
)
{
os << static_cast<const List<T>&>(DL);
os << static_cast<const List<T>&>(lst);
return os;
}
......@@ -52,12 +52,12 @@ Foam::Ostream& Foam::operator<<
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
Foam::Istream& Foam::operator>>
(
Foam::Istream& is,
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>& DL
Istream& is,
DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
)
{
is >> static_cast<List<T>&>(DL);
DL.allocSize_ = DL.List<T>::size();
is >> static_cast<List<T>&>(lst);
lst.allocSize_ = lst.List<T>::size();
return is;
}
......
......@@ -86,9 +86,6 @@ class DynamicList
// Private Member Functions
// Disabled, since the usefulness and semantics are not quite clear
void setSize(const label, const T&);
public:
// Related types
......@@ -116,29 +113,27 @@ public:
// Access
//- Size of the underlying storage.
inline label allocSize() const;
inline label capacity() const;
// Edit
//- Alter the size of the underlying storage.
// The addressed size will be truncated if needed to fit, but will
// otherwise remain untouched.
inline void allocSize(const label);
// CURRENT BEHAVIOUR
//- 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.
//
// PROPOSED BEHAVIOUR
// remain otherwise untouched.
// Use this or reserve() in combination with append().
inline void setCapacity(const label);
//- 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);
//- Alter the addressed list size and fill new space with a constant.
inline void setSize(const label, const T&);
//- Reserve allocation space for at least this size.
// Never shrinks the allocated size, use allocSize() for that.
// Never shrinks the allocated size, use setCapacity() for that.
inline void reserve(const label);
//- Clear the addressed list, i.e. set the size to zero.
......@@ -164,6 +159,9 @@ public:
//- Append an element at the end of the list
inline void append(const T& e);
//- Append a List at the end of this list
inline void append(const UList<T>&);
//- Remove and return the top element
inline T remove();
......@@ -174,7 +172,7 @@ public:
inline void operator=(const T&);
//- Assignment from List<T>. Also handles assignment from DynamicList.
inline void operator=(const List<T>&);
inline void operator=(const UList<T>&);
// IOstream operators
......
......@@ -63,7 +63,7 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::label Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::allocSize()
inline Foam::label Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::capacity()
const
{
return allocSize_;
......@@ -71,7 +71,7 @@ const
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::allocSize
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setCapacity
(
const label s
)
......@@ -79,13 +79,12 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::allocSize
label nextFree = List<T>::size();
allocSize_ = s;
// truncate addressed size too?
if (nextFree > allocSize_)
{
// truncate both allocated and addressed sizes
nextFree = allocSize_;
}
// adjust allocated size, and addressed size if necessary
List<T>::setSize(allocSize_);
List<T>::size(nextFree);
}
......@@ -102,7 +101,7 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::reserve
allocSize_ = max
(
s,
label(SizeMult*allocSize_/SizeDiv + SizeInc)
label(SizeInc + allocSize_ * SizeMult / SizeDiv)
);
// adjust allocated size, leave addressed size untouched
......@@ -119,33 +118,13 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
const label s
)
{
#if 1
// CURRENT BEHAVIOUR:
// slightly ambiguous about what size the list will actually get
// cannot increase the size of the addressed list (for compatibility
// with List), without automatically adjusting the allocated space!
label nextFree = List<T>::size();
if (s <= nextFree)
{
// adjust addressed size, leave allocated size untouched
nextFree = s;
}
else
{
// adjust allocated size, leave addressed size untouched
allocSize_ = s;
List<T>::setSize(allocSize_);
}
List<T>::size(nextFree);
#else
// allocate more space?
if (s > allocSize_)
{
allocSize_ = max
(
s,
label(SizeMult*allocSize_/SizeDiv + SizeInc)
label(SizeInc + allocSize_ * SizeMult / SizeDiv)
);
List<T>::setSize(allocSize_);
......@@ -153,7 +132,24 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
// adjust addressed size
List<T>::size(s);
#endif
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
(
const label s,
const T& t
)
{
label nextFree = List<T>::size();
setSize(s);
// set new elements to constant value
while (nextFree < s)
{
this->operator[](nextFree++) = t;
}
}
......@@ -176,13 +172,15 @@ template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>&
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::shrink()
{
if (allocSize_ > List<T>::size())
label nextFree = List<T>::size();
if (allocSize_ > nextFree)
{
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);
// use the full list when resizing
List<T>::size(allocSize_);
// the new size
allocSize_ = nextFree;
List<T>::setSize(allocSize_);
List<T>::size(nextFree);
}
return *this;
}
......@@ -213,9 +211,12 @@ Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append(const T& e)
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
(
const T& e
)
{
// Work on copy free index since gets overwritten by setSize
// Work on copy free index since the size gets overwritten by setSize
label nextFree = List<T>::size();
reserve(nextFree+1);
......@@ -225,6 +226,32 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append(const T& e)
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
(
const UList<T>& lst
)
{
label nextFree = List<T>::size();
if (this == &lst)
{
FatalErrorIn
(
"DynamicList<T, SizeInc, SizeMult, SizeDiv>::append(UList<T>&)"
) << "attempted appending to self" << abort(FatalError);
}
reserve(nextFree + lst.size());
List<T>::size(nextFree + lst.size());
forAll(lst, elemI)
{
this->operator[](nextFree++) = lst[elemI];
}
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline T Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove()
{
......@@ -270,21 +297,38 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
const T& t
)
{
List<T>::operator=(t);
UList<T>::operator=(t);
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
(
const List<T>& lst
const UList<T>& lst
)
{
// make the entire storage available for the copy operation:
if (this == &lst)
{
FatalErrorIn
(
"DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=(UList<T>&)"
) << "attempted assignment to self" << abort(FatalError);
}
if (allocSize_ >= lst.size())
{
// can copy without reallocation, match sizes to avoid reallocation
List<T>::size(lst.size());
List<T>::operator=(lst);
}
else
{
// make the entire storage available for the copy operation
List<T>::size(allocSize_);
List<T>::operator=(lst);
allocSize_ = List<T>::size();
}
}
......
......@@ -40,8 +40,8 @@ Foam::label Foam::face::triangles
label quadI = 0;
faceList quadFaces;
// adjusts the addressable size (and allocate space if needed)
triFaces(triI + nTriangles());
// adjust the addressable size (and allocate space if needed)
triFaces.setSize(triI + nTriangles());
return split(SPLITTRIANGLE, points, triI, quadI, triFaces, quadFaces);
}
......
......@@ -27,14 +27,9 @@ License
#include "primitiveMesh.H"
#include "ListOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
const labelListList& primitiveMesh::cellPoints() const
const Foam::labelListList& Foam::primitiveMesh::cellPoints() const
{
if (!cpPtr_)
{
......@@ -61,7 +56,7 @@ const labelListList& primitiveMesh::cellPoints() const
}
const labelList& primitiveMesh::cellPoints
const Foam::labelList& Foam::primitiveMesh::cellPoints
(
const label cellI,
DynamicList<label>& storage
......@@ -89,9 +84,9 @@ const labelList& primitiveMesh::cellPoints
}
storage.clear();
if (labelSet_.size() > storage.allocSize())
if (labelSet_.size() > storage.capacity())
{
storage.setSize(labelSet_.size());
storage.setCapacity(labelSet_.size());
}
forAllConstIter(labelHashSet, labelSet_, iter)
......@@ -104,7 +99,7 @@ const labelList& primitiveMesh::cellPoints
}
const labelList& primitiveMesh::cellPoints(const label cellI) const
const Foam::labelList& Foam::primitiveMesh::cellPoints(const label cellI) const
{
return cellPoints(cellI, labels_);
}
......@@ -112,6 +107,4 @@ const labelList& primitiveMesh::cellPoints(const label cellI) const
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
......@@ -30,15 +30,10 @@ License
#include "SortableList.H"
#include "ListOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
// Returns edgeI between two points.
Foam::label primitiveMesh::getEdge
Foam::label Foam::primitiveMesh::getEdge
(
List<DynamicList<label> >& pe,
DynamicList<edge>& es,
......@@ -76,7 +71,7 @@ Foam::label primitiveMesh::getEdge
}
void primitiveMesh::calcEdges(const bool doFaceEdges) const
void Foam::primitiveMesh::calcEdges(const bool doFaceEdges) const
{
if (debug)
{
......@@ -113,7 +108,7 @@ void primitiveMesh::calcEdges(const bool doFaceEdges) const
List<DynamicList<label> > pe(nPoints());
forAll(pe, pointI)
{
pe[pointI].setSize(primitiveMesh::edgesPerPoint_);
pe[pointI].setCapacity(primitiveMesh::edgesPerPoint_);
}
// Estimate edges storage
......@@ -460,7 +455,7 @@ void primitiveMesh::calcEdges(const bool doFaceEdges) const
}
label primitiveMesh::findFirstCommonElementFromSortedLists
Foam::label Foam::primitiveMesh::findFirstCommonElementFromSortedLists
(
const labelList& list1,
const labelList& list2
......@@ -502,7 +497,7 @@ label primitiveMesh::findFirstCommonElementFromSortedLists
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
const edgeList& primitiveMesh::edges() const
const Foam::edgeList& Foam::primitiveMesh::edges() const
{
if (!edgesPtr_)
{
......@@ -513,7 +508,7 @@ const edgeList& primitiveMesh::edges() const
return *edgesPtr_;
}
const labelListList& primitiveMesh::pointEdges() const
const Foam::labelListList& Foam::primitiveMesh::pointEdges() const