NEST  2.6.0,not_revisioned_source_dir@0
dictstack.h
Go to the documentation of this file.
1 /*
2  * dictstack.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 DICTIONARYSTACK_H
24 #define DICTIONARYSTACK_H
25 /*
26  SLI's dictionary stack
27 */
28 #include <typeinfo>
29 #include "dictdatum.h"
30 #include <list>
31 #include "sliexceptions.h"
32 
33 
34 /***************************************************************
35 
36 Problems:
37 
38 - is ist better to uses dictionaries as references to common
39  objects like in PS. What is the exact meaning of undef and
40  where in our current situation (read RedBook).
41 - more efficient implementation exploiting
42  the name ids (documented elsewhere).
43 
44 
45 
46  History:
47  (1) using list<Dictionary>
48  MD, 23.6.1, Freiburg
49  (0) first version (single dictionary)
50  MOG, MD, June 1997, Freiburg
51 ***************************************************************
52 */
53 
64 #ifndef DICTSTACK_CACHE
65 #define DICTSTACK_CACHE 1
66 #endif
67 
68 //#undef DICTSTACK_CACHE
69 
71 {
72 private:
74  std::list<DictionaryDatum> d;
76 #ifdef DICTSTACK_CACHE
77  std::vector<const Token *> cache_;
78  std::vector<const Token *> basecache_;
79 #endif
80 
81 public:
82  DictionaryStack(const Token & = Token());
85 
86 
87 #ifdef DICTSTACK_CACHE
88 
91  void cache_token( const Name &n, const Token *result)
92  {
93  Name::handle_t key=n.toIndex();
94  if (key>=cache_.size())
95  cache_.resize(Name::num_handles()+100,0);
96  cache_[key]= result;
97  }
98 
99  void basecache_token( const Name &n, const Token *result)
100  {
101  Name::handle_t key=n.toIndex();
102  if (key>=basecache_.size())
103  basecache_.resize(Name::num_handles()+100,0);
104  basecache_[key]= result;
105  }
106 
112  {
113  Name::handle_t key=n.toIndex();
114  if(key<cache_.size())
115  cache_[key]=0;
116  }
117 
119  {
120  Name::handle_t key=n.toIndex();
121  if(key<basecache_.size())
122  basecache_[key]=0;
123  }
124 
126  {
127  for(TokenMap::iterator i = d->begin(); i != d->end(); ++i)
128  clear_token_from_cache(i->first);
129  }
130 
131 
136  void clear_cache()
137  {
138  const size_t cache_size=cache_.size();
139 
140  for(size_t i=0; i< cache_size; ++i)
141  cache_[i]=0;
142  }
143 
144 #endif
145 
146  const Token & lookup(const Name &n)
147  {
148  try
149  {
150  return lookup2(n);
151  }
152  catch(UndefinedName)
153  {
154  return VoidToken;
155  }
156  }
157 
158  const Token & lookup2(const Name &n)
159  {
160 #ifdef DICTSTACK_CACHE
161  Name::handle_t key=n.toIndex();
162  if (key<cache_.size())
163  {
164  const Token *result=cache_[key];
165  if(result)
166  return *result;
167  }
168 #endif
169 
170  std::list<DictionaryDatum>::const_iterator i=d.begin();
171 
172  while (i!=d.end())
173  {
174  TokenMap::const_iterator where =(*i)->find(n);
175  if(where!=(*i)->end())
176  {
177 #ifdef DICTSTACK_CACHE
178  cache_token(n,&(where->second)); // Update the cache
179 #endif
180  return where->second;
181  }
182  ++i;
183  }
184  throw UndefinedName(n.toString());
185  }
186 
191  const Token & baselookup(const Name &n) // lookup in a specified
192  { // base dictionary
193 #ifdef DICTSTACK_CACHE
194  Name::handle_t key=n.toIndex();
195  if (key<basecache_.size())
196  {
197  const Token *result=basecache_[key];
198  if(result)
199  return *result;
200  }
201 #endif
202  TokenMap::const_iterator where =base_->find(n);
203 
204  if ( where != base_->end() )
205  {
206 #ifdef DICTSTACK_CACHE
207  cache_token(n, &(where->second)); // Update the cache
208  basecache_token(n, &(where->second)); // and the basecache
209 #endif
210  return where->second;
211  }
212  else
213  return VoidToken;
214  }
215 
218  bool known(const Name &n)
219  {
220 #ifdef DICTSTACK_CACHE
221  Name::handle_t key=n.toIndex();
222  if (key<cache_.size())
223  {
224  const Token *result=cache_[key];
225  if(result)
226  return true;
227  }
228 #endif
229  std::list<DictionaryDatum>::const_iterator i(d.begin());
230 
231  while (i!=d.end())
232  {
233  TokenMap::const_iterator where =(*i)->find(n);
234  if(where!=(*i)->end())
235  {
236 #ifdef DICTSTACK_CACHE
237  cache_token(n,&(where->second)); // Update the cache
238 #endif
239  return true;
240  }
241  ++i;
242  }
243  return false;
244  }
245 
248  bool baseknown(const Name &n) // lookup in a specified
249  { // base dictionary
250 #ifdef DICTSTACK_CACHE
251  Name::handle_t key=n.toIndex();
252  if (key<basecache_.size())
253  {
254  const Token *result=basecache_[key];
255  if(result)
256  return true;
257  }
258 #endif
259  TokenMap::const_iterator where =base_->find(n);
260  if(where!=base_->end())
261  {
262 #ifdef DICTSTACK_CACHE
263  basecache_token(n,&(where->second)); // Update the basecache
264  cache_token(n,&(where->second)); // and the cache
265 #endif
266  return true;
267  }
268  return false;
269  }
270 
271 
272  //
273  // def and def_move operate on the top level dictionary.
274  // undef is not defined for the dictionary stack.
275 
279  void def(const Name &, const Token &);
280 
283  void undef(const Name &);
284 
288  void basedef(const Name &n, const Token &t);
289 
293  void def_move(const Name&, Token &);
294 
298  void set_basedict();
299 
303  void basedef_move(const Name &n, Token &t);
304 
305  bool where(const Name&, Token&);
306 
307  void pop(void);
308 
309 
310  //
311  // a Dictionary is always wrapped in a Token
312  //
313  void top(Token &) const;
314  void push(const DictionaryDatum &);
315  void push(Token &);
316 
317  void clear(void);
318  void toArray(TokenArray &) const;
319  //
320  // move is efficient for interaction with operand and execution
321  // stack, but can not be implemented in this version
322  //
323 // void pop_move(Token &) const;
324 // void push_move(const Token &);
325 
326  //
327  // number of dictionaries currently on the stack
328  //
329  size_t size(void) const;
330 
331 
332  //
333  // info for debugging purposes.
334  // calls info(ostream&) for all dictionaries
335  //
336  void info(std::ostream&) const;
337  void top_info(std::ostream &) const; // calls info of top dictionary
338  const DictionaryStack& operator=(const DictionaryStack&);
339 };
340 
341 inline
342 void DictionaryStack::def(const Name &n, const Token &t)
343 {
344  //
345  // insert (n,t) in top level dictionary
346  // dictionary stack must contain at least one dictionary
347  // VoidToken is an illegal value for t.
348  //
349 #ifdef DICTSTACK_CACHE
350  cache_token(n,&((*d.begin())->insert(n,t)));
351 #endif
352 #ifndef DICTSTACK_CACHE
353  (**d.begin())[n]=t;
354 #endif
355 }
356 
357 inline
359 {
360  //
361  // insert (n,t) in top level dictionary
362  // dictionary stack must contain at least one dictionary
363  // VoidToken is an illegal value for t.
364  // def_move returns t as the VoidToken.
365  //
366  /* clear_token_from_cache(n); */
367 
368 #ifdef DICTSTACK_CACHE
369  cache_token(n,&((*d.begin())->insert_move(n,t)));
370 #endif
371 #ifndef DICTSTACK_CACHE
372  (*d.begin())->insert_move(n,t);
373 #endif
374 }
375 
376 #endif
377 
378 
379 
380 
381 
void clear_dict_from_cache(DictionaryDatum d)
Definition: dictstack.h:125
void pop(void)
Definition: dictstack.cc:88
void push(const DictionaryDatum &)
Definition: dictstack.cc:151
const Token & lookup2(const Name &n)
Definition: dictstack.h:158
bool known(const Name &n)
Test for a name searching all dictionaries on the stack.
Definition: dictstack.h:218
void cache_token(const Name &n, const Token *result)
Add a token to the cache.
Definition: dictstack.h:91
static size_t num_handles()
Definition: name.cc:34
const DictionaryStack & operator=(const DictionaryStack &)
Definition: dictstack.cc:203
const Token & baselookup(const Name &n)
Lookup a name searching only the bottom level dictionary.
Definition: dictstack.h:191
std::list< DictionaryDatum > d
Definition: dictstack.h:74
void def_move(const Name &, Token &)
Bind a Token to a Name in the top level dictionary.
Definition: dictstack.h:358
Represent strings by ints to facilitate fast comparison.
Definition: name.h:53
DictionaryStack(const Token &=Token())
Definition: dictstack.cc:25
void basedef_move(const Name &n, Token &t)
Bind a Token to a Name in the bottom level dictionary.
Definition: dictstack.cc:76
void top_info(std::ostream &) const
Definition: dictstack.cc:198
Definition: tokenarray.h:62
bool baseknown(const Name &n)
Test for a name in the bottom level dictionary.
Definition: dictstack.h:248
const std::string & toString(void) const
Return string represented by Name.
Definition: name.cc:63
const Token VoidToken
Definition: dictstack.h:73
DictionaryDatum base_
Definition: dictstack.h:75
void info(std::ostream &) const
Definition: dictstack.cc:183
void set_basedict()
This function must be called once to initialize the systemdict cache.
Definition: dictstack.cc:168
void basecache_token(const Name &n, const Token *result)
Definition: dictstack.h:99
std::vector< const Token * > cache_
Definition: dictstack.h:77
bool where(const Name &, Token &)
Definition: dictstack.h:70
void top(Token &) const
Definition: dictstack.cc:111
std::vector< const Token * > basecache_
Definition: dictstack.h:78
const Token & lookup(const Name &n)
Definition: dictstack.h:146
handle_t toIndex(void) const
Return table index for Name object.
Definition: name.h:75
size_t size(void) const
Definition: dictstack.cc:173
void clear_cache()
Clear the entire cache.
Definition: dictstack.h:136
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
void clear_token_from_basecache(const Name &n)
Definition: dictstack.h:118
void clear(void)
Definition: dictstack.cc:102
~DictionaryStack()
Definition: dictstack.cc:35
void toArray(TokenArray &) const
Definition: dictstack.cc:125
A type-independent container for C++-types.
Definition: token.h:68
void def(const Name &, const Token &)
Bind a Token to a Name in the top level dictionary.
Definition: dictstack.h:342
Exception to be thrown if an entry referenced inside a dictionary does not exist. ...
Definition: sliexceptions.h:263
void basedef(const Name &n, const Token &t)
Bind a Token to a Name in the bottom level dictionary.
Definition: dictstack.cc:60
void clear_token_from_cache(const Name &n)
Clear a name from the cache.
Definition: dictstack.h:111
unsigned int handle_t
Definition: name.h:57
void undef(const Name &)
Unbind a previously defined Name from its token.
Definition: dictstack.cc:43