From 5f42b5df9f6ec787ec29d50de14a7525cabe235d Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 7 Jan 2019 09:20:51 +0100 Subject: [PATCH] ENH: for-range, forAllIters() ... in OpenFOAM/ - reduced clutter when iterating over containers --- .../DictionaryBase/DictionaryBase.C | 59 ++++------ .../db/CallbackRegistry/CallbackRegistry.C | 4 +- src/OpenFOAM/db/Time/TimeIO.C | 4 +- .../DimensionedField/MapDimensionedFields.H | 4 +- .../GeometricField/MapGeometricFields.H | 6 +- src/OpenFOAM/fields/cloud/mapClouds.H | 4 +- .../collatedFileOperation/OFstreamCollator.C | 4 +- .../fileOperation/fileOperation.C | 7 +- .../masterUncollatedFileOperation.C | 21 ++-- src/OpenFOAM/graph/graph.C | 6 +- .../graph/writers/gnuplotGraph/gnuplotGraph.C | 6 +- .../graph/writers/jplotGraph/jplotGraph.C | 4 +- .../graph/writers/xmgrGraph/xmgrGraph.C | 4 +- .../GAMGAgglomerateLduAddressing.C | 17 +-- .../procFacesGAMGProcAgglomeration.C | 4 +- .../cyclicGAMGInterface/cyclicGAMGInterface.C | 16 +-- .../processorGAMGInterface.C | 16 +-- src/OpenFOAM/matrices/solution/solution.C | 102 +++++++----------- .../Identifiers/patch/coupleGroupIdentifier.C | 6 +- .../meshes/lduMesh/lduPrimitiveMesh.C | 68 ++++-------- .../meshShapes/cellMatcher/cellMatcher.C | 35 +++--- .../polyMesh/globalMeshData/globalMeshData.C | 96 +++++++---------- .../polyMesh/globalMeshData/globalPoints.C | 42 ++++---- .../mapPolyMesh/mapDistribute/mapDistribute.H | 17 +-- .../mapDistribute/mapDistributeBase.C | 72 +++++-------- .../polyBoundaryMesh/polyBoundaryMesh.C | 4 +- .../polyMesh/polyMeshCheck/polyMeshCheck.C | 2 +- .../constraint/cyclic/cyclicPolyPatch.C | 33 +++--- .../PatchTools/PatchToolsMatch.C | 19 ++-- .../PatchTools/PatchToolsNormals.C | 30 +++--- .../PrimitivePatchPointAddressing.C | 35 +++--- .../primitiveMeshCheck/primitiveMeshCheck.C | 19 +--- .../primitiveMesh/primitiveMeshPointPoints.C | 2 - 33 files changed, 316 insertions(+), 452 deletions(-) diff --git a/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C b/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C index b4558e735b..14a75287c9 100644 --- a/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C +++ b/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C @@ -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 void Foam::DictionaryBase::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::lookupPtr const word& keyword ) const { - typename HashTable::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 T* Foam::DictionaryBase::lookupPtr(const word& keyword) { - typename HashTable::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 const T* Foam::DictionaryBase::lookup(const word& keyword) const { - typename HashTable::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::lookup(const word& keyword) const template T* Foam::DictionaryBase::lookup(const word& keyword) { - typename HashTable::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::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::append(const word& keyword, T* tPtr) template T* Foam::DictionaryBase::remove(const word& keyword) { - typename HashTable::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::operator= const DictionaryBase& dict ) { - // Check for assignment to self if (this == &dict) { FatalErrorInFunction diff --git a/src/OpenFOAM/db/CallbackRegistry/CallbackRegistry.C b/src/OpenFOAM/db/CallbackRegistry/CallbackRegistry.C index 6c63c4f111..157b06ed6e 100644 --- a/src/OpenFOAM/db/CallbackRegistry/CallbackRegistry.C +++ b/src/OpenFOAM/db/CallbackRegistry/CallbackRegistry.C @@ -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::CallbackRegistry() template Foam::CallbackRegistry::~CallbackRegistry() { - forAllIter(typename CallbackRegistry, *this, iter) + forAllIters(*this, iter) { iter().Callback::checkOut(); } diff --git a/src/OpenFOAM/db/Time/TimeIO.C b/src/OpenFOAM/db/Time/TimeIO.C index a39dae85e3..57223ceb7b 100644 --- a/src/OpenFOAM/db/Time/TimeIO.C +++ b/src/OpenFOAM/db/Time/TimeIO.C @@ -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& objects = *iter; diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/MapDimensionedFields.H b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/MapDimensionedFields.H index a3c86b64ba..44b063c71b 100644 --- a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/MapDimensionedFields.H +++ b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/MapDimensionedFields.H @@ -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(true)); - forAllConstIter(typename TableType, fields, fieldIter) + forAllConstIters(fields, fieldIter) { FieldType& field = const_cast(*fieldIter()); diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/MapGeometricFields.H b/src/OpenFOAM/fields/GeometricFields/GeometricField/MapGeometricFields.H index 8cf010926c..c43aa49edd 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/MapGeometricFields.H +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/MapGeometricFields.H @@ -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, fields, fieldIter) + forAllConstIters(fields, fieldIter) { FieldType& field = const_cast(*fieldIter()); @@ -99,7 +99,7 @@ void MapGeometricFields } } - forAllConstIter(typename HashTable, fields, fieldIter) + forAllConstIters(fields, fieldIter) { FieldType& field = const_cast(*fieldIter()); diff --git a/src/OpenFOAM/fields/cloud/mapClouds.H b/src/OpenFOAM/fields/cloud/mapClouds.H index 2fed56185c..523a4afe71 100644 --- a/src/OpenFOAM/fields/cloud/mapClouds.H +++ b/src/OpenFOAM/fields/cloud/mapClouds.H @@ -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 clouds(db.lookupClass()); - forAllIter(HashTable, clouds, iter) + forAllIters(clouds, iter) { cloud& c = const_cast(*iter()); diff --git a/src/OpenFOAM/global/fileOperations/collatedFileOperation/OFstreamCollator.C b/src/OpenFOAM/global/fileOperations/collatedFileOperation/OFstreamCollator.C index 20004e5a2b..9c1d0ab8d8 100644 --- a/src/OpenFOAM/global/fileOperations/collatedFileOperation/OFstreamCollator.C +++ b/src/OpenFOAM/global/fileOperations/collatedFileOperation/OFstreamCollator.C @@ -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 guard(mutex_); - forAllConstIter(FIFOStack, objects_, iter) + forAllConstIters(objects_, iter) { totalSize += iter()->size(); } diff --git a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C index 31348a2b4a..21479775f5 100644 --- a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C +++ b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C @@ -258,12 +258,11 @@ Foam::fileOperation::lookupAndCacheProcessorsPath { const fileName procPath(path/pDir); - HashTable::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 diff --git a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C index d52982b1f6..f339bfe5fd 100644 --- a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C +++ b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C @@ -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::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::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::const_iterator iter = times_.find(tm.path()); - if (iter != times_.end()) + const auto iter = times_.cfind(tm.path()); + if (iter.found()) { instantList& times = *iter(); diff --git a/src/OpenFOAM/graph/graph.C b/src/OpenFOAM/graph/graph.C index 3b519726ae..50af571e99 100644 --- a/src/OpenFOAM/graph/graph.C +++ b/src/OpenFOAM/graph/graph.C @@ -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(x_, nX, i0)); x_.transfer(xNew); - forAllIter(HashPtrTable, *this, iter) + forAllIters(*this, iter) { curve* c = iter(); scalarField cNew(SubField(*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]; } diff --git a/src/OpenFOAM/graph/writers/gnuplotGraph/gnuplotGraph.C b/src/OpenFOAM/graph/writers/gnuplotGraph/gnuplotGraph.C index ca32d35d69..5f4d50a948 100644 --- a/src/OpenFOAM/graph/writers/gnuplotGraph/gnuplotGraph.C +++ b/src/OpenFOAM/graph/writers/gnuplotGraph/gnuplotGraph.C @@ -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); diff --git a/src/OpenFOAM/graph/writers/jplotGraph/jplotGraph.C b/src/OpenFOAM/graph/writers/jplotGraph/jplotGraph.C index 8c131ccd5f..c5fd9572ae 100644 --- a/src/OpenFOAM/graph/writers/jplotGraph/jplotGraph.C +++ b/src/OpenFOAM/graph/writers/jplotGraph/jplotGraph.C @@ -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++; diff --git a/src/OpenFOAM/graph/writers/xmgrGraph/xmgrGraph.C b/src/OpenFOAM/graph/writers/xmgrGraph/xmgrGraph.C index 087298455e..e1c9d4254a 100644 --- a/src/OpenFOAM/graph/writers/xmgrGraph/xmgrGraph.C +++ b/src/OpenFOAM/graph/writers/xmgrGraph/xmgrGraph.C @@ -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 diff --git a/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/GAMGAgglomerations/GAMGAgglomeration/GAMGAgglomerateLduAddressing.C b/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/GAMGAgglomerations/GAMGAgglomeration/GAMGAgglomerateLduAddressing.C index e10b83464a..8a510dfd1f 100644 --- a/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/GAMGAgglomerations/GAMGAgglomeration/GAMGAgglomerateLduAddressing.C +++ b/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/GAMGAgglomerations/GAMGAgglomeration/GAMGAgglomerateLduAddressing.C @@ -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