-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRingBuffer.cpp
209 lines (167 loc) · 3.67 KB
/
RingBuffer.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <assert.h>
using namespace std;
template<typename T>
class RingBuffer;
template<typename T>
class RingBuffer_iterator
{
typedef RingBuffer_iterator self_type;
typedef T value_type;
typedef T& reference;
typedef T const& const_reference;
typedef T* pointer;
typedef std::random_access_iterator_tag iterator_category;
typedef ptrdiff_t difference_type;
public:
RingBuffer_iterator(RingBuffer<T> const& buffs, size_t const pos, bool const last_)
:buf(buffs), idx(pos), last(last_)
{}
self_type& operator++()
{
if (last)
throw out_of_range("Exceed the Range!");
idx = (idx + 1) % buf.data.size();
last = idx == buf.nextpos();
return *this;
}
self_type operator++(int)
{
self_type tmp = *this;
++(*this);
return tmp;
}
bool operator==(self_type const& other) const
{
assert(compatible(other));
return idx == other.idx && last == other.last;
}
bool operator!=(self_type const& other) const
{
return !(*this == other);
}
const_reference operator*() const
{
return buf.data[idx];
}
const_reference operator->() const
{
return buf.data[idx];
}
private:
bool compatible(self_type const& other) const
{
return &buf == &other.buf;
}
size_t idx;
bool last;
RingBuffer<T> const& buf;
};
template<typename T>
class RingBuffer
{
typedef RingBuffer_iterator<T> const_iterator;
RingBuffer() = delete;
public:
explicit RingBuffer(size_t const val) :data(val) {};
bool clear() noexcept { head = -1, count = 0; }
size_t size() const noexcept { return count; }
bool empty() const noexcept { return count == 0; }
size_t capacity() const noexcept { return data.size(); }
bool full() const noexcept { return data.size() == count; }
void push(T const value)
{
head = nextpos();
data[head] = value;
if (count < data.size())
count++;
}
T pop()
{
if (empty())
throw runtime_error("Buffer is empty!");
auto pos = first_pos();
count--;
return data[pos];
}
const_iterator begin() const
{
return const_iterator(*this, first_pos(), empty());
}
const_iterator end() const
{
return const_iterator(*this, nextpos(), true);
}
private:
vector<T> data;
size_t count = 0; //¿ø¼Ò°¹¼ö
size_t head = -1; //headÀ§Ä¡
size_t nextpos() const noexcept { return count == 0 ? 0 : (head + 1) % data.size(); }
size_t first_pos() const noexcept { return count == 0 ? 0 : (head + data.size() - count + 1) % data.size(); }
friend class RingBuffer_iterator<T>;
};
template<typename T>
void print(RingBuffer<T>& buf)
{
for (auto const& i : buf)
cout << i << " ";
cout << endl;
}
int main()
{
RingBuffer<int> cbuf(5);
assert(cbuf.empty());
assert(!cbuf.full());
assert(cbuf.size() == 0);
print(cbuf);
cbuf.push(1);
cbuf.push(2);
cbuf.push(3);
assert(!cbuf.empty());
assert(!cbuf.full());
assert(cbuf.size() == 3);
print(cbuf);
auto item = cbuf.pop();
assert(item == 1);
assert(!cbuf.empty());
assert(!cbuf.full());
assert(cbuf.size() == 2);
cbuf.push(4);
cbuf.push(5);
cbuf.push(6);
assert(!cbuf.empty());
assert(cbuf.full());
assert(cbuf.size() == 5);
print(cbuf);
cbuf.push(7);
cbuf.push(8);
assert(!cbuf.empty());
assert(cbuf.full());
assert(cbuf.size() == 5);
print(cbuf);
item = cbuf.pop();
assert(item == 4);
item = cbuf.pop();
assert(item == 5);
item = cbuf.pop();
assert(item == 6);
assert(!cbuf.empty());
assert(!cbuf.full());
assert(cbuf.size() == 2);
print(cbuf);
item = cbuf.pop();
assert(item == 7);
item = cbuf.pop();
assert(item == 8);
assert(cbuf.empty());
assert(!cbuf.full());
assert(cbuf.size() == 0);
print(cbuf);
cbuf.push(9);
assert(!cbuf.empty());
assert(!cbuf.full());
assert(cbuf.size() == 1);
print(cbuf);
}