STYLE: prefer emplace_back() to append() + last()

This commit is contained in:
Mark Olesen
2023-08-21 16:56:35 +02:00
parent 698e05eeb3
commit 0e11f47f74
8 changed files with 67 additions and 138 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -47,7 +47,7 @@ Description
#include "Time.H" #include "Time.H"
#include "polyMesh.H" #include "polyMesh.H"
#include "cellSet.H" #include "cellSet.H"
#include "SortableList.H" #include "SortList.H"
#include "labelIOList.H" #include "labelIOList.H"
#include "fvMesh.H" #include "fvMesh.H"
#include "volFields.H" #include "volFields.H"
@ -128,71 +128,54 @@ int main(int argc, char *argv[])
const scalarField& vols = mesh.cellVolumes(); const scalarField& vols = mesh.cellVolumes();
SortableList<scalar> sortedVols(vols); SortList<scalar> sortedVols(vols);
// All cell labels, sorted per bin. // All cell labels, sorted per bin.
DynamicList<DynamicList<label>> bins; DynamicList<DynamicList<label>> bins;
// Lower/upper limits // Lower/upper limits
DynamicList<scalar> lowerLimits; DynamicList<scalarMinMax> limits;
DynamicList<scalar> upperLimits;
// Create bin0. Have upperlimit as factor times lowerlimit. // Create bin0. Have upperlimit as factor times lowerlimit.
bins.append(DynamicList<label>()); bins.emplace_back();
lowerLimits.append(sortedVols[0]); limits.emplace_back(sortedVols[0], 1.1*sortedVols[0]);
upperLimits.append(1.1 * lowerLimits.last());
forAll(sortedVols, i) forAll(sortedVols, i)
{ {
if (sortedVols[i] > upperLimits.last()) if (sortedVols[i] > limits.back().max())
{ {
// New value outside of current bin // New value outside of current bin
Info<< "Collected " << bins.back() << " elements in bin "
// Shrink old bin. << limits.back().min() << " .. "
DynamicList<label>& bin = bins.last(); << limits.back().max() << endl;
bin.shrink();
Info<< "Collected " << bin.size() << " elements in bin "
<< lowerLimits.last() << " .. "
<< upperLimits.last() << endl;
// Create new bin. // Create new bin.
bins.append(DynamicList<label>()); bins.emplace_back();
lowerLimits.append(sortedVols[i]); limits.emplace_back(sortedVols[i], 1.1 * sortedVols[i]);
upperLimits.append(1.1 * lowerLimits.last());
Info<< "Creating new bin " << lowerLimits.last() Info<< "Creating new bin "
<< " .. " << upperLimits.last() << limits.back().min() << " .. "
<< endl; << limits.back().max() << endl;
} }
// Append to current bin. // Add to current bin.
DynamicList<label>& bin = bins.last(); bins.back().push_back(sortedVols.indices()[i]);
bin.append(sortedVols.indices()[i]);
} }
Info<< endl; Info<< endl;
bins.last().shrink();
bins.shrink();
lowerLimits.shrink();
upperLimits.shrink();
// //
// Write to cellSets. // Write to cellSets.
// //
Info<< "Volume bins:" << nl; Info<< "Volume bins:" << nl;
forAll(bins, binI) forAll(bins, bini)
{ {
const DynamicList<label>& bin = bins[binI]; const auto& bin = bins[bini];
cellSet cells(mesh, "vol" + name(binI), bin.size()); cellSet cells(mesh, "vol" + Foam::name(bini), bin.size());
cells.insert(bin); cells.insert(bin);
Info<< " " << lowerLimits[binI] << " .. " << upperLimits[binI] Info<< " " << limits[bini].min() << " .. " << limits[bini].max()
<< " : writing " << bin.size() << " cells to cellSet " << " : writing " << bin.size() << " cells to cellSet "
<< cells.name() << endl; << cells.name() << endl;
@ -294,13 +277,13 @@ int main(int argc, char *argv[])
); );
// Set cell values // Set cell values
forAll(bins, binI) forAll(bins, bini)
{ {
const DynamicList<label>& bin = bins[binI]; const auto& bin = bins[bini];
forAll(bin, i) forAll(bin, i)
{ {
refLevel[bin[i]] = bins.size() - binI - 1; refLevel[bin[i]] = bins.size() - bini - 1;
postRefLevel[bin[i]] = refLevel[bin[i]]; postRefLevel[bin[i]] = refLevel[bin[i]];
} }
} }

View File

@ -148,21 +148,15 @@ void matchPatchFaces
// Mesh 0 // Mesh 0
//~~~~~~~ //~~~~~~~
interfaceMesh0.append(labelList()); auto& intMesh0 = interfaceMesh0.emplace_back(nSourcei, -1);
auto& intMesh0 = interfaceMesh0.last();
intMesh0.setSize(nSourcei, -1);
intMesh0[sourcei] = meshi; intMesh0[sourcei] = meshi;
interfaceSource0.append(sourcei); interfaceSource0.push_back(sourcei);
interfacePatch0.append(labelList()); auto& intPatch0 = interfacePatch0.emplace_back(nSourcei, -1);
auto& intPatch0 = interfacePatch0.last();
intPatch0.setSize(nSourcei, -1);
intPatch0[sourcei] = ppi.index(); intPatch0[sourcei] = ppi.index();
interfaceNames0.append(wordList()); auto& intNames0 = interfaceNames0.emplace_back(nSourcei);
auto& intNames0 = interfaceNames0.last();
intNames0.setSize(nSourcei);
intNames0[sourcei] = intNames0[sourcei] =
patchName(entryName, meshes[meshi], meshes[meshj]); patchName(entryName, meshes[meshi], meshes[meshj]);
@ -170,33 +164,23 @@ void matchPatchFaces
// Mesh 1 // Mesh 1
//~~~~~~~ //~~~~~~~
interfaceMesh1.append(labelList()); auto& intMesh1 = interfaceMesh1.emplace_back(nSourcej, -1);
auto& intMesh1 = interfaceMesh1.last();
intMesh1.setSize(nSourcej, -1);
intMesh1[sourcej] = meshj; intMesh1[sourcej] = meshj;
interfaceSource1.append(sourcej); interfaceSource1.push_back(sourcej);
interfacePatch1.append(labelList()); auto& intPatch1 = interfacePatch1.emplace_back(nSourcej, -1);
auto& intPatch1 = interfacePatch1.last();
intPatch1.setSize(nSourcej, -1);
intPatch1[sourcej] = ppj.index(); intPatch1[sourcej] = ppj.index();
interfaceNames1.append(wordList()); auto& intNames1 = interfaceNames1.emplace_back(nSourcej);
auto& intNames1 = interfaceNames1.last();
intNames1.setSize(nSourcej);
intNames1[sourcej] = intNames1[sourcej] =
patchName(entryName, meshes[meshj], meshes[meshi]); patchName(entryName, meshes[meshj], meshes[meshi]);
interfaceFaces0.append(List<DynamicList<label>>()); auto& intFaces0 = interfaceFaces0.emplace_back(nSourcei);
auto& intFaces0 = interfaceFaces0.last();
intFaces0.setSize(nSourcei);
DynamicList<label>& faces0 = intFaces0[sourcei]; DynamicList<label>& faces0 = intFaces0[sourcei];
faces0.setCapacity(ppi.size()); faces0.setCapacity(ppi.size());
interfaceFaces1.append(List<DynamicList<label>>()); auto& intFaces1 = interfaceFaces1.emplace_back(nSourcej);
auto& intFaces1 = interfaceFaces1.last();
intFaces1.setSize(nSourcej);
DynamicList<label>& faces1 = intFaces1[sourcej]; DynamicList<label>& faces1 = intFaces1[sourcej];
faces1.setCapacity(ppj.size()); faces1.setCapacity(ppj.size());
@ -249,7 +233,7 @@ void matchPatchFaces
{ {
if (weights[facei] > 0.5 || sourceMask[facei] > SMALL) if (weights[facei] > 0.5 || sourceMask[facei] > SMALL)
{ {
faces0.append(ppi.start()+facei); faces0.push_back(ppi.start()+facei);
} }
} }
} }
@ -259,7 +243,7 @@ void matchPatchFaces
{ {
if (weights[facei] > 0.5 || targetMask[facei] > SMALL) if (weights[facei] > 0.5 || targetMask[facei] > SMALL)
{ {
faces1.append(ppj.start()+facei); faces1.push_back(ppj.start()+facei);
} }
} }
} }
@ -825,7 +809,7 @@ int main(int argc, char *argv[])
if (pDict.get<word>("constructFrom") == "autoPatch") if (pDict.get<word>("constructFrom") == "autoPatch")
{ {
interRegionSources[meshi].append(sourcei); interRegionSources[meshi].push_back(sourcei);
} }
} }
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd. Copyright (C) 2016-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -1810,9 +1810,7 @@ bool Foam::hexRef8::matchHexShape
{ {
reverse(verts); reverse(verts);
} }
quads.append(face(0)); quads.emplace_back(verts);
labelList& quadVerts = quads.last();
quadVerts.transfer(verts);
} }
} }
@ -1891,9 +1889,7 @@ bool Foam::hexRef8::matchHexShape
if (verts.size() == 4) if (verts.size() == 4)
{ {
quads.append(face(0)); quads.emplace_back(verts);
labelList& quadVerts = quads.last();
quadVerts.transfer(verts);
} }
} }
} }
@ -2372,6 +2368,8 @@ Foam::labelList Foam::hexRef8::consistentSlowRefinement
const refinementData& ownData = allCellInfo[faceOwner[facei]]; const refinementData& ownData = allCellInfo[faceOwner[facei]];
label maxDataCount = ownData.count();
if (mesh_.isInternalFace(facei)) if (mesh_.isInternalFace(facei))
{ {
// Seed face if neighbouring cell (after possible refinement) // Seed face if neighbouring cell (after possible refinement)
@ -2379,46 +2377,18 @@ Foam::labelList Foam::hexRef8::consistentSlowRefinement
const refinementData& neiData = allCellInfo[faceNeighbour[facei]]; const refinementData& neiData = allCellInfo[faceNeighbour[facei]];
label faceCount; if (maxDataCount < neiData.count())
label faceRefineCount;
if (neiData.count() > ownData.count())
{ {
faceCount = neiData.count() + maxFaceDiff; maxDataCount = neiData.count();
faceRefineCount = faceCount + maxFaceDiff;
} }
else
{
faceCount = ownData.count() + maxFaceDiff;
faceRefineCount = faceCount + maxFaceDiff;
} }
seedFaces.append(facei); label faceCount = maxDataCount + maxFaceDiff;
seedFacesInfo.append
(
refinementData
(
faceRefineCount,
faceCount
)
);
allFaceInfo[facei] = seedFacesInfo.last();
}
else
{
label faceCount = ownData.count() + maxFaceDiff;
label faceRefineCount = faceCount + maxFaceDiff; label faceRefineCount = faceCount + maxFaceDiff;
seedFaces.append(facei); seedFaces.push_back(facei);
seedFacesInfo.append allFaceInfo[facei] =
( seedFacesInfo.emplace_back(faceRefineCount, faceCount);
refinementData
(
faceRefineCount,
faceCount
)
);
allFaceInfo[facei] = seedFacesInfo.last();
}
} }

View File

@ -32,11 +32,11 @@ License
template<class Type> template<class Type>
Type& Foam::glTF::List<Type>::create(const word& name) Type& Foam::glTF::List<Type>::create(const word& name)
{ {
Type obj(name); const label id = data_.size();
obj.id() = data_.size(); Type& obj = data_.emplace_back(name);
data_.append(obj); obj.id() = id;
return data_.last(); return data_.back();
} }

View File

@ -250,12 +250,7 @@ Foam::CollisionRecordList<PairType, WallType>::matchPairRecord
// member of the list. Setting the status of the record to be accessed // member of the list. Setting the status of the record to be accessed
// on construction. // on construction.
pairRecords_.append return pairRecords_.emplace_back(true, origProcOfOther, origIdOfOther);
(
PairCollisionRecord<PairType>(true, origProcOfOther, origIdOfOther)
);
return pairRecords_.last();
} }
@ -307,9 +302,7 @@ Foam::CollisionRecordList<PairType, WallType>::matchWallRecord
// member of the list. Setting the status of the record to be accessed // member of the list. Setting the status of the record to be accessed
// on construction. // on construction.
wallRecords_.append(WallCollisionRecord<WallType>(true, pRel)); return wallRecords_.emplace_back(true, pRel);
return wallRecords_.last();
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2022 OpenCFD Ltd. Copyright (C) 2017-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -245,9 +245,9 @@ public:
autoPtr<sampledSet> operator()(Istream& is) const autoPtr<sampledSet> operator()(Istream& is) const
{ {
word name(is); word name(is);
capture_.append(dictionary(is)); dictionary& dict = capture_.emplace_back(is);
return sampledSet::New(name, mesh_, search_, capture_.last()); return sampledSet::New(name, mesh_, search_, dict);
} }
}; };

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd. Copyright (C) 2016-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -225,9 +225,9 @@ public:
autoPtr<sampledSurface> operator()(Istream& is) const autoPtr<sampledSurface> operator()(Istream& is) const
{ {
word name(is); word name(is);
capture_.append(dictionary(is)); dictionary& dict = capture_.emplace_back(is);
return sampledSurface::New(name, mesh_, capture_.last()); return sampledSurface::New(name, mesh_, dict);
} }
}; };

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2019 OpenFOAM Foundation Copyright (C) 2011-2019 OpenFOAM Foundation
Copyright (C) 2019-2022 OpenCFD Ltd. Copyright (C) 2019-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -801,8 +801,7 @@ void Foam::isoSurfaceTopo::triangulateOutside
{ {
if (loop.size() > 2) if (loop.size() > 2)
{ {
compactFaces.append(face(loop.size())); face& f = compactFaces.emplace_back(loop.size());
face& f = compactFaces.last();
label fpi = 0; label fpi = 0;
forAll(f, i) forAll(f, i)
@ -1072,7 +1071,7 @@ Foam::isoSurfaceTopo::isoSurfaceTopo
); );
} }
} }
startTri.last() = tetCutAddr.nFaces(); startTri.back() = tetCutAddr.nFaces();
// Information not needed anymore: // Information not needed anymore:
tetBasePtIs.clear(); tetBasePtIs.clear();
@ -1080,7 +1079,7 @@ Foam::isoSurfaceTopo::isoSurfaceTopo
// From list of vertices -> triangular faces // From list of vertices -> triangular faces
faceList allTriFaces(startTri.last()); faceList allTriFaces(startTri.back());
{ {
auto& verts = tetCutAddr.cutPoints(); auto& verts = tetCutAddr.cutPoints();
@ -1097,7 +1096,7 @@ Foam::isoSurfaceTopo::isoSurfaceTopo
// The cells cut by the triangular faces // The cells cut by the triangular faces
meshCells_.resize(startTri.last()); meshCells_.resize(startTri.back());
for (label celli = 0; celli < startTri.size()-1; ++celli) for (label celli = 0; celli < startTri.size()-1; ++celli)
{ {
// All triangles for the current cell // All triangles for the current cell