From 96094e0a6962eed776a1fa1ca5a4c8cf391d5812 Mon Sep 17 00:00:00 2001
From: Ianna Osborne <ianna.osborne@cern.ch>
Date: Wed, 14 Oct 2020 17:40:28 +0200
Subject: [PATCH] address comments and add move functions from CMSSW to DD4hep

---
 DDCore/CMakeLists.txt                   |  2 +-
 DDCore/include/DD4hep/Filter.h          |  5 +++-
 DDCore/include/DD4hep/SpecParRegistry.h |  5 ++--
 DDCore/src/Filter.cpp                   | 31 +++++++++++++++++++++++++
 DDCore/src/SpecParRegistry.cpp          | 11 ++++++++-
 5 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/DDCore/CMakeLists.txt b/DDCore/CMakeLists.txt
index 6a4f9b73e..a3ded85e8 100644
--- a/DDCore/CMakeLists.txt
+++ b/DDCore/CMakeLists.txt
@@ -15,7 +15,7 @@ dd4hep_print("|++> Will be Building DDCore")
 
 file(GLOB DDCore_SOURCES src/*.cpp src/segmentations/*.cpp src/XML/*.cpp)
 
-if(NOT DD4HEP_USE_TBB OR ${CMAKE_CXX_STANDARD} LESS 17)
+if(${CMAKE_CXX_STANDARD} LESS 17)
   list(APPEND EXCLUDE_HEADER include/DD4hep/SpecParRegistry.h)
   list(APPEND EXCLUDE_HEADER include/DD4hep/Filter.h)
   list(FILTER DDCore_SOURCES EXCLUDE REGEX Filter\.cpp|SpecParRegistry\.cpp )
diff --git a/DDCore/include/DD4hep/Filter.h b/DDCore/include/DD4hep/Filter.h
index 734d48b44..b2e5d8651 100644
--- a/DDCore/include/DD4hep/Filter.h
+++ b/DDCore/include/DD4hep/Filter.h
@@ -39,7 +39,7 @@ namespace dd4hep {
 
   struct Filter {
     std::vector<bool> isRegex;
-    std::vector<bool> hasNamaspace;
+    std::vector<bool> hasNamespace;
     std::vector<int> index;
     std::vector<std::string_view> skeys;
     std::vector<std::regex> keys;
@@ -50,11 +50,14 @@ namespace dd4hep {
 
   namespace dd {
     bool accepted(std::vector<std::regex> const&, std::string_view);
+    bool accepted(const Filter*, std::string_view);
     bool isRegex(std::string_view);
     bool hasNamespace(std::string_view);
     bool isMatch(std::string_view, std::string_view);
     bool compareEqual(std::string_view, std::string_view);
     bool compareEqual(std::string_view, std::regex);
+    bool compareEqualName(std::string_view, std::string_view);
+    bool compareEqualCopyNumber(std::string_view, int);
     std::string_view realTopName(std::string_view);
     std::vector<std::string_view> split(std::string_view, const char*);
     std::string_view noNamespace(std::string_view);
diff --git a/DDCore/include/DD4hep/SpecParRegistry.h b/DDCore/include/DD4hep/SpecParRegistry.h
index ae80b3df4..44a1c739f 100644
--- a/DDCore/include/DD4hep/SpecParRegistry.h
+++ b/DDCore/include/DD4hep/SpecParRegistry.h
@@ -38,10 +38,11 @@ namespace dd4hep {
   };
 
   using SpecParMap = std::unordered_map<std::string, SpecPar>;
-  using SpecParRefs = std::vector<std::pair<std::string_view, const SpecPar*>>;
+  using SpecParRefs = std::vector<std::pair<std::string, const SpecPar*>>;
 
   struct SpecParRegistry {
-    void filter(SpecParRefs&, const std::string&, const std::string& = "") const;
+    void filter(SpecParRefs&, const std::string&, const std::string&) const;
+    void filter(SpecParRefs&, const std::string&) const;
     std::vector<std::string_view> names() const;
     std::vector<std::string_view> names(const std::string& path) const;
     bool hasSpecPar(std::string_view) const;
diff --git a/DDCore/src/Filter.cpp b/DDCore/src/Filter.cpp
index fa8e66be9..ff46f738d 100644
--- a/DDCore/src/Filter.cpp
+++ b/DDCore/src/Filter.cpp
@@ -23,11 +23,42 @@ namespace dd4hep {
       return regex_match(std::string(node.data(), node.size()), pattern);
     }
 
+    bool compareEqualName(const std::string_view selection, const std::string_view name) {
+      return (!(dd4hep::dd::isRegex(selection)) ? dd4hep::dd::compareEqual(name, selection)
+	      : regex_match(name.begin(), name.end(), regex(std::string(selection))));
+    }
+
+    bool compareEqualCopyNumber(std::string_view name, int copy) {
+      auto pos = name.rfind('[');
+      if (pos != name.npos) {
+	if (std::stoi(std::string(name.substr(pos + 1, name.rfind(']')))) == copy) {
+	  return true;
+	}
+      }
+      
+      return false;
+    }
+
     bool accepted(vector<std::regex> const& keys, string_view node) {
       return (find_if(begin(keys), end(keys), [&](const auto& n) -> bool { return compareEqual(node, n); }) !=
               end(keys));
     }
 
+    bool accepted(const Filter* filter, std::string_view name) {
+      for(unsigned int i = 0; i < filter->isRegex.size(); i++ ) {
+	if(!filter->isRegex[i]) {
+	  if(compareEqual(filter->skeys[i], name)) {
+	    return true;
+	  }
+	} else {
+	  if(compareEqual(name, filter->keys[filter->index[i]])) {
+	    return true;
+	  }
+	}
+      }
+      return false;
+    }
+
     bool isRegex(string_view input) {
       return (input.find(".") != std::string_view::npos) || (input.find("*") != std::string_view::npos);
     }
diff --git a/DDCore/src/SpecParRegistry.cpp b/DDCore/src/SpecParRegistry.cpp
index db6dcd7d5..062c5dde6 100644
--- a/DDCore/src/SpecParRegistry.cpp
+++ b/DDCore/src/SpecParRegistry.cpp
@@ -110,11 +110,20 @@ void SpecParRegistry::filter(SpecParRefs& refs, const std::string& attribute, co
       }
     });
     if (found) {
-      refs.emplace_back(std::make_pair(k.first, &k.second));
+      refs.emplace_back(std::string(k.first.data(), k.first.size()), &k.second);
     }
   });
 }
 
+void SpecParRegistry::filter(SpecParRefs& refs, const std::string& key) const {
+  for (auto const& it : specpars) {
+    if (it.second.hasValue(key) || (it.second.spars.find(key) != end(it.second.spars))) {
+      refs.emplace_back(it.first, &it.second);
+    }
+  }
+}
+
+
 std::vector<std::string_view> SpecParRegistry::names(const std::string& path) const {
   std::vector<std::string_view> result;
   for_each(begin(specpars), end(specpars), [&](const auto& i) {
-- 
GitLab