NEST  2.6.0,not_revisioned_source_dir@0
dict.h
Go to the documentation of this file.
1 /*
2  * dict.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 DICT_H
24 #define DICT_H
25 /*
26  SLI's dictionary class
27 */
28 #include "name.h"
29 #include "token.h"
30 
31 #include <map>
32 #include "sliexceptions.h"
33 
34 
35 typedef std::map<Name, Token, std::less<Name> > TokenMap;
36 
37 inline bool operator==(const TokenMap & x, const TokenMap &y)
38 {
39  return (x.size() == y.size()) && equal(x.begin(), x.end(), y.begin());
40 }
41 
45 class Dictionary: private TokenMap
46 {
56  private:
57  static bool nocase_compare(char c1, char c2);
58 
59  public:
60  bool operator() (const std::pair<Name, Token>& lhs,
61  const std::pair<Name, Token>& rhs) const
62  {
63  const std::string& ls = lhs.first.toString();
64  const std::string& rs = rhs.first.toString();
65 
66  return std::lexicographical_compare(ls.begin(), ls.end(),
67  rs.begin(), rs.end(),
69  }
70  };
71 
72 public:
75  ~Dictionary();
76 
77  using TokenMap::erase;
78  using TokenMap::size;
79  using TokenMap::begin;
80  using TokenMap::end;
81  using TokenMap::iterator;
82  using TokenMap::find;
83 
84  void clear();
85 
97  const Token & lookup(const Name &n) const;
98 
107  const Token & lookup2(const Name &n) const; //throws UndefinedName
108  bool known(const Name &) const;
109 
111  bool known_but_not_accessed(const Name&) const;
112 
113  Token & insert(const Name &n, const Token &t);
114  Token & insert_move(const Name &, Token &);
115 
117  void remove(const Name& n);
118 
119  const Token& operator[](const Name&) const;
120  Token& operator[](const Name &);
121  const Token& operator[](const char*) const;
122  Token& operator[](const char *);
123 
124  bool empty(void) const { return TokenMap::empty(); }
125 
126  void info(std::ostream &) const;
127 
128  bool operator==(const Dictionary &d) const { return ::operator==(*this, d); }
129 
138  void add_dict(const std::string&, SLIInterpreter&);
139 
144  void remove_dict(const std::string&, SLIInterpreter&);
145 
151  void clear_access_flags();
152 
161  bool all_accessed(std::string& missed) const { return all_accessed_(missed); }
162 
163  friend std::ostream & operator<<(std::ostream &, const Dictionary &);
164 
171  typedef TokenMap::const_iterator const_iterator;
172 
179  // const_iterator begin() const;
180 
187  //const_iterator end() const;
188 
192  void initialize_property_array(Name propname);
193 
201  {
203  }
204 
209  {
211  }
212 
216  bool is_on_dictstack() const
217  {
218  return refs_on_dictstack_ >0;
219  }
220 
221 
222  private:
234  bool all_accessed_(std::string&, std::string prefix = std::string()) const;
235  static const Token VoidToken;
236 };
237 
238 inline
239 const Token& Dictionary::lookup(const Name &n) const
240 {
241  TokenMap::const_iterator where = find(n);
242  if(where != end())
243  return (*where).second;
244  else
245  return Dictionary::VoidToken;
246 }
247 
248 inline
249 const Token& Dictionary::lookup2(const Name &n) const
250 {
251  TokenMap::const_iterator where = find(n);
252  if(where != end())
253  return (*where).second;
254  else
255  throw UndefinedName(n.toString());
256 }
257 
258 inline
259 bool Dictionary::known(const Name &n) const
260 {
261  TokenMap::const_iterator where = find(n);
262  if(where != end())
263  return true;
264  else
265  return false;
266 }
267 
268 inline
270 {
271  TokenMap::const_iterator where = find(n);
272  if(where != end())
273  return not where->second.accessed();
274  else
275  return false;
276 }
277 
278 inline
279 Token& Dictionary::insert(const Name &n, const Token &t)
280 {
281  return TokenMap::operator[](n) = t;
282 }
283 
284 
285 inline
286 const Token& Dictionary::operator[](const Name &n) const
287 {
288  TokenMap::const_iterator where = find(n);
289  if(where != end())
290  return (*where).second;
291  else
292  throw UndefinedName(n.toString());
293 }
294 
295 
296 inline
298 {
299  return TokenMap::operator[](n);
300 }
301 
302 inline
304 {
305  Token &result=TokenMap::operator[](n);
306  result.move(t);
307  return result;
308 }
309 
310 
311 #endif
Helper class for lexicographical sorting of dictionary entries.
Definition: dict.h:55
const Token & lookup2(const Name &n) const
lookup a name in the dictionary.
Definition: dict.h:249
void remove_dict(const std::string &, SLIInterpreter &)
Remove entries found in another dictionary from this.
Definition: dict.cc:124
void info(std::ostream &) const
Definition: dict.cc:66
std::map< Name, Token, std::less< Name > > TokenMap
Definition: dict.h:35
const Name d("d")
Specific to Izhikevich 2003.
Definition: nest_names.h:83
const Token & lookup(const Name &n) const
Lookup and return Token with given name in dictionary.
Definition: dict.h:239
void move(Token &c)
Definition: token.h:142
Dictionary(const Dictionary &d)
Definition: dict.h:74
bool all_accessed(std::string &missed) const
Check whether all elements have been accessed.
Definition: dict.h:161
const Token & operator[](const Name &) const
Definition: dict.h:286
void remove_dictstack_reference()
This function is called when the dictionary is popped from the dictionary stack.
Definition: dict.h:208
bool known_but_not_accessed(const Name &) const
Returns true if name is known but token has not been accessed.
Definition: dict.h:269
Represent strings by ints to facilitate fast comparison.
Definition: name.h:53
TokenMap::const_iterator const_iterator
Constant iterator for dictionary.
Definition: dict.h:171
bool is_on_dictstack() const
Returns true, if the dictionary has references on the dictionary stack.
Definition: dict.h:216
void add_dictstack_reference()
This function is called when a dictionary is pushed to the dictionary stack.
Definition: dict.h:200
int refs_on_dictstack_
Worker function checking whether all elements have been accessed.
Definition: dict.h:233
A class that associates names and tokens.
Definition: dict.h:45
const std::string & toString(void) const
Return string represented by Name.
Definition: name.cc:63
void add_dict(const std::string &, SLIInterpreter &)
Add the contents of this dictionary to another.
Definition: dict.cc:97
const Name y("y")
Definition: topology_names.h:52
void clear_access_flags()
Clear access flags on all dictionary elements.
Definition: dict.cc:142
bool empty(void) const
Definition: dict.h:124
Definition: interpret.h:69
const Name x("x")
current scaling factor of the synaptic weight [0...1] (Tsodyks2_connection)
Definition: nest_names.h:356
Dictionary()
Definition: dict.h:73
bool operator()(const std::pair< Name, Token > &lhs, const std::pair< Name, Token > &rhs) const
Definition: dict.h:60
Token & insert_move(const Name &, Token &)
Definition: dict.h:303
void clear()
Definition: dict.cc:49
bool operator==(const TokenMap &x, const TokenMap &y)
Definition: dict.h:37
Token & insert(const Name &n, const Token &t)
Definition: dict.h:279
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
friend std::ostream & operator<<(std::ostream &, const Dictionary &)
Definition: dict.cc:191
void initialize_property_array(Name propname)
First element in dictionary.
~Dictionary()
Definition: dict.cc:35
A type-independent container for C++-types.
Definition: token.h:68
bool known(const Name &) const
Definition: dict.h:259
Exception to be thrown if an entry referenced inside a dictionary does not exist. ...
Definition: sliexceptions.h:263
bool all_accessed_(std::string &, std::string prefix=std::string()) const
Definition: dict.cc:165
bool operator==(const Dictionary &d) const
Definition: dict.h:128
static const Token VoidToken
Definition: dict.h:235
static bool nocase_compare(char c1, char c2)
Definition: dict.cc:206