1 #ifndef UTIL_RING_BUFFER_H 2 #define UTIL_RING_BUFFER_H 11 #include <util/misc/Memory.h> 117 template <
class Archive>
118 void serialize(Archive& ar,
const unsigned int version);
139 template <
class Data>
154 template <
class Data>
161 if (other.capacity_ > 0) {
162 assert(other.data_ != 0);
163 Memory::allocate<Data>(data_, other.capacity_);
164 capacity_ = other.capacity_;
167 for (
int i = 0; i < capacity_; ++i) {
168 data_[i] = other.data_[i];
180 template <
class Data>
186 if (
this == &other)
return *
this;
190 assert(other.capacity_ > 0);
194 Memory::allocate<Data>(data_, other.capacity_);
195 capacity_ = other.capacity_;
197 }
else if (capacity_ != other.capacity_) {
199 UTIL_THROW(
"Cannot assign RingBuffers of unequal capacity");
206 for (
int i = 0; i < capacity_; ++i) {
207 data_[i] = other.data_[i];
211 UTIL_THROW(
"Cannot assign unallocated array to allocated array");
218 template <
class Data>
222 Memory::deallocate<Data>(data_, capacity_);
227 template <
class Data>
231 Memory::allocate<Data>(data_,
capacity);
234 UTIL_THROW(
"Error: Attempt to re-allocate a RingBuffer");
236 last_ = capacity_ - 1;
243 template <
class Data>
247 last_ = capacity_ - 1;
254 template <
class Data>
258 assert(last_ < capacity_);
260 if (last_ == capacity_) {
266 if (size_ < capacity_) {
275 template <
class Data>
283 template <
class Data>
286 {
return capacity_; }
291 template <
class Data>
294 {
return (data_ != 0); }
299 template <
class Data>
302 {
return (size_ == capacity_); }
307 template <
class Data>
311 if ( offset >= size_ ) {
325 template <
class Data>
329 if ( offset >= size_ ) {
343 template <
class Data>
344 template <
class Archive>
348 if (Archive::is_saving()) {
349 capacity = capacity_;
352 if (Archive::is_loading()) {
360 if (capacity != capacity_) {
361 UTIL_THROW(
"Inconsistent RingBuffer capacities");
367 for (
int i = 0; i < capacity_; ++i) {
void allocate(int capacity)
Allocate a new empty buffer.
Class for storing history of previous values in an array.
void append(Data value)
Add a new value to the buffer.
virtual ~RingBuffer()
Destructor.
File containing preprocessor macros for error handling.
int capacity() const
Return the capacity of the buffer.
bool isFull() const
Return true if full (if size == capacity), false otherwise.
RingBuffer & operator=(const RingBuffer< Data > &other)
Assignment.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
const Data & operator[](int offset) const
Retrieve a const value, a specified number of time steps ago.
Utility classes for scientific computation.
void serialize(Archive &ar, const unsigned int version)
Serialize a RingBuffer to/from an Archive.
bool isAllocated() const
Return true if the RingBuffer has been allocated, false otherwise.
int size() const
Return number of values currently in the buffer.
void clear()
Set previously allocated buffer to empty state.