NEST  2.6.0,not_revisioned_source_dir@0
tarrayobj.h
Go to the documentation of this file.
1 /*
2  * tarrayobj.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 TARRAYOBJ_H
24 #define TARRAYOBJ_H
25 /*
26  Array of Tokens
27 */
28 
29 #include <typeinfo>
30 #include <cstddef>
31 #include "token.h"
32 
33 #define ARRAY_ALLOC_SIZE 64
34 
35 class Token;
36 
38 {
39  private:
40  Token* p;
43  unsigned int alloc_block_size;
44  unsigned int refs_;
45 
46 // bool homogeneous;
47 
48  void allocate(size_t, size_t, size_t, const Token & = Token());
49 
50  static size_t allocations;
51  public:
52 
54  :p(NULL),begin_of_free_storage(NULL),
55  end_of_free_storage(NULL),
56  alloc_block_size(ARRAY_ALLOC_SIZE),
57  refs_(1)
58  {};
59 
60  TokenArrayObj(size_t , const Token & = Token(), size_t = 0);
62 
63  virtual ~TokenArrayObj();
64 
65  Token * begin() const
66  {
67  return p;
68  }
69 
70  Token * end() const
71  {
72  return begin_of_free_storage;
73  }
74 
75  size_t size(void) const
76  {
77  return (size_t)(begin_of_free_storage-p);
78  }
79 
80  size_t capacity(void) const
81  {
82  return (size_t)(end_of_free_storage - p);
83  }
84 
85  Token & operator[](size_t i) { return p[i]; }
86 
87  const Token & operator[](size_t i) const
88  { return p[i]; }
89 
90  const Token & get(long i) const
91  {
92  return *(p+i);
93  // return p[i];
94  }
95 
96  bool index_is_valid(long i) const
97  {
98  return (p+i) < begin_of_free_storage;
99  }
100 
101  void rotate(Token *, Token *, Token *);
102 
103 
104  // Memory allocation
105 
106  bool shrink(void);
107  bool reserve(size_t);
108 
109  unsigned int references(void)
110  {
111  return refs_;
112  }
113 
114  unsigned int remove_reference()
115  {
116  --refs_;
117  if(refs_==0)
118  {
119  delete this;
120  return 0;
121  }
122 
123  return refs_;
124  }
125 
126  unsigned int add_reference()
127  {
128  return ++refs_;
129  }
130 
131  void resize(size_t, size_t, const Token & = Token());
132  void resize(size_t, const Token & = Token());
133 
134  void reserve_token(size_t n)
135  {
136  if(capacity()<size()+1+n)
137  reserve(size()+n);
138  }
139  // Insertion, deletion
140  void push_back(const Token &t)
141  {
142  if(capacity()<size()+1)
144  (begin_of_free_storage++)->init_by_copy(t);
145  }
146 
148  {
149  if(capacity()<size()+1)
151 
152  (begin_of_free_storage++)->init_move(t);
153  // ++begin_of_free_storage;
154  }
155 
161  void push_back_by_ref(const Token &t)
162  {
163  if(capacity()<size()+1)
165  (begin_of_free_storage++)->init_by_ref(t);
166  }
167 
175  {
176  if(capacity()<size()+1)
178  begin_of_free_storage->init_by_pointer(rhs);
180  }
181 
182  void assign_move(Token *tp, Token &t)
183  {
184  tp->move(t);
185  }
186 
187  void pop_back(void)
188  {
190  }
191 
192  // Erase the range given by the iterators.
193  void erase(size_t , size_t);
194  void erase(Token *, Token *);
195  void erase(Token *tp)
196  {
197  erase(tp,tp+1);
198  }
199 
200  // Reduce the array to the range given by the iterators
201  void reduce(Token *, Token *);
202  void reduce(size_t, size_t);
203 
204  void insert(size_t, size_t = 1, const Token& = Token());
205  void insert(size_t i, const Token& t)
206  {
207  insert(i,1,t);
208  }
209 
210  void insert_move(size_t, TokenArrayObj &);
211  void insert_move(size_t, Token &);
212 
213  void assign_move(TokenArrayObj &, size_t, size_t);
214  void assign(const TokenArrayObj &, size_t, size_t);
215 
216  void replace_move(size_t, size_t, TokenArrayObj &);
217 
218  void append_move(TokenArrayObj &);
219 
220  void clear(void);
221 
222 
223  const TokenArrayObj & operator=(const TokenArrayObj &);
224 
225  bool operator==(const TokenArrayObj &) const;
226 
227  bool empty(void) const
228  {
229  return size()==0;
230  }
231 
232  void info(std::ostream &) const;
233 
234  static size_t getallocations(void)
235  { return allocations;}
236 
237  bool valid(void) const; // check integrity
238 
239 };
240 
241 std::ostream & operator<<(std::ostream& , const TokenArrayObj&);
242 
243 
244 
245 #endif
246 
247 
unsigned int alloc_block_size
Definition: tarrayobj.h:43
Token * end() const
Definition: tarrayobj.h:70
bool shrink(void)
Definition: tarrayobj.cc:185
Token * p
Definition: tarrayobj.h:40
size_t capacity(void) const
Definition: tarrayobj.h:80
void info(std::ostream &) const
Definition: tarrayobj.cc:562
void push_back_by_pointer(Datum *rhs)
Push back a datum pointer.
Definition: tarrayobj.h:174
virtual ~TokenArrayObj()
Definition: tarrayobj.cc:60
void insert(size_t i, const Token &t)
Definition: tarrayobj.h:205
bool reserve(size_t)
Definition: tarrayobj.cc:197
void push_back_by_ref(const Token &t)
Push back a reference.
Definition: tarrayobj.h:161
void erase(size_t, size_t)
Definition: tarrayobj.cc:261
Token * begin_of_free_storage
Definition: tarrayobj.h:41
Definition: tarrayobj.h:37
const Token & operator[](size_t i) const
Definition: tarrayobj.h:87
bool operator==(const TokenArrayObj &) const
Definition: tarrayobj.cc:536
void push_back_move(Token &t)
Definition: tarrayobj.h:147
Token & operator[](size_t i)
Definition: tarrayobj.h:85
static size_t getallocations(void)
Definition: tarrayobj.h:234
unsigned int refs_
Definition: tarrayobj.h:44
void insert_move(size_t, TokenArrayObj &)
Definition: tarrayobj.cc:347
size_t size(void) const
Definition: tarrayobj.h:75
void clear(void)
Definition: tarrayobj.cc:269
void rotate(Token *, Token *, Token *)
Definition: tarrayobj.cc:208
void assign(const TokenArrayObj &, size_t, size_t)
Definition: tarrayobj.cc:398
Token * begin() const
Definition: tarrayobj.h:65
void append_move(TokenArrayObj &)
Definition: tarrayobj.cc:515
std::ostream & operator<<(std::ostream &, const TokenArrayObj &)
Definition: tarrayobj.cc:604
Token * end_of_free_storage
Definition: tarrayobj.h:42
const TokenArrayObj & operator=(const TokenArrayObj &)
Definition: tarrayobj.cc:125
void assign_move(Token *tp, Token &t)
Definition: tarrayobj.h:182
void insert(size_t, size_t=1, const Token &=Token())
Definition: tarrayobj.cc:323
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
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
void reserve_token(size_t n)
Definition: tarrayobj.h:134
static size_t allocations
Definition: tarrayobj.h:50
unsigned int add_reference()
Definition: tarrayobj.h:126
Definition: datum.h:33
A type-independent container for C++-types.
Definition: token.h:68
TokenArrayObj(void)
Definition: tarrayobj.h:53
void resize(size_t, size_t, const Token &=Token())
Definition: tarrayobj.cc:112
void allocate(size_t, size_t, size_t, const Token &=Token())
Definition: tarrayobj.cc:66
bool empty(void) const
Definition: tarrayobj.h:227
void pop_back(void)
Definition: tarrayobj.h:187
void push_back(const Token &t)
Definition: tarrayobj.h:140
bool valid(void) const
Definition: tarrayobj.cc:571
void reduce(Token *, Token *)
Definition: tarrayobj.cc:281
unsigned int references(void)
Definition: tarrayobj.h:109
void erase(Token *tp)
Definition: tarrayobj.h:195