forked from EinarArnason/ArduinoQueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.h
205 lines (180 loc) · 3.5 KB
/
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
/*
Author: Einar Arnason
email: einsiarna@gmail.com
A lightweight linked list type queue implementation,
meant for microcontrollers.
Usage and further info:
https://github.com/EinarArnason/ArduinoQueue
*/
#pragma once
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
template <typename T>
class DataQueue {
class Node {
public:
T item;
Node* next;
Node() {
next = NULL;
}
~Node() {
next = NULL;
}
};
Node* _head;
Node* _tail;
unsigned int _max_items;
unsigned int _max_memory;
unsigned int _items_cnt;
public:
DataQueue(unsigned int max_items = 100, unsigned int max_memory = 0) {
_head = NULL;
_tail = NULL;
/*
If the max memory has not
been defined or is 0, then
the queue size is set by
the max number of items:
*/
if (max_memory == 0) {
_max_items = max_items;
_max_memory = max_items * sizeof(T);
}
/*
If the max memory has been
set then the queue size is
defined by the memory size
when the max items is 0.
If the user gave a max item
size, then the queue size
will be capped by the number
of items.
*/
else {
_max_items = max_memory / sizeof(T);
_max_memory = _max_items * sizeof(T);
if (max_items != 0) {
if (_max_items > max_items)
_max_items = max_items;
}
}
_items_cnt = 0;
}
~DataQueue() {
for (Node* node = _head; node != NULL; node = _head) {
_head = node->next;
delete node;
}
}
/*
Push an item to the queue.
Returns false if memory is
full, or true if the item
was added to queue.
*/
bool enqueue(T item) {
if (_items_cnt == _max_items) {
return false;
}
Node* node = new Node;
if (node == NULL) {
return false;
}
node->item = item;
if (_head == NULL) {
_head = node;
_tail = node;
_items_cnt++;
return true;
}
_tail->next = node;
_tail = node;
_items_cnt++;
return true;
}
/*
Pop the front of the queue.
Because exceptions are not
usually implemented for
microcontrollers, if queue
is empty, a dummy item is
returned.
*/
T dequeue() {
if ((_items_cnt == 0) || (_head == NULL)) {
return T();
}
Node* node = _head;
_head = node->next;
T item = node->item;
delete node;
node = NULL;
if (_head == NULL) {
_tail = NULL;
}
_items_cnt--;
return item;
}
/*
Returns true if the queue
is empty, false otherwise.
*/
bool isEmpty() {
return _head == NULL;
}
/*
Returns true if the queue
is full, false otherwise.
*/
bool isFull() {
return _items_cnt == _max_items;
}
/*
Returns the number of items
currently in the queue.
*/
unsigned int item_count() {
return _items_cnt;
}
/*
Returns the size of the
queue item in bytes.
*/
unsigned int item_size() {
return sizeof(T);
}
/*
Returns the size of the queue
(maximum number of items)
*/
unsigned int max_queue_size() {
return _max_items;
}
/*
Returns the size of the queue
(maximum size in bytes)
*/
unsigned int max_memory_size() {
return _max_memory;
}
/*
Get the item in the front
of the queue.
Because exceptions are not
usually implemented for
microcontrollers, if queue
is empty, a dummy item is
returned.
*/
T front() {
if ((_items_cnt == 0) || (_head == NULL)) {
return T();
}
T item = _head->item;
return item;
}
};