Skip to content

Commit

Permalink
Add iterator (#3275)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Cornu authored Dec 10, 2024
1 parent 00ba007 commit baf9ea8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 96 deletions.
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 @@ 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;
}
}
Expand Down Expand Up @@ -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);
}
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 @@ 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];
Expand All @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -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, '-');
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
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

0 comments on commit baf9ea8

Please sign in to comment.