Commit 5f42b5df authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

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

- reduced clutter when iterating over containers
parent a43eb01b
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -32,12 +32,7 @@ License
template<class IDLListType, class T>
void Foam::DictionaryBase<IDLListType, T>::addEntries()
{
for
(
typename IDLListType::iterator iter = this->begin();
iter != this->end();
++iter
)
for (auto iter = this->begin(); iter != this->end(); ++iter)
{
this->hashedTs_.insert((*iter).keyword(), &(*iter));
}
......@@ -103,41 +98,37 @@ const T* Foam::DictionaryBase<IDLListType, T>::lookupPtr
const word& keyword
) const
{
typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
const auto iter = hashedTs_.cfind(keyword);
if (iter != hashedTs_.end())
if (iter.found())
{
return *iter;
}
else
{
return nullptr;
}
return nullptr;
}
template<class IDLListType, class T>
T* Foam::DictionaryBase<IDLListType, T>::lookupPtr(const word& keyword)
{
typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
auto iter = hashedTs_.find(keyword);
if (iter != hashedTs_.end())
if (iter.found())
{
return *iter;
}
else
{
return nullptr;
}
return nullptr;
}
template<class IDLListType, class T>
const T* Foam::DictionaryBase<IDLListType, T>::lookup(const word& keyword) const
{
typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
const auto iter = hashedTs_.cfind(keyword);
if (iter == hashedTs_.end())
if (!iter.found())
{
FatalErrorInFunction
<< "'" << keyword << "' not found"
......@@ -151,9 +142,9 @@ const T* Foam::DictionaryBase<IDLListType, T>::lookup(const word& keyword) const
template<class IDLListType, class T>
T* Foam::DictionaryBase<IDLListType, T>::lookup(const word& keyword)
{
typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
auto iter = hashedTs_.find(keyword);
if (iter == hashedTs_.end())
if (!iter.found())
{
FatalErrorInFunction
<< "'" << keyword << "' not found"
......@@ -170,12 +161,7 @@ Foam::wordList Foam::DictionaryBase<IDLListType, T>::toc() const
wordList keywords(this->size());
label i = 0;
for
(
typename IDLListType::const_iterator iter = this->begin();
iter != this->end();
++iter
)
for (auto iter = this->cbegin(); iter != this->cend(); ++iter)
{
keywords[i++] = iter().keyword();
}
......@@ -223,18 +209,16 @@ void Foam::DictionaryBase<IDLListType, T>::append(const word& keyword, T* tPtr)
template<class IDLListType, class T>
T* Foam::DictionaryBase<IDLListType, T>::remove(const word& keyword)
{
typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
auto iter = hashedTs_.find(keyword);
if (iter != hashedTs_.end())
if (iter.found())
{
T* tPtr = IDLListType::remove(iter());
T* ptr = IDLListType::remove(iter());
hashedTs_.erase(iter);
return tPtr;
}
else
{
return nullptr;
return ptr;
}
return nullptr;
}
......@@ -265,7 +249,6 @@ void Foam::DictionaryBase<IDLListType, T>::operator=
const DictionaryBase<IDLListType, T>& dict
)
{
// Check for assignment to self
if (this == &dict)
{
FatalErrorInFunction
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011 OpenFOAM Foundation
......@@ -42,7 +42,7 @@ Foam::CallbackRegistry<CallbackType>::CallbackRegistry()
template<class CallbackType>
Foam::CallbackRegistry<CallbackType>::~CallbackRegistry()
{
forAllIter(typename CallbackRegistry<CallbackType>, *this, iter)
forAllIters(*this, iter)
{
iter().Callback<CallbackType>::checkOut();
}
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2017 OpenFOAM Foundation
......@@ -290,7 +290,7 @@ void Foam::Time::readDict()
IStringStream dummyIs("");
forAllConstIter(simpleObjectRegistry, objs, iter)
forAllConstIters(objs, iter)
{
const List<simpleRegIOobject*>& objects = *iter;
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2012 OpenFOAM Foundation
......@@ -49,7 +49,7 @@ void MapDimensionedFields(const MeshMapper& mapper)
TableType fields(mapper.thisDb().template lookupClass<FieldType>(true));
forAllConstIter(typename TableType, fields, fieldIter)
forAllConstIters(fields, fieldIter)
{
FieldType& field = const_cast<FieldType&>(*fieldIter());
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -87,7 +87,7 @@ void MapGeometricFields
// old-time-level field is mapped before the field itself, sizes
// will not match.
forAllConstIter(typename HashTable<const FieldType*>, fields, fieldIter)
forAllConstIters(fields, fieldIter)
{
FieldType& field = const_cast<FieldType&>(*fieldIter());
......@@ -99,7 +99,7 @@ void MapGeometricFields
}
}
forAllConstIter(typename HashTable<const FieldType*>, fields, fieldIter)
forAllConstIters(fields, fieldIter)
{
FieldType& field = const_cast<FieldType&>(*fieldIter());
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011 OpenFOAM Foundation
......@@ -51,7 +51,7 @@ inline void mapClouds(const objectRegistry& db, const mapPolyMesh& mapper)
{
HashTable<const cloud*> clouds(db.lookupClass<cloud>());
forAllIter(HashTable<const cloud*>, clouds, iter)
forAllIters(clouds, iter)
{
cloud& c = const_cast<cloud&>(*iter());
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2017-2018 OpenFOAM Foundation
......@@ -261,7 +261,7 @@ void Foam::OFstreamCollator::waitForBufferSpace(const off_t wantedSize) const
{
std::lock_guard<std::mutex> guard(mutex_);
forAllConstIter(FIFOStack<writeData*>, objects_, iter)
forAllConstIters(objects_, iter)
{
totalSize += iter()->size();
}
......
......@@ -258,12 +258,11 @@ Foam::fileOperation::lookupAndCacheProcessorsPath
{
const fileName procPath(path/pDir);
HashTable<dirIndexList>::const_iterator iter =
procsDirs_.find(procPath);
const auto iter = procsDirs_.cfind(procPath);
if (iter != procsDirs_.end())
if (iter.found())
{
return iter();
return iter.val();
}
// Read all directories to see any beginning with processor
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2017-2018 OpenFOAM Foundation
......@@ -254,14 +254,9 @@ Foam::fileOperations::masterUncollatedFileOperation::filePathInfo
// Check for approximately same time. E.g. if time = 1e-2 and
// directory is 0.01 (due to different time formats)
HashPtrTable<instantList>::const_iterator pathFnd
(
times_.find
(
io.time().path()
)
);
if (search && (pathFnd != times_.end()))
const auto pathFnd = times_.cfind(io.time().path());
if (search && pathFnd.found())
{
newInstancePath = findInstancePath
(
......@@ -2307,8 +2302,8 @@ Foam::instantList Foam::fileOperations::masterUncollatedFileOperation::findTimes
const word& constantName
) const
{
HashPtrTable<instantList>::const_iterator iter = times_.find(directory);
if (iter != times_.end())
const auto iter = times_.cfind(directory);
if (iter.found())
{
if (debug)
{
......@@ -2358,8 +2353,8 @@ void Foam::fileOperations::masterUncollatedFileOperation::setTime
return;
}
HashPtrTable<instantList>::const_iterator iter = times_.find(tm.path());
if (iter != times_.end())
const auto iter = times_.cfind(tm.path());
if (iter.found())
{
instantList& times = *iter();
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2015 OpenFOAM Foundation
......@@ -197,7 +197,7 @@ void Foam::graph::setXRange(const scalar x0, const scalar x1)
scalarField xNew(SubField<scalar>(x_, nX, i0));
x_.transfer(xNew);
forAllIter(HashPtrTable<curve>, *this, iter)
forAllIters(*this, iter)
{
curve* c = iter();
scalarField cNew(SubField<scalar>(*c, nX, i0));
......@@ -254,7 +254,7 @@ void Foam::graph::writeTable(Ostream& os) const
{
os << setw(10) << x_[xi];
forAllConstIter(graph, *this, iter)
forAllConstIters(*this, iter)
{
os << token::SPACE << setw(10) << (*iter())[xi];
}
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2012 OpenFOAM Foundation
......@@ -53,7 +53,7 @@ void Foam::gnuplotGraph::write(const graph& g, Ostream& os) const
bool firstField = true;
forAllConstIter(graph, g, iter)
forAllConstIters(g, iter)
{
if (!firstField)
{
......@@ -66,7 +66,7 @@ void Foam::gnuplotGraph::write(const graph& g, Ostream& os) const
os << "; pause -1" << endl;
forAllConstIter(graph, g, iter)
forAllConstIters(g, iter)
{
os << endl;
writeXY(g.x(), *iter(), os);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -49,7 +49,7 @@ void Foam::jplotGraph::write(const graph& g, Ostream& os) const
label fieldi = 0;
forAllConstIter(graph, g, iter)
forAllConstIters(g, iter)
{
os << "# column " << fieldi + 2 << ": " << (*iter()).name() << endl;
fieldi++;
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -50,7 +50,7 @@ void Foam::xmgrGraph::write(const graph& g, Ostream& os) const
label fieldi = 0;
forAllConstIter(graph, g, iter)
forAllConstIters(g, iter)
{
os << "@s" << fieldi << " legend "
<< iter()->name() << nl
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2017 OpenFOAM Foundation
......@@ -682,23 +682,23 @@ void Foam::GAMGAgglomeration::calculateRegionMaster
forAll(procAgglomMap, proci)
{
label coarseI = procAgglomMap[proci];
const label coarsei = procAgglomMap[proci];
Map<label>::iterator fnd = agglomToMaster.find(coarseI);
if (fnd == agglomToMaster.end())
auto iter = agglomToMaster.find(coarsei);
if (iter.found())
{
agglomToMaster.insert(coarseI, proci);
iter.val() = min(iter.val(), proci);
}
else
{
fnd() = min(fnd(), proci);
agglomToMaster.insert(coarsei, proci);
}
}
masterProcs.setSize(agglomToMaster.size());
forAllConstIter(Map<label>, agglomToMaster, iter)
forAllConstIters(agglomToMaster, iter)
{
masterProcs[iter.key()] = iter();
masterProcs[iter.key()] = iter.val();
}
......@@ -709,6 +709,7 @@ void Foam::GAMGAgglomeration::calculateRegionMaster
// Get all processors agglomerating to the same coarse
// processor
agglomProcIDs = findIndices(procAgglomMap, myAgglom);
// Make sure the master is the first element.
const label index =
agglomProcIDs.find(agglomToMaster[myAgglom]);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2013-2016 OpenFOAM Foundation
......@@ -104,7 +104,7 @@ Foam::procFacesGAMGProcAgglomeration::singleCellMesh
// Add all the higher processors
nbrs.clear();
weights.clear();
forAllConstIter(Map<label>, neighbours, iter)
forAllConstIters(neighbours, iter)
{
if (iter.key() > proci)
{
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -124,9 +124,14 @@ Foam::cyclicGAMGInterface::cyclicGAMGInterface
);
}
labelPairLookup::const_iterator fnd = cellsToCoarseFace.find(cellPair);
const auto fnd = cellsToCoarseFace.cfind(cellPair);
if (fnd == cellsToCoarseFace.end())
if (fnd.found())
{
// Already have coarse face
dynFaceRestrictAddressing.append(fnd.val());
}
else
{
// New coarse face
label coarseI = dynFaceCells.size();
......@@ -134,11 +139,6 @@ Foam::cyclicGAMGInterface::cyclicGAMGInterface
dynFaceCells.append(localRestrictAddressing[ffi]);
cellsToCoarseFace.insert(cellPair, coarseI);
}
else
{
// Already have coarse face
dynFaceRestrictAddressing.append(fnd());
}
}
faceCells_.transfer(dynFaceCells);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2017 OpenFOAM Foundation
......@@ -112,9 +112,14 @@ Foam::processorGAMGInterface::processorGAMGInterface
);
}
labelPairLookup::const_iterator fnd = cellsToCoarseFace.find(cellPair);
const auto fnd = cellsToCoarseFace.cfind(cellPair);
if (fnd == cellsToCoarseFace.end())
if (fnd.found())
{
// Already have coarse face
dynFaceRestrictAddressing.append(fnd.val());
}
else
{
// New coarse face
label coarseI = dynFaceCells.size();
......@@ -122,11 +127,6 @@ Foam::processorGAMGInterface::processorGAMGInterface
dynFaceCells.append(localRestrictAddressing[ffi]);
cellsToCoarseFace.insert(cellPair, coarseI);
}
else
{
// Already have coarse face
dynFaceRestrictAddressing.append(fnd());
}
}
faceCells_.transfer(dynFaceCells);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -72,10 +72,8 @@ void Foam::solution::read(const dictionary& dict)
// backwards compatibility
fieldRelaxDict_.clear();
const wordList entryNames(relaxDict.toc());
forAll(entryNames, i)
for (const word& e : relaxDict.toc())
{
const word& e = entryNames[i];
scalar value = relaxDict.get<scalar>(e);
if (e.startsWith("p"))
......@@ -98,15 +96,12 @@ void Foam::solution::read(const dictionary& dict)
eqnRelaxDefault_ =
eqnRelaxDict_.lookupOrDefault<scalar>("default", 0.0);
if (debug)
{
Info<< "Relaxation factors:" << nl
<< "fields: " << fieldRelaxDict_ << nl
<< "equations: " << eqnRelaxDict_ << endl;
}
DebugInfo