From 8728e8353f34dc9ce8d073951d031f67e5111d71 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 10 May 2017 13:44:27 +0200 Subject: [PATCH] STYLE: avoid explicit use of 'word' as HashTable template parameter - less clutter and typing to use the default template parameter when the key is 'word' anyhow. - use EdgeMap instead of the longhand HashTable version where appropriate --- .../ideasUnvToFoam/ideasUnvToFoam.C | 4 +- .../vtkPVFoam/vtkPVFoamUpdateInfo.C | 25 ++++-------- doc/codingStyleGuide.org | 38 +++++++++++++------ .../Identifiers/patch/coupleGroupIdentifier.C | 6 +-- .../polyBoundaryMesh/polyBoundaryMesh.C | 32 +++++++--------- .../polyBoundaryMesh/polyBoundaryMesh.H | 6 +-- .../primitives/strings/lists/hashedWordList.H | 4 +- .../strings/lists/hashedWordListI.H | 2 +- .../boundaryCutter/boundaryCutter.C | 13 ++----- .../boundaryCutter/boundaryCutter.H | 7 ++-- .../meshCutAndRemove/meshCutAndRemove.C | 25 +++++------- .../meshCutAndRemove/meshCutAndRemove.H | 5 ++- .../meshModifiers/meshCutter/meshCutter.C | 25 +++++------- .../meshModifiers/meshCutter/meshCutter.H | 5 ++- 14 files changed, 87 insertions(+), 110 deletions(-) diff --git a/applications/utilities/mesh/conversion/ideasUnvToFoam/ideasUnvToFoam.C b/applications/utilities/mesh/conversion/ideasUnvToFoam/ideasUnvToFoam.C index 9239b81a5d..d66e43344b 100644 --- a/applications/utilities/mesh/conversion/ideasUnvToFoam/ideasUnvToFoam.C +++ b/applications/utilities/mesh/conversion/ideasUnvToFoam/ideasUnvToFoam.C @@ -924,8 +924,8 @@ int main(int argc, char *argv[]) } } - HashTable cellZones; - HashTable faceZones; + HashTable cellZones; + HashTable faceZones; List isAPatch(patchNames.size(),true); if (dofVertIndices.size()) diff --git a/applications/utilities/postProcessing/graphics/PVReaders/PVFoamReader/vtkPVFoam/vtkPVFoamUpdateInfo.C b/applications/utilities/postProcessing/graphics/PVReaders/PVFoamReader/vtkPVFoam/vtkPVFoamUpdateInfo.C index 41855ddd7b..52030e9c81 100644 --- a/applications/utilities/postProcessing/graphics/PVReaders/PVFoamReader/vtkPVFoam/vtkPVFoamUpdateInfo.C +++ b/applications/utilities/postProcessing/graphics/PVReaders/PVFoamReader/vtkPVFoam/vtkPVFoamUpdateInfo.C @@ -233,21 +233,16 @@ void Foam::vtkPVFoam::updateInfoPatches if (meshPtr_) { const polyBoundaryMesh& patches = meshPtr_->boundaryMesh(); - const HashTable& groups = patches.groupPatchIDs(); + const HashTable& groups = patches.groupPatchIDs(); const wordList allPatchNames = patches.names(); // Add patch groups // ~~~~~~~~~~~~~~~~ - for - ( - HashTable::const_iterator iter = groups.begin(); - iter != groups.end(); - ++iter - ) + forAllConstIters(groups, iter) { const word& groupName = iter.key(); - const labelList& patchIDs = iter(); + const labelList& patchIDs = iter.object(); label nFaces = 0; forAll(patchIDs, i) @@ -349,7 +344,7 @@ void Foam::vtkPVFoam::updateInfoPatches // Add (non-zero) patch groups to the list of mesh parts // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - HashTable groups(patchEntries.size()); + HashTable groups(patchEntries.size()); forAll(patchEntries, patchi) { @@ -360,7 +355,7 @@ void Foam::vtkPVFoam::updateInfoPatches forAll(groupNames, groupI) { - HashTable::iterator iter = groups.find + HashTable::iterator iter = groups.find ( groupNames[groupI] ); @@ -375,16 +370,10 @@ void Foam::vtkPVFoam::updateInfoPatches } } - for - ( - HashTable::const_iterator iter = - groups.begin(); - iter != groups.end(); - ++iter - ) + forAllConstIters(groups, iter) { const word& groupName = iter.key(); - const labelList& patchIDs = iter(); + const labelList& patchIDs = iter.object(); label nFaces = 0; forAll(patchIDs, i) diff --git a/doc/codingStyleGuide.org b/doc/codingStyleGuide.org index 3cde03d85d..b8d1b0d98a 100644 --- a/doc/codingStyleGuide.org +++ b/doc/codingStyleGuide.org @@ -22,7 +22,7 @@ + Stream output + =<<= is always four characters after the start of the stream, so that the =<<= symbols align, i.e. - #+begin_src c++ + #+begin_src C++ Info<< ... os << ... #+end_src @@ -215,7 +215,7 @@ *** =for= and =while= Loops #+begin_src C++ - for (i = 0; i < maxI; i++) + for (i = 0; i < maxI; ++i) { code; } @@ -226,7 +226,7 @@ ( i = 0; i < maxI; - i++ + ++i ) { code; @@ -234,15 +234,22 @@ #+end_src *not* this (no space between =for= and =(= used) #+begin_src C++ - for(i = 0; i < maxI; i++) + for(i = 0; i < maxI; ++i) { code; } #+end_src - Note that when indexing through iterators, it is often slightly more - efficient to use the pre-increment form. Eg, =++iter= instead of =iter++= + Range-base for should have a space surrounding the ':' + #+begin_src C++ + for (auto i : range) + { + code; + } + #+end_src + Note that when indexing through iterators, it is often more efficient + to use the pre-increment form. Eg, =++iter= instead of =iter++= -*** =forAll=, =forAllIter=, =forAllConstIter=, /etc./ loops +*** =forAll=, =forAllIters=, =forAllConstIters=, /etc./ loops Like =for= loops, but #+begin_src C++ forAll( @@ -251,15 +258,22 @@ #+begin_src C++ forAll ( #+end_src - Using the =forAllIter= and =forAllConstIter= macros is generally - advantageous - less typing, easier to find later. However, since - they are macros, they will fail if the iterated object contains - any commas /e.g./ following will FAIL!: + In many cases, the new =forAllIters= and =forAllConstIters= macros + provide a good means of cycling through iterators (when a range-base + for doesn't apply). These use the C++11 decltype and work without + explicitly specifying the container class: #+begin_src C++ - forAllIter(HashTable>, foo, iter) + forAllIters(myEdgeHash, iter) + #+end_src + Using the older =forAllIter= and =forAllConstIter= macros will + still be seen. However, since they are macros, they will fail if + the iterated object contains any commas /e.g./ following will FAIL!: + #+begin_src C++ + forAllIter(HashTable>, myEdgeHash, iter) #+end_src These convenience macros are also generally avoided in other container classes and OpenFOAM primitive classes. + In certain cases, the range-based for can also be used. *** Splitting Over Multiple Lines ***** Splitting return type and function name diff --git a/src/OpenFOAM/meshes/Identifiers/patch/coupleGroupIdentifier.C b/src/OpenFOAM/meshes/Identifiers/patch/coupleGroupIdentifier.C index bf01fe7be9..9b7fc8a548 100644 --- a/src/OpenFOAM/meshes/Identifiers/patch/coupleGroupIdentifier.C +++ b/src/OpenFOAM/meshes/Identifiers/patch/coupleGroupIdentifier.C @@ -46,10 +46,10 @@ Foam::label Foam::coupleGroupIdentifier::findOtherPatchID << exit(FatalError); } - HashTable::const_iterator fnd = + HashTable::const_iterator fnd = pbm.groupPatchIDs().find(name()); - if (fnd == pbm.groupPatchIDs().end()) + if (!fnd.found()) { if (&mesh == &thisPatch.boundaryMesh().mesh()) { @@ -65,7 +65,7 @@ Foam::label Foam::coupleGroupIdentifier::findOtherPatchID } // Mesh has patch group - const labelList& patchIDs = fnd(); + const labelList& patchIDs = fnd.object(); if (&mesh == &thisPatch.boundaryMesh().mesh()) { diff --git a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C index edb37c727b..f7068f4249 100644 --- a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C +++ b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C @@ -32,6 +32,7 @@ License #include "lduSchedule.H" #include "globalMeshData.H" #include "stringListOps.H" +#include "EdgeMap.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -287,7 +288,7 @@ Foam::polyBoundaryMesh::neighbourEdges() const // From mesh edge (expressed as a point pair so as not to construct // point addressing) to patch + relative edge index. - HashTable> pointsToEdge(nEdgePairs); + EdgeMap pointsToEdge(nEdgePairs); forAll(*this, patchi) { @@ -308,10 +309,9 @@ Foam::polyBoundaryMesh::neighbourEdges() const // Edge in mesh points. edge meshEdge(pp.meshPoints()[e[0]], pp.meshPoints()[e[1]]); - HashTable>::iterator fnd = - pointsToEdge.find(meshEdge); + EdgeMap::iterator fnd = pointsToEdge.find(meshEdge); - if (fnd == pointsToEdge.end()) + if (!fnd.found()) { // First occurrence of mesh edge. Store patch and my // local index. @@ -328,7 +328,7 @@ Foam::polyBoundaryMesh::neighbourEdges() const else { // Second occurrence. Store. - const labelPair& edgeInfo = fnd(); + const labelPair& edgeInfo = fnd.object(); neighbourEdges[patchi][edgei - pp.nInternalEdges()] = edgeInfo; @@ -413,13 +413,13 @@ const Foam::labelList& Foam::polyBoundaryMesh::patchID() const } -const Foam::HashTable& +const Foam::HashTable& Foam::polyBoundaryMesh::groupPatchIDs() const { if (!groupPatchIDsPtr_.valid()) { - groupPatchIDsPtr_.reset(new HashTable(10)); - HashTable& groupPatchIDs = groupPatchIDsPtr_(); + groupPatchIDsPtr_.reset(new HashTable(10)); + HashTable& groupPatchIDs = groupPatchIDsPtr_(); const polyBoundaryMesh& bm = *this; @@ -431,7 +431,7 @@ Foam::polyBoundaryMesh::groupPatchIDs() const { const word& name = groups[i]; - HashTable::iterator iter = groupPatchIDs.find + HashTable::iterator iter = groupPatchIDs.find ( name ); @@ -612,10 +612,10 @@ Foam::labelList Foam::polyBoundaryMesh::findIndices if (usePatchGroups && groupPatchIDs().size()) { - const HashTable::const_iterator iter = + const HashTable::const_iterator iter = groupPatchIDs().find(key); - if (iter != groupPatchIDs().end()) + if (iter.found()) { labelHashSet indexSet(indices); @@ -815,14 +815,8 @@ void Foam::polyBoundaryMesh::matchGroups // Current set of unmatched patches nonGroupPatches = labelHashSet(patchIDs); - const HashTable& groupPatchIDs = this->groupPatchIDs(); - for - ( - HashTable::const_iterator iter = - groupPatchIDs.begin(); - iter != groupPatchIDs.end(); - ++iter - ) + const HashTable& groupPatchIDs = this->groupPatchIDs(); + forAllConstIters(groupPatchIDs, iter) { // Store currently unmatched patches so we can restore labelHashSet oldNonGroupPatches(nonGroupPatches); diff --git a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.H b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.H index 114fc23168..6193b98f45 100644 --- a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.H +++ b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.H @@ -70,7 +70,7 @@ class polyBoundaryMesh mutable autoPtr patchIDPtr_; - mutable autoPtr> groupPatchIDsPtr_; + mutable autoPtr> groupPatchIDsPtr_; //- Edges of neighbouring patches mutable autoPtr> neighbourEdgesPtr_; @@ -183,8 +183,8 @@ public: //- Per boundary face label the patch index const labelList& patchID() const; - //- Per patch group the patch indices - const HashTable& groupPatchIDs() const; + //- The patch indices per patch group + const HashTable& groupPatchIDs() const; //- Set/add group with patches void setGroup(const word& groupName, const labelList& patchIDs); diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H index 733d012b92..9ecab0f382 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H @@ -62,7 +62,7 @@ class hashedWordList // Private data //- Hash of words/indices - mutable HashTable indices_; + mutable HashTable