Skip to content

Commit

Permalink
Remove some PtrList and List use outside context of IV (#1815)
Browse files Browse the repository at this point in the history
* Tests added to cover some of the PtrList transformations.
Co-authored-by: Michael Hines <michael.hines@yale.edu>
  • Loading branch information
alkino committed May 31, 2022
1 parent 1a94b30 commit 80357bc
Show file tree
Hide file tree
Showing 18 changed files with 473 additions and 638 deletions.
18 changes: 9 additions & 9 deletions src/gnu/RndInt.h
Expand Up @@ -91,22 +91,22 @@ class RandomInteger


inline RandomInteger::RandomInteger(long low, long high, RNG *gen)
: pLow((low < high) ? low : high),
pHigh((low < high) ? high : low),
pGenerator(gen)
: pGenerator(gen),
pLow((low < high) ? low : high),
pHigh((low < high) ? high : low)
{}

inline RandomInteger::RandomInteger(long high, RNG *gen)
: pLow((0 < high) ? 0 : high),
pHigh((0 < high) ? high : 0),
pGenerator(gen)
: pGenerator(gen),
pLow((0 < high) ? 0 : high),
pHigh((0 < high) ? high : 0)
{}


inline RandomInteger::RandomInteger(RNG *gen)
: pLow(0),
pHigh(1),
pGenerator(gen)
: pGenerator(gen),
pLow(0),
pHigh(1)
{}

inline RNG* RandomInteger::generator() const { return pGenerator;}
Expand Down
42 changes: 17 additions & 25 deletions src/ivoc/datapath.cpp
Expand Up @@ -75,9 +75,7 @@ PathValue::~PathValue() {
}
}

declarePtrList(StringList, char);
implementPtrList(StringList, char);

using StringList = std::vector<char*>;

class HocDataPathImpl {
private:
Expand Down Expand Up @@ -172,7 +170,6 @@ Symbol* HocDataPaths::retrieve_sym(double* pd) {

void HocDataPaths::append(char** pd) {
// printf("HocDataPaths::append\n");
PathValue* pv;
if (*pd && impl_->table_.find((void*) pd) == impl_->table_.end()) {
PathValue* pv = new PathValue;
pv->str = *pd;
Expand Down Expand Up @@ -250,11 +247,8 @@ PathValue* HocDataPathImpl::found_v(void* v, const char* buf, Symbol* sym) {
if (pathstyle_ != 2) {
char path[500];
CopyString cs("");
long i, cnt;
int len = 0;
cnt = strlist_.count();
for (i = 0; i < cnt; ++i) {
sprintf(path, "%s%s.", cs.string(), strlist_.item(i));
for (const auto& str: strlist_) {
sprintf(path, "%s%s.", cs.string(), str);
cs = path;
}
sprintf(path, "%s%s", cs.string(), buf);
Expand Down Expand Up @@ -351,21 +345,21 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
if (obp[i]->u.dataspace != od) {
sprintf(buf, "%s%s", sym->name, hoc_araystr(sym, i, od));
cs = buf;
strlist_.append((char*) cs.string());
strlist_.push_back((char*) cs.string());
obp[i]->recurse = 1;
search(obp[i]->u.dataspace, obp[i]->ctemplate->symtable);
obp[i]->recurse = 0;
strlist_.remove(strlist_.count() - 1);
strlist_.pop_back();
}
} else {
/* point processes */
#if CABLE
if (t->is_point_) {
sprintf(buf, "%s%s", sym->name, hoc_araystr(sym, i, od));
cs = buf;
strlist_.append((char*) cs.string());
strlist_.push_back((char*) cs.string());
search((Point_process*) obp[i]->u.this_pointer, sym);
strlist_.remove(strlist_.count() - 1);
strlist_.pop_back();
}
#endif
/* seclists, object lists */
Expand All @@ -380,9 +374,9 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
if (pitm[i]) {
sprintf(buf, "%s%s", sym->name, hoc_araystr(sym, i, od));
cs = buf;
strlist_.append((char*) cs.string());
strlist_.push_back((char*) cs.string());
search(hocSEC(pitm[i]));
strlist_.remove(strlist_.count() - 1);
strlist_.pop_back();
}
}
} break;
Expand All @@ -394,7 +388,7 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
Object* obj = OBJ(q);
sprintf(buf, "%s[%d]", sym->name, obj->index);
cs = buf;
strlist_.append((char*) cs.string());
strlist_.push_back((char*) cs.string());
if (!t->constructor) {
search(obj->u.dataspace, t->symtable);
} else {
Expand All @@ -404,7 +398,7 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
}
#endif
}
strlist_.remove(strlist_.count() - 1);
strlist_.pop_back();
}
} break;
}
Expand All @@ -413,7 +407,6 @@ void HocDataPathImpl::search(Objectdata* od, Symlist* sl) {
}

void HocDataPathImpl::search_vectors() {
int i, cnt;
char buf[200];
CopyString cs("");
cTemplate* t = sym_vec->u.ctemplate;
Expand All @@ -422,17 +415,17 @@ void HocDataPathImpl::search_vectors() {
Object* obj = OBJ(q);
sprintf(buf, "%s[%d]", sym_vec->name, obj->index);
cs = buf;
strlist_.append((char*) cs.string());
strlist_.push_back((char*) cs.string());
Vect* vec = (Vect*) obj->u.this_pointer;
int size = vec->size();
double* pd = vector_vec(vec);
for (i = 0; i < size; ++i) {
for (size_t i = 0; i < size; ++i) {
if (pd[i] == sentinal) {
sprintf(buf, "x[%d]", i);
sprintf(buf, "x[%zu]", i);
found(pd + i, buf, sym_vec);
}
}
strlist_.remove(strlist_.count() - 1);
strlist_.pop_back();
}
}

Expand All @@ -447,9 +440,9 @@ void HocDataPathImpl::search_pysec() {
Section* sec = hocSEC(qsec);
if (sec->prop && sec->prop->dparam[PROP_PY_INDEX]._pvoid) {
cs = secname(sec);
strlist_.append((char*) cs.string());
strlist_.push_back((char*) cs.string());
search(sec);
strlist_.remove(strlist_.count() - 1);
strlist_.pop_back();
}
}
#endif
Expand All @@ -474,7 +467,6 @@ void HocDataPathImpl::search(Section* sec) {
}
void HocDataPathImpl::search(Node* nd, double x) {
char buf[100];
int i, cnt;
CopyString cs("");
if (NODEV(nd) == sentinal) {
sprintf(buf, "v(%g)", x);
Expand Down
22 changes: 9 additions & 13 deletions src/ivoc/ivoc.cpp
@@ -1,6 +1,6 @@
#include <../../nrnconf.h>

#include <OS/list.h>
#include <vector>
#include <ocnotify.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -19,12 +19,11 @@ extern double (*nrnpy_object_to_double_)(Object*);
#include "bimap.hpp"

#if USE_PTHREAD
static MUTDEC
static MUTDEC;
#endif

typedef void (*PF)(void*, int);
declareList(FList, PF);
implementList(FList, PF);
using PF = void (*)(void*, int);
using FList = std::vector<PF>;

static FList* f_list;

Expand All @@ -47,8 +46,7 @@ void nrn_notify_freed(PF pf) {
if (!f_list) {
f_list = new FList;
}
f_list->append(pf);
// printf("appended to f_list in ivoc.cpp\n");
f_list->push_back(pf);
}

void nrn_notify_when_void_freed(void* p, Observer* ob) {
Expand Down Expand Up @@ -94,18 +92,16 @@ void notify_pointer_freed(void* pt) {
}
void notify_freed(void* p) {
if (f_list) {
long i, n = f_list->count();
for (i = 0; i < n; ++i) {
(*f_list->item(i))(p, 1);
for (PF f: *f_list) {
f(p, 1);
}
}
notify_pointer_freed(p);
}
void notify_freed_val_array(double* p, size_t size) {
if (f_list) {
long i, n = f_list->count();
for (i = 0; i < n; ++i) {
(*f_list->item(i))((void*) p, size);
for (PF f: *f_list) {
f((void*) p, size);
}
}
if (pdob) {
Expand Down
85 changes: 31 additions & 54 deletions src/ivoc/ivocrand.cpp
Expand Up @@ -11,7 +11,7 @@
#include "oc2iv.h"
#include "nrnisaac.h"

#include <OS/list.h>
#include <vector>
#include <ocnotify.h>
#include "ocobserv.h"
#include <nrnran123.h>
Expand Down Expand Up @@ -55,8 +55,8 @@ class RandomPlay: public Observer, public Resource {
double* px_;
};

declarePtrList(RandomPlayList, RandomPlay)
implementPtrList(RandomPlayList, RandomPlay) static RandomPlayList* random_play_list_;
using RandomPlayList = std::vector<RandomPlay*>;
static RandomPlayList* random_play_list_;

extern "C" {
double nrn_random_pick(Rand* r);
Expand Down Expand Up @@ -185,7 +185,7 @@ RandomPlay::RandomPlay(Rand* r, double* px) {
// printf("RandomPlay\n");
r_ = r;
px_ = px;
random_play_list_->append(this);
random_play_list_->push_back(this);
ref();
nrn_notify_when_double_freed(px_, this);
nrn_notify_when_void_freed((void*) r->obj_, this);
Expand All @@ -198,11 +198,10 @@ void RandomPlay::play() {
*px_ = (*(r_->rand))();
}
void RandomPlay::list_remove() {
long i, cnt = random_play_list_->count();
for (i = 0; i < cnt; ++i) {
if (random_play_list_->item(i) == (RandomPlay*) this) {
for (auto it = random_play_list_->begin(); it != random_play_list_->end(); ++it) {
if (*it == (RandomPlay*) this) {
// printf("RandomPlay %p removed from list cnt=%d i=%d %p\n", this, cnt, i);
random_play_list_->remove(i);
random_play_list_->erase(it);
unref_deferred();
break;
}
Expand Down Expand Up @@ -618,57 +617,35 @@ static double r_play(void* r) {
}

extern "C" void nrn_random_play() {
long i, cnt = random_play_list_->count();
for (i = 0; i < cnt; ++i) {
random_play_list_->item(i)->play();
for (const auto& rp: *random_play_list_) {
rp->play();
}
}


static Member_func r_members[] = {"ACG",
r_ACG,
"MLCG",
r_MLCG,
"Isaac64",
r_Isaac64,
"MCellRan4",
r_MCellRan4,
"Random123",
r_nrnran123,
"Random123_globalindex",
r_ran123_globalindex,
"seq",
r_sequence,
"repick",
r_repick,
"uniform",
r_uniform,
"discunif",
r_discunif,
"normal",
r_normal,
"lognormal",
r_lognormal,
"binomial",
r_binomial,
"poisson",
r_poisson,
"geometric",
r_geometric,
"hypergeo",
r_hypergeo,
"negexp",
r_negexp,
"erlang",
r_erlang,
"weibull",
r_weibull,
"play",
r_play,
0,
0};
static Member_func r_members[] = {{"ACG", r_ACG},
{"MLCG", r_MLCG},
{"Isaac64", r_Isaac64},
{"MCellRan4", r_MCellRan4},
{"Random123", r_nrnran123},
{"Random123_globalindex", r_ran123_globalindex},
{"seq", r_sequence},
{"repick", r_repick},
{"uniform", r_uniform},
{"discunif", r_discunif},
{"normal", r_normal},
{"lognormal", r_lognormal},
{"binomial", r_binomial},
{"poisson", r_poisson},
{"geometric", r_geometric},
{"hypergeo", r_hypergeo},
{"negexp", r_negexp},
{"erlang", r_erlang},
{"weibull", r_weibull},
{"play", r_play},
{nullptr, nullptr}};

void Random_reg() {
class2oc("Random", r_cons, r_destruct, r_members, NULL, NULL, NULL);
random_play_list_ = new RandomPlayList();
random_play_list_ = new RandomPlayList;
}

0 comments on commit 80357bc

Please sign in to comment.