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

- reduced clutter when iterating over containers
This commit is contained in:
Mark Olesen
2019-01-07 09:20:51 +01:00
committed by Andrew Heather
parent e3e0d7c8b9
commit 8d5894174f
10 changed files with 89 additions and 100 deletions

View File

@ -52,7 +52,7 @@ void Foam::meshReader::addFaceZones(polyMesh& mesh) const
} }
nZone = 0; nZone = 0;
forAllConstIter(HashTable<labelList>, monitoringSets_, iter) forAllConstIters(monitoringSets_, iter)
{ {
Info<< "faceZone " << nZone Info<< "faceZone " << nZone
<< " (size: " << iter().size() << ") name: " << " (size: " << iter().size() << ") name: "

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | \\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2017 OpenFOAM Foundation | Copyright (C) 2011-2017 OpenFOAM Foundation
@ -41,26 +41,26 @@ void Foam::meshReader::warnDuplicates
HashTable<label> hashed(list.size()); HashTable<label> hashed(list.size());
bool duplicates = false; bool duplicates = false;
forAll(list, listI) for (const word& w : list)
{ {
// check duplicate name // Check duplicate name
HashTable<label>::iterator iter = hashed.find(list[listI]); auto iter = hashed.find(w);
if (iter != hashed.end()) if (iter.found())
{ {
(*iter)++; ++(*iter);
duplicates = true; duplicates = true;
} }
else else
{ {
hashed.insert(list[listI], 1); hashed.insert(w, 1);
} }
} }
// warn about duplicate names // Warn about duplicate names
if (duplicates) if (duplicates)
{ {
Info<< nl << "WARNING: " << context << " with identical names:"; Info<< nl << "WARNING: " << context << " with identical names:";
forAllConstIter(HashTable<label>, hashed, iter) forAllConstIters(hashed, iter)
{ {
if (*iter > 1) if (*iter > 1)
{ {

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | \\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011 OpenFOAM Foundation | Copyright (C) 2011 OpenFOAM Foundation
@ -55,7 +55,7 @@ Foam::boundaryRegion::boundaryRegion
Foam::label Foam::boundaryRegion::append(const dictionary& dict) Foam::label Foam::boundaryRegion::append(const dictionary& dict)
{ {
label maxId = -1; label maxId = -1;
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
if (maxId < iter.key()) if (maxId < iter.key())
{ {
@ -72,7 +72,7 @@ Foam::Map<Foam::word> Foam::boundaryRegion::names() const
{ {
Map<word> lookup; Map<word> lookup;
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
lookup.insert lookup.insert
( (
@ -96,7 +96,7 @@ Foam::Map<Foam::word> Foam::boundaryRegion::names
{ {
Map<word> lookup; Map<word> lookup;
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
const word lookupName = iter().lookupOrDefault<word> const word lookupName = iter().lookupOrDefault<word>
( (
@ -118,7 +118,7 @@ Foam::Map<Foam::word> Foam::boundaryRegion::boundaryTypes() const
{ {
Map<word> lookup; Map<word> lookup;
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
lookup.insert lookup.insert
( (
@ -138,7 +138,7 @@ Foam::label Foam::boundaryRegion::findIndex(const word& name) const
return -1; return -1;
} }
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
if (iter().lookupOrDefault<word>("Label", word::null) == name) if (iter().lookupOrDefault<word>("Label", word::null) == name)
{ {
@ -258,18 +258,18 @@ void Foam::boundaryRegion::rename(const dictionary& mapDict)
// This avoid re-matching any renamed regions // This avoid re-matching any renamed regions
Map<word> mapping; Map<word> mapping;
forAllConstIter(dictionary, mapDict, iter) for (const entry& dEntry : mapDict)
{ {
word oldName(iter().stream()); const word oldName(dEntry.stream());
label id = this->findIndex(oldName); const label id = this->findIndex(oldName);
if (id >= 0) if (id >= 0)
{ {
mapping.insert(id, iter().keyword()); mapping.insert(id, dEntry.keyword());
} }
} }
forAllConstIter(Map<word>, mapping, iter) forAllConstIters(mapping, iter)
{ {
dictionary& dict = operator[](iter.key()); dictionary& dict = operator[](iter.key());

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | \\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation | Copyright (C) 2011-2016 OpenFOAM Foundation
@ -40,10 +40,10 @@ Foam::Map<Foam::label> Foam::cellTable::zoneMap() const
{ {
Map<label> lookup; Map<label> lookup;
label zoneI = 0; label zonei = 0;
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
lookup.insert(iter.key(), zoneI++); lookup.insert(iter.key(), zonei++);
} }
return lookup; return lookup;
@ -53,21 +53,21 @@ Foam::Map<Foam::label> Foam::cellTable::zoneMap() const
Foam::wordList Foam::cellTable::namesList() const Foam::wordList Foam::cellTable::namesList() const
{ {
Map<word> lookup = names(); Map<word> lookup = names();
wordList lst(lookup.size()); wordList list(lookup.size());
label zoneI = 0; label zonei = 0;
forAllConstIter(Map<word>, lookup, iter) forAllConstIters(lookup, iter)
{ {
lst[zoneI++] = iter(); list[zonei++] = *iter;
} }
return lst; return list;
} }
void Foam::cellTable::addDefaults() void Foam::cellTable::addDefaults()
{ {
forAllIter(Map<dictionary>, *this, iter) forAllIters(*this, iter)
{ {
if (!iter().found("MaterialType")) if (!iter().found("MaterialType"))
{ {
@ -88,7 +88,7 @@ void Foam::cellTable::setEntry
dict.add(keyWord, value); dict.add(keyWord, value);
iterator iter = find(id); iterator iter = find(id);
if (iter != end()) if (iter.found())
{ {
iter().merge(dict); iter().merge(dict);
} }
@ -125,7 +125,7 @@ Foam::cellTable::cellTable
Foam::label Foam::cellTable::append(const dictionary& dict) Foam::label Foam::cellTable::append(const dictionary& dict)
{ {
label maxId = -1; label maxId = -1;
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
if (maxId < iter.key()) if (maxId < iter.key())
{ {
@ -142,7 +142,7 @@ Foam::Map<Foam::word> Foam::cellTable::names() const
{ {
Map<word> lookup; Map<word> lookup;
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
lookup.insert lookup.insert
( (
@ -166,7 +166,7 @@ Foam::Map<Foam::word> Foam::cellTable::names
{ {
Map<word> lookup; Map<word> lookup;
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
const word lookupName = iter().lookupOrDefault<word> const word lookupName = iter().lookupOrDefault<word>
( (
@ -188,8 +188,8 @@ Foam::word Foam::cellTable::name(const label id) const
{ {
word theName("cellTable_" + Foam::name(id)); word theName("cellTable_" + Foam::name(id));
const_iterator iter = find(id); const_iterator iter = cfind(id);
if (iter != end()) if (iter.found())
{ {
iter().readIfPresent("Label", theName); iter().readIfPresent("Label", theName);
} }
@ -205,7 +205,7 @@ Foam::label Foam::cellTable::findIndex(const word& name) const
return -1; return -1;
} }
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
if (iter().lookupOrDefault<word>("Label", word::null) == name) if (iter().lookupOrDefault<word>("Label", word::null) == name)
{ {
@ -221,7 +221,7 @@ Foam::Map<Foam::word> Foam::cellTable::materialTypes() const
{ {
Map<word> lookup; Map<word> lookup;
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
lookup.insert lookup.insert
( (
@ -238,7 +238,7 @@ Foam::Map<Foam::word> Foam::cellTable::selectType(const word& matl) const
{ {
Map<word> lookup; Map<word> lookup;
forAllConstIter(Map<dictionary>, *this, iter) forAllConstIters(*this, iter)
{ {
const label index = iter.key(); const label index = iter.key();
const dictionary& dict = iter.val(); const dictionary& dict = iter.val();
@ -300,7 +300,7 @@ void Foam::cellTable::setName(const label id)
{ {
iterator iter = find(id); iterator iter = find(id);
if (iter == end() || !iter().found("Label")) if (!iter.found() || !iter().found("Label"))
{ {
setName(id, "cellTable_" + Foam::name(id)); setName(id, "cellTable_" + Foam::name(id));
} }
@ -446,10 +446,10 @@ void Foam::cellTable::addCellZones
forAll(tableIds, celli) forAll(tableIds, celli)
{ {
Map<label>::const_iterator iter = typeToZone.find(tableIds[celli]); const auto iter = typeToZone.cfind(tableIds[celli]);
if (iter != typeToZone.end()) if (iter.found())
{ {
zoneCells[iter()].append(celli); zoneCells[*iter].append(celli);
} }
} }
@ -513,13 +513,13 @@ void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
labelList mapping(identity(max(origNames.toc()) + 1)); labelList mapping(identity(max(origNames.toc()) + 1));
bool remap = false; bool remap = false;
forAllConstIter(dictionary, mapDict, iter) forAllConstIters(mapDict, iter)
{ {
wordRes patterns(iter().stream()); wordRes patterns(iter().stream());
// find all matches // find all matches
Map<word> matches; Map<word> matches;
forAllConstIter(Map<word>, origNames, namesIter) forAllConstIters(origNames, namesIter)
{ {
if (patterns.match(namesIter())) if (patterns.match(namesIter()))
{ {
@ -554,7 +554,7 @@ void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
this->erase(matches); this->erase(matches);
origNames.erase(matches); origNames.erase(matches);
forAllConstIter(Map<word>, matches, matchIter) forAllConstIters(matches, matchIter)
{ {
mapping[matchIter.key()] = targetId; mapping[matchIter.key()] = targetId;
Info<< " " << matchIter(); Info<< " " << matchIter();

View File

@ -140,7 +140,7 @@ namespace Foam
forAll(valueSetNames, i) forAll(valueSetNames, i)
{ {
if (i > 0) if (i)
{ {
writeSeparator(os); writeSeparator(os);
} }
@ -166,7 +166,7 @@ void Foam::csvSetWriter<Type>::writeHeader
{ {
for (label j=0; j<Type::nComponents; j++) for (label j=0; j<Type::nComponents; j++)
{ {
if (i>0 || j>0) if (i || j)
{ {
writeSeparator(os); writeSeparator(os);
} }
@ -189,12 +189,7 @@ void Foam::csvSetWriter<Type>::writeCoordHeader
if (points.hasVectorAxis()) if (points.hasVectorAxis())
{ {
for for (auto iter = axisName.cbegin(); iter != axisName.cend(); ++iter)
(
word::const_iterator iter = axisName.begin();
iter != axisName.end();
++iter
)
{ {
os << *iter; os << *iter;
writeSeparator(os); writeSeparator(os);

View File

@ -85,7 +85,7 @@ void Foam::gnuplotSetWriter<Type>::write
forAll(valueSets, i) forAll(valueSets, i)
{ {
if (i != 0) if (i)
{ {
os << ','; os << ',';
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -159,15 +159,12 @@ void Foam::nastranSetWriter<Type>::write
// } // }
label globalPti = 0; label globalPti = 0;
forAll(tracks, tracki) for (const coordSet& points : tracks)
{ {
const coordSet& points = tracks[tracki]; for (const point& pt : points)
forAll(points, pointi)
{ {
fileFormats::NASCore::writeKeyword(os, "GRID", fieldFormat::FREE); fileFormats::NASCore::writeKeyword(os, "GRID", fieldFormat::FREE);
const point& pt = points[pointi];
//os.setf(std::ios_base::right); //os.setf(std::ios_base::right);
//os << setw(8) << globalPti++ //os << setw(8) << globalPti++
// << setw(8) << ' ' // << setw(8) << ' '
@ -190,10 +187,8 @@ void Foam::nastranSetWriter<Type>::write
// Write ids of track points to file // Write ids of track points to file
label globalEdgei = 0; label globalEdgei = 0;
label globalPointi = 0; label globalPointi = 0;
forAll(tracks, tracki) for (const coordSet& points : tracks)
{ {
const coordSet& points = tracks[tracki];
const label nEdges = points.size()-1; const label nEdges = points.size()-1;
for (label edgei = 0; edgei < nEdges; ++edgei) for (label edgei = 0; edgei < nEdges; ++edgei)
{ {

View File

@ -95,7 +95,7 @@ void Foam::vtkSetWriter<Type>::write
forAll(fld, pointi) forAll(fld, pointi)
{ {
if (pointi != 0) if (pointi)
{ {
os << ' '; os << ' ';
} }
@ -137,9 +137,8 @@ void Foam::vtkSetWriter<Type>::write
<< "DATASET POLYDATA" << nl << "DATASET POLYDATA" << nl
<< "POINTS " << nPoints << " double" << nl; << "POINTS " << nPoints << " double" << nl;
forAll(tracks, trackI) for (const coordSet& points : tracks)
{ {
const coordSet& points = tracks[trackI];
for (const point& pt : points) for (const point& pt : points)
{ {
os << float(pt.x()) << ' ' os << float(pt.x()) << ' '
@ -158,11 +157,13 @@ void Foam::vtkSetWriter<Type>::write
{ {
const coordSet& points = tracks[trackI]; const coordSet& points = tracks[trackI];
os << points.size(); const label len = points.size();
forAll(points, i)
os << len;
for (label i = 0; i < len; ++i)
{ {
os << ' ' << globalPtI; os << ' ' << globalPtI;
globalPtI++; ++globalPtI;
} }
os << nl; os << nl;
} }
@ -179,13 +180,11 @@ void Foam::vtkSetWriter<Type>::write
const List<Field<Type>>& fieldVals = valueSets[setI]; const List<Field<Type>>& fieldVals = valueSets[setI];
forAll(fieldVals, i) for (const Field<Type>& vals : fieldVals)
{ {
const Field<Type>& vals = fieldVals[i];
forAll(vals, j) forAll(vals, j)
{ {
if (j != 0) if (j)
{ {
os << ' '; os << ' ';
} }

View File

@ -622,14 +622,14 @@ void Foam::vtk::vtuSizing::populateArrays
if (sizing.nFaceLabels()) if (sizing.nFaceLabels())
{ {
// End face offsets, leaving -1 untouched // End face offsets, leaving -1 untouched
label prev = 0; LabelType prev = 0;
forAll(faceOffset, i) for (LabelType& off : faceOffset)
{ {
const label sz = faceOffset[i]; const auto sz = off;
if (sz > 0) if (sz > 0)
{ {
prev += sz; prev += sz;
faceOffset[i] = prev; off = prev;
} }
} }
} }
@ -639,10 +639,10 @@ void Foam::vtk::vtuSizing::populateArrays
{ {
// Has prefix, determine begin offsets // Has prefix, determine begin offsets
label beg = 0; label beg = 0;
forAll(vertOffset, i) for (LabelType& off : vertOffset)
{ {
const label sz = vertOffset[i]; const auto sz = off;
vertOffset[i] = beg; off = beg;
beg += 1 + sz; beg += 1 + sz;
} }
@ -650,12 +650,12 @@ void Foam::vtk::vtuSizing::populateArrays
if (sizing.nFaceLabels()) if (sizing.nFaceLabels())
{ {
beg = 0; beg = 0;
forAll(faceOffset, i) for (LabelType& off : faceOffset)
{ {
const label sz = faceOffset[i]; const auto sz = off;
if (sz > 0) if (sz > 0)
{ {
faceOffset[i] = beg; off = beg;
beg += sz; beg += sz;
} }
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2012-2016 OpenFOAM Foundation | Copyright (C) 2012-2016 OpenFOAM Foundation
@ -186,9 +186,9 @@ void Foam::vtkUnstructuredReader::extractCells
lineMap_[lineI] = i; lineMap_[lineI] = i;
labelList& segment = lines_[lineI++]; labelList& segment = lines_[lineI++];
segment.setSize(nRead); segment.setSize(nRead);
forAll(segment, i) for (label& pointi : segment)
{ {
segment[i] = cellVertData[dataIndex++]; pointi = cellVertData[dataIndex++];
} }
} }
break; break;
@ -236,9 +236,9 @@ void Foam::vtkUnstructuredReader::extractCells
face& f = faces_[facei++]; face& f = faces_[facei++];
label nRead = cellVertData[dataIndex++]; label nRead = cellVertData[dataIndex++];
f.setSize(nRead); f.setSize(nRead);
forAll(f, fp) for (label& pointi : f)
{ {
f[fp] = cellVertData[dataIndex++]; pointi = cellVertData[dataIndex++];
} }
} }
break; break;
@ -398,9 +398,9 @@ void Foam::vtkUnstructuredReader::readField
inFile.getLine(fieldVals()[0]); inFile.getLine(fieldVals()[0]);
// Read without parsing // Read without parsing
forAll(fieldVals(), i) for (string& s : fieldVals())
{ {
inFile.getLine(fieldVals()[i]); inFile.getLine(s);
} }
regIOobject::store(fieldVals); regIOobject::store(fieldVals);
} }
@ -627,9 +627,9 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
lineMap_[lineI] = lineI; lineMap_[lineI] = lineI;
labelList& f = lines_[lineI]; labelList& f = lines_[lineI];
f.setSize(lineVerts[elemI++]); f.setSize(lineVerts[elemI++]);
forAll(f, fp) for (label& pointi : f)
{ {
f[fp] = lineVerts[elemI++]; pointi = lineVerts[elemI++];
} }
lineI++; lineI++;
} }
@ -656,9 +656,9 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
faceMap_[facei] = facei; faceMap_[facei] = facei;
face& f = faces_[facei]; face& f = faces_[facei];
f.setSize(faceVerts[elemI++]); f.setSize(faceVerts[elemI++]);
forAll(f, fp) for (label& pointi : f)
{ {
f[fp] = faceVerts[elemI++]; pointi = faceVerts[elemI++];
} }
facei++; facei++;
} }
@ -769,11 +769,11 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
); );
label elemI = 0; label elemI = 0;
forAll(fieldVals(), i) for (vector& val : fieldVals())
{ {
fieldVals()[i].x() = s[elemI++]; val.x() = s[elemI++];
fieldVals()[i].y() = s[elemI++]; val.y() = s[elemI++];
fieldVals()[i].z() = s[elemI++]; val.z() = s[elemI++];
} }
regIOobject::store(fieldVals); regIOobject::store(fieldVals);
} }