Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify if statement with container #3047

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/nmodl/solve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#include "parse1.hpp"
#include "symbol.h"

#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>

/* make it an error if 2 solve statements are called on a single call to
model() */
Expand All @@ -25,6 +28,10 @@
void whileloop(Item*, long, int);
void check_ss_consist(Item*);

namespace {
enum class CVodeMethod { nomethod = 0, after_cvode = 1, cvode_t = 2, cvode_t_v = 3 };
}

/* Need list of solve statements. We impress the
general list structure to handle it. The element is a pointer to an
item which is the first item in the statement sequence in another list.
Expand Down Expand Up @@ -109,7 +116,7 @@
List* errstmt;
Symbol *fun, *method;
int numeqn, listnum, btype, steadystate;
int cvodemethod_;
CVodeMethod cvodemethod_;

if (!solvq)
solvq = newlist();
Expand All @@ -125,21 +132,20 @@
qsol = ITM(lq);
lq = lq->next;
method = SYM(lq);
cvodemethod_ = 0;
if (method && strcmp(method->name, "after_cvode") == 0) {
method = (Symbol*) 0;
lq->element.sym = (Symbol*) 0;
cvodemethod_ = 1;
}
if (method && strcmp(method->name, "cvode_t") == 0) {
method = (Symbol*) 0;
lq->element.sym = (Symbol*) 0;
cvodemethod_ = 2;
}
if (method && strcmp(method->name, "cvode_v") == 0) {
method = (Symbol*) 0;
lq->element.sym = (Symbol*) 0;
cvodemethod_ = 3;
cvodemethod_ = CVodeMethod::nomethod;
const std::vector<std::pair<std::string, CVodeMethod>> cvode_methods = {
{"after_cvode", CVodeMethod::after_cvode},
{"cvode_t", CVodeMethod::cvode_t},

Check warning on line 138 in src/nmodl/solve.cpp

View check run for this annotation

Codecov / codecov/patch

src/nmodl/solve.cpp#L137-L138

Added lines #L137 - L138 were not covered by tests
{"cvode_t_v", CVodeMethod::cvode_t_v}};

const auto& method_match =
std::find_if(cvode_methods.begin(), cvode_methods.end(), [&method](const auto& item) {
return method && method->name == item.first;
});
if (method && method_match != cvode_methods.end()) {
cvodemethod_ = method_match->second;
Copy link
Collaborator

@matz-e matz-e Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... this is basically like a map lookup?

Suggested change
const std::vector<std::pair<std::string, CVodeMethod>> cvode_methods = {
{"after_cvode", CVodeMethod::after_cvode},
{"cvode_t", CVodeMethod::cvode_t},
{"cvode_t_v", CVodeMethod::cvode_t_v}};
const auto& method_match =
std::find_if(cvode_methods.begin(), cvode_methods.end(), [&method](const auto& item) {
return method && method->name == item.first;
});
if (method && method_match != cvode_methods.end()) {
cvodemethod_ = method_match->second;
const std::map<std::string, CVodeMethod> cvode_methods = {
{"after_cvode", CVodeMethod::after_cvode},
{"cvode_t", CVodeMethod::cvode_t},
{"cvode_t_v", CVodeMethod::cvode_t_v}};
decltype(cvode_methods)::const_iterator m;
if (method && (m = cvode_methods.find(method)) != cvode_methods.end()) {
cvodemethod_ = m->second;

method = nullptr;
lq->element.sym = nullptr;
}
lq = lq->next;
errstmt = LST(lq);
Expand Down Expand Up @@ -231,16 +237,16 @@
case PROCED:
if (btype == BREAKPOINT) {
whileloop(qsol, (long) DERF, 0);
if (cvodemethod_ == 1) { /*after_cvode*/
if (cvodemethod_ == CVodeMethod::after_cvode) {
cvode_interface(fun, listnum, 0);
}
if (cvodemethod_ == 2) { /*cvode_t*/
if (cvodemethod_ == CVodeMethod::cvode_t) {
cvode_interface(fun, listnum, 0);
insertstr(qsol, "if (!cvode_active_)");
cvode_nrn_cur_solve_ = fun;
linsertstr(procfunc, "extern int cvode_active_;\n");
}
if (cvodemethod_ == 3) { /*cvode_t_v*/
if (cvodemethod_ == CVodeMethod::cvode_t_v) {
cvode_interface(fun, listnum, 0);
insertstr(qsol, "if (!cvode_active_)");
cvode_nrn_current_solve_ = fun;
Expand Down
Loading