35 #ifndef CIRCULAR_BUFFER_H
36 #define CIRCULAR_BUFFER_H
108 void put(
const T& in);
123 void get(
Vec<T>& out,
const int N = -1);
126 void get(
Array<T>& out,
const int N = -1);
129 void peek(T& out)
const;
135 void peek(
const int index, T& out)
const;
138 void peek(
Vec<T>& out,
const int N = -1)
const;
141 void peek(
const ivec& index,
Vec<T>& out)
const;
168 int size()
const {
return _ndata; }
174 void set_size(
int n,
bool copy =
false);
217 _rw_dist = cb._rw_dist;
220 for (
int i = 0; i < cb._ndata; i++) { _data[i] = cb._data[i]; }
232 it_assert_debug(_rw_dist > 0,
"Buffer empty. No data left to read from the buffer.");
237 if (_read == _ndata) { _read = 0; }
261 for (
int i = 0;i < N_out;i++) {
262 it_assert_debug(_rw_dist > 0,
"Buffer empty. No data left to read from the buffer.");
263 out(i) = _data[_read];
284 for (
int i = 0;i < N_out;i++) {
285 it_assert_debug(_rw_dist > 0,
"Buffer empty. No data left to read from the buffer.");
286 out(i) = _data[_read];
298 it_assert_debug(_rw_dist > 0,
"Attempted to peek at an empty buffer.");
314 it_assert_debug(_rw_dist > index && index >= 0,
"The index exceeds the number of elements stored in the buffer.");
315 out = _data[(_read+index)%_ndata];
322 int read_tmp = _read;
329 it_assert_debug(_rw_dist >= N_out,
"Attempted to peek at more elements than there are stored in the buffer.");
332 for (
int i = 0;i < N_out;i++) {
333 out(i) = _data[read_tmp];
335 if (read_tmp == _ndata)
345 for (
int i = 0;i < index.size();i++) {
346 it_assert_debug(_rw_dist >= index(i) && index(i) >= 0,
"Attempted to peek at an element, whose index exceeds the number of buffered elements.");
347 out(i) = _data[(_read+index(i))%_ndata];
355 int read_tmp = _read;
362 it_assert_debug(_rw_dist >= N_out,
"Attempted to peek at more elements than there are stored in the buffer.");
365 for (
int i = 0;i < N_out;i++) {
366 out(i) = _data[read_tmp];
368 if (read_tmp == _ndata)
378 for (
int i = 0;i < index.size();i++) {
379 it_assert_debug(_rw_dist >= index(i) && index(i) >= 0,
"Attempted to peek at an element, whose index exceeds the number of buffered elements.");
380 out(i) = _data[(_read+index(i))%_ndata];
389 it_assert_debug(_rw_dist > 0,
"Attempted to peek at an empty buffer.");
392 read_tmp = _write - 1;
394 read_tmp = _ndata - 1;
396 out = _data[read_tmp];
419 it_assert_debug(_rw_dist >= N_out,
"Attempted to peek at more elements than there are stored in the buffer.");
423 read_tmp = _write - 1;
425 read_tmp = _ndata - 1;
427 for (
int i = 0;i < N_out;i++) {
428 out(i) = _data[read_tmp];
431 read_tmp = _ndata - 1;
446 it_assert_debug(_rw_dist >= N_out,
"Attempted to peek at more elements than there are stored in the buffer.");
450 read_tmp = _write - 1;
452 read_tmp = _ndata - 1;
454 for (
int i = 0;i < N_out;i++) {
455 out(i) = _data[read_tmp];
458 read_tmp = _ndata - 1;
466 if (_rw_dist >= _ndata) {
477 if (_write >= _ndata)
485 for (
int i = 0;i < in.
size();i++) {
487 if (_rw_dist >= _ndata) {
493 _data[_write] = in(i);
498 if (_write >= _ndata)
507 for (
int i = 0;i < in.
size();i++) {
509 if (_rw_dist >= _ndata) {
515 _data[_write] = in(i);
520 if (_write >= _ndata)
542 _data =
new T[_ndata];
543 it_assert(_data != 0,
"Out of memory in Circular_Buffer::alloc");
546 it_error(
"Circular_Buffer<T>::alloc(int n): n must be positive");
551 void Circular_Buffer<T>::free()
566 for (
int i = 0; i < _ndata; i++)
567 _data[i] = s._data[i];
570 _rw_dist = _write - _read;
576 int i, min_nrof_elem;
584 peek_reverse(tmp, -1);
585 min_nrof_elem = _rw_dist < sz ? _rw_dist : sz;
588 for (i = 0; i < min_nrof_elem; i++)
589 put(tmp(min_nrof_elem - 1 - i));
601 #endif // #ifndef CIRCULAR_BUFFER_H
void operator=(const Circular_Buffer< T > &s)
Assignment operator.
void clear()
Empty the circular buffer.
int nrof_elements() const
Returns the number of data elements currently stored in the circular buffer.
Circular_Buffer()
Default constructor.
void set_size(int size, bool copy=false)
Set length of vector. if copy = true then keeping the old values.
#define it_assert(t, s)
Abort if t is not true.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition of Array class (container)
void set_size(int n, bool copy=false)
Resizing a Circular_Buffer.
void put(const T &in)
Write the element in to the buffer.
T get()
Get the oldest element in the circular buffer.
void set_size(int n, bool copy=false)
Resizing an Array.
General circular buffer class.
int size() const
Returns the maximum number of data elements the circular buffer can store.
T peek_reverse() const
Peek at the latest element in the circular buffer, without removing it.
int size() const
The size of the vector.
int size() const
Returns the number of data elements in the array object.
#define it_error(s)
Abort unconditionally.
T peek() const
Peek at the oldest element in the circular buffer, without removing it.
virtual ~Circular_Buffer()
Default destructor.
Templated Vector Class Definitions.