NEURON
ocptrvector.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 /*
3  construct a vector of pointers to variables and
4  provide fast scatter/gather between Vector and those variables.
5  p = new PtrVector(size)
6  p.pset(i, &varname)
7  val = p.getval(i)
8  p.setval(i, value)
9  p.scatter(Vector)
10  p.gather(Vector)
11 */
12 #include "classreg.h"
13 #include "code.h"
14 #include "oc2iv.h"
15 #include "ocptrvector.h"
16 #include "objcmd.h"
17 #include "ivocvect.h"
18 #if HAVE_IV
19 #include "graph.h"
20 #endif
21 #include "gui-redirect.h"
22 
23 static double dummy;
24 
26 
27 OcPtrVector::OcPtrVector(std::size_t sz)
28  : pd_{sz, neuron::container::data_handle<double>{neuron::container::do_not_search, &dummy}} {}
29 
31  if (label_) {
32  free(label_);
33  } // allocated by strdup
34 }
35 
36 void OcPtrVector::resize(int sz) {
37  pd_.resize(sz,
39 }
40 
42  assert(i < pd_.size());
43  pd_[i] = std::move(dh);
44 }
45 
46 void OcPtrVector::scatter(double* src, int sz) {
47  assert(pd_.size() == sz);
48  for (int i = 0; i < sz; ++i) {
49  *pd_[i] = src[i];
50  }
51 }
52 
53 void OcPtrVector::gather(double* dest, int sz) {
54  assert(pd_.size() == sz);
55  for (int i = 0; i < sz; ++i) {
56  dest[i] = *pd_[i];
57  }
58 }
59 
60 void OcPtrVector::setval(int i, double x) {
61  assert(i < pd_.size());
62  *pd_[i] = x;
63 }
64 
65 double OcPtrVector::getval(int i) {
66  assert(i < pd_.size());
67  return *pd_[i];
68 }
69 
70 static const char* nullstr = "";
71 
72 static const char** ptr_label(void* v) {
73  char*& s = ((OcPtrVector*) v)->label_;
74  if (ifarg(1)) {
75  if (s) {
76  free(s);
77  }
78  s = strdup(gargstr(1));
79  }
80  if (s) {
81  return (const char**) (&((OcPtrVector*) v)->label_);
82  }
83  return &nullstr;
84 }
85 
86 static double resize(void* v) {
88  ((OcPtrVector*) v)->resize((int(chkarg(1, 1., 2e9))));
89  return double(((OcPtrVector*) v)->size());
90 }
91 
92 static double get_size(void* v) {
94  return ((OcPtrVector*) v)->size();
95 }
96 
97 static double pset(void* v) {
98  OcPtrVector* opv = (OcPtrVector*) v;
99  int i = int(chkarg(1, 0., opv->size()));
100  opv->pset(i, hoc_hgetarg<double>(2));
101  return opv->getval(i);
102 }
103 
104 static double getval(void* v) {
105  OcPtrVector* opv = (OcPtrVector*) v;
106  int i = int(chkarg(1, 0., opv->size()));
107  return opv->getval(i);
108 }
109 
110 static double setval(void* v) {
111  OcPtrVector* opv = (OcPtrVector*) v;
112  int i = int(chkarg(1, 0., opv->size()));
113  opv->setval(i, *hoc_getarg(2));
114  return opv->getval(i);
115 }
116 
117 static double scatter(void* v) {
118  OcPtrVector* opv = (OcPtrVector*) v;
119  Vect* src = vector_arg(1);
120  opv->scatter(vector_vec(src), vector_capacity(src));
121  return 0.;
122 }
123 
124 static double gather(void* v) {
125  OcPtrVector* opv = (OcPtrVector*) v;
126  Vect* dest = vector_arg(1);
127  opv->gather(vector_vec(dest), vector_capacity(dest));
128  return 0.;
129 }
130 
131 // a copy of ivocvect::v_plot with y+i replaced by y[i]
132 static int narg() {
133  int i = 0;
134  while (ifarg(i++))
135  ;
136  return i - 2;
137 }
138 
139 static double ptr_plot(void* v) {
141  OcPtrVector* opv = (OcPtrVector*) v;
142 #if HAVE_IV
143  if (hoc_usegui) {
144  int i;
145  auto const& y = opv->pd_;
146  auto n = opv->size();
147  char* label = opv->label_;
148 
149  Object* ob1 = *hoc_objgetarg(1);
150  check_obj_type(ob1, "Graph");
151  Graph* g = (Graph*) (ob1->u.this_pointer);
152 
153  GraphVector* gv = new GraphVector("");
154 
155  if (ifarg(5)) {
156  hoc_execerror("PtrVector.plot:", "too many arguments");
157  }
158  if (narg() == 3) {
159  gv->color((colors->color(int(*getarg(2)))));
160  gv->brush((brushes->brush(int(*getarg(3)))));
161  } else if (narg() == 4) {
162  gv->color((colors->color(int(*getarg(3)))));
163  gv->brush((brushes->brush(int(*getarg(4)))));
164  }
165 
166  if (narg() == 2 || narg() == 4) {
167  // passed a vector or xinterval and possibly line attributes
168  if (hoc_is_object_arg(2)) {
169  // passed a vector
170  Vect* vp2 = vector_arg(2);
171  n = std::min(n, vp2->size());
172  for (i = 0; i < n; ++i)
173  gv->add(vp2->elem(i), y[i]);
174  } else {
175  // passed xinterval
176  double interval = *getarg(2);
177  for (i = 0; i < n; ++i)
178  gv->add(i * interval, y[i]);
179  }
180  } else {
181  // passed line attributes or nothing
182  for (i = 0; i < n; ++i)
183  gv->add(i, y[i]);
184  }
185 
186  if (label) {
187  GLabel* glab = g->label(label);
188  gv->label(glab);
189  ((GraphItem*) g->component(g->glyph_index(glab)))->save(false);
190  }
191  g->append(new GPolyLineItem(gv));
192 
193  g->flush();
194  }
195 #endif
196  return 0.0;
197 }
198 
199 
200 static Member_func members[] = {{"size", get_size},
201  {"resize", resize},
202  {"pset", pset},
203  {"setval", setval},
204  {"getval", getval},
205  {"scatter", scatter},
206  {"gather", gather},
207  {"plot", ptr_plot},
208  {nullptr, nullptr}};
209 
210 static Member_ret_str_func retstr_members[] = {{"label", ptr_label}, {nullptr, nullptr}};
211 
212 static void* cons(Object*) {
213  int sz;
214  sz = int(chkarg(1, 1., 2e9));
215  OcPtrVector* ocpv = new OcPtrVector(sz);
216  return (void*) ocpv;
217 }
218 
219 static void destruct(void* v) {
220  delete (OcPtrVector*) v;
221 }
222 
224  class2oc("PtrVector", cons, destruct, members, nullptr, retstr_members);
225  pv_class_sym_ = hoc_lookup("PtrVector");
226 }
const Brush * brush(int) const
const Color * color(int) const
Definition: graph.h:418
virtual void save(std::ostream &, Coord, Coord)
void brush(const Brush *)
void color(const Color *)
GLabel * label() const
Definition: graph.h:298
Definition: graph.h:54
virtual GlyphIndex glyph_index(const Glyph *)
void flush()
GLabel * label(float x, float y, const char *s, int fixtype, float scale, float x_align, float y_align, const Color *)
void add(float, neuron::container::data_handle< double >)
size_t size() const
Definition: ivocvect.h:42
double & elem(int n)
Definition: ivocvect.h:26
virtual void append(Glyph *)
virtual Glyph * component(GlyphIndex) const
void class2oc(const char *, ctor_f *cons, dtor_f *destruct, Member_func *, Member_ret_obj_func *, Member_ret_str_func *)
Definition: hoc_oop.cpp:1631
char * gargstr(int narg)
Definition: code2.cpp:227
HocReturnType hoc_return_type_code
Definition: code.cpp:42
#define v
Definition: md1redef.h:11
#define i
Definition: md1redef.h:19
double chkarg(int, double low, double high)
Definition: code2.cpp:626
BrushPalette * brushes
ColorPalette * colors
int hoc_is_object_arg(int narg)
Definition: code.cpp:876
double * hoc_getarg(int narg)
Definition: code.cpp:1641
void check_obj_type(Object *obj, const char *type_name)
Definition: hoc_oop.cpp:2098
IvocVect * vector_arg(int i)
Definition: ivocvect.cpp:265
Symbol * hoc_lookup(const char *)
Definition: symbol.cpp:59
#define TRY_GUI_REDIRECT_METHOD_ACTUAL_DOUBLE(name, sym, v)
Definition: gui-redirect.h:16
int hoc_usegui
Definition: hoc.cpp:121
#define assert(ex)
Definition: hocassrt.h:24
#define getarg
Definition: hocdec.h:17
Object ** hoc_objgetarg(int)
Definition: code.cpp:1614
void move(Item *q1, Item *q2, Item *q3)
Definition: list.cpp:200
double * vector_vec(IvocVect *v)
Definition: ivocvect.cpp:19
void hoc_execerror(const char *s1, const char *s2)
Definition: nrnoc_aux.cpp:39
int vector_capacity(IvocVect *v)
Definition: ivocvect.cpp:16
constexpr do_not_search_t do_not_search
Definition: data_handle.hpp:11
In mechanism libraries, cannot use auto const token = nrn_ensure_model_data_are_sorted(); because the...
Definition: tnode.hpp:17
int const size_t const size_t n
Definition: nrngsl.h:10
s
Definition: multisend.cpp:521
int ifarg(int)
Definition: code.cpp:1607
static const char ** ptr_label(void *v)
Definition: ocptrvector.cpp:72
static Symbol * pv_class_sym_
Definition: ocptrvector.cpp:25
static double pset(void *v)
Definition: ocptrvector.cpp:97
static double getval(void *v)
static Member_ret_str_func retstr_members[]
static Member_func members[]
static void * cons(Object *)
static double gather(void *v)
static double setval(void *v)
static void destruct(void *v)
void OcPtrVector_reg()
static const char * nullstr
Definition: ocptrvector.cpp:70
static int narg()
static double resize(void *v)
Definition: ocptrvector.cpp:86
static double get_size(void *v)
Definition: ocptrvector.cpp:92
static double ptr_plot(void *v)
static double dummy
Definition: ocptrvector.cpp:23
static double scatter(void *v)
Definition: hocdec.h:173
void * this_pointer
Definition: hocdec.h:178
union Object::@47 u
void resize(int)
Definition: ocptrvector.cpp:36
virtual ~OcPtrVector()
Definition: ocptrvector.cpp:30
void pset(int i, neuron::container::data_handle< double > dh)
Definition: ocptrvector.cpp:41
void scatter(double *, int sz)
Definition: ocptrvector.cpp:46
double getval(int)
Definition: ocptrvector.cpp:65
std::vector< neuron::container::data_handle< double > > pd_
Definition: ocptrvector.h:21
std::size_t size() const
Definition: ocptrvector.h:10
char * label_
Definition: ocptrvector.h:22
OcPtrVector(std::size_t sz)
Definition: ocptrvector.cpp:27
void gather(double *, int sz)
Definition: ocptrvector.cpp:53
void setval(int, double)
Definition: ocptrvector.cpp:60
Definition: model.h:47