A Hopf, Skip and a Jump
|
SlidingBuffer is class template which can be used when reading a data stream to provide access to the stream's immediate history. More...
#include <slidingbuffer.h>
Public Member Functions | |
SlidingBuffer (P &sp, const std::size_t max_segs, const std::size_t seg_size) | |
Create a SlidingBuffer. More... | |
virtual | ~SlidingBuffer () |
Destructor. | |
T & | operator[] (long int idx) |
Access an element of the SlidingBuffer. More... | |
Protected Attributes | |
std::size_t | origin |
Lowest index in buffer. | |
const std::size_t | max_segs |
The largest number of segments in the buffer A history of at least \(seg_size\cdot(max_segs-1)\) entities will be made available subject to sufficient data having been read from the data stream. | |
const std::size_t | seg_size |
The number of entities in each segment. | |
std::deque< S *, UniqueAllocator< S * > > | segs |
Segments held by the buffer. | |
P & | sp |
The producer to call to fill a segment. | |
SlidingBuffer is class template which can be used when reading a data stream to provide access to the stream's immediate history.
A SlidingBuffer is composed of a number of Segments . It provides a method of accessing the segments through the [] operator so that the reading and discarding of segments is automatic and transparent. This means the data stream can be accessed as if it it were a conventional array (or other container supporting the operator[]
method), except that as the greatest index to date increases, older elements are forgotten.
Segments are created and filled on demand. As the number of Segments specified at instantiation is exceeded, the oldest Segments are discarded. This means that the recent history of the buffered data is always available, "recent" being defined by the size of the SlidingBuffer.
SlidingBuffers may only slide forward, which is to say that the accessed index must never be less than the length of the buffer prior to the greatest index ever accessed. Failing to comply with this constraint causes an exception to be thrown.
For example, suppose that a buffer comprising four segments each of three integers is created.
TODO The following isn't implemented yet... SegmentProducer::generate()
is called asynchronously to fill new Segments. A separate thread is created to calculate the datasamples used to populate the segment. In the event that a SlidingBuffer Segment is accessed before the SegmentProducer has finished filling it, the access blocks until such time as tne data becomes available. END TODO
Mindful that the act of filling a SlidingBuffer may be (and usually is) stateful, although the accessed index can be advanced arbitrarily towards the end of the SlidingBuffer, the SegmentProducer will be called to generate data up until the point it is required, attempting to access data before the beginning of the SlidingBuffer will cause an exception to be thrown, even though the index may be well above 0.
T | The type of the primitive data held in the buffer |
S | The types of the segments comprising the buffer |
P | The type of the producer |
Definition at line 245 of file slidingbuffer.h.
|
inline |
Create a SlidingBuffer.
Starting at any point in the buffer, a history of at least seg_size*(max_segs-1)
entities is available.
sp | The producer whose generate() method is invoked to populate a new segment |
max_segs | The maxiumum number of segments stored in the buffer |
seg_size | The number of entities in each segment |
Definition at line 268 of file slidingbuffer.h.
|
inline |
Access an element of the SlidingBuffer.
Return a reference to the element of the SlidingBuffer or throw an exception if it is not available
idx | Index to access. |
NegativeIndexException | The index is less than 0. |
ExpiredIndexException | The indexed element is no longer available. |
IndexOutOfRangeException | The indexed element is not yet in the SlidingBuffer and can not be produced by the Producer. |
Definition at line 295 of file slidingbuffer.h.