From 357bcc1b3539665c880198e94afb35b8e4e58566 Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Thu, 11 Jun 2020 10:32:38 +0200
Subject: [PATCH] [GL / MeL] Use BL::getUniqueName.

---
 GeoLib/GEOObjects.cpp                    | 40 +++++-------------------
 MeshLib/MeshEditing/RasterDataToMesh.cpp | 19 ++---------
 2 files changed, 11 insertions(+), 48 deletions(-)

diff --git a/GeoLib/GEOObjects.cpp b/GeoLib/GEOObjects.cpp
index 39cc9cf110f..fa9ce4bdf8d 100644
--- a/GeoLib/GEOObjects.cpp
+++ b/GeoLib/GEOObjects.cpp
@@ -15,6 +15,7 @@
 #include "GEOObjects.h"
 
 #include <fstream>
+#include "BaseLib/StringTools.h"
 #include "BaseLib/Logging.h"
 
 #include "Triangle.h"
@@ -339,41 +340,16 @@ const SurfaceVec* GEOObjects::getSurfaceVecObj(const std::string &name) const
 
 bool GEOObjects::isUniquePointVecName(std::string &name)
 {
-    int count = 0;
-    bool isUnique = false;
-    std::string cpName;
+    std::vector<std::string> existing_names;
+    getGeometryNames(existing_names);
+    auto const& unique_name = BaseLib::getUniqueName(existing_names, name);
 
-    while (!isUnique)
+    if (unique_name != name)
     {
-        isUnique = true;
-        cpName = name;
-
-        count++;
-        // If the original name already exists we start to add numbers to name for
-        // as long as it takes to make the name unique.
-        if (count > 1)
-        {
-            cpName = cpName + "-" + std::to_string(count);
-        }
-
-        for (auto& point : _pnt_vecs)
-        {
-            if (cpName == point->getName())
-            {
-                isUnique = false;
-            }
-        }
-    }
-
-    // At this point cpName is a unique name and isUnique is true.
-    // If cpName is not the original name, "name" is changed and isUnique is set to false,
-    // indicating that a vector with the original name already exists.
-    if (count > 1)
-    {
-        isUnique = false;
-        name = cpName;
+        name = unique_name;
+        return false;
     }
-    return isUnique;
+    return true;
 }
 
 bool GEOObjects::isPntVecUsed (const std::string &name) const
diff --git a/MeshLib/MeshEditing/RasterDataToMesh.cpp b/MeshLib/MeshEditing/RasterDataToMesh.cpp
index 9216d4f1acf..dfd66b9ef64 100644
--- a/MeshLib/MeshEditing/RasterDataToMesh.cpp
+++ b/MeshLib/MeshEditing/RasterDataToMesh.cpp
@@ -10,6 +10,7 @@
 
 #include "RasterDataToMesh.h"
 
+#include "BaseLib/StringTools.h"
 #include "MeshLib/Node.h"
 #include "MeshLib/Elements/Element.h"
 
@@ -28,20 +29,6 @@ static bool checkMesh(MeshLib::Mesh const& mesh)
     return true;
 }
 
-static std::string getValidName(std::vector<std::string> const& vec_names,
-                                std::string const& array_name)
-{
-    std::string new_name = array_name;
-    std::size_t count = 1;
-    while (std::find(vec_names.cbegin(), vec_names.cend(), new_name) !=
-           vec_names.end())
-    {
-        count++;
-        new_name = array_name + "-" + std::to_string(count);
-    }
-    return new_name;
-}
-
 static double evaluatePixel(double const value, double const no_data,
                             double const replacement)
 {
@@ -64,7 +51,7 @@ bool projectToNodes(MeshLib::Mesh& mesh, GeoLib::Raster const& raster,
     auto& nodes = mesh.getNodes();
     auto& props = mesh.getProperties();
     std::string const name =
-        getValidName(props.getPropertyVectorNames(), array_name);
+        BaseLib::getUniqueName(props.getPropertyVectorNames(), array_name);
     auto vec = props.createNewPropertyVector<double>(
         name, MeshLib::MeshItemType::Node, 1);
     double const no_data = raster.getHeader().no_data;
@@ -88,7 +75,7 @@ bool projectToElements(MeshLib::Mesh& mesh, GeoLib::Raster const& raster,
     auto& elems = mesh.getElements();
     auto& props = mesh.getProperties();
     std::string const name =
-        getValidName(props.getPropertyVectorNames(), array_name);
+        BaseLib::getUniqueName(props.getPropertyVectorNames(), array_name);
     auto vec = props.createNewPropertyVector<double>(
         name, MeshLib::MeshItemType::Cell, 1);
     double const no_data = raster.getHeader().no_data;
-- 
GitLab