diff --git a/src/nrniv/secbrows.cpp b/src/nrniv/secbrows.cpp index 84263229b6..207a5b6b4f 100644 --- a/src/nrniv/secbrows.cpp +++ b/src/nrniv/secbrows.cpp @@ -121,17 +121,13 @@ OcSectionBrowser::OcSectionBrowser(Object* ob) psec_[scnt_++] = sec; } } else { - struct hoc_Item* qsec; scnt_ = 0; - // ForAllSections(sec) //{ - ITERATE(qsec, section_list) { + for (const Section* sec: range_sec(section_list)) { ++scnt_; } psec_ = new Section*[scnt_]; scnt_ = 0; - // ForAllSections(sec) //{ - ITERATE(qsec, section_list) { - Section* sec = hocSEC(qsec); + for (Section* sec: range_sec(section_list)) { psec_[scnt_++] = sec; } } @@ -359,18 +355,13 @@ void BrowserAccept::execute() { } 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)) { ++scnt_; } psec_ = new Section*[scnt_]; scnt_ = 0; - // ForAllSections(sec) //{ - ITERATE(qsec, section_list) { - Section* sec = hocSEC(qsec); + for (Section* sec: range_sec(section_list)) { psec_[scnt_++] = sec; section_ref(sec); } diff --git a/src/nrnoc/cabcode.cpp b/src/nrnoc/cabcode.cpp index fe6de4e3a2..85c4b1b392 100644 --- a/src/nrnoc/cabcode.cpp +++ b/src/nrnoc/cabcode.cpp @@ -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; @@ -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; @@ -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 */ @@ -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), @@ -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; diff --git a/src/nrnoc/extcelln.cpp b/src/nrnoc/extcelln.cpp index d807f2bf47..ccfc4250a1 100644 --- a/src/nrnoc/extcelln.cpp +++ b/src/nrnoc/extcelln.cpp @@ -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); } @@ -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++) { @@ -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". @@ -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 @@ -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]; diff --git a/src/nrnoc/solve.cpp b/src/nrnoc/solve.cpp index a3e55c2535..a42fbe960a 100644 --- a/src/nrnoc/solve.cpp +++ b/src/nrnoc/solve.cpp @@ -100,17 +100,13 @@ Section** secorder; 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]; @@ -124,9 +120,7 @@ double debugsolve(void) /* returns solution error */ 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]; @@ -141,10 +135,9 @@ double debugsolve(void) /* returns solution error */ 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; @@ -291,12 +284,9 @@ static void dashes(Section* sec, int offset, int first); 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, '-'); @@ -434,10 +424,7 @@ void bksub(NrnThread* _nt) { } 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)) { sec->volatile_mark = 0; } } @@ -744,15 +731,10 @@ void section_order(void) /* create a section order consistent */ { 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; } @@ -765,9 +747,7 @@ void section_order(void) /* create a section order consistent */ 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; @@ -777,10 +757,7 @@ void section_order(void) /* create a section order consistent */ 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)) { Section *psec, *s = sec; for (psec = sec->parentsec; psec; s = psec, psec = psec->parentsec) { if (!psec || s->order >= 0) { @@ -798,7 +775,7 @@ void section_order(void) /* create a section order consistent */ } } } - sec = secorder[isec]; + Section* sec = secorder[isec]; for (ch = sec->child; ch; ch = ch->sibling) { secorder[order] = ch; ch->order = order; diff --git a/src/nrnoc/treeset.cpp b/src/nrnoc/treeset.cpp index f27b02fa4f..e7fc5c78c0 100644 --- a/src/nrnoc/treeset.cpp +++ b/src/nrnoc/treeset.cpp @@ -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; @@ -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; @@ -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)) { @@ -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); diff --git a/src/oc/hoclist.h b/src/oc/hoclist.h index de9666efa0..3a7036dadc 100644 --- a/src/oc/hoclist.h +++ b/src/oc/hoclist.h @@ -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) /*