NEST  2.6.0,not_revisioned_source_dir@0
tokenarray.h
Go to the documentation of this file.
1 /*
2  * tokenarray.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 TOKENARRAY_H
24 #define TOKENARRAY_H
25 /*
26  Reference Counted Array class specialized on Token objects.
27 */
28 
29 #include <typeinfo>
30 #include <cstddef>
31 #include <vector>
32 #include <valarray>
33 #include "tarrayobj.h"
34 #include "sliexceptions.h"
35 
36 // class Tokenarray uses reference counting and lazy evaluation.
37 // This means that only a pointer
38 // to the actual array representation, class TokenArrayObj, is stored.
39 // In most cases only this pointer is copied. Only when the array is
40 // to be modified, will the TokenArray be copied.
41 
42 // class TokenArrayObj is the complete token array class without reference-
43 // counting and lazu evaluation. class TokenArray serves as a smart interface.
44 //
45 // Note, that after the use of non const member functions, iterators (i.e.
46 // Token *) may become invalid due to cloning or reallocation of the array
47 //
48 //
49 // gewaltig, July 18 1997
50 
51 
52 // _move member functions may invalidate their iterator arguments.
53 // These functions are most efficient if for the source (argument)
54 // TokenArray references()==1 holds and therfore no cloning has to
55 // be performed.
56 //
57 // can be optimized for the case references()>1 by only
58 // constructing the elements needed without proper clone()
59 //
60 // Diesmann, April 14 1998
61 
63 {
64  private:
66 
67  bool clone(void)
68  {
69  if( data->references() > 1)
70  {
72  data = new TokenArrayObj(*data);
73  return true;
74  }
75  else return false;
76  }
77 
78  bool detach(void)
79  {
80  if( data->references() > 1)
81  {
83  data = new TokenArrayObj();
84  return true;
85  }
86  else return false;
87  }
88  protected:
89  friend class TokenArrayObj;
90  friend class TokenStack;
91  operator TokenArrayObj() const
92  { return *data; }
93 
94 public:
95 
96  TokenArray(void)
97  : data(new TokenArrayObj()) {};
98 
99  explicit
100  TokenArray(size_t n, const Token &t = Token(), size_t alloc = 128)
101  : data( new TokenArrayObj(n,t,alloc)) {}
102 
104  : data(a.data)
105  { data->add_reference(); }
106 
108  : data( new TokenArrayObj(a)){}
109 
110  TokenArray(const std::vector<size_t> &);
111  TokenArray(const std::vector<long> &);
112  TokenArray(const std::valarray<long> &);
113  TokenArray(const std::valarray<double> &);
114  TokenArray(const std::valarray<float> &);
115  TokenArray(const std::vector<double> &);
116  TokenArray(const std::vector<float> &);
117 
118  virtual ~TokenArray()
119  {
120  data->remove_reference(); // this will dispose data if needed.
121  }
122 
126  Token * begin() const
127  {
128  return data->begin();
129  }
130 
134  Token * end() const
135  {
136  return data->end();
137  }
138 
142  size_t size(void) const
143  {
144  return data->size();
145  }
146 
150  size_t capacity(void) const
151  {
152  return data->capacity();
153  }
154 
155  // Note, in order to use the const version of operator[]
156  // through a pointer, it is in some cases necessary to
157  // use an explicit TokenArray const * pointer!
158  // Use the member function get(size_t) const to force
159  // constness.
160 
161  Token & operator[](size_t i)
162  {
163  clone();
164  return (*data)[i];
165  }
166 
167  const Token & operator[](size_t i) const
168  {
169  return (*data)[i];
170  }
171 
172  const Token &get(long i) const
173  {
174  return data->get(i);
175  }
176 
177  bool index_is_valid(long i) const
178  {
179  return data->index_is_valid(i);
180  }
181 
182  void rotate(Token *t1, Token *t2, Token *t3)
183  {
184  size_t s1= t1 - data->begin();
185  size_t s2= t2 - data->begin();
186  size_t s3= t3 - data->begin();
187 
188  clone();
189  Token *b=data->begin();
190 
191  data->rotate(b+s1,b+s2,b+s3);
192  }
193 
194  void rotate(long n);
195 
196  // The following two members shrink and reserve do
197  // NOT invoke cloning, since they have no immediate
198  // consequences.
199 
206  bool shrink(void)
207  {
208  return data->shrink();
209  }
210 
216  bool reserve(size_t n)
217  {
218  return data->reserve(n);
219  }
220 
221  unsigned int references(void)
222  {
223  return data->references();
224  }
225 
230  void resize(size_t s, const Token &t = Token())
231  {
232  clone(); data->resize(s,t);
233  }
234 
235  // Insertion, deletion
236  void push_back(const Token &t)
237  {
238  clone(); data->push_back(t);
239  }
240 
242  {
243  Token t(d);
244  clone(); data->push_back_move(t);
245  }
246 
248  {
249  clone(); data->push_back_move(t);
250  }
251 
253  {
254  data->push_back_move(t);
255  }
256 
257  void assign_move(size_t i, Token &t) // 8.4.98 Diesmann
258  {
259  clone(); data->assign_move(data->begin()+i,t);
260  }
261 
262  void assign_move(TokenArray &a, size_t i, size_t n)
263  {
264  clear(); // no cloning, because we overwrite everything
265  // This is slightly inefficient, because if a has references,
266  // cloning is more expensive than just copying the desired range.
267  if(a.references()==1)
268  data->assign_move(*(a.data), i,n);
269  else
270  data->assign(*(a.data), i,n);
271 
272  }
273 
274  void insert_move(size_t i, TokenArray &a) // 8.4.98 Diesmann
275  {
276  clone(); // make copy if others point to representation
277  a.clone(); // also for a because we are going to empy it
278 // assert(data->refs==1); // private copy
279 // assert(a.data->refs==1); // private copy
280 
281  data->insert_move(i, *(a.data));
282  // the representations insert_move moves the
283  // the contens of all Tokens in a.data and marks it empty.
284 
285 // assert(a.data->size()==0); // empty, but memory is still allocated incase
286  // it will be used again. data->clear() would
287  // free the memory. In any case the destructor
288  // finally frees the memory.
289  }
290 
291  void insert_move(size_t i, Token &t)
292  {
293  clone();
294  data->insert_move(i,t);
295  }
296 
297 
298  void replace_move(size_t i, size_t n, TokenArray &a)
299  {
300  clone();
301  a.clone();
302 
303  data->replace_move(i,n,*(a.data));
304  }
305 
306 
308  {
309  clone(); // make copy if others point to representation
310  a.clone(); // also for a because we are going to empy it
311 
312  data->append_move(*(a.data));
313  }
314 
315  void pop_back(void)
316  {
317  clone(); data->pop_back();
318  }
319 
320  void clear(void)
321  {
322  erase();
323  }
324 
325  void erase(void)
326  {
327  if(!detach())
328  erase(begin(),end());
329  }
330 
331 
332  void erase(Token * from, Token * to)
333  {
334  if(from != to)
335  {
336  size_t sf = from - data->begin();
337  size_t st = to - data->begin();
338 
339  clone(); data->erase(data->begin()+sf, data->begin()+st);
340  }
341  }
342 
343  void erase(size_t i, size_t n)
344  {
345  if(i<size() && n>0)
346  {
347  clone();
348  data->erase(i,n);
349  }
350  }
351 
352  // Reduce the Array to the Range given by the iterators
353  void reduce(size_t i, size_t n)
354  {
355  if(i>0 || n<size())
356  {
357  clone();
358  data->reduce(i,n);
359  }
360  }
361 
362  void reverse();
363 
365  {
366  std::swap(data,a.data);
367  }
368 
369  const TokenArray & operator=(const TokenArray &);
370  const TokenArray & operator=(const std::vector<long> &);
371  const TokenArray & operator=(const std::vector<double> &);
372  const TokenArray & operator=(const std::valarray<long> &);
373  const TokenArray & operator=(const std::valarray<double> &);
374 
375  bool operator==(const TokenArray &a) const
376  {
377  return *data == *a.data;
378  }
379 
380  bool empty(void) const
381  {
382  return size()==0;
383  }
384 
385  void info(std::ostream &) const;
386 
389  void toVector(std::vector<size_t> &) const;
390  void toVector(std::vector<long> &) const;
391  void toVector(std::vector<double> &) const;
392  void toVector(std::vector<std::string> &) const;
393  void toValarray(std::valarray<long> &) const;
394  void toValarray(std::valarray<double> &) const;
395 
396  bool valid(void) const; // check integrity
397 
399  // class TypeMismatch {};
400  class OutOfRange {};
401 
402 
403 };
404 
405 inline
407 {
408  if(size()==0)
409  return;
410  clone();
411  Token *b = begin();
412  Token *e = end() -1;
413  while(b < e)
414  {
415  b->swap(*e);
416  ++b; --e;
417  }
418 }
419 
420 inline
421 void TokenArray::rotate(long n=1)
422 {
423  if (size() == 0 || n == 0)
424  return;
425 
426  clone();
427  long rot = n % static_cast<long>(size());
428  rot = (rot < 0) ? rot+size() : rot;
429  std::rotate(begin(), begin()+rot, end());
430 }
431 
432 
433 std::ostream & operator<<(std::ostream& , const TokenArray&);
434 
435 
436 
437 #ifdef TokenArray_H_DEBUG
438 #undef TokenArray_H_DEBUG
439 #undef NDEBUG
440 #endif
441 
442 
443 #endif
444 
Token * end() const
Definition: tarrayobj.h:70
bool shrink(void)
Definition: tarrayobj.cc:185
void push_back_dont_clone(Token &t)
Definition: tokenarray.h:252
void info(std::ostream &) const
TokenArray(const TokenArray &a)
Definition: tokenarray.h:103
bool shrink(void)
Reduce allocated space such that size()==capacity().
Definition: tokenarray.h:206
size_t capacity(void) const
Definition: tarrayobj.h:80
void replace_move(size_t i, size_t n, TokenArray &a)
Definition: tokenarray.h:298
const Name d("d")
Specific to Izhikevich 2003.
Definition: nest_names.h:83
void push_back(Datum *d)
Definition: tokenarray.h:241
void resize(size_t s, const Token &t=Token())
Resizes the container to size s.
Definition: tokenarray.h:230
void assign_move(size_t i, Token &t)
Definition: tokenarray.h:257
void pop_back(void)
Definition: tokenarray.h:315
void append_move(TokenArray &a)
Definition: tokenarray.h:307
void rotate(Token *t1, Token *t2, Token *t3)
Definition: tokenarray.h:182
virtual ~TokenArray()
Definition: tokenarray.h:118
bool reserve(size_t n)
Reserve space such that after the call the new capacity is n.
Definition: tokenarray.h:216
bool operator==(const TokenArray &a) const
Definition: tokenarray.h:375
bool reserve(size_t)
Definition: tarrayobj.cc:197
void erase(size_t, size_t)
Definition: tarrayobj.cc:261
bool valid(void) const
Definition: tokenarray.cc:225
void clear(void)
Definition: tokenarray.h:320
Definition: tokenstack.h:38
void push_back(const Token &t)
Definition: tokenarray.h:236
Token * end() const
Return pointer to next to last element.
Definition: tokenarray.h:134
Definition: tarrayobj.h:37
bool index_is_valid(long i) const
Definition: tokenarray.h:177
Definition: tokenarray.h:62
const Name a("a")
Specific to Brette & Gerstner 2005 (aeif_cond-*)
Definition: nest_names.h:41
void toVector(std::vector< size_t > &) const
Fill vectors with homogenous integer and double arrays.
Definition: tokenarray.cc:137
void reduce(size_t i, size_t n)
Definition: tokenarray.h:353
void push_back_move(Token &t)
Definition: tarrayobj.h:147
friend class TokenArrayObj
Definition: tokenarray.h:89
void reverse()
Definition: tokenarray.h:406
void insert_move(size_t, TokenArrayObj &)
Definition: tarrayobj.cc:347
const Token & operator[](size_t i) const
Definition: tokenarray.h:167
void swap(sparsegroup< T, GROUP_SIZE, Alloc > &x, sparsegroup< T, GROUP_SIZE, Alloc > &y)
Definition: sparsetable.h:1348
Exception classes.
Definition: tokenarray.h:400
void push_back_move(Token &t)
Definition: tokenarray.h:247
size_t size(void) const
Definition: tarrayobj.h:75
const TokenArray & operator=(const TokenArray &)
Definition: tokenarray.cc:30
void erase(void)
Definition: tokenarray.h:325
void insert_move(size_t i, TokenArray &a)
Definition: tokenarray.h:274
bool empty(void) const
Definition: tokenarray.h:380
void erase(Token *from, Token *to)
Definition: tokenarray.h:332
void swap(TokenArray &a)
Definition: tokenarray.h:364
void rotate(Token *, Token *, Token *)
Definition: tarrayobj.cc:208
TokenArray(size_t n, const Token &t=Token(), size_t alloc=128)
Definition: tokenarray.h:100
void assign(const TokenArrayObj &, size_t, size_t)
Definition: tarrayobj.cc:398
Token * begin() const
Definition: tarrayobj.h:65
void toValarray(std::valarray< long > &) const
Definition: tokenarray.cc:189
void append_move(TokenArrayObj &)
Definition: tarrayobj.cc:515
void erase(size_t i, size_t n)
Definition: tokenarray.h:343
size_t size(void) const
Return number of elements in the array.
Definition: tokenarray.h:142
const Token & get(long i) const
Definition: tarrayobj.h:90
std::ostream & operator<<(std::ostream &, const TokenArray &)
Definition: tokenarray.cc:235
void assign_move(Token *tp, Token &t)
Definition: tarrayobj.h:182
const Name b("b")
Specific to Brette & Gerstner 2005 (aeif_cond-*)
Definition: nest_names.h:58
bool index_is_valid(long i) const
Definition: tarrayobj.h:96
void replace_move(size_t, size_t, TokenArrayObj &)
Definition: tarrayobj.cc:440
unsigned int remove_reference()
Definition: tarrayobj.h:114
void insert_move(size_t i, Token &t)
Definition: tokenarray.h:291
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
void assign_move(TokenArray &a, size_t i, size_t n)
Definition: tokenarray.h:262
bool clone(void)
Definition: tokenarray.h:67
unsigned int add_reference()
Definition: tarrayobj.h:126
Token * begin() const
Return pointer to the first element.
Definition: tokenarray.h:126
Definition: datum.h:33
TokenArrayObj * data
Definition: tokenarray.h:65
size_t capacity(void) const
Return maximal number of elements that fit into the container.
Definition: tokenarray.h:150
A type-independent container for C++-types.
Definition: token.h:68
void resize(size_t, size_t, const Token &=Token())
Definition: tarrayobj.cc:112
TokenArray(void)
Definition: tokenarray.h:96
void pop_back(void)
Definition: tarrayobj.h:187
const double e
Definition: numerics.cpp:62
bool detach(void)
Definition: tokenarray.h:78
unsigned int references(void)
Definition: tokenarray.h:221
void push_back(const Token &t)
Definition: tarrayobj.h:140
void reduce(Token *, Token *)
Definition: tarrayobj.cc:281
unsigned int references(void)
Definition: tarrayobj.h:109
TokenArray(const TokenArrayObj &a)
Definition: tokenarray.h:107
Token & operator[](size_t i)
Definition: tokenarray.h:161