forked from vinitjames/circularbuffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular_buffer.h
542 lines (467 loc) · 12.9 KB
/
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#ifndef CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H
#include <algorithm>
#include <iterator>
#include <mutex>
#include <memory>
#include <stdexcept>
#include <utility>
template<typename T>
class CircularBuffer {
private:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
template <bool isConst> struct BufferIterator;
public:
explicit CircularBuffer(size_t size)
:_buff{std::unique_ptr<T[]>(new value_type[size])}, _max_size{size}{}
CircularBuffer(const CircularBuffer& other)
:_buff{std::unique_ptr<T[]>(new value_type[other._max_size])},
_max_size{other._max_size},
_size{other._size},
_head{other._head},
_tail{other._tail}{
std::copy(other.data(), other.data() + _max_size, _buff.get());
}
CircularBuffer& operator=(const CircularBuffer& other){
if ( this != &other){
_buff.reset(new value_type[other._max_size]);
_max_size = other._max_size;
_size = other._size;
_head = other._head;
_tail = other._tail;
std::copy(other.data(), other.data() + _max_size, _buff.get());
}
return *this;
}
CircularBuffer(CircularBuffer&& other) noexcept
:_buff{std::move(other._buff)},
_max_size{other._max_size},
_size{other._size},
_head{other._head},
_tail{other._tail}{
other._buff = nullptr;
other._max_size = 0;
other._size = 0;
other._head = 0;
other._tail = 0;
}
CircularBuffer& operator=(CircularBuffer&& other) noexcept{
if ( this != &other){
_buff = std::move(other._buff);
_max_size = other._max_size;
_size = other._size;
_head = other._head;
_tail = other._tail;
other._buff = nullptr;
other._max_size = 0;
other._size = 0;
other._head = 0;
other._tail = 0;
}
return *this;
}
void push_back(const value_type& data);
void push_back(value_type&& data) noexcept;
void pop_front();
reference front();
reference back();
const_reference front() const;
const_reference back() const;
void clear();
bool empty() const ;
bool full() const ;
size_type capacity() const ;
size_type size() const;
size_type buffer_size() const {return sizeof(value_type)*_max_size;};
const_pointer data() const { return _buff.get(); }
const_reference operator[](size_type index) const;
reference operator[](size_type index);
const_reference at(size_type index) const;
reference at(size_type index);
typedef BufferIterator<false> iterator;
typedef BufferIterator<true> const_iterator;
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
iterator rbegin() noexcept;
const_iterator rbegin() const noexcept;
iterator rend() noexcept;
const_iterator rend() const noexcept;
private:
void _increment_bufferstate();
void _decrement_bufferstate();
mutable std::mutex _mtx;
std::unique_ptr<value_type[]> _buff;
size_type _head = 0;
size_type _tail = 0;
size_type _size = 0;
size_type _max_size = 0;
template<bool isConst = false>
struct BufferIterator{
public:
friend class CircularBuffer<T>;
typedef std::random_access_iterator_tag iterator_category;
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef typename std::conditional<isConst, const value_type&, value_type&>::type reference;
typedef typename std::conditional<isConst, const value_type*, value_type*>::type pointer;
typedef typename std::conditional<isConst, const CircularBuffer<value_type>*,
CircularBuffer<value_type>*>::type cbuf_pointer;
private:
cbuf_pointer _ptrToBuffer;
size_type _offset;
size_type _index;
bool _reverse;
bool _comparable(const BufferIterator<isConst>& other) const{
return (_ptrToBuffer == other._ptrToBuffer)&&(_reverse == other._reverse);
}
public:
BufferIterator()
:_ptrToBuffer{nullptr}, _offset{0}, _index{0}, _reverse{false}{}
BufferIterator(const BufferIterator<false>& it)
:_ptrToBuffer{it._ptrToBuffer},
_offset{it._offset},
_index{it._index},
_reverse{it._reverse}{}
reference operator*(){
if(_reverse)
return (*_ptrToBuffer)[(_ptrToBuffer->size() - _index - 1)];
return (*_ptrToBuffer)[_index];
}
pointer operator->() { return &(operator*()); }
reference operator[](size_type index){
BufferIterator iter = *this;
iter._index += index;
return *iter;
}
BufferIterator& operator++(){
++_index;
return *this;
}
BufferIterator operator++(int){
BufferIterator iter = *this;
++_index;
return iter;
}
BufferIterator& operator--(){
--_index;
return *this;
}
BufferIterator operator--(int){
BufferIterator iter = *this;
--_index;
return iter;
}
friend BufferIterator operator+(BufferIterator lhsiter, difference_type n){
lhsiter._index += n;
return lhsiter;
}
friend BufferIterator operator+(difference_type n, BufferIterator rhsiter){
rhsiter._index += n;
return rhsiter;
}
BufferIterator& operator+=(difference_type n){
_index += n;
return *this;
}
friend BufferIterator operator-(BufferIterator lhsiter, difference_type n){
lhsiter._index -= n;
return lhsiter;
}
friend difference_type operator-(const BufferIterator& lhsiter, const BufferIterator& rhsiter){
return lhsiter._index - rhsiter._index;
}
BufferIterator& operator-=(difference_type n){
_index -= n;
return *this;
}
bool operator==(const BufferIterator& other) const{
if (!_comparable(other))
return false;
return ((_index == other._index)&&(_offset == other._offset));
}
bool operator!=(const BufferIterator& other) const{
if (!_comparable(other))
return true;
return ((_index != other._index)||(_offset != other._offset));
}
bool operator<(const BufferIterator& other) const {
if (!_comparable(other))
return false;
return ((_index + _offset)<(other._index+other._offset));
}
bool operator>(const BufferIterator& other) const{
if (!_comparable(other))
return false;
return ((_index + _offset)>(other._index+other._offset));
}
bool operator<=(const BufferIterator& other) const {
if (!_comparable(other))
return false;
return ((_index + _offset)<=(other._index+other._offset));
}
bool operator>=(const BufferIterator& other) const {
if (!_comparable(other))
return false;
return ((_index + _offset)>=(other._index+other._offset));
}
};
};
template<typename T>
inline
bool CircularBuffer<T>::full() const{
return _size == _max_size;
}
template<typename T>
inline
bool CircularBuffer<T>::empty() const{
return _size == 0;
}
template<typename T>
inline
typename CircularBuffer<T>::size_type CircularBuffer<T>::capacity() const{
return _max_size;
}
template<typename T>
inline
void CircularBuffer<T>::clear(){
std::lock_guard<std::mutex> _lck(_mtx);
_head = _tail = _size = 0;
}
template<typename T>
inline
typename CircularBuffer<T>::size_type CircularBuffer<T>::size() const{
std::lock_guard<std::mutex> _lck(_mtx);
return _size;
}
template<typename T>
inline
typename CircularBuffer<T>::reference CircularBuffer<T>::front() {
std::lock_guard<std::mutex> _lck(_mtx);
if(empty())
throw std::length_error("front function called on empty buffer");
return _buff[_tail];
}
template<typename T>
inline
typename CircularBuffer<T>::reference CircularBuffer<T>::back() {
std::lock_guard<std::mutex> _lck(_mtx);
if(empty())
throw std::length_error("back function called on empty buffer");
return _head == 0 ? _buff[_max_size - 1] : _buff[_head - 1];
}
template<typename T>
inline
typename CircularBuffer<T>::const_reference CircularBuffer<T>::front() const{
std::lock_guard<std::mutex> _lck(_mtx);
if(empty())
throw std::length_error("front function called on empty buffer");
return _buff[_tail];
}
template<typename T>
inline
typename CircularBuffer<T>::const_reference CircularBuffer<T>::back() const{
std::lock_guard<std::mutex> _lck(_mtx);
if(empty())
throw std::length_error("back function called on empty buffer");
return _head == 0 ? _buff[_max_size - 1] : _buff[_head - 1];
}
template<typename T>
inline
void CircularBuffer<T>::push_back(const T& data){
std::lock_guard<std::mutex> _lck(_mtx);
//if(full())
// _buff[_tail].~T();
_buff[_head] = data;
_increment_bufferstate();
}
template<typename T>
inline
void CircularBuffer<T>::push_back(T&& data) noexcept{
std::lock_guard<std::mutex> _lck(_mtx);
_buff[_head] = std::move(data);
_increment_bufferstate();
}
template<typename T>
inline
void CircularBuffer<T>::_increment_bufferstate(){
if(full())
_tail = (_tail + 1)%_max_size;
else
++_size;
_head = (_head + 1)%_max_size;
}
template<typename T>
inline
void CircularBuffer<T>::pop_front(){
std::lock_guard<std::mutex> _lck(_mtx);
if(empty())
throw std::length_error("pop_front called on empty buffer");
_decrement_bufferstate();
}
template<typename T>
inline
void CircularBuffer<T>::_decrement_bufferstate(){
--_size;
_tail = (_tail + 1)%_max_size;
}
template<typename T>
inline
typename CircularBuffer<T>::reference CircularBuffer<T>::operator[](size_t index) {
std::lock_guard<std::mutex> _lck(_mtx);
if((index<0)||(index>=_size))
throw std::out_of_range("Index is out of Range of buffer size");
index += _tail;
index %= _max_size;
return _buff[index];
}
template<typename T>
inline
typename CircularBuffer<T>::const_reference CircularBuffer<T>::operator[](size_t index) const {
std::lock_guard<std::mutex> _lck(_mtx);
if((index<0)||(index>=_size))
throw std::out_of_range("Index is out of Range of buffer size");
index += _tail;
index %= _max_size;
return _buff[index];
}
template<typename T>
inline
typename CircularBuffer<T>::reference CircularBuffer<T>::at(size_t index) {
std::lock_guard<std::mutex> _lck(_mtx);
if((index<0)||(index>=_size))
throw std::out_of_range("Index is out of Range of buffer size");
index += _tail;
index %= _max_size;
return _buff[index];
}
template<typename T>
inline
typename CircularBuffer<T>::const_reference CircularBuffer<T>::at(size_t index) const {
std::lock_guard<std::mutex> _lck(_mtx);
if((index<0)||(index>=_size))
throw std::out_of_range("Index is out of Range of buffer size");
index += _tail;
index %= _max_size;
return _buff[index];
}
template<typename T>
inline
typename CircularBuffer<T>::iterator CircularBuffer<T>::begin() {
std::lock_guard<std::mutex> _lck(_mtx);
iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = 0;
iter._reverse = false;
return iter;
}
template<typename T>
inline
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::begin() const{
std::lock_guard<std::mutex> _lck(_mtx);
const_iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = 0;
iter._reverse = false;
return iter;
}
template<typename T>
inline
typename CircularBuffer<T>::iterator CircularBuffer<T>::end() {
std::lock_guard<std::mutex> _lck(_mtx);
iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = _size;
iter._reverse = false;
return iter;
}
template<typename T>
inline
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::end() const{
std::lock_guard<std::mutex> _lck(_mtx);
const_iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = _size;
iter._reverse = false;
return iter;
}
template<typename T>
inline
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::cbegin() const noexcept{
std::lock_guard<std::mutex> _lck(_mtx);
const_iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = 0;
iter._reverse = false;
return iter;
}
template<typename T>
inline
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::cend() const noexcept{
std::lock_guard<std::mutex> _lck(_mtx);
const_iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = _size;
iter._reverse = false;
return iter;
}
template<typename T>
inline
typename CircularBuffer<T>::iterator CircularBuffer<T>::rbegin() noexcept{
std::lock_guard<std::mutex> _lck(_mtx);
iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = 0;
iter._reverse = true;
return iter;
}
template<typename T>
inline
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::rbegin() const noexcept{
std::lock_guard<std::mutex> _lck(_mtx);
const_iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = 0;
iter._reverse = true;
return iter;
}
template<typename T>
inline
typename CircularBuffer<T>::iterator CircularBuffer<T>::rend() noexcept{
std::lock_guard<std::mutex> _lck(_mtx);
iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = _size;
iter._reverse = true;
return iter;
}
template<typename T>
inline
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::rend() const noexcept{
std::lock_guard<std::mutex> _lck(_mtx);
const_iterator iter;
iter._ptrToBuffer = this;
iter._offset = _tail;
iter._index = _size;
iter._reverse = true;
return iter;
}
#endif /* CIRCULAR_BUFFER_H */