Skip to content

Commit 95bd396

Browse files
authored
Add .size() to the SectionList object, returns the number of sections in the list (#3520)
* used `.size()` to match what is in Vector() * add src/nrnoc/seclist.h * switch to static_cast in src/nrnoc/seclist.cpp
1 parent b1843f7 commit 95bd396

8 files changed

Lines changed: 77 additions & 51 deletions

File tree

cmake/NeuronFileLists.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(HEADER_FILES_TO_INSTALL
3333
nrnoc/nrnredef.h
3434
nrnoc/nrnversionmacros.h
3535
nrnoc/options.h
36+
nrnoc/seclist.h
3637
nrnoc/section_fwd.hpp
3738
nrnoc/treeset.h
3839
oc/classreg.h

docs/hoc/modelspec/programmatic/topology/seclist.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,12 @@ SectionList
165165
``forsec sectionlist {statement}``
166166

167167

168+
.. hoc:method:: SectionList.size
169+
170+
171+
Syntax:
172+
``.size()``
173+
174+
175+
Description:
176+
Returns the number of sections in the list.

docs/python/modelspec/programmatic/topology/seclist.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,16 @@ SectionList
234234
print(sec)
235235
236236
237+
----
238+
239+
240+
241+
.. method:: SectionList.size
242+
243+
244+
Syntax:
245+
``.size()``
246+
247+
248+
Description:
249+
Returns the number of sections in the list.

src/nrnoc/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <unistd.h>
1212
#endif
1313
#include "section.h"
14+
#include "seclist.h" // SectionList_reg
1415
#include "parse.hpp"
1516
#include "nrniv_mf.h"
1617
#include "cabvars.h"

src/nrnoc/nrn_ansi.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "membfunc.h" // nrn_bamech_t
44
#include "cabcode.h"
55
#include "neuron/container/data_handle.hpp"
6+
#include "seclist.h"
67
#include "neuron/container/generic_data_handle.hpp"
78
#include <memory>
89

@@ -82,7 +83,6 @@ void long_difus_solve(neuron::model_sorted_token const&, int method, NrnThread&
8283
extern void nrn_fihexec(int);
8384
extern int special_pnt_call(Object*, Symbol*, int);
8485
extern void nrn_mk_prop_pools(int);
85-
extern void SectionList_reg(void);
8686
extern void SectionRef_reg(void);
8787

8888
extern void hoc_symbol_tolerance(Symbol*, double);
@@ -129,7 +129,6 @@ extern void recalc_diam(void);
129129
extern bool nrn_use_fast_imem;
130130
void nrn_fast_imem_alloc();
131131
extern void nrn_calc_fast_imem(NrnThread*);
132-
extern Section* nrn_secarg(int iarg);
133132
extern void nrn_seg_or_x_arg(int iarg, Section** psec, double* px);
134133
extern void nrn_seg_or_x_arg2(int iarg, Section** psec, double* px);
135134
extern Section* nrnpy_pysecname2sec(const char*);

src/nrnoc/seclist.cpp

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <../../nrnconf.h>
22

3-
#include <functional>
4-
53
#define HOC_L_LIST 1
64
#include "section.h"
75
#include "neuron.h"
@@ -63,7 +61,7 @@ static void* constructor(Object* ho) {
6361

6462
static void destructor(void* v) {
6563
Item* q;
66-
List* sl = (List*) v;
64+
List* sl = static_cast<List*>(v);
6765
ITERATE(q, sl) {
6866
section_unref(q->element.sec);
6967
}
@@ -97,10 +95,8 @@ static void children1(List* sl, Section* sec) {
9795
}
9896

9997
static double children(void* v) {
100-
Section* sec;
101-
List* sl;
102-
sec = nrn_secarg(1);
103-
sl = (List*) v;
98+
Section* sec = nrn_secarg(1);
99+
List* sl = static_cast<List*>(v);
104100
children1(sl, sec);
105101
return 1.;
106102
}
@@ -121,17 +117,15 @@ static void subtree1(List* sl, Section* sec) {
121117
}
122118

123119
static double subtree(void* v) {
124-
Section* sec;
125-
List* sl;
126-
sec = nrn_secarg(1);
127-
sl = (List*) v;
120+
Section* sec = nrn_secarg(1);
121+
List* sl = static_cast<List*>(v);
128122
subtree1(sl, sec);
129123
return 1.;
130124
}
131125

132126
static double wholetree(void* v) {
133127
Section* sec = nrn_secarg(1);
134-
List* sl = (List*) v;
128+
List* sl = static_cast<List*>(v);
135129
/*find root*/
136130
Section* s = nullptr;
137131
for (s = sec; s->parentsec; s = s->parentsec) {
@@ -142,10 +136,8 @@ static double wholetree(void* v) {
142136
}
143137

144138
static double allroots(void* v) {
145-
List* sl;
139+
List* sl = static_cast<List*>(v);
146140
Item* qsec;
147-
sl = (List*) v;
148-
// ForAllSections(sec)
149141
ITERATE(qsec, section_list) {
150142
Section* sec = hocSEC(qsec);
151143
if (!sec->parentsec) {
@@ -158,7 +150,7 @@ static double allroots(void* v) {
158150
}
159151

160152
static double seclist_remove(void* v) {
161-
List* sl = (List*) v;
153+
List* sl = static_cast<List*>(v);
162154
int i = 0;
163155
#if USE_PYTHON
164156
if (!ifarg(1) || (*hoc_objgetarg(1))->ctemplate->sym == nrnpy_pyobj_sym_) {
@@ -182,9 +174,9 @@ static double seclist_remove(void* v) {
182174
o = *hoc_objgetarg(1);
183175
check_obj_type(o, "SectionList");
184176
seclist_iterate_remove(sl, [](Section* s) { s->volatile_mark = 0; });
185-
sl = (List*) o->u.this_pointer;
177+
sl = static_cast<List*>(o->u.this_pointer);
186178
seclist_iterate_remove(sl, [](Section* s) { s->volatile_mark = 1; });
187-
sl = (List*) v;
179+
sl = static_cast<List*>(v);
188180
Item* q1;
189181
for (Item* q = sl->next; q != sl; q = q1) {
190182
q1 = q->next;
@@ -200,8 +192,8 @@ static double seclist_remove(void* v) {
200192
}
201193

202194
static double unique(void* v) {
203-
Item* q1;
204-
List* sl = (List*) v;
195+
Item* q1 = nullptr;
196+
List* sl = static_cast<List*>(v);
205197
hoc_return_type_code = 1; /* integer */
206198
seclist_iterate_remove(sl, [](Section* s) { s->volatile_mark = 0; });
207199
int i = 0; /* number deleted */
@@ -218,7 +210,7 @@ static double unique(void* v) {
218210
}
219211

220212
static double contains(void* v) {
221-
List* sl = (List*) v;
213+
List* sl = static_cast<List*>(v);
222214
hoc_return_type_code = 2; /* boolean */
223215
Section* s = nrn_secarg(1);
224216
return seclist_iterate_remove_until(
@@ -228,7 +220,7 @@ static double contains(void* v) {
228220
}
229221

230222
static double printnames(void* v) {
231-
List* sl = (List*) v;
223+
List* sl = static_cast<List*>(v);
232224
seclist_iterate_remove(sl, [](Section* s) {
233225
if (s->prop) {
234226
Printf("%s\n", secname(s));
@@ -237,6 +229,15 @@ static double printnames(void* v) {
237229
return 1.;
238230
}
239231

232+
double seclist_size(void* v) {
233+
double count = 0.;
234+
List* sl = static_cast<List*>(v);
235+
for (Item* q1 = sl->next; q1 != sl; q1 = q1->next) {
236+
count++;
237+
}
238+
return count;
239+
}
240+
240241
static Member_func members[] = {{"append", append},
241242
{"remove", seclist_remove},
242243
{"wholetree", wholetree},
@@ -246,6 +247,7 @@ static Member_func members[] = {{"append", append},
246247
{"printnames", printnames},
247248
{"contains", contains},
248249
{"allroots", allroots},
250+
{"size", seclist_size},
249251
{nullptr, nullptr}};
250252

251253
void SectionList_reg(void) {
@@ -265,28 +267,24 @@ static void check(Object* ob) {
265267
}
266268
}
267269

268-
void forall_sectionlist(void) {
269-
Inst* savepc = pc;
270-
Item *q, *q1;
271-
Section* sec;
272-
List* sl;
273-
Object* ob;
274-
Object** obp;
275-
int istk;
276-
270+
void forall_sectionlist() {
277271
/* if arg is a string use forall_section */
278272
if (hoc_stacktype() == STRING) {
279273
forall_section();
280274
return;
281275
}
282-
obp = hoc_objpop();
283-
ob = *obp;
276+
277+
Inst* savepc = pc;
278+
Object** obp = hoc_objpop();
279+
Object* ob = *obp;
284280
check(ob);
285-
sl = (List*) (ob->u.this_pointer);
286-
istk = nrn_isecstack();
287-
for (q = sl->next; q != sl; q = q1) {
281+
List* sl = static_cast<List*>(ob->u.this_pointer);
282+
int istk = nrn_isecstack();
283+
284+
Item* q1;
285+
for (Item* q = sl->next; q != sl; q = q1) {
288286
q1 = q->next;
289-
sec = q->element.sec;
287+
Section* sec = q->element.sec;
290288
if (!sec->prop) {
291289
hoc_l_delete(q);
292290
section_unref(sec);
@@ -313,19 +311,19 @@ void forall_sectionlist(void) {
313311
}
314312
}
315313

316-
void hoc_ifseclist(void) {
317-
Inst* savepc = pc;
318-
Section* sec = chk_access();
319-
314+
void hoc_ifseclist() {
320315
/* if arg is a string use forall_section */
321316
if (hoc_stacktype() == STRING) {
322317
hoc_ifsec();
323318
return;
324319
}
320+
321+
Inst* savepc = pc;
322+
Section* sec = chk_access();
325323
Object** obp = hoc_objpop();
326324
Object* ob = *obp;
327325
check(ob);
328-
List* sl = (List*) (ob->u.this_pointer);
326+
List* sl = static_cast<List*>(ob->u.this_pointer);
329327
if (seclist_iterate_remove_until(
330328
sl,
331329
[&](Item*) {

src/nrnoc/seclist.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
struct Section;
4+
5+
void SectionList_reg(void);
6+
void lvappendsec_and_ref(void* sl, Section* sec);
7+
Section* nrn_secarg(int iarg);
8+
double seclist_size(void* sl);
9+
void forall_sectionlist();
10+
void hoc_ifseclist();

src/nrnpython/nrnpy_hoc.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "ocjump.h"
1616
#include "oclist.h"
1717
#include "shapeplt.h"
18+
#include "seclist.h" // lvappendsec_and_ref, seclist_size
1819

1920
#include <cstdint>
2021
#include <vector>
@@ -34,7 +35,6 @@ extern void (*nrnpy_restore_savestate)(int64_t, char*);
3435
extern void (*nrnpy_store_savestate)(char** save_data, uint64_t* save_data_size);
3536
extern void (*nrnpy_decref)(void* pyobj);
3637
extern double (*nrnpy_call_func)(Object*, double);
37-
extern void lvappendsec_and_ref(void* sl, Section* sec);
3838
extern void hoc_pushs(Symbol*);
3939
extern double* hoc_evalpointer();
4040
extern double cable_prop_eval(Symbol* sym);
@@ -1639,12 +1639,7 @@ static int araychk(Arrayinfo* a, PyHocObject* po, int ix) {
16391639

16401640
static Py_ssize_t seclist_count(Object* ho) {
16411641
assert(ho->ctemplate == hoc_sectionlist_template_);
1642-
hoc_List* sl = (hoc_List*) (ho->u.this_pointer);
1643-
Py_ssize_t n = 0;
1644-
for (hoc_Item* q1 = sl->next; q1 != sl; q1 = q1->next) {
1645-
n++;
1646-
}
1647-
return n;
1642+
return static_cast<Py_ssize_t>(seclist_size(static_cast<hoc_List*>(ho->u.this_pointer)));
16481643
}
16491644

16501645
static Py_ssize_t hocobj_len(PyObject* self) {

0 commit comments

Comments
 (0)