-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
CircularBuffer.tpp
186 lines (166 loc) · 4.68 KB
/
CircularBuffer.tpp
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
/*
CircularBuffer.tpp - Circular buffer library for Arduino.
Copyright (c) 2017 Roberto Lo Giacco.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
template<typename T, size_t S, typename IT>
constexpr CircularBuffer<T,S,IT>::CircularBuffer() :
head(buffer), tail(buffer), count(0) {
}
template<typename T, size_t S, typename IT>
bool CircularBuffer<T,S,IT>::unshift(T value) {
if (head == buffer) {
head = buffer + capacity;
}
*--head = value;
if (count == capacity) {
if (tail-- == buffer) {
tail = buffer + capacity - 1;
}
return false;
} else {
if (count++ == 0) {
tail = head;
}
return true;
}
}
template<typename T, size_t S, typename IT>
bool CircularBuffer<T,S,IT>::push(T value) {
if (++tail == buffer + capacity) {
tail = buffer;
}
*tail = value;
if (count == capacity) {
if (++head == buffer + capacity) {
head = buffer;
}
return false;
} else {
if (count++ == 0) {
head = tail;
}
return true;
}
}
template<typename T, size_t S, typename IT>
T CircularBuffer<T,S,IT>::shift() {
if (count == 0) return *head;
T result = *head++;
if (head >= buffer + capacity) {
head = buffer;
}
count--;
return result;
}
template<typename T, size_t S, typename IT>
T CircularBuffer<T,S,IT>::pop() {
if (count == 0) return *tail;
T result = *tail--;
if (tail < buffer) {
tail = buffer + capacity - 1;
}
count--;
return result;
}
template<typename T, size_t S, typename IT>
T inline CircularBuffer<T,S,IT>::first() const {
return *head;
}
template<typename T, size_t S, typename IT>
T inline CircularBuffer<T,S,IT>::last() const {
return *tail;
}
template<typename T, size_t S, typename IT>
T CircularBuffer<T,S,IT>::operator [](IT index) const {
if (index >= count) return *tail;
return *(buffer + ((head - buffer + index) % capacity));
}
template<typename T, size_t S, typename IT>
IT inline CircularBuffer<T,S,IT>::size() const {
return count;
}
template<typename T, size_t S, typename IT>
IT inline CircularBuffer<T,S,IT>::available() const {
return capacity - count;
}
template<typename T, size_t S, typename IT>
bool inline CircularBuffer<T,S,IT>::isEmpty() const {
return count == 0;
}
template<typename T, size_t S, typename IT>
bool inline CircularBuffer<T,S,IT>::isFull() const {
return count == capacity;
}
template<typename T, size_t S, typename IT>
void inline CircularBuffer<T,S,IT>::clear() {
head = tail = buffer;
count = 0;
}
template<typename T, size_t S, typename IT>
void inline CircularBuffer<T,S,IT>::copyToArray(T* dest) const {
const T* finish = dest + count;
for (const T* current = head; current < (buffer + capacity) && dest < finish; current++, dest++) {
*dest = *current;
}
for (const T* current = buffer; current <= tail && dest < finish; current++, dest++) {
*dest = *current;
}
}
template<typename T, size_t S, typename IT>
template<typename R>
void inline CircularBuffer<T,S,IT>::copyToArray(R* dest, R (&convertFn)(const T&)) const {
const R* finish = dest + count;
for (const T* current = head; current < (buffer + capacity) && dest < finish; current++, dest++) {
*dest = convertFn(*current);
}
for (const T* current = buffer; current <= tail && dest < finish; current++, dest++) {
*dest = convertFn(*current);
}
}
#ifdef CIRCULAR_BUFFER_DEBUG
#include <string.h>
template<typename T, size_t S, typename IT>
void inline CircularBuffer<T,S,IT>::debug(Print* out) {
for (IT i = 0; i < capacity; i++) {
int hex = (int)buffer + i;
out->print("[");
out->print(hex, HEX);
out->print("] ");
out->print(*(buffer + i));
if (head == buffer + i) {
out->print("<-head");
}
if (tail == buffer + i) {
out->print("<-tail");
}
out->println();
}
}
template<typename T, size_t S, typename IT>
void inline CircularBuffer<T,S,IT>::debugFn(Print* out, void (*printFunction)(Print*, T)) {
for (IT i = 0; i < capacity; i++) {
int hex = (int)buffer + i;
out->print("[");
out->print(hex, HEX);
out->print("] ");
printFunction(out, *(buffer + i));
if (head == buffer + i) {
out->print("<-head");
}
if (tail == buffer + i) {
out->print("<-tail");
}
out->println();
}
}
#endif