From c2d46b77784cdd4a4321694e33c7d341fc9eb3c1 Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <github@naumov.de>
Date: Sat, 15 Jun 2019 18:58:15 +0200
Subject: [PATCH] Use STL algorithms: copy, transform, find_if.

Also use own findElementOrError() algorithm.
---
 .../FileIO/GocadIO/GocadSGridReader.cpp       |  6 ++----
 GeoLib/GEOObjects.cpp                         | 20 +++++++++----------
 MeshGeoToolsLib/GeoMapper.cpp                 |  6 ++----
 MeshLib/ElementStatus.cpp                     |  7 +++----
 MeshLib/MeshSearch/ElementSearch.cpp          |  7 ++++---
 5 files changed, 20 insertions(+), 26 deletions(-)

diff --git a/Applications/FileIO/GocadIO/GocadSGridReader.cpp b/Applications/FileIO/GocadIO/GocadSGridReader.cpp
index 5f8b7dd1384..91e70bad385 100644
--- a/Applications/FileIO/GocadIO/GocadSGridReader.cpp
+++ b/Applications/FileIO/GocadIO/GocadSGridReader.cpp
@@ -820,10 +820,8 @@ void GocadSGridReader::addFaceSetQuad(
         default:
             ERR("Could not create face for node with id %d.", id);
     }
-    for (auto quad_node : quad_nodes)
-    {
-        face_set_nodes.push_back(quad_node);
-    }
+    std::copy(begin(quad_nodes), end(quad_nodes),
+              back_inserter(face_set_nodes));
     face_set_elements.push_back(new MeshLib::Quad(quad_nodes));
 }
 
diff --git a/GeoLib/GEOObjects.cpp b/GeoLib/GEOObjects.cpp
index 23809f0e63b..9902bb96533 100644
--- a/GeoLib/GEOObjects.cpp
+++ b/GeoLib/GEOObjects.cpp
@@ -120,13 +120,14 @@ void GEOObjects::addStationVec(std::unique_ptr<std::vector<Point*>> stations,
 const std::vector<GeoLib::Point*>* GEOObjects::getStationVec(
     const std::string& name) const
 {
-    for (auto point : _pnt_vecs)
+    auto const it = std::find_if(
+        begin(_pnt_vecs), end(_pnt_vecs), [&name](PointVec const* const p) {
+            return p->getName() == name &&
+                   p->getType() == PointVec::PointType::STATION;
+        });
+    if (it != end(_pnt_vecs))
     {
-        if (point->getName() == name &&
-            point->getType() == PointVec::PointType::STATION)
-        {
-            return point->getVector();
-        }
+        return (*it)->getVector();
     }
     DBUG("GEOObjects::getStationVec() - No entry found with name '%s'.",
          name.c_str());
@@ -283,11 +284,8 @@ bool GEOObjects::appendSurfaceVec(const std::vector<Surface*>& surfaces,
 
     // the copy is needed because addSurfaceVec is passing it to SurfaceVec
     // ctor, which needs write access to the surface vector.
-    auto sfc = std::make_unique<std::vector<GeoLib::Surface*>>();
-    for (auto surface : surfaces)
-    {
-        sfc->push_back(surface);
-    }
+    auto sfc = std::make_unique<std::vector<GeoLib::Surface*>>(begin(surfaces),
+                                                               end(surfaces));
     addSurfaceVec(std::move(sfc), name);
     return false;
 }
diff --git a/MeshGeoToolsLib/GeoMapper.cpp b/MeshGeoToolsLib/GeoMapper.cpp
index 21ebfc6da48..f8bd85cdfef 100644
--- a/MeshGeoToolsLib/GeoMapper.cpp
+++ b/MeshGeoToolsLib/GeoMapper.cpp
@@ -285,10 +285,8 @@ static std::vector<MathLib::Point3d> computeElementSegmentIntersections(
             true};
         std::vector<MathLib::Point3d> const intersections(
             GeoLib::lineSegmentIntersect2d(segment, elem_segment));
-        for (auto const& p : intersections)
-        {
-            element_intersections.push_back(std::move(p));
-        }
+        element_intersections.insert(end(element_intersections),
+                                     begin(intersections), end(intersections));
     }
     return element_intersections;
 }
diff --git a/MeshLib/ElementStatus.cpp b/MeshLib/ElementStatus.cpp
index 29bdff5e67e..effdd6a0eed 100644
--- a/MeshLib/ElementStatus.cpp
+++ b/MeshLib/ElementStatus.cpp
@@ -26,10 +26,9 @@ ElementStatus::ElementStatus(Mesh const* const mesh, bool hasAnyInactive)
       _hasAnyInactive(hasAnyInactive)
 {
     const std::vector<MeshLib::Node*>& nodes(_mesh->getNodes());
-    for (auto node : nodes)
-    {
-        _active_nodes.push_back(node->getNumberOfElements());
-    }
+    std::transform(
+        begin(nodes), end(nodes), back_inserter(_active_nodes),
+        [](Node const* const n) { return n->getNumberOfElements(); });
 }
 
 ElementStatus::ElementStatus(Mesh const* const mesh,
diff --git a/MeshLib/MeshSearch/ElementSearch.cpp b/MeshLib/MeshSearch/ElementSearch.cpp
index 2fb71a06974..94627e1be43 100644
--- a/MeshLib/MeshSearch/ElementSearch.cpp
+++ b/MeshLib/MeshSearch/ElementSearch.cpp
@@ -80,9 +80,10 @@ std::size_t ElementSearch::searchByNodeIDs(const std::vector<std::size_t> &nodes
     std::vector<std::size_t> connected_elements;
     for (std::size_t node_id : nodes)
     {
-        for (auto* e : _mesh.getNode(node_id)->getElements()) {
-            connected_elements.push_back(e->getID());
-        }
+        auto const& elements = _mesh.getNode(node_id)->getElements();
+        std::transform(begin(elements), end(elements),
+                       back_inserter(connected_elements),
+                       [](Element const* const e) { return e->getID(); });
     }
 
     BaseLib::makeVectorUnique(connected_elements);
-- 
GitLab