-
Notifications
You must be signed in to change notification settings - Fork 19
/
CircularBuffer.h
127 lines (105 loc) · 2.68 KB
/
CircularBuffer.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
#ifndef CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H
#include <algorithm>
#include <cstddef>
#include <cassert>
#include <stdexcept>
#include <iostream>
template <typename T>
class CircularBuffer
{
public:
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
explicit CircularBuffer(uint64_t capacity);
CircularBuffer(const CircularBuffer<T> &rhs);
CircularBuffer(CircularBuffer<T>&& rhs);
~CircularBuffer() { if (_buffer) delete[] _buffer; }
CircularBuffer<T>& operator=(CircularBuffer<T> rhs);
uint64_t size() const;
uint64_t capacity() const { return _capacity; }
void add(T item); // we add at the
T pop();
T& front();
friend void swap(CircularBuffer<T> &a, CircularBuffer<T> &b) {
std::swap(a._buffer, b._buffer);
std::swap(a._capacity, b._capacity);
std::swap(a._front, b._front);
std::swap(a._full, b._full);
}
private:
pointer _buffer;
uint64_t _capacity;
// we add at the tail and we pop from front
uint64_t _front;
uint64_t _tail;
CircularBuffer();
};
template<typename T>
uint64_t CircularBuffer<T>::size() const {
if (_tail < _front) { // we wrapped around
return _capacity - _front + _tail;
} else {
return _tail - _front;
}
}
template<typename T>
T CircularBuffer<T>::pop() {
if (size() == 0) {
throw std::runtime_error("pop: Empty circular buffer");
}
T ret = _buffer[_front];
_front = (_front + 1) % _capacity;
return ret;
}
template<typename T>
T& CircularBuffer<T>::front() {
if (size() == 0) {
throw std::runtime_error("front(): Empty circular buffer");
}
T& ret = _buffer[_front];
return ret;
}
template<typename T>
CircularBuffer<T>::CircularBuffer()
: _buffer(nullptr), _capacity(0), _front(0), _tail(0)
{
}
template<typename T>
CircularBuffer<T>::CircularBuffer(uint64_t capacity)
: CircularBuffer() {
if (capacity < 1) throw std::length_error("Invalid capacity");
_buffer = new T[capacity];
_capacity = capacity;
}
template<typename T>
CircularBuffer<T>::CircularBuffer(const CircularBuffer<T> &rhs)
: _buffer(new T[rhs._capacity]), _capacity(rhs._capacity), _front(rhs._front), _tail(rhs._tail)
{
std::copy(rhs._buffer, rhs._buffer + _capacity, _buffer);
}
template<typename T>
CircularBuffer<T>::CircularBuffer(CircularBuffer<T>&& rhs)
: CircularBuffer()
{
swap(*this, rhs);
}
template<typename T>
CircularBuffer<T>&
CircularBuffer<T>::operator=(CircularBuffer<T> rhs)
{
swap(*this, rhs);
return *this;
}
template<typename T>
void
CircularBuffer<T>::add(T item)
{
_buffer[_tail++] = item;
if (_tail == _capacity) {
_tail = 0;
}
}
#endif // CIRCULAR_BUFFER_H