Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 8085e7c

Browse files
Nicolas Cornupramodk
authored andcommitted
nrn_setup: cleaning up variable usage (#232)
* use auto and scoped variables
1 parent a78233a commit 8085e7c

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

coreneuron/nrniv/nrn_setup.cpp

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,6 @@ void read_phase2(FileHandler& F, int imult, NrnThread& nt) {
12151215
NrnThreadChkpnt& ntc = nrnthread_chkpnt[nt.id];
12161216
ntc.file_id = gidgroups_w[nt.id];
12171217
#endif
1218-
NrnThreadMembList* tml;
12191218

12201219
int n_outputgid, ndiam, nmech, *tml_index, *ml_nodecount;
12211220
if (direct) {
@@ -1273,7 +1272,6 @@ void read_phase2(FileHandler& F, int imult, NrnThread& nt) {
12731272
/// together
12741273
// printf("ncell=%d end=%d nmech=%d\n", nt.ncell, nt.end, nmech);
12751274
// printf("nart=%d\n", nart);
1276-
NrnThreadMembList* tml_last = nullptr;
12771275
nt._ml_list = (Memb_list**)ecalloc_align(corenrn.get_memb_funcs().size(), sizeof(Memb_list*));
12781276

12791277
#if CHKPNTDEBUG
@@ -1300,14 +1298,16 @@ void read_phase2(FileHandler& F, int imult, NrnThread& nt) {
13001298
nt.stream_id = omp_get_thread_num();
13011299
#endif
13021300
auto& memb_func = corenrn.get_memb_funcs();
1301+
NrnThreadMembList* tml_last = nullptr;
13031302
for (int i = 0; i < nmech; ++i) {
1304-
tml = (NrnThreadMembList*)emalloc_align(sizeof(NrnThreadMembList));
1303+
auto tml = (NrnThreadMembList*)emalloc_align(sizeof(NrnThreadMembList));
1304+
tml->next = nullptr;
1305+
tml->index = tml_index[i];
1306+
13051307
tml->ml = (Memb_list*)ecalloc_align(1, sizeof(Memb_list));
13061308
tml->ml->_net_receive_buffer = nullptr;
13071309
tml->ml->_net_send_buffer = nullptr;
13081310
tml->ml->_permute = nullptr;
1309-
tml->next = nullptr;
1310-
tml->index = tml_index[i];
13111311
if (memb_func[tml->index].alloc == nullptr) {
13121312
hoc_execerror(memb_func[tml->index].sym, "mechanism does not exist");
13131313
}
@@ -1380,7 +1380,7 @@ void read_phase2(FileHandler& F, int imult, NrnThread& nt) {
13801380
// Memb_list.data points into the nt.data array.
13811381
// Also count the number of Point_process
13821382
int npnt = 0;
1383-
for (tml = nt.tml; tml; tml = tml->next) {
1383+
for (auto tml = nt.tml; tml; tml = tml->next) {
13841384
Memb_list* ml = tml->ml;
13851385
int type = tml->index;
13861386
int layout = corenrn.get_mech_data_layout()[type];
@@ -1409,7 +1409,7 @@ void read_phase2(FileHandler& F, int imult, NrnThread& nt) {
14091409
nt._actual_v = nt._data + 4 * ne;
14101410
nt._actual_area = nt._data + 5 * ne;
14111411
nt._actual_diam = ndiam ? nt._data + 6 * ne : nullptr;
1412-
for (tml = nt.tml; tml; tml = tml->next) {
1412+
for (auto tml = nt.tml; tml; tml = tml->next) {
14131413
Memb_list* ml = tml->ml;
14141414
ml->data = nt._data + (ml->data - (double*)0);
14151415
}
@@ -1444,7 +1444,7 @@ void read_phase2(FileHandler& F, int imult, NrnThread& nt) {
14441444
// Complete spec of Point_process except for the acell presyn_ field.
14451445
int itml = 0;
14461446
int dsz_inst = 0;
1447-
for (tml = nt.tml, itml = 0; tml; tml = tml->next, ++itml) {
1447+
for (auto tml = nt.tml; tml; tml = tml->next, ++itml) {
14481448
int type = tml->index;
14491449
Memb_list* ml = tml->ml;
14501450
int is_art = corenrn.get_is_artificial()[type];
@@ -1523,7 +1523,7 @@ void read_phase2(FileHandler& F, int imult, NrnThread& nt) {
15231523
// -9 (diam),
15241524
// or 0-999 (ion variables). Note that pdata has a layout and the
15251525
// type block in nt.data into which it indexes, has a layout.
1526-
for (tml = nt.tml; tml; tml = tml->next) {
1526+
for (auto tml = nt.tml; tml; tml = tml->next) {
15271527
int type = tml->index;
15281528
int layout = corenrn.get_mech_data_layout()[type];
15291529
int* pdata = tml->ml->pdata;
@@ -1623,14 +1623,14 @@ for (int i=0; i < nt.end; ++i) {
16231623
#endif
16241624

16251625
// specify the ml->_permute and sort the nodeindices
1626-
for (tml = nt.tml; tml; tml = tml->next) {
1626+
for (auto tml = nt.tml; tml; tml = tml->next) {
16271627
if (tml->ml->nodeindices) { // not artificial
16281628
permute_nodeindices(tml->ml, p);
16291629
}
16301630
}
16311631

16321632
// permute mechanism data, pdata (and values)
1633-
for (tml = nt.tml; tml; tml = tml->next) {
1633+
for (auto tml = nt.tml; tml; tml = tml->next) {
16341634
if (tml->ml->nodeindices) { // not artificial
16351635
permute_ml(tml->ml, tml->index, nt);
16361636
}
@@ -1660,7 +1660,7 @@ for (int i=0; i < nt.end; ++i) {
16601660
/* temporary array for dependencies */
16611661
int* mech_deps = (int*)ecalloc(memb_func.size(), sizeof(int));
16621662

1663-
for (tml = nt.tml; tml; tml = tml->next) {
1663+
for (auto tml = nt.tml; tml; tml = tml->next) {
16641664
/* initialize to null */
16651665
tml->dependencies = nullptr;
16661666
tml->ndependencies = 0;
@@ -1720,21 +1720,19 @@ for (int i=0; i < nt.end; ++i) {
17201720
/// Fill the BA lists
17211721
std::vector<BAMech*> bamap(memb_func.size());
17221722
for (int i = 0; i < BEFORE_AFTER_SIZE; ++i) {
1723-
BAMech* bam;
1724-
NrnThreadBAList *tbl, **ptbl;
17251723
for (int ii = 0; ii < memb_func.size(); ++ii) {
1726-
bamap[ii] = (BAMech*)0;
1724+
bamap[ii] = nullptr;
17271725
}
1728-
for (bam = corenrn.get_bamech()[i]; bam; bam = bam->next) {
1726+
for (auto bam = corenrn.get_bamech()[i]; bam; bam = bam->next) {
17291727
bamap[bam->type] = bam;
17301728
}
17311729
/* unnecessary but keep in order anyway */
1732-
ptbl = nt.tbl + i;
1733-
for (tml = nt.tml; tml; tml = tml->next) {
1730+
NrnThreadBAList **ptbl = nt.tbl + i;
1731+
for (auto tml = nt.tml; tml; tml = tml->next) {
17341732
if (bamap[tml->index]) {
17351733
Memb_list* ml = tml->ml;
1736-
tbl = (NrnThreadBAList*)emalloc(sizeof(NrnThreadBAList));
1737-
tbl->next = (NrnThreadBAList*)0;
1734+
auto tbl = (NrnThreadBAList*)emalloc(sizeof(NrnThreadBAList));
1735+
tbl->next = nullptr;
17381736
tbl->bam = bamap[tml->index];
17391737
tbl->ml = ml;
17401738
*ptbl = tbl;
@@ -1747,15 +1745,15 @@ for (int i=0; i < nt.end; ++i) {
17471745
// setup a list of types that have WATCH statement
17481746
{
17491747
int sz = 0; // count the types with WATCH
1750-
for (NrnThreadMembList* tml = nt.tml; tml; tml = tml->next) {
1748+
for (auto tml = nt.tml; tml; tml = tml->next) {
17511749
if (corenrn.get_watch_check()[tml->index]) {
17521750
++sz;
17531751
}
17541752
}
17551753
if (sz) {
17561754
nt._watch_types = (int*)ecalloc(sz + 1, sizeof(int)); // nullptr terminated
17571755
sz = 0;
1758-
for (NrnThreadMembList* tml = nt.tml; tml; tml = tml->next) {
1756+
for (auto tml = nt.tml; tml; tml = tml->next) {
17591757
if (corenrn.get_watch_check()[tml->index]) {
17601758
nt._watch_types[sz++] = tml->index;
17611759
}
@@ -1874,14 +1872,12 @@ for (int i=0; i < nt.end; ++i) {
18741872
if (type > 0) {
18751873
int index = pnt_offset[type] + pntindex[i]; /// Potentially uninitialized pnt_offset[],
18761874
/// check for previous assignments
1877-
Point_process* pnt = nt.pntprocs + index;
18781875
NetCon& nc = nt.netcons[i];
1879-
nc.target_ = pnt;
1876+
nc.target_ = nt.pntprocs + index;
18801877
nc.active_ = true;
18811878
}
18821879
}
18831880

1884-
int extracon_target_type = -1;
18851881
int extracon_target_nweight = 0;
18861882
if (nrn_setup_extracon > 0) {
18871883
// Fill in the extracon target_ and active_.
@@ -1891,7 +1887,7 @@ for (int i=0; i < nt.end; ++i) {
18911887
// Edge case: if there is no such synapse, let the target_ be nullptr
18921888
// and the netcon be inactive.
18931889
// Same pattern as algorithm for extracon netcon_srcgid above in phase1.
1894-
extracon_target_type = nrn_get_mechtype("ProbAMPANMDA_EMS");
1890+
int extracon_target_type = nrn_get_mechtype("ProbAMPANMDA_EMS");
18951891
assert(extracon_target_type > 0);
18961892
extracon_target_nweight = corenrn.get_pnt_receive_size()[extracon_target_type];
18971893
int j = 0;
@@ -1972,7 +1968,7 @@ for (int i=0; i < nt.end; ++i) {
19721968
ntc.bcpdcnt = new int[npnt];
19731969
ntc.bcptype = new int[npnt];
19741970
#endif
1975-
for (NrnThreadMembList* tml = nt.tml; tml; tml = tml->next) {
1971+
for (auto tml = nt.tml; tml; tml = tml->next) {
19761972
int type = tml->index;
19771973
if (!corenrn.get_bbcore_read()[type]) {
19781974
continue;
@@ -2121,13 +2117,9 @@ for (int i=0; i < nt.end; ++i) {
21212117
// begin with a size of 5% of the number of instances
21222118
nrb->_size = ml->nodecount;
21232119
// or at least 8
2124-
if (nrb->_size < 8) {
2125-
nrb->_size = 8;
2126-
}
2120+
nrb->_size = std::max(8, nrb->_size);
21272121
// but not more than nodecount
2128-
if (nrb->_size > ml->nodecount) {
2129-
nrb->_size = ml->nodecount;
2130-
}
2122+
nrb->_size = std::min(ml->nodecount, nrb->_size);
21312123

21322124
nrb->_pnt_index = (int*)ecalloc_align(nrb->_size, sizeof(int));
21332125
nrb->_displ = (int*)ecalloc_align(nrb->_size + 1, sizeof(int));

0 commit comments

Comments
 (0)