Skip to content

Commit

Permalink
Add iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Cornu committed Dec 6, 2024
1 parent 942d5ef commit 8bd14c0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 80 deletions.
16 changes: 4 additions & 12 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 (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 @@ -361,16 +357,12 @@ void BrowserAccept::execute() {
SectionBrowserImpl::SectionBrowserImpl() {
struct hoc_Item* qsec;
scnt_ = 0;
// ForAllSections(sec) //{
ITERATE(qsec, section_list) {
Section* sec = hocSEC(qsec);
for (Section* sec: range_sec(section_list)) {

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

View check run for this annotation

Codecov / codecov/patch

src/nrniv/secbrows.cpp#L360

Added line #L360 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 365 in src/nrniv/secbrows.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrniv/secbrows.cpp#L365

Added line #L365 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
19 changes: 5 additions & 14 deletions src/nrnoc/extcelln.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,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 (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 @@ -491,9 +490,7 @@ void ext_con_coef(void) /* setup a and b */
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 +526,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 (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 +544,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 +571,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 (Section* sec: range_sec(section_list)) {
if (sec->pnode[0]->extnode) {
for (j = 0; j < sec->nnode; j++) {
nd = sec->pnode[j];
Expand Down
35 changes: 12 additions & 23 deletions src/nrnoc/solve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,15 @@ double debugsolve(void) /* returns solution error */
{
short inode;
int i;
Section *sec, *psec, *ch;
Section *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 */
// ForAllSections(sec)
for (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 @@ -125,8 +124,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 (Section* sec: range_sec(section_list)) {
for (inode = sec->nnode - 1; inode >= 0; inode--) {
ndP = sec->pnode + inode;
nd = sec->pnode[inode];
Expand Down Expand Up @@ -295,8 +293,7 @@ void nrnhoc_topology(void) /* print the topology of the branched cable */

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 +431,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)) {

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

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/solve.cpp#L434

Added line #L434 was not covered by tests
sec->volatile_mark = 0;
}
}
Expand Down Expand Up @@ -744,15 +738,13 @@ 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 +757,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 @@ -779,8 +769,7 @@ void section_order(void) /* create a section order consistent */
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 772 in src/nrnoc/solve.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnoc/solve.cpp#L772

Added line #L772 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 +787,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 (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
27 changes: 26 additions & 1 deletion src/oc/hoclist.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ struct hoc_Item {
};
using hoc_List = hoc_Item;

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{(hoc_Item*)(iterable)};
}
};
return iterable_wrapper{iterable};
}

#define ITEM0 (hoc_Item*) 0
#define LIST0 (hoc_List*) 0

Expand Down Expand Up @@ -91,7 +116,7 @@ extern hoc_Item* hoc_l_lappendobj(hoc_List*, struct Object*);
extern void hoc_l_freelist(hoc_List**);
extern hoc_Item* hoc_l_next(hoc_Item*);
extern hoc_Item* hoc_l_prev(hoc_Item*);
extern void hoc_l_delete(hoc_Item*);
extern hoc_Item* hoc_l_delete(hoc_Item*);
extern void hoc_l_move(hoc_Item*, hoc_Item*, hoc_Item*);
extern void hoc_l_movelist(hoc_Item*, hoc_Item*, hoc_List*);
extern void hoc_l_replacstr(hoc_Item*, const char*);
Expand Down
4 changes: 3 additions & 1 deletion src/oc/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@ Item* lappendvoid(List* list, void* obj) {
return insertvoid(list, obj);
}

void hoc_l_delete(Item* item) {
Item* hoc_l_delete(Item* item) {
assert(item->itemtype); /* can't delete list */
Item* next = item->next;
item->next->prev = item->prev;
item->prev->next = item->next;
Free(item);
return next;
}

char* stralloc(const char* buf, char* rel) {
Expand Down

0 comments on commit 8bd14c0

Please sign in to comment.