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

Replace macro ITERATE_REMOVE by functions #3263

Merged
merged 9 commits into from
Dec 3, 2024
Merged
Changes from 5 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
190 changes: 104 additions & 86 deletions src/nrnoc/seclist.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <../../nrnconf.h>

#include <functional>

#define HOC_L_LIST 1
#include "section.h"
#include "neuron.h"
Expand All @@ -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<void(Item*)> 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;

Check warning on line 20 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L19-L20

Added lines #L19 - L20 were not covered by tests
}
if (q1->element.sec == sec) {
fun(q1);
return true;
}
}
return false;

Check warning on line 27 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L27

Added line #L27 was not covered by tests
}

void seclist_iterate_remove(List* sl, std::function<void(Item*)> 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;

Check warning on line 36 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L30-L36

Added lines #L30 - L36 were not covered by tests
}
fun(q1);

Check warning on line 38 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L38

Added line #L38 was not covered by tests
}
}

Check warning on line 40 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L40

Added line #L40 was not covered by tests

extern int hoc_return_type_code;
Section* (*nrnpy_o2sec_p_)(Object* o);
Expand Down Expand Up @@ -156,41 +178,43 @@
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");

Check warning on line 191 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L191

Added line #L191 was not covered by tests
} 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;

Check warning on line 213 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L194-L213

Added lines #L194 - L213 were not covered by tests
}
}
}
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;

Check warning on line 217 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L217

Added line #L217 was not covered by tests
}

static double unique(void* v) {
Expand All @@ -199,46 +223,42 @@
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;
alkino marked this conversation as resolved.
Show resolved Hide resolved
});
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;

Check warning on line 237 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L226-L237

Added lines #L226 - L237 were not covered by tests
}
}
}
return (double) i;
return (double) i;

Check warning on line 240 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L240

Added line #L240 was not covered by tests
}

static double contains(void* v) {
Section* s;
Item *q, *q1;
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)
? 1.
: 0.;

Check warning on line 251 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L248-L251

Added lines #L248 - L251 were not covered by tests
}

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));

Check warning on line 258 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L256-L258

Added lines #L256 - L258 were not covered by tests
}
});
return 1.;

Check warning on line 261 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L260-L261

Added lines #L260 - L261 were not covered by tests
}

static Member_func members[] = {{"append", append},
Expand Down Expand Up @@ -319,33 +339,31 @@

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 warning on line 350 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L349-L350

Added lines #L349 - L350 were not covered by tests
check(ob);
sl = (List*) (ob->u.this_pointer);
ITERATE_REMOVE(q, q1, sl) /*{*/
if (sec == q->element.sec) {
hoc_execute(relative(savepc));
if (!hoc_returning) {
pc = relative(savepc + 1);
}
hoc_tobj_unref(obp);
List* sl = (List*) (ob->u.this_pointer);
if (seclist_iterate_remove_until(

Check warning on line 353 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L352-L353

Added lines #L352 - L353 were not covered by tests
sl,
[&](Item* q) {
hoc_execute(relative(savepc));
if (!hoc_returning) {
pc = relative(savepc + 1);

Check warning on line 358 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L355-L358

Added lines #L355 - L358 were not covered by tests
}
hoc_tobj_unref(obp);
},

Check warning on line 361 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L360-L361

Added lines #L360 - L361 were not covered by tests
sec)) {
return;
}
}
hoc_tobj_unref(obp);
if (!hoc_returning) {
pc = relative(savepc + 1);
}
hoc_tobj_unref(obp);
if (!hoc_returning) {
pc = relative(savepc + 1);

Check warning on line 367 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L365-L367

Added lines #L365 - L367 were not covered by tests
}
}
Loading