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

- reduced clutter when iterating over containers
This commit is contained in:
Mark Olesen
2019-01-07 09:20:51 +01:00
parent 1458b4f689
commit 14a404170b
76 changed files with 592 additions and 728 deletions

View File

@ -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);
}
else if // Square-based pyramid
(
cellIter()[4] == cellIter()[5]
&& cellIter()[5] == cellIter()[6]
&& cellIter()[6] == cellIter()[7]
labels[4] == labels[5]
&& labels[5] == labels[6]
&& labels[6] == labels[7]
)
{
labelsPyramid[0] = pointMap[cellIter()[0] ];
labelsPyramid[1] = pointMap[cellIter()[1] ];
labelsPyramid[2] = pointMap[cellIter()[2] ];
labelsPyramid[3] = pointMap[cellIter()[3] ];
labelsPyramid[4] = pointMap[cellIter()[4] ];
labelsPyramid[0] = pointMap[labels[0]];
labelsPyramid[1] = pointMap[labels[1]];
labelsPyramid[2] = pointMap[labels[2]];
labelsPyramid[3] = pointMap[labels[3]];
labelsPyramid[4] = pointMap[labels[4]];
cellShapes[nCells++] = cellShape(pyr, labelsPyramid);
}
else if // Triangular prism
(
cellIter()[2] == cellIter()[3]
&& cellIter()[6] == cellIter()[7]
labels[2] == labels[3]
&& labels[6] == labels[7]
)
{
labelsPrism[0] = pointMap[cellIter()[0] ];
labelsPrism[1] = pointMap[cellIter()[1] ];
labelsPrism[2] = pointMap[cellIter()[2] ];
labelsPrism[3] = pointMap[cellIter()[4] ];
labelsPrism[4] = pointMap[cellIter()[5] ];
labelsPrism[5] = pointMap[cellIter()[6] ];
labelsPrism[0] = pointMap[labels[0]];
labelsPrism[1] = pointMap[labels[1]];
labelsPrism[2] = pointMap[labels[2]];
labelsPrism[3] = pointMap[labels[4]];
labelsPrism[4] = pointMap[labels[5]];
labelsPrism[5] = pointMap[labels[6]];
cellShapes[nCells++] = cellShape(prism, labelsPrism);
}
else // Hex
{
labelsHex[0] = pointMap[cellIter()[0] ];
labelsHex[1] = pointMap[cellIter()[1] ];
labelsHex[2] = pointMap[cellIter()[2] ];
labelsHex[3] = pointMap[cellIter()[3] ];
labelsHex[4] = pointMap[cellIter()[4] ];
labelsHex[5] = pointMap[cellIter()[5] ];
labelsHex[6] = pointMap[cellIter()[6] ];
labelsHex[7] = pointMap[cellIter()[7] ];
labelsHex[0] = pointMap[labels[0]];
labelsHex[1] = pointMap[labels[1]];
labelsHex[2] = pointMap[labels[2]];
labelsHex[3] = pointMap[labels[3]];
labelsHex[4] = pointMap[labels[4]];
labelsHex[5] = pointMap[labels[5]];
labelsHex[6] = pointMap[labels[6]];
labelsHex[7] = pointMap[labels[7]];
cellShapes[nCells++] = cellShape(hex, labelsHex);
}
@ -474,7 +474,7 @@ int main(int argc, char *argv[])
// Warning: tet face order has changed between version 1.9.6 and 2.0
//
label faceIndex[7][6] =
const label faceIndex[7][6] =
{
{-1, -1, -1, -1, -1, -1}, // 0
{-1, -1, -1, -1, -1, -1}, // 1
@ -494,14 +494,13 @@ int main(int argc, char *argv[])
{
SLList<face> patchFaces;
SLList<label>::iterator cellIter(slPatchCells[patchi].begin());
SLList<label>::iterator faceIter(slPatchCellFaces[patchi].begin());
auto cellIter = slPatchCells[patchi].cbegin();
auto faceIter = slPatchCellFaces[patchi].cbegin();
for
(
;
cellIter != slPatchCells[patchi].end()
&& faceIter != slPatchCellFaces[patchi].end();
cellIter.good() && faceIter.good();
++cellIter, ++faceIter
)
{
@ -519,7 +518,7 @@ int main(int argc, char *argv[])
}
boundary[patchi] = patchFaces;
patchNames[patchi] = word("patch") + name(patchi + 1);
patchNames[patchi] = "patch" + Foam::name(patchi + 1);
}
@ -660,20 +659,20 @@ int main(int argc, char *argv[])
// CellZones
labelList types = cellTypes.sortedToc();
forAll(types, j)
forAll(types, typei)
{
label cellType = types[j];
const label cellType = types[typei];
// Pick up cells in zone
DynamicList<label> addr;
SLList<label>::iterator cellMapIter = slCellMap.begin();
SLList<label>::iterator typeIter = slCellType.begin();
auto cellMapIter = slCellMap.cbegin();
auto typeIter = slCellType.cbegin();
for
(
;
typeIter != slCellType.end();
typeIter.good();
++typeIter, ++cellMapIter
)
{
@ -692,7 +691,7 @@ int main(int argc, char *argv[])
(
cellTypes[cellType],
addr,
j,
typei,
pShapeMesh.cellZones()
)
);
@ -709,7 +708,7 @@ int main(int argc, char *argv[])
pShapeMesh.removeFiles();
pShapeMesh.write();
Info<< nl << "end" << endl;
Info<< nl << "End" << endl;
return 0;
}

View File

@ -1057,9 +1057,9 @@ int main(int argc, char *argv[])
uniquify(name, patchNames);
HashTable<word>::const_iterator iter = fluentToFoamType.find(type);
const auto iter = fluentToFoamType.cfind(type);
if (iter != fluentToFoamType.end())
if (iter.found())
{
// See if we have a periodic and can derive the other side.
word neighbPatchName;

View File

@ -1236,9 +1236,9 @@ int main(int argc, char *argv[])
for
(
;
faceGroupZoneIDIter != faceGroupZoneID.end()
&& faceGroupStartIndexIter != faceGroupStartIndex.end()
&& faceGroupEndIndexIter != faceGroupEndIndex.end();
faceGroupZoneIDIter.good()
&& faceGroupStartIndexIter.good()
&& faceGroupEndIndexIter.good();
++faceGroupZoneIDIter,
++faceGroupStartIndexIter,
++faceGroupEndIndexIter
@ -1613,7 +1613,7 @@ int main(int argc, char *argv[])
SLList<label>::iterator start = cellGroupStartIndex.begin();
SLList<label>::iterator end = cellGroupEndIndex.begin();
for (; cg != cellGroupZoneID.end(); ++cg, ++start, ++end)
for (; cg.good(); ++cg, ++start, ++end)
{
const word& name = patchNameIDs[cg()];
const word& type = patchTypeIDs[cg()];
@ -1736,7 +1736,7 @@ int main(int argc, char *argv[])
// Note: cellGroupXXX are all Fluent indices (starting at 1)
// so offset before using.
for (; cg != cellGroupZoneID.end(); ++cg, ++start, ++end)
for (; cg.good(); ++cg, ++start, ++end)
{
const word& name=patchNameIDs[cg()];
const word& type=patchTypeIDs[cg()];

View File

@ -219,27 +219,27 @@ void storeCellInZone
List<DynamicList<label>>& zoneCells
)
{
Map<label>::const_iterator zoneFnd = physToZone.find(regPhys);
const auto zoneFnd = physToZone.cfind(regPhys);
if (zoneFnd == physToZone.end())
{
// New region. Allocate zone for it.
label zoneI = zoneCells.size();
zoneCells.setSize(zoneI+1);
zoneToPhys.setSize(zoneI+1);
Info<< "Mapping region " << regPhys << " to Foam cellZone "
<< zoneI << endl;
physToZone.insert(regPhys, zoneI);
zoneToPhys[zoneI] = regPhys;
zoneCells[zoneI].append(celli);
}
else
if (zoneFnd.found())
{
// Existing zone for region
zoneCells[zoneFnd()].append(celli);
}
else
{
// New region. Allocate zone for it.
const label zonei = zoneCells.size();
zoneCells.setSize(zonei+1);
zoneToPhys.setSize(zonei+1);
Info<< "Mapping region " << regPhys << " to Foam cellZone "
<< zonei << endl;
physToZone.insert(regPhys, zonei);
zoneToPhys[zonei] = regPhys;
zoneCells[zonei].append(celli);
}
}
@ -516,10 +516,15 @@ void readCells
renumber(mshToFoam, triPoints);
Map<label>::iterator regFnd = physToPatch.find(regPhys);
const auto regFnd = physToPatch.cfind(regPhys);
label patchi = -1;
if (regFnd == physToPatch.end())
if (regFnd.found())
{
// Existing patch for region
patchi = regFnd();
}
else
{
// New region. Allocate patch for it.
patchi = patchFaces.size();
@ -532,11 +537,6 @@ void readCells
physToPatch.insert(regPhys, patchi);
patchToPhys[patchi] = regPhys;
}
else
{
// Existing patch for region
patchi = regFnd();
}
// Add triangle to correct patchFaces.
patchFaces[patchi].append(triPoints);
@ -549,10 +549,15 @@ void readCells
renumber(mshToFoam, quadPoints);
Map<label>::iterator regFnd = physToPatch.find(regPhys);
const auto regFnd = physToPatch.cfind(regPhys);
label patchi = -1;
if (regFnd == physToPatch.end())
if (regFnd.found())
{
// Existing patch for region
patchi = regFnd();
}
else
{
// New region. Allocate patch for it.
patchi = patchFaces.size();
@ -565,11 +570,6 @@ void readCells
physToPatch.insert(regPhys, patchi);
patchToPhys[patchi] = regPhys;
}
else
{
// Existing patch for region
patchi = regFnd();
}
// Add quad to correct patchFaces.
patchFaces[patchi].append(quadPoints);
@ -748,15 +748,15 @@ void readCells
Info<< "CellZones:" << nl
<< "Zone\tSize" << endl;
forAll(zoneCells, zoneI)
forAll(zoneCells, zonei)
{
zoneCells[zoneI].shrink();
zoneCells[zonei].shrink();
const labelList& zCells = zoneCells[zoneI];
const labelList& zCells = zoneCells[zonei];
if (zCells.size())
{
Info<< " " << zoneI << '\t' << zCells.size() << endl;
Info<< " " << zonei << '\t' << zCells.size() << endl;
}
}
Info<< endl;
@ -870,11 +870,11 @@ int main(int argc, char *argv[])
label nValidCellZones = 0;
forAll(zoneCells, zoneI)
forAll(zoneCells, zonei)
{
if (zoneCells[zoneI].size())
if (zoneCells[zonei].size())
{
nValidCellZones++;
++nValidCellZones;
}
}
@ -895,18 +895,13 @@ int main(int argc, char *argv[])
forAll(boundaryPatchNames, patchi)
{
label physReg = patchToPhys[patchi];
boundaryPatchNames[patchi] =
physicalNames.lookup
(
patchToPhys[patchi],
"patch" + Foam::name(patchi) // default name
);
Map<word>::const_iterator iter = physicalNames.find(physReg);
if (iter != physicalNames.end())
{
boundaryPatchNames[patchi] = iter();
}
else
{
boundaryPatchNames[patchi] = word("patch") + name(patchi);
}
Info<< "Patch " << patchi << " gets name "
<< boundaryPatchNames[patchi] << endl;
}
@ -1001,17 +996,17 @@ int main(int argc, char *argv[])
Info<< "FaceZones:" << nl
<< "Zone\tSize" << endl;
forAll(zoneFaces, zoneI)
forAll(zoneFaces, zonei)
{
zoneFaces[zoneI].shrink();
zoneFaces[zonei].shrink();
const labelList& zFaces = zoneFaces[zoneI];
const labelList& zFaces = zoneFaces[zonei];
if (zFaces.size())
{
nValidFaceZones++;
++nValidFaceZones;
Info<< " " << zoneI << '\t' << zFaces.size() << endl;
Info<< " " << zonei << '\t' << zFaces.size() << endl;
}
}
Info<< endl;
@ -1036,31 +1031,30 @@ int main(int argc, char *argv[])
nValidCellZones = 0;
forAll(zoneCells, zoneI)
forAll(zoneCells, zonei)
{
if (zoneCells[zoneI].size())
if (zoneCells[zonei].size())
{
label physReg = zoneToPhys[zoneI];
const word zoneName
(
physicalNames.lookup
(
zoneToPhys[zonei],
"cellZone_" + Foam::name(zonei) // default name
)
);
Map<word>::const_iterator iter = physicalNames.find(physReg);
word zoneName = "cellZone_" + name(zoneI);
if (iter != physicalNames.end())
{
zoneName = iter();
}
Info<< "Writing zone " << zoneI << " to cellZone "
Info<< "Writing zone " << zonei << " to cellZone "
<< zoneName << " and cellSet"
<< endl;
cellSet cset(mesh, zoneName, zoneCells[zoneI]);
cellSet cset(mesh, zoneName, zoneCells[zonei]);
cset.write();
cz[nValidCellZones] = new cellZone
(
zoneName,
zoneCells[zoneI],
zoneCells[zonei],
nValidCellZones,
mesh.cellZones()
);
@ -1075,31 +1069,30 @@ int main(int argc, char *argv[])
nValidFaceZones = 0;
forAll(zoneFaces, zoneI)
forAll(zoneFaces, zonei)
{
if (zoneFaces[zoneI].size())
if (zoneFaces[zonei].size())
{
label physReg = patchToPhys[zoneI];
const word zoneName
(
physicalNames.lookup
(
patchToPhys[zonei],
"faceZone_" + Foam::name(zonei) // default name
)
);
Map<word>::const_iterator iter = physicalNames.find(physReg);
word zoneName = "faceZone_" + name(zoneI);
if (iter != physicalNames.end())
{
zoneName = iter();
}
Info<< "Writing zone " << zoneI << " to faceZone "
Info<< "Writing zone " << zonei << " to faceZone "
<< zoneName << " and faceSet"
<< endl;
faceSet fset(mesh, zoneName, zoneFaces[zoneI]);
faceSet fset(mesh, zoneName, zoneFaces[zonei]);
fset.write();
fz[nValidFaceZones] = new faceZone
(
zoneName,
zoneFaces[zoneI],
zoneFaces[zonei],
true, // all are flipped
nValidFaceZones,
mesh.faceZones()

View File

@ -327,10 +327,8 @@ if
{
scalar minz = GREAT;
forAllConstIter(SLList<face>, pFaces[CYLINDERHEAD][0], iter)
for (const face& pf : pFaces[CYLINDERHEAD][0])
{
const face& pf = iter();
forAll(pf, pfi)
{
minz = min(minz, points[pf[pfi]].z());
@ -341,10 +339,8 @@ if
SLList<face> newLinerFaces;
forAllConstIter(SLList<face>, pFaces[LINER][0], iter)
for (const face& pf : pFaces[LINER][0])
{
const face& pf = iter();
scalar minfz = GREAT;
forAll(pf, pfi)
{
@ -370,10 +366,8 @@ if
SLList<face> newCylinderHeadFaces;
forAllConstIter(SLList<face>, pFaces[CYLINDERHEAD][0], iter)
for (const face& pf : pFaces[CYLINDERHEAD][0])
{
const face& pf = iter();
scalar minfz = GREAT;
forAll(pf, pfi)
{
@ -421,12 +415,13 @@ if (pFaces[WEDGE].size() && pFaces[WEDGE][0].size())
const scalar tanTheta = Foam::tan(degToRad(2.5));
SLList<face>::iterator iterf = pFaces[WEDGE][0].begin();
SLList<face>::iterator iterb = pFaces[WEDGE][1].begin();
auto iterf = pFaces[WEDGE][0].begin();
auto iterb = pFaces[WEDGE][1].begin();
for
(
;
iterf != pFaces[WEDGE][0].end() && iterb != pFaces[WEDGE][1].end();
iterf.good() && iterb.good();
++iterf, ++iterb
)
{
@ -441,9 +436,9 @@ if (pFaces[WEDGE].size() && pFaces[WEDGE][0].size())
{
pFaces[CYCLIC].setSize(1);
pFaces[CYCLIC][0] = pFaces[WEDGE][0];
forAllIter(SLList<face>, pFaces[WEDGE][1], iterb)
for (const face& pf : pFaces[WEDGE][1])
{
pFaces[CYCLIC][0].append(iterb());
pFaces[CYCLIC][0].append(pf);
}
pFaces[WEDGE].clear();

View File

@ -213,18 +213,16 @@ int main(int argc, char *argv[])
// Get the four (outwards pointing) faces of the cell
faceList tris(cll.faces());
forAll(tris, i)
for (const face& f : tris)
{
const face& f = tris[i];
// Is there any boundary face with same vertices?
// (uses commutative hash)
auto iter = vertsToBoundary.find(triFace(f[0], f[1], f[2]));
if (iter.found())
{
label facei = iter.object();
const triFace& tri = iter.key();
const label facei = iter.val();
// Determine orientation of tri v.s. cell centre.
point cc(cll.centre(points));

View File

@ -438,23 +438,22 @@ int main(int argc, char *argv[])
// Get Foam patchID and update region->patch table.
label patchi = 0;
Map<label>::iterator patchFind =
regionToPatch.find(region);
const auto patchFind = regionToPatch.cfind(region);
if (patchFind == regionToPatch.end())
if (patchFind.found())
{
patchi = *patchFind;
}
else
{
patchi = nPatches;
Info<< "Mapping tetgen region " << region
<< " to Foam patch "
<< " to patch "
<< patchi << endl;
regionToPatch.insert(region, nPatches++);
}
else
{
patchi = patchFind();
}
boundaryPatch[facei] = patchi;
@ -479,7 +478,7 @@ int main(int argc, char *argv[])
// Print region to patch mapping
Info<< "Regions:" << endl;
forAllConstIter(Map<label>, regionToPatch, iter)
forAllConstIters(regionToPatch, iter)
{
Info<< " region:" << iter.key() << '\t' << "patch:"
<< iter() << endl;
@ -493,7 +492,7 @@ int main(int argc, char *argv[])
forAll(patchNames, patchi)
{
patchNames[patchi] = word("patch") + name(patchi);
patchNames[patchi] = "patch" + Foam::name(patchi);
}
wordList patchTypes(nPatches, polyPatch::typeName);

View File

@ -119,32 +119,32 @@ void writePoints
label v0;
Map<label>::iterator e0Fnd = pointToObj.find(e[0]);
const auto e0Fnd = pointToObj.cfind(e[0]);
if (e0Fnd == pointToObj.end())
if (e0Fnd.found())
{
meshTools::writeOBJ(str, mesh.points()[e[0]]);
v0 = vertI++;
pointToObj.insert(e[0], v0);
v0 = *e0Fnd;
}
else
{
v0 = e0Fnd();
v0 = vertI++;
meshTools::writeOBJ(str, mesh.points()[e[0]]);
pointToObj.insert(e[0], v0);
}
label v1;
Map<label>::iterator e1Fnd = pointToObj.find(e[1]);
const auto e1Fnd = pointToObj.cfind(e[1]);
if (e1Fnd == pointToObj.end())
if (e1Fnd.found())
{
meshTools::writeOBJ(str, mesh.points()[e[1]]);
v1 = vertI++;
pointToObj.insert(e[1], v1);
v1 = *e1Fnd;
}
else
{
v1 = e1Fnd();
v1 = vertI++;
meshTools::writeOBJ(str, mesh.points()[e[1]]);
pointToObj.insert(e[1], v1);
}