From 64f795ed51d0046d43aaa8ac24b3116d32163443 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 7 Jan 2019 09:20:51 +0100
Subject: [PATCH] ENH: for-range, forAllIters() ... in sampling/, surfMesh/

- reduced clutter when iterating over containers
---
 .../meshToMesh/meshToMeshParallelOps.C        |  9 ++--
 .../UnsortedMeshedSurface.C                   | 53 +++++++------------
 2 files changed, 23 insertions(+), 39 deletions(-)

diff --git a/src/sampling/meshToMesh/meshToMeshParallelOps.C b/src/sampling/meshToMesh/meshToMeshParallelOps.C
index 417ecd458e8..c340b14c5a2 100644
--- a/src/sampling/meshToMesh/meshToMeshParallelOps.C
+++ b/src/sampling/meshToMesh/meshToMeshParallelOps.C
@@ -638,10 +638,9 @@ void Foam::meshToMesh::distributeAndMergeCells
                 key[1] = max(proci, nbrProci[i]);
                 key[2] = localFacei[i];
 
-                procCoupleInfo::const_iterator fnd =
-                    procFaceToGlobalCell.find(key);
+                const auto fnd = procFaceToGlobalCell.cfind(key);
 
-                if (fnd == procFaceToGlobalCell.end())
+                if (!fnd.found())
                 {
                     procFaceToGlobalCell.insert(key, -1);
                 }
@@ -754,9 +753,9 @@ void Foam::meshToMesh::distributeAndMergeCells
                 key[1] = max(proci, nbrProci[i]);
                 key[2] = localFacei[i];
 
-                procCoupleInfo::iterator fnd = procFaceToGlobalCell.find(key);
+                auto fnd = procFaceToGlobalCell.find(key);
 
-                if (fnd != procFaceToGlobalCell.end())
+                if (fnd.found())
                 {
                     label tgtFacei = fnd();
                     if (tgtFacei == -1)
diff --git a/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.C b/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.C
index 2785f281321..1a70d09e11f 100644
--- a/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.C
+++ b/src/surfMesh/UnsortedMeshedSurface/UnsortedMeshedSurface.C
@@ -367,7 +367,7 @@ void Foam::UnsortedMeshedSurface<Face>::setZones
     {
         zoneToc_[zonei] = surfZoneIdentifier
         (
-            word("zone") + ::Foam::name(zonei),
+            "zone" + ::Foam::name(zonei),
             zonei
         );
 
@@ -477,53 +477,39 @@ Foam::surfZoneList Foam::UnsortedMeshedSurface<Face>::sortedZones
 
     // Step 1: get zone sizes and store (origId => zoneI)
     Map<label> lookup;
-    forAll(zoneIds_, facei)
+    for (const label origId : zoneIds_)
     {
-        const label origId = zoneIds_[facei];
-
-        Map<label>::iterator fnd = lookup.find(origId);
-        if (fnd != lookup.end())
-        {
-            fnd()++;
-        }
-        else
-        {
-            lookup.insert(origId, 1);
-        }
+        ++(lookup(origId, 0));
     }
 
     // Step 2: assign start/size (and name) to the newZones
     // re-use the lookup to map (zoneId => zoneI)
     surfZoneList zoneLst(lookup.size());
     label start = 0;
-    label zoneI = 0;
-    forAllIter(Map<label>, lookup, iter)
+    label zonei = 0;
+    forAllIters(lookup, iter)
     {
-        label origId = iter.key();
+        const label origId = iter.key();
 
-        word name;
-        Map<word>::const_iterator fnd = zoneNames.find(origId);
-        if (fnd != zoneNames.end())
-        {
-            name = fnd();
-        }
-        else
-        {
-            name = word("zone") + ::Foam::name(zoneI);
-        }
+        const word zoneName =
+            zoneNames.lookup
+            (
+                origId,
+                "zone" + ::Foam::name(zonei)  // default name
+            );
 
-        zoneLst[zoneI] = surfZone
+        zoneLst[zonei] = surfZone
         (
-            name,
+            zoneName,
             0,           // initialize with zero size
             start,
-            zoneI
+            zonei
         );
 
         // increment the start for the next zone
         // and save the (zoneId => zoneI) mapping
         start += iter();
-        iter() = zoneI++;
+        iter() = zonei++;
     }
 
 
@@ -532,7 +518,7 @@ Foam::surfZoneList Foam::UnsortedMeshedSurface<Face>::sortedZones
 
     forAll(zoneIds_, facei)
     {
-        label zonei = lookup[zoneIds_[facei]];
+        const label zonei = lookup[zoneIds_[facei]];
         faceMap[facei] = zoneLst[zonei].start() + zoneLst[zonei].size()++;
     }
 
@@ -575,10 +561,9 @@ Foam::UnsortedMeshedSurface<Face>::subsetMesh
         newFaces[facei] = Face(locFaces[origFacei]);
 
         // Renumber labels for face
-        Face& f = newFaces[facei];
-        forAll(f, fp)
+        for (label& pointi : newFaces[facei])
         {
-            f[fp] = oldToNew[f[fp]];
+            pointi = oldToNew[pointi];
         }
 
         newZones[facei] = zoneIds_[origFacei];
-- 
GitLab