From 26e81ca931058d8acdb727dcd727b23ce8d479cd Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Mon, 2 Dec 2024 19:57:53 +0100 Subject: [PATCH 1/8] Replace macro ITERATE_REMOVE by function --- src/nrnoc/seclist.cpp | 175 +++++++++++++++++++++++------------------- 1 file changed, 95 insertions(+), 80 deletions(-) diff --git a/src/nrnoc/seclist.cpp b/src/nrnoc/seclist.cpp index d1509e2a00..8679ff2b03 100644 --- a/src/nrnoc/seclist.cpp +++ b/src/nrnoc/seclist.cpp @@ -1,4 +1,7 @@ #include <../../nrnconf.h> + +#include + #define HOC_L_LIST 1 #include "section.h" #include "neuron.h" @@ -8,14 +11,33 @@ #include "code.h" #include "classreg.h" -/* needs trailing '}' */ -#define ITERATE_REMOVE(q1, q2, lst) \ - for (q1 = (lst)->next; q1 != (lst); q1 = q2) { \ - q2 = q1->next; \ - if (q1->element.sec->prop == NULL) { \ - hoc_l_delete(q1); \ - continue; \ +bool seclist_iterate_remove_until(List* sl, std::function fun, Section* sec) { + Item* q2 = nullptr; + for (Item* q1 = sl->next; q1 != sl; q1 = q2) { + q2 = q1->next; + if (q1->element.sec->prop == nullptr) { + hoc_l_delete(q1); + continue; + } + if (q1->element.sec == sec) { + fun(q1); + return true; + } + } + return false; +} + +void seclist_iterate_remove(List* sl, std::function fun) { + Item* q2 = nullptr; + for (Item* q1 = sl->next; q1 != sl; q1 = q2) { + q2 = q1->next; + if (q1->element.sec->prop == nullptr) { + hoc_l_delete(q1); + continue; } + fun(q1); + } +} extern int hoc_return_type_code; Section* (*nrnpy_o2sec_p_)(Object* o); @@ -156,41 +178,43 @@ static double seclist_remove(void* v) { if (!ifarg(1)) { #endif sec = nrn_secarg(1); - ITERATE_REMOVE(q, q1, sl) /*{*/ - if (sec == q->element.sec) { - hoc_l_delete(q); - section_unref(sec); + if (seclist_iterate_remove_until( + sl, + [](Item* q) { + Section* s = q->element.sec; + hoc_l_delete(q); + section_unref(s); + }, + sec)) { return 1.; } + hoc_warning(secname(sec), "not in this section list"); + } else { + Object* o; + o = *hoc_objgetarg(1); + check_obj_type(o, "SectionList"); + seclist_iterate_remove(sl, [](Item* q) { + Section* s = hocSEC(q); + s->volatile_mark = 0; + }); + sl = (List*) o->u.this_pointer; + seclist_iterate_remove(sl, [](Item* q) { + Section* s = hocSEC(q); + s->volatile_mark = 1; + }); + sl = (List*) v; + i = 0; + for (q = sl->next; q != sl; q = q1) { + q1 = q->next; + s = hocSEC(q); + if (s->volatile_mark) { + hoc_l_delete(q); + section_unref(s); + ++i; + } + } } - hoc_warning(secname(sec), "not in this section list"); -} -else { - Object* o; - o = *hoc_objgetarg(1); - check_obj_type(o, "SectionList"); - ITERATE_REMOVE(q, q1, sl) /*{*/ - s = hocSEC(q); - s->volatile_mark = 0; -} -sl = (List*) o->u.this_pointer; -ITERATE_REMOVE(q, q1, sl) /*{*/ -s = hocSEC(q); -s->volatile_mark = 1; -} -sl = (List*) v; -i = 0; -for (q = sl->next; q != sl; q = q1) { - q1 = q->next; - s = hocSEC(q); - if (s->volatile_mark) { - hoc_l_delete(q); - section_unref(s); - ++i; - } -} -} -return (double) i; + return (double) i; } static double unique(void* v) { @@ -199,21 +223,21 @@ static double unique(void* v) { Item *q, *q1; List* sl = (List*) v; hoc_return_type_code = 1; /* integer */ - ITERATE_REMOVE(q, q1, sl) /*{*/ - s = hocSEC(q); - s->volatile_mark = 0; -} -i = 0; -for (q = sl->next; q != sl; q = q1) { - q1 = q->next; - s = hocSEC(q); - if (s->volatile_mark++) { - hoc_l_delete(q); - section_unref(s); - ++i; + seclist_iterate_remove(sl, [](Item* q) { + Section* s = hocSEC(q); + s->volatile_mark = 1; + }); + i = 0; + for (q = sl->next; q != sl; q = q1) { + q1 = q->next; + s = hocSEC(q); + if (s->volatile_mark++) { + hoc_l_delete(q); + section_unref(s); + ++i; + } } -} -return (double) i; + return (double) i; } static double contains(void* v) { @@ -222,23 +246,18 @@ static double contains(void* v) { List* sl = (List*) v; hoc_return_type_code = 2; /* boolean */ s = nrn_secarg(1); - ITERATE_REMOVE(q, q1, sl) /*{*/ - if (hocSEC(q) == s) { - return 1.; - } -} -return (0.); + return seclist_iterate_remove_until( + sl, [](Item*) {}, s); } static double printnames(void* v) { - Item *q, *q1; List* sl = (List*) v; - ITERATE_REMOVE(q, q1, sl) /*{*/ - if (q->element.sec->prop) { - Printf("%s\n", secname(q->element.sec)); - } -} -return 1.; + seclist_iterate_remove(sl, [](Item* q) { + if (q->element.sec->prop) { + Printf("%s\n", secname(q->element.sec)); + } + }); + return 1.; } static Member_func members[] = {{"append", append}, @@ -319,33 +338,29 @@ void forall_sectionlist(void) { void hoc_ifseclist(void) { Inst* savepc = pc; - Item *q, *q1; Section* sec = chk_access(); - List* sl; - Object* ob; - Object** obp; /* if arg is a string use forall_section */ if (hoc_stacktype() == STRING) { hoc_ifsec(); return; } - obp = hoc_objpop(); - ob = *obp; + Object** obp = hoc_objpop(); + Object* ob = *obp; check(ob); - sl = (List*) (ob->u.this_pointer); - ITERATE_REMOVE(q, q1, sl) /*{*/ - if (sec == q->element.sec) { + List* sl = (List*) (ob->u.this_pointer); + bool result = seclist_iterate_remove_until(sl, [&](Item* q) { hoc_execute(relative(savepc)); if (!hoc_returning) { pc = relative(savepc + 1); } hoc_tobj_unref(obp); + }, sec); + if (result) { return; } -} -hoc_tobj_unref(obp); -if (!hoc_returning) { - pc = relative(savepc + 1); -} + hoc_tobj_unref(obp); + if (!hoc_returning) { + pc = relative(savepc + 1); + } } From dd14e844dfb175eae55ff696143f25048b71945a Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Mon, 2 Dec 2024 20:16:56 +0100 Subject: [PATCH 2/8] Format --- src/nrnoc/seclist.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/nrnoc/seclist.cpp b/src/nrnoc/seclist.cpp index 8679ff2b03..c749b12612 100644 --- a/src/nrnoc/seclist.cpp +++ b/src/nrnoc/seclist.cpp @@ -247,7 +247,9 @@ static double contains(void* v) { hoc_return_type_code = 2; /* boolean */ s = nrn_secarg(1); return seclist_iterate_remove_until( - sl, [](Item*) {}, s); + sl, [](Item*) {}, s) + ? 1. + : 0.; } static double printnames(void* v) { @@ -349,13 +351,16 @@ void hoc_ifseclist(void) { Object* ob = *obp; check(ob); List* sl = (List*) (ob->u.this_pointer); - bool result = seclist_iterate_remove_until(sl, [&](Item* q) { - hoc_execute(relative(savepc)); - if (!hoc_returning) { - pc = relative(savepc + 1); - } - hoc_tobj_unref(obp); - }, sec); + bool result = seclist_iterate_remove_until( + sl, + [&](Item* q) { + hoc_execute(relative(savepc)); + if (!hoc_returning) { + pc = relative(savepc + 1); + } + hoc_tobj_unref(obp); + }, + sec); if (result) { return; } From 814dd33fd591b80d65b67f3c7b80dac1ea30fdd3 Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Mon, 2 Dec 2024 20:18:01 +0100 Subject: [PATCH 3/8] Format --- src/nrnoc/seclist.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/nrnoc/seclist.cpp b/src/nrnoc/seclist.cpp index c749b12612..780af95964 100644 --- a/src/nrnoc/seclist.cpp +++ b/src/nrnoc/seclist.cpp @@ -351,7 +351,7 @@ void hoc_ifseclist(void) { Object* ob = *obp; check(ob); List* sl = (List*) (ob->u.this_pointer); - bool result = seclist_iterate_remove_until( + if (seclist_iterate_remove_until( sl, [&](Item* q) { hoc_execute(relative(savepc)); @@ -360,8 +360,7 @@ void hoc_ifseclist(void) { } hoc_tobj_unref(obp); }, - sec); - if (result) { + sec)) { return; } hoc_tobj_unref(obp); From cd365313485b579c573e5ddb80b0b0904e7161b2 Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Tue, 3 Dec 2024 10:17:24 +0100 Subject: [PATCH 4/8] format --- src/nrnoc/seclist.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/nrnoc/seclist.cpp b/src/nrnoc/seclist.cpp index 780af95964..4230e6a8a9 100644 --- a/src/nrnoc/seclist.cpp +++ b/src/nrnoc/seclist.cpp @@ -352,15 +352,15 @@ void hoc_ifseclist(void) { check(ob); List* sl = (List*) (ob->u.this_pointer); if (seclist_iterate_remove_until( - sl, - [&](Item* q) { - hoc_execute(relative(savepc)); - if (!hoc_returning) { - pc = relative(savepc + 1); - } - hoc_tobj_unref(obp); - }, - sec)) { + sl, + [&](Item* q) { + hoc_execute(relative(savepc)); + if (!hoc_returning) { + pc = relative(savepc + 1); + } + hoc_tobj_unref(obp); + }, + sec)) { return; } hoc_tobj_unref(obp); From af6e029aa45c9502b2790886a4276e458ab74a88 Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Tue, 3 Dec 2024 11:01:19 +0100 Subject: [PATCH 5/8] Remove useless q, q1 --- src/nrnoc/seclist.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/nrnoc/seclist.cpp b/src/nrnoc/seclist.cpp index 4230e6a8a9..8efb7cb53e 100644 --- a/src/nrnoc/seclist.cpp +++ b/src/nrnoc/seclist.cpp @@ -242,7 +242,6 @@ static double unique(void* v) { static double contains(void* v) { Section* s; - Item *q, *q1; List* sl = (List*) v; hoc_return_type_code = 2; /* boolean */ s = nrn_secarg(1); From cce8fc98491a89b2a59312dabee39b3d93a5d8bf Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Tue, 3 Dec 2024 14:05:57 +0100 Subject: [PATCH 6/8] Fix for review --- src/nrnoc/seclist.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/nrnoc/seclist.cpp b/src/nrnoc/seclist.cpp index 8efb7cb53e..3e66903c57 100644 --- a/src/nrnoc/seclist.cpp +++ b/src/nrnoc/seclist.cpp @@ -27,7 +27,7 @@ bool seclist_iterate_remove_until(List* sl, std::function fun, Sect return false; } -void seclist_iterate_remove(List* sl, std::function fun) { +void seclist_iterate_remove(List* sl, std::function fun) { Item* q2 = nullptr; for (Item* q1 = sl->next; q1 != sl; q1 = q2) { q2 = q1->next; @@ -35,7 +35,7 @@ void seclist_iterate_remove(List* sl, std::function fun) { hoc_l_delete(q1); continue; } - fun(q1); + fun(q1->element.sec); } } @@ -193,13 +193,11 @@ static double seclist_remove(void* v) { Object* o; o = *hoc_objgetarg(1); check_obj_type(o, "SectionList"); - seclist_iterate_remove(sl, [](Item* q) { - Section* s = hocSEC(q); + seclist_iterate_remove(sl, [](Section* s) { s->volatile_mark = 0; }); sl = (List*) o->u.this_pointer; - seclist_iterate_remove(sl, [](Item* q) { - Section* s = hocSEC(q); + seclist_iterate_remove(sl, [](Section* s) { s->volatile_mark = 1; }); sl = (List*) v; @@ -223,9 +221,8 @@ static double unique(void* v) { Item *q, *q1; List* sl = (List*) v; hoc_return_type_code = 1; /* integer */ - seclist_iterate_remove(sl, [](Item* q) { - Section* s = hocSEC(q); - s->volatile_mark = 1; + seclist_iterate_remove(sl, [](Section* s) { + s->volatile_mark = 0; }); i = 0; for (q = sl->next; q != sl; q = q1) { @@ -253,9 +250,9 @@ static double contains(void* v) { static double printnames(void* v) { List* sl = (List*) v; - seclist_iterate_remove(sl, [](Item* q) { - if (q->element.sec->prop) { - Printf("%s\n", secname(q->element.sec)); + seclist_iterate_remove(sl, [](Section* s) { + if (s->prop) { + Printf("%s\n", secname(s)); } }); return 1.; @@ -352,7 +349,7 @@ void hoc_ifseclist(void) { List* sl = (List*) (ob->u.this_pointer); if (seclist_iterate_remove_until( sl, - [&](Item* q) { + [&](Item*) { hoc_execute(relative(savepc)); if (!hoc_returning) { pc = relative(savepc + 1); From 234681a1f74591e847393e9e8ba223e30b1c7e31 Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Tue, 3 Dec 2024 15:49:32 +0100 Subject: [PATCH 7/8] Format --- src/nrnoc/seclist.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/nrnoc/seclist.cpp b/src/nrnoc/seclist.cpp index 3e66903c57..9936f0ff56 100644 --- a/src/nrnoc/seclist.cpp +++ b/src/nrnoc/seclist.cpp @@ -11,7 +11,8 @@ #include "code.h" #include "classreg.h" -bool seclist_iterate_remove_until(List* sl, std::function fun, Section* sec) { +template +bool seclist_iterate_remove_until(List* sl, F fun, const Section* sec) { Item* q2 = nullptr; for (Item* q1 = sl->next; q1 != sl; q1 = q2) { q2 = q1->next; @@ -27,7 +28,8 @@ bool seclist_iterate_remove_until(List* sl, std::function fun, Sect return false; } -void seclist_iterate_remove(List* sl, std::function fun) { +template +void seclist_iterate_remove(List* sl, F fun) { Item* q2 = nullptr; for (Item* q1 = sl->next; q1 != sl; q1 = q2) { q2 = q1->next; @@ -193,13 +195,9 @@ static double seclist_remove(void* v) { Object* o; o = *hoc_objgetarg(1); check_obj_type(o, "SectionList"); - seclist_iterate_remove(sl, [](Section* s) { - s->volatile_mark = 0; - }); + seclist_iterate_remove(sl, [](Section* s) { s->volatile_mark = 0; }); sl = (List*) o->u.this_pointer; - seclist_iterate_remove(sl, [](Section* s) { - s->volatile_mark = 1; - }); + seclist_iterate_remove(sl, [](Section* s) { s->volatile_mark = 1; }); sl = (List*) v; i = 0; for (q = sl->next; q != sl; q = q1) { @@ -221,9 +219,7 @@ static double unique(void* v) { Item *q, *q1; List* sl = (List*) v; hoc_return_type_code = 1; /* integer */ - seclist_iterate_remove(sl, [](Section* s) { - s->volatile_mark = 0; - }); + seclist_iterate_remove(sl, [](Section* s) { s->volatile_mark = 0; }); i = 0; for (q = sl->next; q != sl; q = q1) { q1 = q->next; From 94ae144b6cf562ea0558685a80530d8e43c66b98 Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Tue, 3 Dec 2024 15:56:49 +0100 Subject: [PATCH 8/8] Sonar --- src/nrnoc/seclist.cpp | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/nrnoc/seclist.cpp b/src/nrnoc/seclist.cpp index 9936f0ff56..7ab9e0850c 100644 --- a/src/nrnoc/seclist.cpp +++ b/src/nrnoc/seclist.cpp @@ -167,19 +167,14 @@ static double allroots(void* v) { } static double seclist_remove(void* v) { - Section *sec, *s; - Item *q, *q1; - List* sl; - int i; - - sl = (List*) v; - i = 0; + List* sl = (List*) v; + int i = 0; #if USE_PYTHON if (!ifarg(1) || (*hoc_objgetarg(1))->ctemplate->sym == nrnpy_pyobj_sym_) { #else if (!ifarg(1)) { #endif - sec = nrn_secarg(1); + Section* sec = nrn_secarg(1); if (seclist_iterate_remove_until( sl, [](Item* q) { @@ -199,10 +194,10 @@ static double seclist_remove(void* v) { sl = (List*) o->u.this_pointer; seclist_iterate_remove(sl, [](Section* s) { s->volatile_mark = 1; }); sl = (List*) v; - i = 0; - for (q = sl->next; q != sl; q = q1) { + Item* q1; + for (Item* q = sl->next; q != sl; q = q1) { q1 = q->next; - s = hocSEC(q); + Section* s = hocSEC(q); if (s->volatile_mark) { hoc_l_delete(q); section_unref(s); @@ -214,16 +209,14 @@ static double seclist_remove(void* v) { } static double unique(void* v) { - int i; /* number deleted */ - Section* s; - Item *q, *q1; + Item* q1; List* sl = (List*) v; hoc_return_type_code = 1; /* integer */ seclist_iterate_remove(sl, [](Section* s) { s->volatile_mark = 0; }); - i = 0; - for (q = sl->next; q != sl; q = q1) { + int i = 0; /* number deleted */ + for (Item* q = sl->next; q != sl; q = q1) { q1 = q->next; - s = hocSEC(q); + Section* s = hocSEC(q); if (s->volatile_mark++) { hoc_l_delete(q); section_unref(s); @@ -234,10 +227,9 @@ static double unique(void* v) { } static double contains(void* v) { - Section* s; List* sl = (List*) v; hoc_return_type_code = 2; /* boolean */ - s = nrn_secarg(1); + Section* s = nrn_secarg(1); return seclist_iterate_remove_until( sl, [](Item*) {}, s) ? 1.