1#ifndef UTIL_RING_BUFFER_H
2#define UTIL_RING_BUFFER_H
11#include <util/misc/Memory.h>
146 template <
class Archive>
147 void serialize(Archive& ar,
const unsigned int version);
168 template <
class Data>
183 template <
class Data>
190 if (other.capacity_ > 0) {
191 assert(other.data_ != 0);
192 Memory::allocate<Data>(data_, other.capacity_);
193 capacity_ = other.capacity_;
196 for (int i = 0; i < capacity_; ++i) {
197 data_[i] = other.data_[i];
209 template <
class Data>
215 if (
this == &other)
return *
this;
219 assert(other.capacity_ > 0);
224 capacity_ = other.capacity_;
226 }
else if (capacity_ != other.capacity_) {
228 UTIL_THROW(
"Cannot assign RingBuffers of unequal capacity");
235 for (
int i = 0; i < capacity_; ++i) {
236 data_[i] = other.data_[i];
240 UTIL_THROW(
"Cannot assign unallocated array to allocated array");
247 template <
class Data>
256 template <
class Data>
263 UTIL_THROW(
"Error: Attempt to re-allocate a RingBuffer");
265 last_ = capacity_ - 1;
272 template <
class Data>
276 last_ = capacity_ - 1;
283 template <
class Data>
287 assert(last_ < capacity_);
289 if (last_ == capacity_) {
295 if (size_ < capacity_) {
304 template <
class Data>
308 assert(last_ < capacity_);
310 if (last_ == capacity_) {
315 if (size_ < capacity_) {
323 template <
class Data>
331 template <
class Data>
334 {
return capacity_; }
339 template <
class Data>
342 {
return (data_ != 0); }
347 template <
class Data>
350 {
return (size_ == capacity_); }
355 template <
class Data>
359 if ( offset >= size_ ) {
373 template <
class Data>
377 if ( offset >= size_ ) {
391 template <
class Data>
392 template <
class Archive>
396 if (Archive::is_saving()) {
400 if (Archive::is_loading()) {
409 UTIL_THROW(
"Inconsistent RingBuffer capacities");
415 for (
int i = 0; i < capacity_; ++i) {
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
bool isAllocated() const
Return true if the RingBuffer has been allocated, false otherwise.
bool isFull() const
Return true if full (if size == capacity), false otherwise.
RingBuffer & operator=(RingBuffer< Data > const &other)
Assignment.
void serialize(Archive &ar, const unsigned int version)
Serialize a RingBuffer to/from an Archive.
void allocate(int capacity)
Allocate a new empty buffer.
void clear()
Set previously allocated buffer to empty state.
Data const & operator[](int offset) const
Retrieve a const value, a specified number of time steps ago.
void advance()
Advances the pointer to an added element without assigning a value.
virtual ~RingBuffer()
Destructor.
int size() const
Return number of values currently in the buffer.
void append(Data const &value)
Add a new value to the buffer.
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Utility classes for scientific computation.