Commit 14a40417 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: for-range, forAllIters() ... in applications/utilities

- reduced clutter when iterating over containers
parent 1458b4f6
......@@ -72,7 +72,7 @@ public:
void testCallbackFunction() const
{
forAllConstIter(callbackRegistry, *this, iter)
forAllConstIters(*this, iter)
{
iter().testCallbackFunction();
}
......
......@@ -55,7 +55,7 @@ int main(int argc, char *argv[])
Info<< fvc::div(U);
Info<< "end" << endl;
Info<< "End" << endl;
}
......
......@@ -54,7 +54,7 @@ int main()
a = h + i + j;
Info<< a << endl;
Info<< "end" << endl;
Info<< "End" << endl;
return 0;
}
......
......@@ -57,7 +57,7 @@ int main(int argc, char *argv[])
Info<< (gradx4a - gradx4[i])/gradx4a << endl;
}
Info<< "end" << endl;
Info<< "End" << endl;
}
......
......@@ -65,7 +65,7 @@ int main(int argc, char *argv[])
mesh
);
Info<< "end" << endl;
Info<< "End" << endl;
}
......
......@@ -65,7 +65,7 @@ int main()
);
phi.write("phi", "xmgr");
Info<< "end" << endl;
Info<< "End" << endl;
}
......
......@@ -57,7 +57,7 @@ int main()
"xmgr"
);
Info<< "end" << endl;
Info<< "End" << endl;
}
......
......@@ -57,7 +57,7 @@ int main()
graph("r", "x", "r", x, r).write("r", "xmgr");
Info<< "end" << endl;
Info<< "End" << endl;
return 0;
}
......
......@@ -59,11 +59,11 @@ int main(int argc, char *argv[])
Pout<< "Adding a particle." << endl;
particles.addParticle(new passiveParticle(mesh, Zero, -1));
forAllConstIter(passiveParticleCloud, particles, iter)
for (const passiveParticle& p : particles)
{
Pout<< " " << iter().position() << " cell:" << iter().cell()
<< " origProc:" << iter().origProc()
<< " origId:" << iter().origId()
Pout<< " " << p.position() << " cell:" << p.cell()
<< " origProc:" << p.origProc()
<< " origId:" << p.origId()
<< endl;
}
......@@ -80,11 +80,11 @@ int main(int argc, char *argv[])
passiveParticleCloud particles(mesh, cloudName);
Pout<< "Reread particles:" << particles.size() << endl;
forAllConstIter(passiveParticleCloud, particles, iter)
for (const passiveParticle& p : particles)
{
Pout<< " " << iter().position() << " cell:" << iter().cell()
<< " origProc:" << iter().origProc()
<< " origId:" << iter().origId()
Pout<< " " << p.position() << " cell:" << p.cell()
<< " origProc:" << p.origProc()
<< " origId:" << p.origId()
<< endl;
}
}
......
......@@ -261,10 +261,10 @@ void testSparseData(const polyMesh& mesh, Random& rndGen)
}
// 2. Does sparseData contain more?
forAllConstIter(Map<point>, sparseData, iter)
forAllConstIters(sparseData, iter)
{
const point& sparsePt = iter();
label meshPointi = iter.key();
const label meshPointi = iter.key();
const point& sparsePt = iter.val();
const point& fullPt = fullData[meshPointi];
if (fullPt != sparsePt)
......@@ -350,9 +350,9 @@ void testSparseData(const polyMesh& mesh, Random& rndGen)
{
const edge& e = mesh.edges()[meshEdgeI];
EdgeMap<point>::const_iterator iter = sparseData.find(e);
const auto iter = sparseData.cfind(e);
if (iter != sparseData.end())
if (iter.found())
{
const point& sparsePt = iter();
const point& fullPt = fullData[meshEdgeI];
......
......@@ -711,18 +711,18 @@ int main(int argc, char *argv[])
setsAndPatches[setI][1]
);
forAllConstIter(faceSet, fSet, iter)
for (const label facei : fSet)
{
if (wantedPatch[iter.key()] != -1)
if (wantedPatch[facei] != -1)
{
FatalErrorInFunction
<< "Face " << iter.key()
<< "Face " << facei
<< " is in faceSet " << setsAndPatches[setI][0]
<< " destined for patch " << setsAndPatches[setI][1]
<< " but also in patch " << wantedPatch[iter.key()]
<< " but also in patch " << wantedPatch[facei]
<< exit(FatalError);
}
wantedPatch[iter.key()] = patchi;
wantedPatch[facei] = patchi;
}
}
......@@ -749,20 +749,21 @@ int main(int argc, char *argv[])
faceSet fSet(mesh, coupledAndPatches[setI][0]);
label patchi = findPatch(patches, coupledAndPatches[setI][1]);
forAllConstIter(faceSet, fSet, iter)
for (const label facei : fSet)
{
if (coupledWantedPatch[iter.key()] != -1)
if (coupledWantedPatch[facei] != -1)
{
FatalErrorInFunction
<< "Face " << iter.key()
<< "Face " << facei
<< " is in faceSet " << coupledAndPatches[setI][0]
<< " destined for patch " << coupledAndPatches[setI][1]
<< " but also in patch " << coupledWantedPatch[iter.key()]
<< " but also in patch " << coupledWantedPatch[facei]
<< exit(FatalError);
}
coupledWantedPatch[iter.key()] = patchi;
cyclicWantedPatch_half0[iter.key()] = cyclicId;
cyclicWantedPatch_half1[iter.key()] = cyclicSlaveId;
coupledWantedPatch[facei] = patchi;
cyclicWantedPatch_half0[facei] = cyclicId;
cyclicWantedPatch_half1[facei] = cyclicSlaveId;
}
}
......
......@@ -80,25 +80,24 @@ Foam::label Foam::cellSplitter::newOwner
const Map<labelList>& cellToCells
) const
{
label oldOwn = mesh_.faceOwner()[facei];
const label old = mesh_.faceOwner()[facei];
Map<labelList>::const_iterator fnd = cellToCells.find(oldOwn);
const auto iter = cellToCells.cfind(old);
if (fnd == cellToCells.end())
if (!iter.found())
{
// Unsplit cell
return oldOwn;
return old;
}
else
{
// Look up index of face in the cells' faces.
const labelList& newCells = fnd();
const cell& cFaces = mesh_.cells()[oldOwn];
// Look up index of face in the cells' faces.
return newCells[cFaces.find(facei)];
}
const labelList& newCells = *iter;
const cell& cFaces = mesh_.cells()[old];
return newCells[cFaces.find(facei)];
}
......@@ -108,31 +107,29 @@ Foam::label Foam::cellSplitter::newNeighbour
const Map<labelList>& cellToCells
) const
{
label oldNbr = mesh_.faceNeighbour()[facei];
const label old = mesh_.faceNeighbour()[facei];
Map<labelList>::const_iterator fnd = cellToCells.find(oldNbr);
const auto iter = cellToCells.cfind(old);
if (fnd == cellToCells.end())
if (!iter.found())
{
// Unsplit cell
return oldNbr;
return old;
}
else
{
// Look up index of face in the cells' faces.
const labelList& newCells = fnd();
const cell& cFaces = mesh_.cells()[oldNbr];
// Look up index of face in the cells' faces.
return newCells[cFaces.find(facei)];
}
const labelList& newCells = *iter;
const cell& cFaces = mesh_.cells()[old];
return newCells[cFaces.find(facei)];
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// Construct from components
Foam::cellSplitter::cellSplitter(const polyMesh& mesh)
:
mesh_(mesh),
......@@ -140,12 +137,6 @@ Foam::cellSplitter::cellSplitter(const polyMesh& mesh)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::cellSplitter::~cellSplitter()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::cellSplitter::setRefinement
......@@ -162,9 +153,9 @@ void Foam::cellSplitter::setRefinement
// Introduce cellToMidPoints.
//
forAllConstIter(Map<point>, cellToMidPoint, iter)
forAllConstIters(cellToMidPoint, iter)
{
label celli = iter.key();
const label celli = iter.key();
label anchorPoint = mesh_.cellPoints()[celli][0];
......@@ -173,7 +164,7 @@ void Foam::cellSplitter::setRefinement
(
polyAddPoint
(
iter(), // point
iter.val(), // point
anchorPoint, // master point
-1, // zone for point
true // supports a cell
......@@ -193,9 +184,9 @@ void Foam::cellSplitter::setRefinement
Map<labelList> cellToCells(cellToMidPoint.size());
forAllConstIter(Map<point>, cellToMidPoint, iter)
forAllConstIters(cellToMidPoint, iter)
{
label celli = iter.key();
const label celli = iter.key();
const cell& cFaces = mesh_.cells()[celli];
......@@ -238,9 +229,9 @@ void Foam::cellSplitter::setRefinement
// point.
//
forAllConstIter(Map<point>, cellToMidPoint, iter)
forAllConstIters(cellToMidPoint, iter)
{
label celli = iter.key();
const label celli = iter.key();
label midPointi = addedPoints_[celli];
......@@ -368,9 +359,9 @@ void Foam::cellSplitter::setRefinement
// Mark off affected face.
bitSet faceUpToDate(mesh_.nFaces(), true);
forAllConstIter(Map<point>, cellToMidPoint, iter)
forAllConstIters(cellToMidPoint, iter)
{
label celli = iter.key();
const label celli = iter.key();
const cell& cFaces = mesh_.cells()[celli];
......@@ -462,15 +453,13 @@ void Foam::cellSplitter::updateMesh(const mapPolyMesh& morphMap)
// point get mapped do they get inserted.
Map<label> newAddedPoints(addedPoints_.size());
forAllConstIter(Map<label>, addedPoints_, iter)
forAllConstIters(addedPoints_, iter)
{
label oldCelli = iter.key();
label newCelli = morphMap.reverseCellMap()[oldCelli];
label oldPointi = iter();
const label oldCelli = iter.key();
const label oldPointi = iter.val();
label newPointi = morphMap.reversePointMap()[oldPointi];
const label newCelli = morphMap.reverseCellMap()[oldCelli];
const label newPointi = morphMap.reversePointMap()[oldPointi];
if (newCelli >= 0 && newPointi >= 0)
{
......
......@@ -109,7 +109,7 @@ public:
//- Destructor
~cellSplitter();
~cellSplitter() = default;
// Member Functions
......
......@@ -83,10 +83,8 @@ label findPoint(const primitivePatch& pp, const point& nearPoint)
scalar almostMinDistSqr = GREAT;
label almostMinI = -1;
forAll(meshPoints, i)
for (const label pointi : meshPoints)
{
label pointi = meshPoints[i];
scalar distSqr = magSqr(nearPoint - points[pointi]);
if (distSqr < minDistSqr)
......@@ -147,10 +145,8 @@ label findEdge
scalar almostMinDist = GREAT;
label almostMinI = -1;
forAll(edges, edgeI)
for (const edge& e : edges)
{
const edge& e = edges[edgeI];
pointHit pHit(e.line(localPoints).nearestDist(nearPoint));
if (pHit.hit())
......@@ -296,10 +292,8 @@ label findCell(const primitiveMesh& mesh, const point& nearPoint)
label minI = -1;
scalar minDistSqr = GREAT;
forAll(cPoints, i)
for (const label pointi : cPoints)
{
label pointi = cPoints[i];
scalar distSqr = magSqr(nearPoint - mesh.points()[pointi]);
if (distSqr < minDistSqr)
......@@ -432,11 +426,9 @@ int main(int argc, char *argv[])
Info<< nl << "Looking up points to move ..." << nl << endl;
Map<point> pointToPos(pointsToMove.size());
forAll(pointsToMove, i)
for (const Pair<point>& pts : pointsToMove)
{
const Pair<point>& pts = pointsToMove[i];
label pointi = findPoint(allBoundary, pts.first());
const label pointi = findPoint(allBoundary, pts.first());
if (pointi == -1 || !pointToPos.insert(pointi, pts.second()))
{
......@@ -450,10 +442,8 @@ int main(int argc, char *argv[])
Info<< nl << "Looking up edges to split ..." << nl << endl;
Map<List<point>> edgeToCuts(edgesToSplit.size());
forAll(edgesToSplit, i)
for (const Pair<point>& pts : edgesToSplit)
{
const Pair<point>& pts = edgesToSplit[i];
label edgeI = findEdge(mesh, allBoundary, pts.first());
if
......@@ -473,10 +463,8 @@ int main(int argc, char *argv[])
Info<< nl << "Looking up faces to triangulate ..." << nl << endl;
Map<point> faceToDecompose(facesToTriangulate.size());
forAll(facesToTriangulate, i)
for (const Pair<point>& pts : facesToTriangulate)
{
const Pair<point>& pts = facesToTriangulate[i];
label facei = findFace(mesh, allBoundary, pts.first());
if (facei == -1 || !faceToDecompose.insert(facei, pts.second()))
......@@ -494,10 +482,8 @@ int main(int argc, char *argv[])
Info<< nl << "Looking up cells to convert to pyramids around"
<< " cell centre ..." << nl << endl;
Map<point> cellToPyrCentre(cellsToPyramidise.size());
forAll(cellsToPyramidise, i)
for (const Pair<point>& pts : cellsToPyramidise)
{
const Pair<point>& pts = cellsToPyramidise[i];
label celli = findCell(mesh, pts.first());
if (celli == -1 || !cellToPyrCentre.insert(celli, pts.second()))
......@@ -513,10 +499,8 @@ int main(int argc, char *argv[])
Info<< nl << "Looking up edges to collapse ..." << nl << endl;
Map<point> edgeToPos(edgesToCollapse.size());
forAll(edgesToCollapse, i)
for (const Pair<point>& pts : edgesToCollapse)
{
const Pair<point>& pts = edgesToCollapse[i];
label edgeI = findEdge(mesh, allBoundary, pts.first());
if (edgeI == -1 || !edgeToPos.insert(edgeI, pts.second()))
......@@ -590,7 +574,7 @@ int main(int argc, char *argv[])
Map<point> collapsePointToLocation(mesh.nPoints());
// Get new positions and construct collapse network
forAllConstIter(Map<point>, edgeToPos, iter)
forAllConstIters(edgeToPos, iter)
{
label edgeI = iter.key();
const edge& e = edges[edgeI];
......@@ -598,7 +582,7 @@ int main(int argc, char *argv[])
collapseEdge.set(edgeI);
collapsePointToLocation.set(e[1], points[e[0]]);
newPoints[e[0]] = iter();
newPoints[e[0]] = iter.val();
}
// Move master point to destination.
......
......@@ -222,10 +222,8 @@ bool splitHex
const cell& cFaces = mesh.cells()[celli];
forAll(cFaces, i)
for (const label facei : cFaces)
{
label facei = cFaces[i];
const face& f = faces[facei];
label fp0 = f.find(e[0]);
......@@ -399,15 +397,12 @@ void collectCuts
boolList edgeIsCut(mesh.nEdges(), false);
scalarField edgeWeight(mesh.nEdges(), -GREAT);
forAllConstIter(cellSet, cellsToCut, iter)
for (const label celli : cellsToCut)
{
const label celli = iter.key();
const labelList& cEdges = cellEdges[celli];
forAll(cEdges, i)
for (const label edgeI : cEdges)
{
label edgeI = cEdges[i];
label f0, f1;
meshTools::getEdgeFaces(mesh, celli, edgeI, f0, f1);
......
......@@ -197,7 +197,7 @@ elementType ^{space}"TYPE"{cspace}
forAll(slPatchCells, i)
{
if (!slPatchCells(i))
if (!slPatchCells.set(i))
{
slPatchCells.set(i, new SLList<label>);
}
......@@ -210,7 +210,7 @@ elementType ^{space}"TYPE"{cspace}
forAll(slPatchCells, i)
{
if (!slPatchCellFaces(i))
if (!slPatchCellFaces.set(i))
{
slPatchCellFaces.set(i, new SLList<label>);
}
......@@ -344,19 +344,19 @@ int main(int argc, char *argv[])
pointField points(slPoints.size());
label i = 0;
forAllConstIter(SLList<point>, slPoints, pointIter)
for (const point& pt : slPoints)
{
// Scale points for the given scale factor
points[i++] = scaleFactor * pointIter();
points[i++] = scaleFactor * pt;
}
labelList pointMap(maxNodei+1);
i = 0;
forAllConstIter(SLList<label>, slPointMap, pointMapIter)
for (const label pointi : slPointMap)
{
pointMap[pointMapIter()] = i++;
pointMap[pointi] = i++;
}
Info<< "Creating cells" << endl;
......@@ -364,9 +364,9 @@ int main(int argc, char *argv[])
labelList cellMap(maxCelli+1);
i = 0;
forAllConstIter(SLList<label>, slCellMap, cellMapIter)
for (const label celli : slCellMap)
{
cellMap[cellMapIter()] = i++;
cellMap[celli] = i++;
}
......@@ -383,66 +383,66 @@ int main(int argc, char *argv[])
cellShapeList cellShapes(slCellLabels.size());
label nCells = 0;
forAllConstIter(SLPtrList<labelList>, slCellLabels, cellIter)
for (const labelList& labels : slCellLabels)
{
if // Tetrahedron
(
cellIter()[2] == cellIter()[3]
&& cellIter()[4] == cellIter()[5]
&& cellIter()[5] == cellIter()[6]
&& cellIter()[6] == cellIter()[7]
labels[2] == labels[3]
&& labels[4] == labels[5]
&& labels[5] == labels[6]
&& labels[6] == labels[7]
)
{
labelsTet[0] = pointMap[cellIter()[0] ];
labelsTet[1] = pointMap[cellIter()[1] ];
labelsTet[2] = pointMap[cellIter()[2] ];
labelsTet[3] = pointMap[cellIter()[4] ];
labelsTet[0] = pointMap[labels[0]];
labelsTet[1] = pointMap[labels[1]];
labelsTet[2] = pointMap[labels[2]];
labelsTet[3] = pointMap[labels[4]];
cellShapes[nCells++] = cellShape(tet, labelsTet);