From 018a795330916e639be4d4ad5222b2a1678a614e Mon Sep 17 00:00:00 2001 From: Jason Marechal Date: Mon, 16 Dec 2024 16:27:57 +0100 Subject: [PATCH 1/3] Replace expansive regex with find --- .../benders_core/CriterionInputDataReader.cpp | 10 +++------- .../benders/benders_core/VariablesGroup.cpp | 15 ++++++++------- .../benders_core/CriterionInputDataReader.h | 3 +-- tests/cpp/outer_loop/outer_loop_test.cpp | 18 +++++++++--------- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/cpp/benders/benders_core/CriterionInputDataReader.cpp b/src/cpp/benders/benders_core/CriterionInputDataReader.cpp index 17cc1a029..4820908f0 100644 --- a/src/cpp/benders/benders_core/CriterionInputDataReader.cpp +++ b/src/cpp/benders/benders_core/CriterionInputDataReader.cpp @@ -13,14 +13,10 @@ using namespace Benders::Criterion; CriterionPattern::CriterionPattern(std::string prefix, std::string body) : prefix_(std::move(prefix)), body_(std::move(body)) {} -/** - * just do - * just cat ;) - */ -std::regex CriterionPattern::MakeRegex() const { - auto pattern = "(^" + prefix_ + "area<" + body_ + ">" + ")"; - return std::regex(pattern); +std::string CriterionPattern::Pattern() const { + return prefix_ + "area<" + body_ + ">"; } + const std::string &CriterionPattern::GetPrefix() const { return prefix_; } void CriterionPattern::SetPrefix(const std::string &prefix) { prefix_ = prefix; diff --git a/src/cpp/benders/benders_core/VariablesGroup.cpp b/src/cpp/benders/benders_core/VariablesGroup.cpp index c359dbf3c..af704cd13 100644 --- a/src/cpp/benders/benders_core/VariablesGroup.cpp +++ b/src/cpp/benders/benders_core/VariablesGroup.cpp @@ -25,15 +25,16 @@ std::vector> VariablesGroup::Indices() const { void VariablesGroup::Search() { indices_.assign(criterion_single_input_data_.size(), {}); - int var_index(0); - for (const auto& variable : all_variables_) { - int pattern_index(0); - for (const auto& single_input_data : criterion_single_input_data_) { - if (std::regex_search(variable, single_input_data.Pattern().MakeRegex())) { + int pattern_index(0); + for (const auto& single_input_data : criterion_single_input_data_) { + auto pattern = single_input_data.Pattern().GetPrefix() + "area<" + single_input_data.Pattern().GetBody() + ">"; + int var_index(0); + for (const auto& variable : all_variables_) { + if (variable.find(single_input_data.Pattern().GetPrefix()) != std::string::npos) { indices_[pattern_index].push_back(var_index); } - ++pattern_index; + var_index++; } - ++var_index; + ++pattern_index; } } diff --git a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h index df576689a..f4462ea93 100644 --- a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h +++ b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h @@ -45,7 +45,7 @@ class CriterionPattern { public: explicit CriterionPattern(std::string prefix, std::string body); CriterionPattern() = default; - [[nodiscard]] std::regex MakeRegex() const; + [[nodiscard]] std::string Pattern() const; [[nodiscard]] const std::string &GetPrefix() const; void SetPrefix(const std::string &prefix); [[nodiscard]] const std::string &GetBody() const; @@ -54,7 +54,6 @@ class CriterionPattern { private: std::string prefix_; std::string body_; - }; /// @brief holds the pattern and the criterion diff --git a/tests/cpp/outer_loop/outer_loop_test.cpp b/tests/cpp/outer_loop/outer_loop_test.cpp index b203a9350..cad5e4a81 100644 --- a/tests/cpp/outer_loop/outer_loop_test.cpp +++ b/tests/cpp/outer_loop/outer_loop_test.cpp @@ -181,17 +181,17 @@ TEST_F(OuterLoopPatternTest, RegexGivenPrefixAndBody) { const std::string body = "body"; CriterionPattern o(prefix, body); - auto ret_regex = o.MakeRegex(); + auto ret_regex = o.Pattern(); - ASSERT_EQ(std::regex_search(prefix + body, ret_regex), false); - ASSERT_EQ(std::regex_search(prefix + "::" + body + "::suffix", ret_regex), + ASSERT_EQ((prefix + body).find(ret_regex) != std::string::npos, false); + ASSERT_EQ((prefix + "::" + body + "::suffix").find(ret_regex) != std::string::npos, false); - ASSERT_EQ(std::regex_search(body + prefix, ret_regex), false); - ASSERT_EQ(std::regex_search(prefix + "::", ret_regex), false); - ASSERT_EQ(std::regex_search(body, ret_regex), false); - ASSERT_EQ(std::regex_search(prefix + "area<" + body + ">", ret_regex), true); - ASSERT_EQ(std::regex_search(prefix + "area<" + body + ">::suffix", ret_regex), true); - ASSERT_EQ(std::regex_search(prefix + "area<" + body + "_other_area>::suffix", ret_regex), false); + ASSERT_EQ((body + prefix).find(ret_regex) != std::string::npos , false); + ASSERT_EQ((prefix + "::").find(ret_regex) != std::string::npos, false); + ASSERT_EQ((body).find(ret_regex) != std::string::npos, false); + ASSERT_EQ((prefix + "area<" + body + ">").find(ret_regex) != std::string::npos, true); + ASSERT_EQ((prefix + "area<" + body + ">::suffix").find(ret_regex) != std::string::npos, true); //Match + ASSERT_EQ((prefix + "area<" + body + "_other_area>::suffix").find(ret_regex) != std::string::npos, false); } class OuterLoopInputFromYamlTest : public ::testing::Test {}; From 1de598b936fc43420eed3859529b3edf8678b710 Mon Sep 17 00:00:00 2001 From: Jason Marechal Date: Tue, 17 Dec 2024 10:21:01 +0100 Subject: [PATCH 2/3] Fix --- src/cpp/benders/benders_core/CriterionInputDataReader.cpp | 2 +- src/cpp/benders/benders_core/VariablesGroup.cpp | 8 ++++---- .../benders/benders_core/CriterionInputDataReader.h | 2 +- tests/cpp/outer_loop/outer_loop_test.cpp | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cpp/benders/benders_core/CriterionInputDataReader.cpp b/src/cpp/benders/benders_core/CriterionInputDataReader.cpp index 4820908f0..0fce7710d 100644 --- a/src/cpp/benders/benders_core/CriterionInputDataReader.cpp +++ b/src/cpp/benders/benders_core/CriterionInputDataReader.cpp @@ -13,7 +13,7 @@ using namespace Benders::Criterion; CriterionPattern::CriterionPattern(std::string prefix, std::string body) : prefix_(std::move(prefix)), body_(std::move(body)) {} -std::string CriterionPattern::Pattern() const { +std::string CriterionPattern::Value() const { return prefix_ + "area<" + body_ + ">"; } diff --git a/src/cpp/benders/benders_core/VariablesGroup.cpp b/src/cpp/benders/benders_core/VariablesGroup.cpp index af704cd13..e949467be 100644 --- a/src/cpp/benders/benders_core/VariablesGroup.cpp +++ b/src/cpp/benders/benders_core/VariablesGroup.cpp @@ -27,14 +27,14 @@ void VariablesGroup::Search() { indices_.assign(criterion_single_input_data_.size(), {}); int pattern_index(0); for (const auto& single_input_data : criterion_single_input_data_) { - auto pattern = single_input_data.Pattern().GetPrefix() + "area<" + single_input_data.Pattern().GetBody() + ">"; + auto pattern = single_input_data.Pattern().Value(); int var_index(0); for (const auto& variable : all_variables_) { - if (variable.find(single_input_data.Pattern().GetPrefix()) != std::string::npos) { + if (variable.find(pattern) != std::string::npos) { indices_[pattern_index].push_back(var_index); } - var_index++; + ++var_index; } ++pattern_index; } -} +} \ No newline at end of file diff --git a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h index f4462ea93..263742609 100644 --- a/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h +++ b/src/cpp/benders/benders_core/include/antares-xpansion/benders/benders_core/CriterionInputDataReader.h @@ -45,7 +45,7 @@ class CriterionPattern { public: explicit CriterionPattern(std::string prefix, std::string body); CriterionPattern() = default; - [[nodiscard]] std::string Pattern() const; + [[nodiscard]] std::string Value() const; [[nodiscard]] const std::string &GetPrefix() const; void SetPrefix(const std::string &prefix); [[nodiscard]] const std::string &GetBody() const; diff --git a/tests/cpp/outer_loop/outer_loop_test.cpp b/tests/cpp/outer_loop/outer_loop_test.cpp index cad5e4a81..e69cf673a 100644 --- a/tests/cpp/outer_loop/outer_loop_test.cpp +++ b/tests/cpp/outer_loop/outer_loop_test.cpp @@ -181,7 +181,7 @@ TEST_F(OuterLoopPatternTest, RegexGivenPrefixAndBody) { const std::string body = "body"; CriterionPattern o(prefix, body); - auto ret_regex = o.Pattern(); + auto ret_regex = o.Value(); ASSERT_EQ((prefix + body).find(ret_regex) != std::string::npos, false); ASSERT_EQ((prefix + "::" + body + "::suffix").find(ret_regex) != std::string::npos, From 8f67be51acc92dfc348d33e3b085018e4c07f333 Mon Sep 17 00:00:00 2001 From: Jason Marechal Date: Tue, 17 Dec 2024 10:55:28 +0100 Subject: [PATCH 3/3] Fix: pattern should only match at the start not inside the string --- src/cpp/benders/benders_core/VariablesGroup.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpp/benders/benders_core/VariablesGroup.cpp b/src/cpp/benders/benders_core/VariablesGroup.cpp index e949467be..8cd97d3be 100644 --- a/src/cpp/benders/benders_core/VariablesGroup.cpp +++ b/src/cpp/benders/benders_core/VariablesGroup.cpp @@ -30,7 +30,7 @@ void VariablesGroup::Search() { auto pattern = single_input_data.Pattern().Value(); int var_index(0); for (const auto& variable : all_variables_) { - if (variable.find(pattern) != std::string::npos) { + if (variable.starts_with(pattern)) { indices_[pattern_index].push_back(var_index); } ++var_index;