Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
circularbuffers.h
Go to the documentation of this file.
1
3#include "elementa/license.inc"
4#include "elementa/checks.inc"
5
6#ifndef ELEMENTA_ADTS_CIRCULARBUFFER_H
7#define ELEMENTA_ADTS_CIRCULARBUFFER_H
8
9
10#include <vector>
12
13
14namespace elementa
15{
16
17namespace adts
18{
19
38/* ***************************************************************************
39
40 Template class: CircularBuffer
41
42*******************************************************************************/
43
45
47template <typename ElemType>
49{
50 public:
51
52
54
55 CircularBuffer(size_t s);
56
57
59
61 void clear(void) { initpos_ = 0; size_ = 0; }
62
64 size_t capacity(void) const noexcept { return(capacity_); }
65
67 size_t size(void) const noexcept { return(size_); }
68
70 bool empty(void) const noexcept { return(size_ == 0); }
71
73
74 ElemType & back(void);
75
77
79 void push_back(const ElemType & e);
80
82
83 ElemType & front(void);
84
86
88 void pop_front(void);
89
90
92 ElemType & operator[](size_t index);
93
94
95 private:
96
97 const size_t capacity_;
98 std::vector<ElemType> storage_;
99 size_t initpos_,size_;
100
101 constexpr size_t index(size_t i) noexcept
102 { return((initpos_ + i) % capacity_); }
103};
104
105
106
107/* ===========================================================================
108
109 ========== IMPLEMENTATION OF TEMPLATES ==========
110
111============================================================================= */
112
113
114/* ***************************************************************************
115
116 Template class: CircularBuffer
117
118*******************************************************************************/
119
120template <typename ElemType>
122 storage_(capacity_),
123 initpos_{0},
124 size_{0}
125{
126 if (s == 0) ELE_CODE_INVARG("Invalid capacity for circular buffer");
127}
128
129template <typename ElemType>
131{
132 if (empty()) ELE_CODE_INVSTATE("Empty circular buffer has no back");
133 return(storage_[index(size_ - 1)]);
134}
135
136template <typename ElemType>
138{
139 if (size_ == capacity_) pop_front();
140 storage_[index(size_)] = e;
141 ++size_;
142}
143
144template <typename ElemType>
146{
147 if (empty()) ELE_CODE_INVSTATE("Empty circular buffer has no front");
148 return(storage_[initpos_]);
149}
150
151template <typename ElemType>
153{
154 if (empty()) ELE_CODE_INVSTATE("Cannot pop from empty circular buffer");
155 ++initpos_;
156 if (initpos_ >= capacity_) initpos_ = 0;
157 --size_;
158}
159
160template <typename ElemType>
162{
163 if (ind >= size_) ELE_CODE_OUTOFRANGE("Not such index in the buffer");
164 return(storage_[index(ind)]);
165}
166
167 // circulabuffer
169
170
171} // end adts namespace
172
173} // end elementa namespace
174
175
176#endif
177
size_t size(void) const noexcept
Return the number of elements currently in the buffer.
void clear(void)
Clear all content.
bool empty(void) const noexcept
Return true if the buffer is empty.
size_t capacity(void) const noexcept
Return the fixed room for elements in the buffer.
A circular buffer with fixed size, contiguous storage and generic elements.
CircularBuffer(size_t s)
Constructor.
void pop_front(void)
Erases the oldest element in the buffer.
void push_back(const ElemType &e)
Copy E into the buffer, as the last element.
ElemType & front(void)
Get a ref to the oldest element in the buffer.
ElemType & operator[](size_t index)
Access the n-th element in the buffer or throw if not such element.
ElemType & back(void)
Get a ref to the newest element in the buffer.
#define ELE_CODE_INVSTATE(expl)
To throw an invalid-state exception with an explanation.
Definition: exceptions.h:306
#define ELE_CODE_OUTOFRANGE(expl)
To throw an out-of-range exception with an explanation.
Definition: exceptions.h:314
#define ELE_CODE_INVARG(expl)
To throw an invalid-argument exception with an explanation.
Definition: exceptions.h:310