-
Notifications
You must be signed in to change notification settings - Fork 3
/
ustd_queue.h
228 lines (193 loc) · 5.44 KB
/
ustd_queue.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
// ustd_queue.h - ustd queue class
#pragma once
namespace ustd {
// Helper class for queue iterators:
template <typename T> class queueIterator {
private:
T *values_ptr;
unsigned int position;
unsigned int maxSize;
public:
queueIterator(T *values_ptr, unsigned int p, unsigned int maxSize)
: values_ptr{values_ptr}, position{p}, maxSize(maxSize) {
}
bool operator!=(const queueIterator<T> &other) const {
return !(*this == other);
}
bool operator==(const queueIterator<T> &other) const {
return position == other.position;
}
queueIterator &operator++() {
position = (position + 1) % maxSize;
return *this;
}
T &operator*() const {
return *(values_ptr + position);
}
};
/*! \brief Lightweight c++11 ring buffer queue implementation.
ustd_queue.h is a minimal, yet highly portable ring buffer queue implementation
that runs well on architectures with very limited resources such as attiny 8kb
avr.
Make sure to provide the <a
href="https://github.com/muwerk/ustd/blob/master/README.md">required platform
define</a> before including ustd headers.
## An example:
~~~{.cpp}
#define __ATTINY__ 1 // Appropriate platform define required
#include <ustd_queue.h>
queue<int> que = queue<int>(128);
int wi;
wi=1;
que.push(wi);
int wo;
wo=que.pop();
printf("%d\n",wo);
~~~
## Queue inspection with iterators
queue<int> que = queue<int>(16);
que.push(12);
que.push(13);
// Iterate through queue content (does not modify content)
for (auto i : que) {
printf("%d\n", i);
}
// New pop() the values:
int w0,w1;
w0=que.pop();
w1=que.pop();
// Queue is now empty.
printf("%d %d, len=%d\n",w0,w1,que.length());
*/
template <class T> class queue {
private:
T *que;
unsigned int peakSize;
unsigned int maxSize;
unsigned int size;
unsigned int quePtr0;
unsigned int quePtr1;
T bad = {};
public:
queue(unsigned int maxQueueSize) : maxSize(maxQueueSize) {
/*! Constructs a queue object
@param maxQueueSize The maximum number of entries, the queue
can hold.
*/
quePtr0 = 0;
quePtr1 = 0;
size = 0;
peakSize = 0;
que = (T *)malloc(sizeof(T) * maxSize);
if (que == nullptr)
maxSize = 0;
}
queue(const queue &qu) {
peakSize = qu.peakSize;
maxSize = qu.maxSize;
size = qu.size;
quePtr0 = qu.quePtr0;
quePtr1 = qu.quePtr1;
bad = qu.bad;
que = (T *)malloc(sizeof(T) * maxSize);
if (que == nullptr) {
maxSize = 0;
size = 0;
} else {
unsigned int in = quePtr0;
for (unsigned int i = 0; i < size; i++) {
que[in] = qu.que[in];
in = (in + 1) % maxSize;
}
}
}
~queue() {
/*!
Deallocate the queue structure.
*/
if (que != nullptr) {
free(que);
que = nullptr;
}
}
// iterators
queueIterator<T> begin() {
/*! Iterator support: begin() */
return queueIterator<T>(que, quePtr0, maxSize);
}
queueIterator<T> end() {
/*! Iterator support: end() */
return queueIterator<T>(que, (quePtr0 + size) % maxSize, maxSize);
}
queueIterator<const T> begin() const {
/*! Iterator support: begin() */
return queueIterator<const T>(que, quePtr0, maxSize);
}
queueIterator<const T> end() const {
/*! Iterator support: end() */
return queueIterator<const T>(que, (quePtr0 + size) % maxSize, maxSize);
}
void getInternalStartStopPtrs(unsigned int *p0, unsigned int *p1) {
*p0 = quePtr0;
*p1 = quePtr1;
}
bool push(T ent) {
/*! Push a new entry into the queue.
@param ent T element
@return true on success, false if queue is full.
*/
if (size >= maxSize) {
return false;
}
que[quePtr1] = ent;
quePtr1 = (quePtr1 + 1) % maxSize;
++size;
if (size > peakSize) {
peakSize = size;
}
return true;
}
T pop() {
/*! Pop the oldest entry from the queue.
@return badEntry if queue is empty, or T element otherwise.
*/
if (size == 0)
return bad;
T ent = que[quePtr0];
quePtr0 = (quePtr0 + 1) % maxSize;
--size;
return ent;
}
void setInvalidValue(T &entryInvalidValue) {
/*! Set the value that's given back, if read from an empty
queue is requested. By default, an entry all set to zero is given
back. Using this function, the value of an invalid read can be
configured.
* @param entryInvalidValue The value that is given back in case an
invalid operation (e.g. read out of bounds) is tried.
*/
bad = entryInvalidValue;
}
bool isEmpty() {
/*! Check, if queue is empty.
@return true: queue empty, false: not empty.
*/
if (size == 0)
return true;
else
return false;
}
unsigned int length() {
/*! Check number of queue entries.
@return number of entries in the queue.
*/
return (size);
}
unsigned int peak() {
/*! Check the maxiumum number of entries that have been in the queue.
@return max number of queue entries.
*/
return (peakSize);
}
};
} // namespace ustd