Uniot Core
0.8.1
Loading...
Searching...
No Matches
ClearQueue.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2023 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <Arduino.h>
22
23#include <functional>
24
37template <typename T>
39 public:
43 typedef std::function<void(const T &)> VoidCallback;
44
48 ClearQueue(ClearQueue const &) = delete;
49
53 void operator=(ClearQueue const &) = delete;
54
59
63 virtual ~ClearQueue();
64
70 void push(const T &value);
71
79 bool pushUnique(const T &value);
80
88
95 const T &hardPeek() const;
96
104 T pop(const T &errorCode);
105
113 const T &peek(const T &errorCode) const;
114
122 bool removeOne(const T &value);
123
131 bool contains(const T &value) const;
132
140 T *find(const T &value) const;
141
148 inline bool isEmpty() const;
149
158 size_t calcSize() const;
159
163 void clean();
164
170 void forEach(VoidCallback callback) const;
171
172 protected:
176 typedef struct node {
180
183};
185
186template <typename T>
188 mHead = nullptr;
189 mTail = nullptr;
190}
191
192template <typename T>
196
197template <typename T>
198bool ClearQueue<T>::pushUnique(const T &value) {
199 if (!contains(value)) {
200 push(value);
201 return true;
202 }
203 return false;
204}
205
206template <typename T>
207void ClearQueue<T>::push(const T &value) {
208 pnode cur = mTail;
209 mTail = (pnode) new node;
210
211 mTail->next = nullptr;
212 mTail->value = value;
213 if (isEmpty()) {
214 mHead = mTail;
215 } else {
216 cur->next = mTail;
217 }
218}
219
220template <typename T>
222 T value = mHead->value;
223 pnode cur = mHead->next;
224 delete mHead;
225 mHead = cur;
226
227 if (mHead == nullptr) {
228 mTail = nullptr;
229 }
230
231 return value;
232}
233
234template <typename T>
235const T &ClearQueue<T>::hardPeek() const {
236 return mHead->value;
237}
238
239template <typename T>
240T ClearQueue<T>::pop(const T &errorCode) {
241 if (!isEmpty()) {
242 return hardPop();
243 }
244 return errorCode;
245}
246
247template <typename T>
248const T &ClearQueue<T>::peek(const T &errorCode) const {
249 if (!isEmpty()) {
250 return hardPeek();
251 }
252 return errorCode;
253}
254
255template <typename T>
256bool ClearQueue<T>::removeOne(const T &value) {
257 if (!isEmpty()) {
258 if (mHead->value == value) {
259 hardPop();
260 return true;
261 }
262 for (pnode cur = mHead; cur->next != nullptr; cur = cur->next) {
263 if (cur->next->value == value) {
264 pnode newNext = cur->next->next;
265 delete cur->next;
266 cur->next = newNext;
267
268 if (newNext == nullptr) {
269 mTail = cur;
270 }
271
272 return true;
273 }
274 }
275 }
276 return false;
277}
278
279template <typename T>
280bool ClearQueue<T>::contains(const T &value) const {
281 for (pnode cur = mHead; cur != nullptr; cur = cur->next) {
282 if (cur->value == value) {
283 return true;
284 }
285 }
286 return false;
287}
288
289template <typename T>
290T *ClearQueue<T>::find(const T &value) const {
291 for (pnode cur = mHead; cur != nullptr; cur = cur->next) {
292 if (cur->value == value) {
293 return &(cur->value);
294 }
295 }
296 return nullptr;
297}
298
299template <typename T>
300inline bool ClearQueue<T>::isEmpty() const {
301 return mHead == nullptr;
302}
303
304template <typename T>
306 size_t count = 0;
307 for (pnode cur = mHead; cur != nullptr; cur = cur->next) {
308 count++;
309 }
310 return count;
311}
312
313template <typename T>
315 for (pnode cur = mHead; cur != nullptr; mHead = cur) {
316 cur = mHead->next;
317 delete mHead;
318 }
319
320 mHead = nullptr;
321 mTail = nullptr;
322}
323
324template <typename T>
326 for (pnode cur = mHead; cur != nullptr; cur = cur->next) {
327 callback(cur->value);
328 }
329}
void forEach(VoidCallback callback) const
Executes a callback function on each element in the queue.
Definition ClearQueue.h:325
ClearQueue(ClearQueue const &)=delete
Deleted copy constructor to prevent copying.
T * find(const T &value) const
Finds and returns a pointer to the first occurrence of a value.
Definition ClearQueue.h:290
size_t calcSize() const
Calculate the number of elements in the queue by traversal.
Definition ClearQueue.h:305
bool contains(const T &value) const
Checks if the queue contains a specific value.
Definition ClearQueue.h:280
bool removeOne(const T &value)
Removes the first occurrence of a specific value from the queue.
Definition ClearQueue.h:256
void push(const T &value)
Adds an element to the end of the queue.
Definition ClearQueue.h:207
T pop(const T &errorCode)
Safely removes and returns the element at the front of the queue.
Definition ClearQueue.h:240
bool pushUnique(const T &value)
Adds an element to the queue only if it doesn't already exist.
Definition ClearQueue.h:198
T hardPop()
Removes and returns the element at the front of the queue.
Definition ClearQueue.h:221
struct ClearQueue::node * pnode
Node structure for the linked list implementation.
const T & peek(const T &errorCode) const
Safely returns the element at the front of the queue without removing it.
Definition ClearQueue.h:248
pnode mTail
Pointer to the last node in the queue.
Definition ClearQueue.h:182
pnode mHead
Pointer to the first node in the queue.
Definition ClearQueue.h:181
bool isEmpty() const
Checks if the queue is empty.
Definition ClearQueue.h:300
virtual ~ClearQueue()
Destroys the queue and releases all allocated memory.
Definition ClearQueue.h:193
std::function< void(const T &)> VoidCallback
Callback function type for forEach operations.
Definition ClearQueue.h:43
const T & hardPeek() const
Returns the element at the front of the queue without removing it.
Definition ClearQueue.h:235
void operator=(ClearQueue const &)=delete
Deleted assignment operator to prevent copying.
void clean()
Removes all elements from the queue.
Definition ClearQueue.h:314
ClearQueue()
Constructs an empty queue.
Definition ClearQueue.h:187
Node structure for the linked list implementation.
Definition ClearQueue.h:176
T value
The stored value.
Definition ClearQueue.h:177
node * next
Pointer to the next node.
Definition ClearQueue.h:178