35#ifndef CIRCULAR_BUFFER_H
36#define CIRCULAR_BUFFER_H
108 void put(
const T&
in);
135 void peek(
const int index, T&
out)
const;
168 int size()
const {
return _ndata; }
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; }
262 it_assert_debug(_rw_dist > 0,
"Buffer empty. No data left to read from the buffer.");
263 out(
i) = _data[_read];
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];
329 it_assert_debug(_rw_dist >=
N_out,
"Attempted to peek at more elements than there are stored in the buffer.");
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];
362 it_assert_debug(_rw_dist >=
N_out,
"Attempted to peek at more elements than there are stored in the buffer.");
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.");
419 it_assert_debug(_rw_dist >=
N_out,
"Attempted to peek at more elements than there are stored in the buffer.");
446 it_assert_debug(_rw_dist >=
N_out,
"Attempted to peek at more elements than there are stored in the buffer.");
466 if (_rw_dist >= _ndata) {
477 if (_write >= _ndata)
487 if (_rw_dist >= _ndata) {
493 _data[_write] =
in(
i);
498 if (_write >= _ndata)
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");
551void Circular_Buffer<T>::free()
566 for (
int i = 0;
i < _ndata;
i++)
567 _data[
i] = s._data[
i];
570 _rw_dist = _write - _read;
584 peek_reverse(
tmp, -1);
Definition of Array class (container)
void alloc(int n)
Allocate storage for an array of length n.
void free()
Free the storage space allocated by the array.
int size() const
Returns the number of data elements in the array object.
void set_size(int n, bool copy=false)
Resizing an Array<T>.
General circular buffer class.
void clear()
Empty the circular buffer.
void put(const T &in)
Write the element in to the buffer.
int size() const
Returns the maximum number of data elements the circular buffer can store.
int nrof_elements() const
Returns the number of data elements currently stored in the circular buffer.
void operator=(const Circular_Buffer< T > &s)
Assignment operator.
T peek_reverse() const
Peek at the latest element in the circular buffer, without removing it.
void set_size(int n, bool copy=false)
Resizing a Circular_Buffer<T>.
virtual ~Circular_Buffer()
Default destructor.
T get()
Get the oldest element in the circular buffer.
Circular_Buffer()
Default constructor.
T peek() const
Peek at the oldest element in the circular buffer, without removing it.
#define it_error(s)
Abort unconditionally.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
#define it_assert(t, s)
Abort if t is not true.
Templated Vector Class Definitions.