NEST  2.6.0,not_revisioned_source_dir@0
dictutils.h
Go to the documentation of this file.
1 /*
2  * dictutils.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef DICTUTILS_H
24 #define DICTUTILS_H
25 
26 #include "dictdatum.h"
27 #include "namedatum.h"
28 #include "tokenutils.h"
29 #include "arraydatum.h"
30 #include "integerdatum.h"
31 #include "doubledatum.h"
32 #include <string>
33 #include <algorithm>
34 #include <functional>
35 
52 template<typename FT>
53 FT getValue(const DictionaryDatum &d, Name const n)
54 {
55  // We must take a reference, so that access information can be stored in the
56  // token.
57  const Token& t = d->lookup2(n);
58 
59  /* if (!t.empty()) */
60  /* throw UndefinedName(n.toString()); */
61 
62  return getValue<FT>(t);
63 }
64 
78 inline
79 double get_double_in_range(const DictionaryDatum &d, Name const n, double min, double max, int mode=2)
80 {
81  // We must take a reference, so that access information can be stored in the
82  // token.
83  const Token& t = d->lookup2(n);
84  DoubleDatum *dd= dynamic_cast<DoubleDatum *>(t.datum());
85  double x=0.0;
86 
87  if (dd != 0)
88  {
89  x = dd->get();
90  }
91  else
92  {
93  IntegerDatum *id= dynamic_cast<IntegerDatum *>(t.datum());
94  if (id == 0)
95  {
96  throw TypeMismatch();
97  }
98 
99  x = static_cast<double>(id->get());
100  }
101  switch(mode)
102  {
103  case 0:
104  if(min< x and x < max)
105  return x;
106  break;
107  case 1:
108  if (min <= x and x < max)
109  return x;
110  break;
111  case 2:
112  if (min <= x and x <= max)
113  return x;
114  break;
115  default:
116  return x;
117  }
118  throw RangeCheck();
119 }
120 
134 inline
135 long get_long_in_range(const DictionaryDatum &d, Name const n, long min, long max, int mode=2)
136 {
137  // We must take a reference, so that access information can be stored in the
138  // token.
139  const Token& t = d->lookup2(n);
140  DoubleDatum *dd= dynamic_cast<DoubleDatum *>(t.datum());
141  long x=0;
142 
143  if (dd != 0)
144  {
145  x = dd->get();
146  }
147  else
148  {
149  IntegerDatum *id= dynamic_cast<IntegerDatum *>(t.datum());
150  if (id == 0)
151  {
152  throw TypeMismatch();
153  }
154 
155  x = static_cast<double>(id->get());
156  }
157  switch(mode)
158  {
159  case 0:
160  if(min< x and x < max)
161  return x;
162  break;
163  case 1:
164  if (min <= x and x < max)
165  return x;
166  break;
167  case 2:
168  if (min <= x and x <= max)
169  return x;
170  break;
171  default:
172  return x;
173  }
174  throw RangeCheck();
175 }
176 
177 
178 
183 template<typename FT, class D>
184 void def2(DictionaryDatum &d, Name const n, FT const &value)
185 {
186  Token t = newToken2<FT,D>(value);
187  d->insert_move(n, t);
188 }
189 
195 template<typename FT>
196 void def(DictionaryDatum &d, Name const n, FT const &value)
197 {
198  Token t(value); // we hope that we have a constructor for this.
199  d->insert_move(n, t);
200 }
201 
206 template<typename FT, typename VT>
207 bool updateValue(DictionaryDatum const &d, Name const n, VT &value)
208 {
209  // We will test for the name, and do nothing if it does not exist,
210  // instead of simply trying to getValue() it and catching a possible
211  // exception. The latter works, however, but non-existing names are
212  // the rule with updateValue(), not the exception, hence using the
213  // exception mechanism would be inappropriate. (Markus pointed this
214  // out, 05.02.2001, Ruediger.)
215 
216  // We must take a reference, so that access information can be stored in the
217  // token.
218  const Token& t = d->lookup(n);
219 
220  if (t.empty())
221  return false;
222 
223  value = getValue<FT>(t);
224  return true;
225 }
226 
232 template<typename FT, typename VT, class C>
233 void updateValue2(DictionaryDatum const &d, Name const n, C &obj, void (C::* setfunc)(VT))
234 {
235  if (d->known(n)) // Does name exist in the dictionary?
236  {
237  // yes, call the function for update.
238  (obj.*setfunc)(getValue<FT>(d, n));
239  }
240 }
241 
242 
247 
248 
253 
254 
259 
260 
265 template<typename PropT>
266 inline
267 void append_property(DictionaryDatum &d, Name propname, const PropT &prop)
268 {
269  Token t = d->lookup(propname);
270  assert (!t.empty());
271 
272  ArrayDatum* arrd = dynamic_cast<ArrayDatum*>(t.datum());
273  assert(arrd != 0);
274 
275  Token prop_token(prop);
276  arrd->push_back_dont_clone(prop_token);
277 }
278 
283 template<>
284 inline
285 void append_property<std::vector<double> >(DictionaryDatum &d, Name propname, const std::vector<double> &prop)
286 {
287  Token t = d->lookup(propname);
288  assert (!t.empty());
289 
290  DoubleVectorDatum* arrd = dynamic_cast<DoubleVectorDatum*>(t.datum());
291  assert(arrd != 0);
292 
293  (*arrd)->insert((*arrd)->end(), prop.begin(), prop.end());
294 }
295 
296 
301 template<>
302 inline
303 void append_property<std::vector<long> >(DictionaryDatum &d, Name propname, const std::vector<long> &prop)
304 {
305  Token t = d->lookup(propname);
306  assert (!t.empty());
307 
308  IntVectorDatum* arrd = dynamic_cast<IntVectorDatum*>(t.datum());
309  assert(arrd != 0);
310 
311  (*arrd)->insert((*arrd)->end(), prop.begin(), prop.end());
312 }
313 
314 
321 void provide_property(DictionaryDatum&, Name, const std::vector<double>&);
322 
329 void provide_property(DictionaryDatum&, Name, const std::vector<long>&);
330 
331 
338 void accumulate_property(DictionaryDatum&, Name, const std::vector<double>&);
339 
340 #endif
341 
342 
Exception to be thrown if a given SLI array has the wrong size.
Definition: sliexceptions.h:189
double get_double_in_range(const DictionaryDatum &d, Name const n, double min, double max, int mode=2)
Get the value of an existing dictionary entry and check that it is in a specified range...
Definition: dictutils.h:79
const Name d("d")
Specific to Izhikevich 2003.
Definition: nest_names.h:83
void initialize_property_doublevector(DictionaryDatum &d, Name propname)
Create a property of type DoubleVectorDatum in the dictionary, if it does not already exist...
Definition: dictutils.cc:35
Exception to be thrown if a given SLI type does not match the expected type.
Definition: sliexceptions.h:147
void initialize_property_array(DictionaryDatum &d, Name propname)
Create a property of type ArrayDatum in the dictionary, if it does not already exist.
Definition: dictutils.cc:25
Represent strings by ints to facilitate fast comparison.
Definition: name.h:53
void accumulate_property(DictionaryDatum &, Name, const std::vector< double > &)
Add values of a vector to a property DoubleVectorDatum in the dictionary. ...
Definition: dictutils.cc:82
void append_property(DictionaryDatum &d, Name propname, const PropT &prop)
Append a value to a property ArrayDatum in the dictionary.
Definition: dictutils.h:267
assert(pNet!=0)
bool empty(void) const
Definition: token.h:239
const D & get(void) const
Definition: genericdatum.h:61
void def(DictionaryDatum &d, Name const n, FT const &value)
Define a new dictionary entry from a fundamental type.
Definition: dictutils.h:196
bool updateValue(DictionaryDatum const &d, Name const n, VT &value)
Update a variable from a dictionary entry if it exists, skip call if it doesn't.
Definition: dictutils.h:207
const Name x("x")
current scaling factor of the synaptic weight [0...1] (Tsodyks2_connection)
Definition: nest_names.h:356
const Name min("min")
Definition: topology_names.h:74
long get_long_in_range(const DictionaryDatum &d, Name const n, long min, long max, int mode=2)
Get the value of an existing dictionary entry and check that it is in a specified range...
Definition: dictutils.h:135
Definition: numericdatum.h:40
const Name max("max")
Definition: topology_names.h:75
void initialize_property_intvector(DictionaryDatum &d, Name propname)
Create a property of type IntVectorDatum in the dictionary, if it does not already exist...
Definition: dictutils.cc:45
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
void def2(DictionaryDatum &d, Name const n, FT const &value)
Define a new dictionary entry from a fundamental type.
Definition: dictutils.h:184
A type-independent container for C++-types.
Definition: token.h:68
void provide_property(DictionaryDatum &, Name, const std::vector< double > &)
Provide a value to a property DoubleVectorDatum in the dictionary.
Definition: dictutils.cc:55
FT getValue(const DictionaryDatum &d, Name const n)
Get the value of an existing dictionary entry.
Definition: dictutils.h:53
void updateValue2(DictionaryDatum const &d, Name const n, C &obj, void(C::*setfunc)(VT))
Call a member function of an object, passing the value of an dictionary entry if it exists...
Definition: dictutils.h:233
Datum * datum(void) const
Definition: token.h:249