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