muwerk ustd Library
A low-resource, minimal implementation of Arrays, Maps and Queues for low-resource avrs.
Loading...
Searching...
No Matches
ustd_queue.h
1// ustd_queue.h - ustd queue class
2
3#pragma once
4
5namespace ustd {
6
7// Helper class for queue iterators:
8template <typename T> class queueIterator {
9 private:
10 T *values_ptr;
11 unsigned int position;
12 unsigned int maxSize;
13
14 public:
15 queueIterator(T *values_ptr, unsigned int p, unsigned int maxSize)
16 : values_ptr{values_ptr}, position{p}, maxSize(maxSize) {
17 }
18
19 bool operator!=(const queueIterator<T> &other) const {
20 return !(*this == other);
21 }
22
23 bool operator==(const queueIterator<T> &other) const {
24 return position == other.position;
25 }
26
27 queueIterator &operator++() {
28 position = (position + 1) % maxSize;
29 return *this;
30 }
31
32 T &operator*() const {
33 return *(values_ptr + position);
34 }
35};
36
84template <class T> class queue {
85 private:
86 T *que;
87 unsigned int peakSize;
88 unsigned int maxSize;
89 unsigned int size;
90 unsigned int quePtr0;
91 unsigned int quePtr1;
92 T bad = {};
93
94 public:
95 queue(unsigned int maxQueueSize) : maxSize(maxQueueSize) {
100 quePtr0 = 0;
101 quePtr1 = 0;
102 size = 0;
103 peakSize = 0;
104 que = (T *)malloc(sizeof(T) * maxSize);
105 if (que == nullptr)
106 maxSize = 0;
107 }
108
109 queue(const queue &qu) {
110 peakSize = qu.peakSize;
111 maxSize = qu.maxSize;
112 size = qu.size;
113 quePtr0 = qu.quePtr0;
114 quePtr1 = qu.quePtr1;
115 bad = qu.bad;
116 que = (T *)malloc(sizeof(T) * maxSize);
117 if (que == nullptr) {
118 maxSize = 0;
119 size = 0;
120 } else {
121 unsigned int in = quePtr0;
122 for (unsigned int i = 0; i < size; i++) {
123 que[in] = qu.que[in];
124 in = (in + 1) % maxSize;
125 }
126 }
127 }
128
133 if (que != nullptr) {
134 free(que);
135 que = nullptr;
136 }
137 }
138
139 // iterators
140 queueIterator<T> begin() {
142 return queueIterator<T>(que, quePtr0, maxSize);
143 }
144 queueIterator<T> end() {
146 return queueIterator<T>(que, (quePtr0 + size) % maxSize, maxSize);
147 }
148
149 queueIterator<const T> begin() const {
151 return queueIterator<const T>(que, quePtr0, maxSize);
152 }
153
154 queueIterator<const T> end() const {
156 return queueIterator<const T>(que, (quePtr0 + size) % maxSize, maxSize);
157 }
158
159 void getInternalStartStopPtrs(unsigned int *p0, unsigned int *p1) {
160 *p0 = quePtr0;
161 *p1 = quePtr1;
162 }
163
164 bool push(T ent) {
169 if (size >= maxSize) {
170 return false;
171 }
172 que[quePtr1] = ent;
173 quePtr1 = (quePtr1 + 1) % maxSize;
174 ++size;
175 if (size > peakSize) {
176 peakSize = size;
177 }
178 return true;
179 }
180
181 T pop() {
185 if (size == 0)
186 return bad;
187 T ent = que[quePtr0];
188 quePtr0 = (quePtr0 + 1) % maxSize;
189 --size;
190 return ent;
191 }
192
193 void setInvalidValue(T &entryInvalidValue) {
201 bad = entryInvalidValue;
202 }
203
204 bool isEmpty() {
208 if (size == 0)
209 return true;
210 else
211 return false;
212 }
213
214 unsigned int length() {
218 return (size);
219 }
220
221 unsigned int peak() {
225 return (peakSize);
226 }
227};
228} // namespace ustd
Lightweight c++11 ring buffer queue implementation.
Definition: ustd_queue.h:84
void setInvalidValue(T &entryInvalidValue)
Definition: ustd_queue.h:193
queueIterator< T > end()
Definition: ustd_queue.h:144
unsigned int peak()
Definition: ustd_queue.h:221
T pop()
Definition: ustd_queue.h:181
~queue()
Definition: ustd_queue.h:129
queueIterator< const T > end() const
Definition: ustd_queue.h:154
bool push(T ent)
Definition: ustd_queue.h:164
queue(unsigned int maxQueueSize)
Definition: ustd_queue.h:95
queueIterator< const T > begin() const
Definition: ustd_queue.h:149
queueIterator< T > begin()
Definition: ustd_queue.h:140
unsigned int length()
Definition: ustd_queue.h:214
bool isEmpty()
Definition: ustd_queue.h:204
The ustd namespace.
Definition: ustd_array.h:34