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

Add iterator #3275

Merged
merged 12 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 4 additions & 13 deletions src/nrniv/secbrows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,13 @@
psec_[scnt_++] = sec;
}
} else {
struct hoc_Item* qsec;
scnt_ = 0;
// ForAllSections(sec) //{
ITERATE(qsec, section_list) {
for (const Section* sec: range_sec(section_list)) {

Check warning on line 125 in src/nrniv/secbrows.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/secbrows.cpp#L125

Added line #L125 was not covered by tests
++scnt_;
}
psec_ = new Section*[scnt_];
scnt_ = 0;
// ForAllSections(sec) //{
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {

Check warning on line 130 in src/nrniv/secbrows.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/secbrows.cpp#L130

Added line #L130 was not covered by tests
psec_[scnt_++] = sec;
}
}
Expand Down Expand Up @@ -359,18 +355,13 @@
}

SectionBrowserImpl::SectionBrowserImpl() {
struct hoc_Item* qsec;
scnt_ = 0;
// ForAllSections(sec) //{
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (const Section* sec: range_sec(section_list)) {

Check warning on line 359 in src/nrniv/secbrows.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/secbrows.cpp#L359

Added line #L359 was not covered by tests
++scnt_;
}
psec_ = new Section*[scnt_];
scnt_ = 0;
// ForAllSections(sec) //{
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {

Check warning on line 364 in src/nrniv/secbrows.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/secbrows.cpp#L364

Added line #L364 was not covered by tests
psec_[scnt_++] = sec;
section_ref(sec);
}
Expand Down
24 changes: 5 additions & 19 deletions src/nrnoc/cabcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,7 @@ Section* chk_access() {
Section* sec = secstack[isecstack];
if (!sec || !sec->prop) {
/* use any existing section as a default section */
hoc_Item* qsec;
// ForAllSections(lsec)
ITERATE(qsec, section_list) {
Section* lsec = hocSEC(qsec);
for (Section* lsec: range_sec(section_list)) {
if (lsec->prop) {
sec = lsec;
++sec->refcount;
Expand All @@ -479,10 +476,7 @@ Section* nrn_noerr_access(void) /* return 0 if no accessed section */
Section* sec = secstack[isecstack];
if (!sec || !sec->prop) {
/* use any existing section as a default section */
hoc_Item* qsec;
// ForAllSections(lsec)
ITERATE(qsec, section_list) {
Section* lsec = hocSEC(qsec);
for (Section* lsec: range_sec(section_list)) {
if (lsec->prop) {
sec = lsec;
++sec->refcount;
Expand Down Expand Up @@ -1639,8 +1633,6 @@ void nrn_parent_info(Section* s) {
}

void setup_topology(void) {
Item* qsec;

/* use connection info in section property to connect nodes. */
/* for the moment we assume uniform dx and range 0-1 */

Expand All @@ -1656,9 +1648,7 @@ void setup_topology(void) {

nrn_global_ncell = 0;

// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {
#if 0
if (sec->nnode < 1) { /* last node is not a segment */
hoc_execerror(secname(sec),
Expand Down Expand Up @@ -2200,15 +2190,11 @@ void hoc_level_pushsec(Section* sec) {
}

void push_section(void) {
Section* sec;
Section* sec = nullptr;
if (hoc_is_str_arg(1)) {
Item* qsec;
char* s;
sec = (Section*) 0;
s = gargstr(1);
// ForAllSections(sec1) /* I can't imagine a more inefficient way */
ITERATE(qsec, section_list) {
Section* sec1 = hocSEC(qsec);
for (Section* sec1: range_sec(section_list)) {
if (strcmp(s, nrn_sec2pysecname(sec1)) == 0) {
sec = sec1;
break;
Expand Down
21 changes: 5 additions & 16 deletions src/nrnoc/extcelln.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ void extnode_free_elements(Extnode* nde) {
}

static void check_if_extracellular_in_use() {
hoc_Item* qsec;
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (const Section* sec: range_sec(section_list)) {
if (sec->pnode[0]->extnode) {
hoc_execerror("Cannot change nlayer_extracellular when instances exist", NULL);
}
Expand Down Expand Up @@ -486,14 +484,11 @@ void ext_con_coef(void) /* setup a and b */
{
int j, k;
double dx, area;
hoc_Item* qsec;
Node *nd, **pnd;
Extnode* nde;

/* temporarily store half segment resistances in rhs */
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {
if (sec->pnode[0]->extnode) {
dx = section_length(sec) / ((double) (sec->nnode - 1));
for (j = 0; j < sec->nnode - 1; j++) {
Expand Down Expand Up @@ -529,9 +524,7 @@ void ext_con_coef(void) /* setup a and b */
section connects straight to the point*/
/* for the near future we always have a last node at x=1 with
no properties */
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (const Section* sec: range_sec(section_list)) {
if (sec->pnode[0]->extnode) {
/* node half resistances in general get added to the
node and to the node's "child node in the same section".
Expand All @@ -549,9 +542,7 @@ void ext_con_coef(void) /* setup a and b */
}
}
}
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {
if (sec->pnode[0]->extnode) {
/* convert to siemens/cm^2 for all nodes except last
and microsiemens for last. This means that a*V = mamps/cm2
Expand All @@ -578,9 +569,7 @@ void ext_con_coef(void) /* setup a and b */
}
}
/* now the effect of parent on node equation. */
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (const Section* sec: range_sec(section_list)) {
if (sec->pnode[0]->extnode) {
for (j = 0; j < sec->nnode; j++) {
nd = sec->pnode[j];
Expand Down
49 changes: 13 additions & 36 deletions src/nrnoc/solve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,13 @@
double debugsolve(void) /* returns solution error */
{
short inode;
int i;
Section *sec, *psec, *ch;
Node *nd, *pnd, **ndP;
double err, sum;

/* save parts of matrix that will be destroyed */
assert(0)
/* need to save the rootnodes too */
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
assert(0);
/* need to save the rootnodes too */
for (const Section* sec: range_sec(section_list)) {
assert(sec->pnode && sec->nnode);
for (inode = sec->nnode - 1; inode >= 0; inode--) {
nd = sec->pnode[inode];
Expand All @@ -124,9 +120,7 @@

err = 0.;
/* need to check the rootnodes too */
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (const Section* sec: range_sec(section_list)) {
for (inode = sec->nnode - 1; inode >= 0; inode--) {
ndP = sec->pnode + inode;
nd = sec->pnode[inode];
Expand All @@ -141,10 +135,9 @@
if (inode < sec->nnode - 1) {
sum += NODEA(ndP[1]) * NODERHS(ndP[1]);
}
for (ch = nd->child; ch; ch = ch->sibling) {
psec = ch;
pnd = psec->pnode[0];
assert(pnd && psec->nnode);
for (const Section* ch = nd->child; ch; ch = ch->sibling) {
pnd = ch->pnode[0];
assert(pnd && ch->nnode);
sum += NODEA(pnd) * NODERHS(pnd);
}
sum -= nd->savrhs;
Expand Down Expand Up @@ -291,12 +284,9 @@

void nrnhoc_topology(void) /* print the topology of the branched cable */
{
hoc_Item* q;

v_setup_vectors();
Printf("\n");
ITERATE(q, section_list) {
Section* sec = (Section*) VOIDITM(q);
for (Section* sec: range_sec(section_list)) {
if (sec->parentsec == (Section*) 0) {
Printf("|");
dashes(sec, 0, '-');
Expand Down Expand Up @@ -434,10 +424,7 @@
}

void nrn_clear_mark(void) {
hoc_Item* qsec;
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {

Check warning on line 427 in src/nrnoc/solve.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/solve.cpp#L427

Added line #L427 was not covered by tests
sec->volatile_mark = 0;
}
}
Expand Down Expand Up @@ -744,15 +731,10 @@
{
int order, isec;
Section* ch;
Section* sec;
hoc_Item* qsec;

/* count the sections */
section_count = 0;
/*SUPPRESS 765*/
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {
sec->order = -1;
++section_count;
}
Expand All @@ -765,9 +747,7 @@
secorder = (Section**) emalloc(section_count * sizeof(Section*));
}
order = 0;
// ForAllSections(sec) /* all the roots first */
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {
if (!sec->parentsec) {
secorder[order] = sec;
sec->order = order;
Expand All @@ -777,10 +757,7 @@

for (isec = 0; isec < section_count; isec++) {
if (isec >= order) {
// Sections form a loop.
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {

Check warning on line 760 in src/nrnoc/solve.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/solve.cpp#L760

Added line #L760 was not covered by tests
Section *psec, *s = sec;
for (psec = sec->parentsec; psec; s = psec, psec = psec->parentsec) {
if (!psec || s->order >= 0) {
Expand All @@ -798,7 +775,7 @@
}
}
}
sec = secorder[isec];
Section* sec = secorder[isec];
for (ch = sec->child; ch; ch = ch->sibling) {
secorder[order] = ch;
ch->order = order;
Expand Down
13 changes: 3 additions & 10 deletions src/nrnoc/treeset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,6 @@ void connection_coef(void) /* setup a and b */
{
int j;
double area;
hoc_Item* qsec;
Node* nd;
#if RA_WARNING
extern int nrn_ra_set;
Expand All @@ -836,9 +835,7 @@ void connection_coef(void) /* setup a and b */
#endif
++recalc_diam_count_;
nrn_area_ri_nocount_ = 1;
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {
nrn_area_ri(sec);
}
nrn_area_ri_nocount_ = 0;
Expand All @@ -858,9 +855,7 @@ void connection_coef(void) /* setup a and b */
std::fill_n(nt.node_a_storage(), nt.end, 0.0);
std::fill_n(nt.node_b_storage(), nt.end, 0.0);
}
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {
// Unnecessary because they are unused, but help when looking at fmatrix.
if (!sec->parentsec) {
if (auto* const ptr = nrn_classicalNodeA(sec->parentnode)) {
Expand Down Expand Up @@ -889,9 +884,7 @@ void connection_coef(void) /* setup a and b */
}
}
/* now the effect of parent on node equation. */
// ForAllSections(sec)
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (const Section* sec: range_sec(section_list)) {
for (j = 0; j < sec->nnode; j++) {
nd = sec->pnode[j];
*nrn_classicalNodeB(nd) = -1.e2 * NODERINV(nd) / NODEAREA(nd);
Expand Down
29 changes: 27 additions & 2 deletions src/oc/hoclist.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,33 @@ struct hoc_Item {
};
using hoc_List = hoc_Item;

#define ITEM0 (hoc_Item*) 0
#define LIST0 (hoc_List*) 0
constexpr auto range_sec(hoc_List* iterable) {
struct iterator {
hoc_Item* iter;
bool operator!=(const iterator& other) const {
return iter != other.iter;
}
void operator++() {
iter = iter->next;
}
Section* operator*() const {
return iter->element.sec;
}
};
struct iterable_wrapper {
hoc_List* iterable;
auto begin() {
return iterator{iterable->next};
}
auto end() {
return iterator{iterable};
}
};
return iterable_wrapper{iterable};
}

#define ITEM0 nullptr
#define LIST0 nullptr

#define ITERATE(itm, lst) for (itm = (lst)->next; itm != (lst); itm = itm->next)
/*
Expand Down
Loading