From 4ec6c327168c5355dc22f5af8b67ad641ba1d034 Mon Sep 17 00:00:00 2001 From: David Heejong Park Date: Sat, 16 Nov 2024 11:43:54 +0100 Subject: [PATCH] Added a filtered list for modify_and_get. --- src/cosim/slave_simulator.cpp | 38 ++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/cosim/slave_simulator.cpp b/src/cosim/slave_simulator.cpp index 5cd4746a..1eb595ff 100644 --- a/src/cosim/slave_simulator.cpp +++ b/src/cosim/slave_simulator.cpp @@ -224,20 +224,34 @@ class set_variable_cache if (!hasRunModifiers_) { for (const auto& entry : modifiers_) { const auto ref = entry.first; - if (!filter || filter && (*filter)(ref)) { - auto& arrayIndex = exposedVariables_.at(ref).arrayIndex; - if (arrayIndex < 0) { - arrayIndex = references_.size(); - assert(references_.size() == values_.size()); - references_.emplace_back(ref); - values_.emplace_back(exposedVariables_.at(ref).lastValue); - } - values_[arrayIndex] = entry.second(values_[arrayIndex], deltaT); + auto& arrayIndex = exposedVariables_.at(ref).arrayIndex; + if (arrayIndex < 0) { + arrayIndex = references_.size(); + assert(references_.size() == values_.size()); + references_.emplace_back(ref); + values_.emplace_back(exposedVariables_.at(ref).lastValue); } + values_[arrayIndex] = entry.second(values_[arrayIndex], deltaT); } assert(references_.size() == values_.size()); hasRunModifiers_ = true; } + + if (filter) { + references_filtered_.clear(); + values_filtered_.clear(); + + for (int i = 0; i < references_.size(); i++) { + auto& ref = references_.at(i); + if ((*filter)(ref)) { + references_filtered_.push_back(ref); + values_filtered_.push_back(values_.at(i)); + } + } + + return std::pair(gsl::make_span(references_filtered_), gsl::make_span(values_filtered_)); + } + return std::pair(gsl::make_span(references_), gsl::make_span(values_)); } @@ -248,6 +262,8 @@ class set_variable_cache } references_.clear(); values_.clear(); + references_filtered_.clear(); + values_filtered_.clear(); hasRunModifiers_ = false; } @@ -273,6 +289,10 @@ class set_variable_cache // The references and values of the variables that will be set next. std::vector references_; boost::container::vector values_; + + // Filtered references and values of the values to be set next (if a filter is applied). + std::vector references_filtered_; + boost::container::vector values_filtered_; };