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 all 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
201 changes: 102 additions & 99 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,35 @@
#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; \
template <typename F>
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;
if (q1->element.sec->prop == nullptr) {
hoc_l_delete(q1);
continue;

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L20-L21

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

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L28

Added line #L28 was not covered by tests
}

template <typename F>
void seclist_iterate_remove(List* sl, F 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 38 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L32-L38

Added lines #L32 - L38 were not covered by tests
}
fun(q1->element.sec);

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
}
}

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L42

Added line #L42 was not covered by tests

extern int hoc_return_type_code;
Section* (*nrnpy_o2sec_p_)(Object* o);
Expand Down Expand Up @@ -143,102 +167,83 @@
}

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);
ITERATE_REMOVE(q, q1, sl) /*{*/
if (sec == q->element.sec) {
hoc_l_delete(q);
section_unref(sec);
Section* sec = nrn_secarg(1);
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 188 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L188

Added line #L188 was not covered by tests
} else {
Object* o;
o = *hoc_objgetarg(1);
check_obj_type(o, "SectionList");
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; });
sl = (List*) v;

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L191-L196

Added lines #L191 - L196 were not covered by tests
Item* q1;
for (Item* q = sl->next; q != sl; q = q1) {
q1 = q->next;
Section* s = hocSEC(q);
if (s->volatile_mark) {
hoc_l_delete(q);
section_unref(s);
++i;

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L198-L204

Added lines #L198 - L204 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 208 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L208

Added line #L208 was not covered by tests
}

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 */
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, [](Section* s) { s->volatile_mark = 0; });
int i = 0; /* number deleted */
for (Item* q = sl->next; q != sl; q = q1) {
q1 = q->next;
Section* s = hocSEC(q);
if (s->volatile_mark++) {
hoc_l_delete(q);
section_unref(s);
++i;

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L215-L223

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

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L226

Added line #L226 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.);
Section* s = nrn_secarg(1);
return seclist_iterate_remove_until(
sl, [](Item*) {}, s)
? 1.
: 0.;

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L232-L236

Added lines #L232 - L236 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, [](Section* s) {
if (s->prop) {
Printf("%s\n", secname(s));

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L241-L243

Added lines #L241 - L243 were not covered by tests
}
});
return 1.;

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L245-L246

Added lines #L245 - L246 were not covered by tests
}

static Member_func members[] = {{"append", append},
Expand Down Expand Up @@ -319,33 +324,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 335 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L334-L335

Added lines #L334 - L335 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 338 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L337-L338

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

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L340-L343

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

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L345-L346

Added lines #L345 - L346 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 352 in src/nrnoc/seclist.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/seclist.cpp#L350-L352

Added lines #L350 - L352 were not covered by tests
}
}
Loading