From 55ec75a1f6bb065fc2dcf485670f4e74c559db11 Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <github@naumov.de>
Date: Fri, 17 Jun 2016 02:35:34 +0000
Subject: [PATCH] Use const and references.

This avoids unnecessary copy initialization
and value parameters.
---
 .../Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp   | 2 +-
 Applications/Utils/MeshEdit/moveMeshNodes.cpp             | 2 +-
 Applications/Utils/MeshEdit/removeMeshElements.cpp        | 2 +-
 GeoLib/MinimalBoundingSphere.cpp                          | 2 +-
 GeoLib/Polygon.cpp                                        | 6 +++---
 GeoLib/Polygon.h                                          | 7 ++++---
 MathLib/LinAlg/Eigen/EigenLinearSolver.cpp                | 2 +-
 MathLib/LinAlg/Eigen/EigenLinearSolver.h                  | 2 +-
 MeshGeoToolsLib/GeoMapper.cpp                             | 2 +-
 MeshLib/Mesh.cpp                                          | 4 ++--
 MeshLib/MeshEditing/FlipElements.cpp                      | 2 +-
 MeshLib/MeshGenerators/MeshGenerator.cpp                  | 2 +-
 MeshLib/MeshGenerators/MeshGenerator.h                    | 2 +-
 ProcessLib/Output.cpp                                     | 2 +-
 ProcessLib/Output.h                                       | 2 +-
 Tests/GeoLib/TestOctTree.cpp                              | 8 ++++----
 Tests/GeoLib/TestSurfaceIsPointInSurface.cpp              | 2 +-
 Tests/MathLib/TestPoint3d.cpp                             | 6 +++---
 Tests/MathLib/TestPoint3dWithID.cpp                       | 2 +-
 Tests/MeshLib/TestUniqueMeshId.cpp                        | 2 +-
 Tests/NumLib/LocalToGlobalIndexMapMultiComponent.cpp      | 4 ++--
 Tests/NumLib/TestTimeStep.cpp                             | 4 ++--
 22 files changed, 35 insertions(+), 34 deletions(-)

diff --git a/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp b/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp
index 536db8ada0f..a801c0f3db2 100644
--- a/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp
+++ b/Applications/Utils/MeshEdit/ResetPropertiesInPolygonalRegion.cpp
@@ -197,7 +197,7 @@ int main (int argc, char* argv[])
     std::vector<std::string> property_names(
         mesh->getProperties().getPropertyVectorNames());
     INFO("Mesh contains %d property vectors:", property_names.size());
-    for (auto name : property_names) {
+    for (const auto& name : property_names) {
         INFO("- %s", name.c_str());
     }
     std::string const& property_name(property_name_arg.getValue());
diff --git a/Applications/Utils/MeshEdit/moveMeshNodes.cpp b/Applications/Utils/MeshEdit/moveMeshNodes.cpp
index f2795dc720d..5eedb9ad389 100644
--- a/Applications/Utils/MeshEdit/moveMeshNodes.cpp
+++ b/Applications/Utils/MeshEdit/moveMeshNodes.cpp
@@ -155,7 +155,7 @@ int main (int argc, char* argv[])
         //double max_dist (25.0);    // squared maximum distance at which reference points are used
         double offset (0.0); // additional offset for elevation (should be 0)
         MeshLib::Mesh* ground_truth (MeshLib::IO::readMeshFromFile(value));
-        const std::vector<MeshLib::Node*> ground_truth_nodes (ground_truth->getNodes());
+        const std::vector<MeshLib::Node*>& ground_truth_nodes (ground_truth->getNodes());
         GeoLib::AABB bounding_box(ground_truth_nodes.begin(), ground_truth_nodes.end());
         MathLib::Point3d const& min(bounding_box.getMinPoint());
         MathLib::Point3d const& max(bounding_box.getMaxPoint());
diff --git a/Applications/Utils/MeshEdit/removeMeshElements.cpp b/Applications/Utils/MeshEdit/removeMeshElements.cpp
index d83277e46e6..60e427f975b 100644
--- a/Applications/Utils/MeshEdit/removeMeshElements.cpp
+++ b/Applications/Utils/MeshEdit/removeMeshElements.cpp
@@ -109,7 +109,7 @@ int main (int argc, char* argv[])
     }
     if (eleTypeArg.isSet()) {
         const std::vector<std::string> eleTypeNames = eleTypeArg.getValue();
-        for (auto typeName : eleTypeNames) {
+        for (const auto& typeName : eleTypeNames) {
             const MeshLib::MeshElemType type = MeshLib::String2MeshElemType(typeName);
             if (type == MeshLib::MeshElemType::INVALID) continue;
             const std::size_t n_removed_elements = searcher.searchByElementType(type);
diff --git a/GeoLib/MinimalBoundingSphere.cpp b/GeoLib/MinimalBoundingSphere.cpp
index 2482792c8ad..e670e21c784 100644
--- a/GeoLib/MinimalBoundingSphere.cpp
+++ b/GeoLib/MinimalBoundingSphere.cpp
@@ -128,7 +128,7 @@ MinimalBoundingSphere::MinimalBoundingSphere(
     std::vector<MathLib::Point3d*> const& points)
 : _radius(-1), _center(0,0,0)
 {
-    std::vector<MathLib::Point3d*> sphere_points(points);
+    const std::vector<MathLib::Point3d*>& sphere_points(points);
     MinimalBoundingSphere const bounding_sphere = recurseCalculation(sphere_points, 0, sphere_points.size(), 0);
     _center = bounding_sphere.getCenter();
     _radius = bounding_sphere.getRadius();
diff --git a/GeoLib/Polygon.cpp b/GeoLib/Polygon.cpp
index 53abbc860d8..8bd0f84dd3f 100644
--- a/GeoLib/Polygon.cpp
+++ b/GeoLib/Polygon.cpp
@@ -359,10 +359,10 @@ void Polygon::ensureCWOrientation ()
 
 #if __GNUC__ <= 4 && (__GNUC_MINOR__ < 9)
 void Polygon::splitPolygonAtIntersection(
-    std::list<Polygon*>::iterator polygon_it)
+    const std::list<Polygon*>::iterator& polygon_it)
 #else
 void Polygon::splitPolygonAtIntersection(
-    std::list<Polygon*>::const_iterator polygon_it)
+    const std::list<Polygon*>::const_iterator& polygon_it)
 #endif
 {
     GeoLib::Polyline::SegmentIterator seg_it0((*polygon_it)->begin());
@@ -409,7 +409,7 @@ void Polygon::splitPolygonAtIntersection(
     splitPolygonAtIntersection(polygon1_it);
 }
 
-void Polygon::splitPolygonAtPoint (std::list<GeoLib::Polygon*>::iterator polygon_it)
+void Polygon::splitPolygonAtPoint (const std::list<GeoLib::Polygon*>::iterator& polygon_it)
 {
     std::size_t n((*polygon_it)->getNumberOfPoints() - 1), idx0(0), idx1(0);
     std::vector<std::size_t> id_vec(n);
diff --git a/GeoLib/Polygon.h b/GeoLib/Polygon.h
index 8ffdb024128..3f3f49ba333 100644
--- a/GeoLib/Polygon.h
+++ b/GeoLib/Polygon.h
@@ -134,12 +134,13 @@ private:
     void ensureCWOrientation ();
 
 #if __GNUC__ <= 4 && (__GNUC_MINOR__ < 9)
-    void splitPolygonAtIntersection(std::list<Polygon*>::iterator polygon_it);
+    void splitPolygonAtIntersection(
+        const std::list<Polygon*>::iterator& polygon_it);
 #else
     void splitPolygonAtIntersection(
-        std::list<Polygon*>::const_iterator polygon_it);
+        const std::list<Polygon*>::const_iterator& polygon_it);
 #endif
-    void splitPolygonAtPoint (std::list<Polygon*>::iterator polygon_it);
+    void splitPolygonAtPoint (const std::list<Polygon*>::iterator& polygon_it);
     std::list<Polygon*> _simple_polygon_list;
     AABB _aabb;
 };
diff --git a/MathLib/LinAlg/Eigen/EigenLinearSolver.cpp b/MathLib/LinAlg/Eigen/EigenLinearSolver.cpp
index 05e5206689d..3d05dfbdbb1 100644
--- a/MathLib/LinAlg/Eigen/EigenLinearSolver.cpp
+++ b/MathLib/LinAlg/Eigen/EigenLinearSolver.cpp
@@ -105,7 +105,7 @@ private:
 } // details
 
 EigenLinearSolver::EigenLinearSolver(
-                            const std::string /*solver_name*/,
+                            const std::string& /*solver_name*/,
                             const BaseLib::ConfigTree* const option)
 {
     using Matrix = EigenMatrix::RawMatrixType;
diff --git a/MathLib/LinAlg/Eigen/EigenLinearSolver.h b/MathLib/LinAlg/Eigen/EigenLinearSolver.h
index 011a8d7a854..affab4c694d 100644
--- a/MathLib/LinAlg/Eigen/EigenLinearSolver.h
+++ b/MathLib/LinAlg/Eigen/EigenLinearSolver.h
@@ -36,7 +36,7 @@ public:
      *                    this argument, default settings follow those of
      *                    LisOption struct.
      */
-    EigenLinearSolver(const std::string solver_name,
+    EigenLinearSolver(const std::string& solver_name,
                       BaseLib::ConfigTree const*const option);
 
     ~EigenLinearSolver();
diff --git a/MeshGeoToolsLib/GeoMapper.cpp b/MeshGeoToolsLib/GeoMapper.cpp
index e02d68ff08e..7d20f1e0dfa 100644
--- a/MeshGeoToolsLib/GeoMapper.cpp
+++ b/MeshGeoToolsLib/GeoMapper.cpp
@@ -313,7 +313,7 @@ void GeoMapper::advancedMapOnMesh(
             const GeoLib::Point* geo_point (ply->getPoint(node_index_in_ply));
 
             // check if line segments connected to closest geo point intersect connected elements of current node
-            const std::vector<MeshLib::Element*> elements (node->getElements());
+            const std::vector<MeshLib::Element*>& elements (node->getElements());
             const std::size_t nElems = elements.size();
             for (std::size_t e=0; e<nElems; ++e)
             {
diff --git a/MeshLib/Mesh.cpp b/MeshLib/Mesh.cpp
index 0fc3476513c..f1308e6e3a2 100644
--- a/MeshLib/Mesh.cpp
+++ b/MeshLib/Mesh.cpp
@@ -59,12 +59,12 @@ Mesh::Mesh(const Mesh &mesh)
       _n_base_nodes(mesh.getNumberOfBaseNodes()),
       _properties(mesh._properties)
 {
-    const std::vector<Node*> nodes (mesh.getNodes());
+    const std::vector<Node*>& nodes (mesh.getNodes());
     const std::size_t nNodes (nodes.size());
     for (unsigned i=0; i<nNodes; ++i)
         _nodes[i] = new Node(*nodes[i]);
 
-    const std::vector<Element*> elements (mesh.getElements());
+    const std::vector<Element*>& elements (mesh.getElements());
     const std::size_t nElements (elements.size());
     for (unsigned i=0; i<nElements; ++i)
     {
diff --git a/MeshLib/MeshEditing/FlipElements.cpp b/MeshLib/MeshEditing/FlipElements.cpp
index 0d3cc50ef1f..828e5183849 100644
--- a/MeshLib/MeshEditing/FlipElements.cpp
+++ b/MeshLib/MeshEditing/FlipElements.cpp
@@ -56,7 +56,7 @@ std::unique_ptr<MeshLib::Mesh> createFlippedMesh(MeshLib::Mesh const& mesh)
     for (std::size_t i=0; i<n_elems; ++i)
         new_elems.push_back(createFlippedElement(*elems[i], new_nodes).release());
 
-    MeshLib::Properties new_props (mesh.getProperties());
+    const MeshLib::Properties& new_props (mesh.getProperties());
     return std::unique_ptr<MeshLib::Mesh>(new MeshLib::Mesh("FlippedElementMesh", new_nodes, new_elems, new_props));
 }
 
diff --git a/MeshLib/MeshGenerators/MeshGenerator.cpp b/MeshLib/MeshGenerators/MeshGenerator.cpp
index 4843e1f6bf5..388775d83ef 100644
--- a/MeshLib/MeshGenerators/MeshGenerator.cpp
+++ b/MeshLib/MeshGenerators/MeshGenerator.cpp
@@ -464,7 +464,7 @@ MeshLib::Mesh*
 MeshGenerator::createSurfaceMesh(std::string const& mesh_name,
     MathLib::Point3d const& ll, MathLib::Point3d const& ur,
     std::array<std::size_t, 2> const& n_steps,
-    std::function<double(double,double)> f)
+    const std::function<double(double,double)>& f)
 {
     std::array<double, 2> step_size{{
         (ur[0]-ll[0])/(n_steps[0]-1), (ur[1]-ll[1])/(n_steps[1]-1)}};
diff --git a/MeshLib/MeshGenerators/MeshGenerator.h b/MeshLib/MeshGenerators/MeshGenerator.h
index ef14651aee3..a08a747e427 100644
--- a/MeshLib/MeshGenerators/MeshGenerator.h
+++ b/MeshLib/MeshGenerators/MeshGenerator.h
@@ -429,7 +429,7 @@ MeshLib::Mesh*
 createSurfaceMesh(std::string const& mesh_name,
     MathLib::Point3d const& ll, MathLib::Point3d const& ur,
     std::array<std::size_t, 2> const& n_steps,
-    std::function<double(double,double)> f);
+    const std::function<double(double,double)>& f);
 
 }  //MeshGenerator
 } //MeshLib
diff --git a/ProcessLib/Output.cpp b/ProcessLib/Output.cpp
index 65d1a30a2c8..0870c68eb56 100644
--- a/ProcessLib/Output.cpp
+++ b/ProcessLib/Output.cpp
@@ -81,7 +81,7 @@ newInstance(const BaseLib::ConfigTree &config, std::string const& output_directo
 }
 
 void Output::
-initialize(Output::ProcessIter first, Output::ProcessIter last)
+initialize(Output::ProcessIter first, const Output::ProcessIter& last)
 {
     for (unsigned pcs_idx = 0; first != last; ++first, ++pcs_idx)
     {
diff --git a/ProcessLib/Output.h b/ProcessLib/Output.h
index cda6c91c133..711c8f0c4a2 100644
--- a/ProcessLib/Output.h
+++ b/ProcessLib/Output.h
@@ -33,7 +33,7 @@ public:
                         ::const_iterator;
 
     //! Opens a PVD file for each process.
-    void initialize(ProcessIter first, ProcessIter last);
+    void initialize(ProcessIter first, const ProcessIter& last);
 
     //! Writes output for the given \c process if it should be written in the
     //! given \c timestep.
diff --git a/Tests/GeoLib/TestOctTree.cpp b/Tests/GeoLib/TestOctTree.cpp
index b6608d3df59..e65d998688d 100644
--- a/Tests/GeoLib/TestOctTree.cpp
+++ b/Tests/GeoLib/TestOctTree.cpp
@@ -258,8 +258,8 @@ TEST_F(GeoLibOctTree, TestWithAlternatingPoints3d)
     ps_ptr.push_back(new GeoLib::Point(5*small_displacement,1,0,6));
 
     GeoLib::AABB const aabb(ps_ptr.cbegin(), ps_ptr.cend());
-    MathLib::Point3d min(aabb.getMinPoint());
-    MathLib::Point3d max(aabb.getMaxPoint());
+    const MathLib::Point3d& min(aabb.getMinPoint());
+    const MathLib::Point3d& max(aabb.getMaxPoint());
     std::unique_ptr<GeoLib::OctTree<GeoLib::Point, 8>> oct_tree(
         GeoLib::OctTree<GeoLib::Point, 8>::createOctTree(min, max, eps));
 
@@ -309,8 +309,8 @@ TEST_F(GeoLibOctTree, TestSmallDistanceDifferentLeaves)
 
     // create OctTree
     GeoLib::AABB const aabb(ps_ptr.cbegin(), ps_ptr.cend());
-    MathLib::Point3d min(aabb.getMinPoint());
-    MathLib::Point3d max(aabb.getMaxPoint());
+    const MathLib::Point3d& min(aabb.getMinPoint());
+    const MathLib::Point3d& max(aabb.getMaxPoint());
     std::unique_ptr<GeoLib::OctTree<GeoLib::Point, 2>> oct_tree(
         GeoLib::OctTree<GeoLib::Point, 2>::createOctTree(min, max, eps));
 
diff --git a/Tests/GeoLib/TestSurfaceIsPointInSurface.cpp b/Tests/GeoLib/TestSurfaceIsPointInSurface.cpp
index 81f32b6afc8..37b548923ed 100644
--- a/Tests/GeoLib/TestSurfaceIsPointInSurface.cpp
+++ b/Tests/GeoLib/TestSurfaceIsPointInSurface.cpp
@@ -80,7 +80,7 @@ TEST(GeoLib, SurfaceIsPointInSurface)
     surface_functions.push_back(constant);
     surface_functions.push_back(coscos);
 
-    for (auto f : surface_functions) {
+    for (const auto& f : surface_functions) {
         std::random_device rd;
 
         std::string name("Surface");
diff --git a/Tests/MathLib/TestPoint3d.cpp b/Tests/MathLib/TestPoint3d.cpp
index 6706c65a2fa..b2428ae4c1a 100644
--- a/Tests/MathLib/TestPoint3d.cpp
+++ b/Tests/MathLib/TestPoint3d.cpp
@@ -37,7 +37,7 @@ TEST_F(MathLibPoint3d, ComparisonOperatorLessEqSamePoint)
     // A point is always less or equal to itself and its copy.
     auto samePointLessEqualCompare = [](MathLib::Point3d const& p)
     {
-        auto q = p;
+        const auto& q = p;
         return lessEq(p, p) && lessEq(p, q) && lessEq(q, p);
     };
 
@@ -104,7 +104,7 @@ TEST_F(MathLibPoint3d, ComparisonOperatorEqualSamePoint)
     // A point is always equal to itself and its copy.
     auto samePointEqualCompare = [](MathLib::Point3d const& p)
     {
-        auto q = p;
+        const auto& q = p;
         return (p == p) && (p == q) && (q == p);
     };
 
@@ -170,7 +170,7 @@ TEST_F(MathLibPoint3d, ComparisonOperatorLessSamePoint)
     // A point is never less than itself or its copy.
     auto samePointLessCompare = [](MathLib::Point3d const& p)
     {
-        auto q = p;
+        const auto& q = p;
         return !(p < p) && !(p < q) && !(q < p);
     };
 
diff --git a/Tests/MathLib/TestPoint3dWithID.cpp b/Tests/MathLib/TestPoint3dWithID.cpp
index 6f1533d6c09..b9af8f0a813 100644
--- a/Tests/MathLib/TestPoint3dWithID.cpp
+++ b/Tests/MathLib/TestPoint3dWithID.cpp
@@ -20,7 +20,7 @@ using namespace MathLib;
 TEST(MathLib, Point3dWithID)
 {
     Point3dWithID p0(0,0,0,1);
-    Point3dWithID p1(p0); // copy constructor
+    const Point3dWithID& p1(p0); // copy constructor
     Point3dWithID p2(p0, 2); // constructor for resetting the id
 
     EXPECT_EQ(p0.getID(), p1.getID());
diff --git a/Tests/MeshLib/TestUniqueMeshId.cpp b/Tests/MeshLib/TestUniqueMeshId.cpp
index 96fd77bde6b..73666e37bd4 100644
--- a/Tests/MeshLib/TestUniqueMeshId.cpp
+++ b/Tests/MeshLib/TestUniqueMeshId.cpp
@@ -36,7 +36,7 @@ TEST(MeshLib, UniqueMeshId)
     ASSERT_EQ(counter_value + std::size_t(3), m3.getID());
 
     // Copy mesh keeps also increments the counter.
-    Mesh m4(m0);
+    Mesh m4 = m0;
     ASSERT_EQ(counter_value + std::size_t(4), m4.getID());
 
 }
diff --git a/Tests/NumLib/LocalToGlobalIndexMapMultiComponent.cpp b/Tests/NumLib/LocalToGlobalIndexMapMultiComponent.cpp
index 09a4b10946b..aa4fdc15b21 100644
--- a/Tests/NumLib/LocalToGlobalIndexMapMultiComponent.cpp
+++ b/Tests/NumLib/LocalToGlobalIndexMapMultiComponent.cpp
@@ -103,7 +103,7 @@ public:
 
     template <AL::ComponentOrder order>
     void test(const unsigned num_components, const unsigned selected_component,
-              std::function<std::size_t(std::size_t, std::size_t)> const
+              std::function<std::size_t(std::size_t, std::size_t)> const&
                   compute_global_index);
 
     std::unique_ptr<const MeshLib::Mesh> mesh;
@@ -146,7 +146,7 @@ template <AL::ComponentOrder ComponentOrder>
 void NumLibLocalToGlobalIndexMapMultiDOFTest::test(
     const unsigned num_components,
     const unsigned selected_component,
-    std::function<std::size_t(std::size_t, std::size_t)> const
+    std::function<std::size_t(std::size_t, std::size_t)> const&
         compute_global_index)
 {
     initComponents(num_components, selected_component, ComponentOrder);
diff --git a/Tests/NumLib/TestTimeStep.cpp b/Tests/NumLib/TestTimeStep.cpp
index 447e3db4d2a..acb6d179af5 100644
--- a/Tests/NumLib/TestTimeStep.cpp
+++ b/Tests/NumLib/TestTimeStep.cpp
@@ -29,13 +29,13 @@ TEST(NumLib, TimeStep)
     ASSERT_EQ(1u, t2.steps());
 
     // copy
-    NumLib::TimeStep t3(t2);
+    const NumLib::TimeStep& t3(t2);
     ASSERT_EQ(1., t3.current());
     ASSERT_EQ(0., t3.previous());
     ASSERT_EQ(1., t3.dt());
     ASSERT_EQ(1u, t3.steps());
 
-    NumLib::TimeStep t4 = t2;
+    const NumLib::TimeStep& t4 = t2;
     ASSERT_EQ(1., t4.current());
     ASSERT_EQ(0., t4.previous());
     ASSERT_EQ(1., t4.dt());
-- 
GitLab