-
Notifications
You must be signed in to change notification settings - Fork 156
/
util_circular_buffer.h
374 lines (296 loc) · 12.6 KB
/
util_circular_buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2007 - 2015.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef UTIL_CIRCULAR_BUFFER_2007_11_22_H_
#define UTIL_CIRCULAR_BUFFER_2007_11_22_H_
#include <algorithm>
#include <cstddef>
#include <iterator>
namespace util
{
// Forward declaration of the circular_buffer class.
template<typename T,
const std::size_t N>
class circular_buffer;
namespace circular_buffer_detail
{
template<typename T,
const std::size_t N,
typename traits_type>
struct circular_iterator : public std::iterator<std::random_access_iterator_tag,
typename traits_type::value_type,
typename traits_type::difference_type,
typename traits_type::pointer,
typename traits_type::reference>
{
public:
typedef std::iterator<std::random_access_iterator_tag,
typename traits_type::value_type,
typename traits_type::difference_type,
typename traits_type::pointer,
typename traits_type::reference> iterator_type;
typedef typename iterator_type::value_type value_type;
typedef typename iterator_type::pointer pointer;
typedef typename iterator_type::reference reference;
typedef const reference const_reference;
typedef typename std::size_t size_type;
typedef typename iterator_type::difference_type difference_type;
explicit circular_iterator(const pointer pb,
const pointer pc = pb) : buffer_pointer (pb),
circular_pointer(pc) { }
~circular_iterator() { }
circular_iterator& operator=(const circular_iterator& other_iterator)
{
if(this != &other_iterator)
{
buffer_pointer = other_iterator.buffer_pointer;
circular_pointer = other_iterator.circular_pointer;
}
return *this;
}
const_reference operator*() const
{
return *circular_pointer;
}
reference operator*()
{
return *const_cast<pointer>(circular_pointer);
}
circular_iterator& operator++()
{
++circular_pointer;
if(circular_pointer > (buffer_pointer + difference_type(N)))
{
circular_pointer = buffer_pointer;
}
return *this;
}
circular_iterator& operator--()
{
--circular_pointer;
if(circular_pointer < buffer_pointer)
{
circular_pointer = buffer_pointer + difference_type(N);
}
return *this;
}
circular_iterator operator++(int)
{
circular_iterator tmp = *this;
++(*this);
return tmp;
}
circular_iterator operator--(int)
{
circular_iterator tmp = *this;
--(*this);
return tmp;
}
circular_iterator& operator+=(difference_type n)
{
if(n > difference_type(0))
{
circular_pointer += n;
if(circular_pointer > (buffer_pointer + difference_type(N)))
{
circular_pointer -= difference_type(N);
}
}
else if(n < 0)
{
*this -= -n;
}
return *this;
}
circular_iterator& operator-=(difference_type n)
{
if(n > difference_type(0))
{
circular_pointer -= n;
if(circular_pointer < buffer_pointer)
{
circular_pointer += difference_type(N);
}
}
else if(n < 0)
{
*this += -n;
}
return *this;
}
private:
pointer buffer_pointer;
pointer circular_pointer;
circular_iterator();
friend inline circular_iterator operator+(const circular_iterator& a, difference_type n) { return circular_iterator(a) += n; }
friend inline circular_iterator operator-(const circular_iterator& a, difference_type n) { return circular_iterator(a) -= n; }
friend inline bool operator==(const circular_iterator& a, const circular_iterator& b) { return (a.circular_pointer == b.circular_pointer); }
friend inline bool operator!=(const circular_iterator& a, const circular_iterator& b) { return (a.circular_pointer != b.circular_pointer); }
// TBD: Implement operator<().
// TBD: Implement the remaining operators.
};
}
template<typename T,
const std::size_t N = std::size_t(16U)>
class circular_buffer
{
public:
static_assert(N > std::size_t(0U),
"Error: The circular_buffer class requires more than zero elements.");
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef circular_buffer_detail::circular_iterator<T, N, std::iterator_traits<T*> > iterator;
typedef circular_buffer_detail::circular_iterator<T, N, std::iterator_traits<const T*> > const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
circular_buffer() : buffer (),
my_size(difference_type(0)),
first (buffer + difference_type(N)),
last (buffer + difference_type(N))
{
std::fill(buffer, buffer + difference_type(N), value_type());
}
explicit circular_buffer(const size_type count,
const value_type value = value_type()) : buffer (),
my_size(difference_type(count)),
first (buffer + difference_type(N - count)),
last (buffer + difference_type(N))
{
std::fill(buffer, buffer + difference_type(N - count), value_type());
std::fill(buffer + difference_type(N - count), buffer + difference_type(N), value);
}
circular_buffer(const circular_buffer& other) : buffer (),
my_size(difference_type(other.size())),
first (buffer + difference_type(other.first - other.buffer)),
last (buffer + difference_type(other.last - other.buffer))
{
std::copy(other.buffer, other.buffer + difference_type(N), buffer);
}
~circular_buffer() { }
circular_buffer& operator=(const circular_buffer& other)
{
if(this != &other)
{
std::copy(other.buffer, other.buffer + difference_type(N), buffer);
my_size = difference_type(other.size());
first = buffer + difference_type(other.first - other.buffer);
last = buffer + difference_type(other.last - other.buffer);
}
return *this;
}
static size_type capacity() { return N; }
bool empty() const
{
return (my_size == difference_type(0U));
}
size_type size() const
{
return size_type(my_size);
}
void clear()
{
first = buffer;
last = buffer + difference_type(N);
my_size = difference_type(0);
}
void push_front(const value_type value)
{
--first;
if(first < buffer)
{
first = buffer + difference_type(N - 1U);
}
*first = value;
if(my_size < difference_type(N))
{
++my_size;
}
}
void pop_back()
{
if(my_size > difference_type(0))
{
--my_size;
// TBD: Do we actually need to manually call the destructor here?
last->~value_type();
--last;
if(last <= buffer)
{
last = buffer + difference_type(N);
}
}
}
iterator begin () { return iterator (buffer, first); }
const_iterator begin () const { return const_iterator(buffer, first); }
const_iterator cbegin() const { return const_iterator(buffer, first); }
iterator end () { return iterator (buffer, last); }
const_iterator end () const { return const_iterator(buffer, last); }
const_iterator cend () const { return const_iterator(buffer, last); }
reverse_iterator rbegin () { return std::reverse_iterator<iterator> (iterator (buffer, last)); }
const_reverse_iterator rbegin () const { return std::reverse_iterator<const_iterator>(const_iterator(buffer, last)); }
const_reverse_iterator crbegin() const { return std::reverse_iterator<const_iterator>(const_iterator(buffer, last)); }
reverse_iterator rend () { return std::reverse_iterator<iterator> (iterator (buffer, first)); }
const_reverse_iterator rend () const { return std::reverse_iterator<const_iterator>(const_iterator(buffer, first)); }
const_reverse_iterator crend () const { return std::reverse_iterator<const_iterator>(const_iterator(buffer, first)); }
reference front () { return *begin(); }
const_reference front () const { return *begin(); }
reference back () { return *(end() - iterator::difference_type(1)); }
const_reference back () const { return *(cend() - const_iterator::difference_type(1)); }
private:
value_type buffer[N];
difference_type my_size;
pointer first;
pointer last;
};
}
/*
#include <algorithm>
#include <iostream>
#include <iterator>
#include <util/utility/util_circular_buffer.h>
template<typename circular_buffer_type>
void do_something()
{
circular_buffer_type cb(1U, 1U);
std::cout << cb.size() << ", " << cb.front() << ", " << cb.back() << std::endl;
cb.push_front(2U);
std::cout << cb.size() << ", " << cb.front() << ", " << cb.back() << std::endl;
cb.push_front(3U);
std::cout << cb.size() << ", " << cb.front() << ", " << cb.back() << std::endl;
cb.pop_back();
std::cout << cb.size() << ", " << cb.front() << ", " << cb.back() << std::endl;
cb.pop_back();
std::cout << cb.size() << ", " << cb.front() << ", " << cb.back() << std::endl;
cb.pop_back();
std::cout << cb.size() << ", " << cb.front() << ", " << cb.back() << std::endl;
cb.push_front(101U);
cb.push_front(102U);
cb.push_front(103U);
std::cout << cb.size() << ", " << cb.front() << ", " << cb.back() << std::endl;
for(auto it = cb.cbegin(); it != cb.cbegin() + cb.size(); ++it) { std::cout << *it << std::endl; }
for(auto it = cb.crbegin(); it != cb.crend(); ++it) { std::cout << *it << std::endl; }
cb.pop_back();
std::cout << cb.size() << ", " << cb.front() << ", " << cb.back() << std::endl;
cb.pop_back();
std::cout << cb.size() << ", " << cb.front() << ", " << cb.back() << std::endl;
cb.push_front(42U);
std::cout << cb.size() << ", " << cb.front() << ", " << cb.back() << std::endl;
circular_buffer_type other_cb;
other_cb = cb;
volatile unsigned debug = 0U;
static_cast<void>(debug);
}
int main()
{
do_something<util::circular_buffer<unsigned, 3U>>();
}
*/
#endif // UTIL_CIRCULAR_BUFFER_2007_11_22_H_