NEST  2.6.0,not_revisioned_source_dir@0
ring_buffer.h
Go to the documentation of this file.
1 /*
2  * ring_buffer.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 RING_BUFFER_H
24 #define RING_BUFFER_H
25 #include <valarray>
26 #include <list>
27 #include "nest.h"
28 #include "scheduler.h"
29 #include "nest_time.h"
30 
31 namespace nest
32 {
33 
77  class RingBuffer {
78  public:
79 
80  RingBuffer();
81 
87  void add_value(const long_t offs, const double_t);
88 
94  void set_value(const long_t offs, const double_t);
95 
101  double get_value(const long_t offs);
102 
107  void clear();
108 
114  void resize();
115 
119  size_t size() const { return buffer_.size(); }
120 
121  private:
122 
124  std::valarray<double_t> buffer_;
125 
132  size_t get_index_(const delay d) const;
133 
134  };
135 
136  inline
137  void RingBuffer::add_value(const long_t offs, const double_t v)
138  {
139  buffer_[get_index_(offs)] += v;
140  }
141 
142  inline
143  void RingBuffer::set_value(const long_t offs, const double_t v)
144  {
145  buffer_[get_index_(offs)] = v;
146  }
147 
148  inline
149  double RingBuffer::get_value(const long_t offs)
150  {
151  assert(0 <= offs && (size_t)offs < buffer_.size());
153 
154  // offs == 0 is beginning of slice, but we have to
155  // take modulo into account when indexing
156  long_t idx = get_index_(offs);
157  double_t val = buffer_[idx];
158  buffer_[idx] = 0.0; // clear buffer after reading
159  return val;
160  }
161 
162  inline
163  size_t RingBuffer::get_index_(const delay d) const
164  {
165  const long_t idx = Scheduler::get_modulo(d);
166  assert(0 <= idx);
167  assert((size_t)idx < buffer_.size());
168  return idx;
169  }
170 
171 
172 
173 
174  class MultRBuffer {
175  public:
176 
177  MultRBuffer();
178 
184  void add_value(const long_t offs, const double_t);
185 
191  double get_value(const long_t offs);
192 
196  void clear();
197 
201  void resize();
202 
206  size_t size() const { return buffer_.size(); }
207 
208  private:
209 
211  std::valarray<double_t> buffer_;
212 
219  size_t get_index_(const delay d) const;
220 
221  };
222 
223  inline
224  void MultRBuffer::add_value(const long_t offs, const double_t v)
225  {
226  assert(0<= offs && (size_t)offs < buffer_.size());
227  buffer_[get_index_(offs)] *= v;
228  }
229 
230  inline
231  double MultRBuffer::get_value(const long_t offs)
232  {
233  assert(0 <= offs && (size_t)offs < buffer_.size());
235 
236  // offs == 0 is beginning of slice, but we have to
237  // take modulo into account when indexing
238  long_t idx = get_index_(offs);
239  double_t val = buffer_[idx];
240  buffer_[idx] = 0.0; // clear buffer after reading
241  return val;
242  }
243 
244  inline
245  size_t MultRBuffer::get_index_(const delay d) const
246  {
247  const long_t idx = Scheduler::get_modulo(d);
248  assert(0 <= idx && (size_t)idx < buffer_.size());
249  return idx;
250  }
251 
252 
253 
255  public:
256 
257  ListRingBuffer();
258 
264  void append_value(const long_t offs, const double_t);
265 
266  std::list<double_t>& get_list(const long_t offs);
267 
272  void clear();
273 
279  void resize();
280 
284  size_t size() const { return buffer_.size(); }
285 
286  private:
287 
289  std::vector<std::list<double_t> > buffer_;
290 
297  size_t get_index_(const delay d) const;
298 
299  };
300 
301  inline
302  void ListRingBuffer::append_value(const long_t offs, const double_t v)
303  {
304  buffer_[get_index_(offs)].push_back(v);
305  }
306 
307  inline
308  std::list<double_t>& ListRingBuffer::get_list(const long_t offs)
309  {
310  assert(0 <= offs && (size_t)offs < buffer_.size());
312 
313  // offs == 0 is beginning of slice, but we have to
314  // take modulo into account when indexing
315  long_t idx = get_index_(offs);
316  return buffer_[idx];
317  }
318 
319  inline
320  size_t ListRingBuffer::get_index_(const delay d) const
321  {
322  const long_t idx = Scheduler::get_modulo(d);
323  assert(0 <= idx);
324  assert((size_t)idx < buffer_.size());
325  return idx;
326  }
327 
328 }
329 
330 
331 #endif
const Name d("d")
Specific to Izhikevich 2003.
Definition: nest_names.h:83
std::list< double_t > & get_list(const long_t offs)
Definition: ring_buffer.h:308
std::valarray< double_t > buffer_
Buffered data.
Definition: ring_buffer.h:124
void resize()
Resize the buffer according to max_thread and max_delay.
Definition: ring_buffer.cpp:29
double get_value(const long_t offs)
Read one value from ring buffer.
Definition: ring_buffer.h:149
static delay get_min_delay()
Return minimal connection delay.
Definition: scheduler.h:805
void resize()
Resize the buffer according to max_thread and max_delay.
Definition: ring_buffer.cpp:52
void clear()
Initialize the buffer with noughts.
Definition: ring_buffer.cpp:39
MultRBuffer()
Definition: ring_buffer.cpp:48
std::valarray< double_t > buffer_
Buffered data.
Definition: ring_buffer.h:211
void set_value(const long_t offs, const double_t)
Set a ring buffer entry to a given value.
Definition: ring_buffer.h:143
assert(pNet!=0)
size_t size() const
Returns buffer size, for memory measurement.
Definition: ring_buffer.h:206
void resize()
Resize the buffer according to max_thread and max_delay.
Definition: ring_buffer.cpp:75
Definition: ring_buffer.h:254
size_t get_index_(const delay d) const
Obtain buffer index.
Definition: ring_buffer.h:163
void clear()
Initialize the buffer with empty lists.
Definition: ring_buffer.cpp:84
size_t get_index_(const delay d) const
Obtain buffer index.
Definition: ring_buffer.h:245
ListRingBuffer()
Definition: ring_buffer.cpp:71
double get_value(const long_t offs)
Read one value from ring buffer.
Definition: ring_buffer.h:231
void add_value(const long_t offs, const double_t)
Add a value to the ring buffer.
Definition: ring_buffer.h:137
void clear()
Initialize the buffer with noughts.
Definition: ring_buffer.cpp:62
std::vector< std::list< double_t > > buffer_
Buffered data.
Definition: ring_buffer.h:289
double double_t
Double precision floating point numbers.
Definition: nest.h:93
size_t size() const
Returns buffer size, for memory measurement.
Definition: ring_buffer.h:119
size_t size() const
Returns buffer size, for memory measurement.
Definition: ring_buffer.h:284
long_t delay
Delay of a connection.
Definition: nest.h:178
void add_value(const long_t offs, const double_t)
Add a value to the ring buffer.
Definition: ring_buffer.h:224
Default types used by the NEST kernel.
void append_value(const long_t offs, const double_t)
Append a value to the ring buffer list.
Definition: ring_buffer.h:302
static delay get_modulo(delay d)
Return (T+d) mod max_delay.
Definition: scheduler.h:785
RingBuffer()
Definition: ring_buffer.cpp:25
size_t get_index_(const delay d) const
Obtain buffer index.
Definition: ring_buffer.h:320
long long_t
Integer number with at least 32 bit.
Definition: nest.h:96
Definition: ring_buffer.h:174
Buffer Layout.
Definition: ring_buffer.h:77