-
Notifications
You must be signed in to change notification settings - Fork 6
/
StackArray.h
332 lines (255 loc) · 7.68 KB
/
StackArray.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* StackArray.h
*
* Library implementing a generic, dynamic stack (array version).
*
* ---
*
* Copyright (C) 2010 Efstathios Chatzikyriakidis ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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/>.
*
* ---
*
* Version 1.0
*
* 2010-09-25 Efstathios Chatzikyriakidis <[email protected]>
*
* - added resize(): for growing, shrinking the array size.
*
* 2010-09-23 Efstathios Chatzikyriakidis <[email protected]>
*
* - added exit(), blink(): error reporting and handling methods.
*
* 2010-09-20 Alexander Brevig <[email protected]>
*
* - added setPrinter(): indirectly reference a Serial object.
*
* 2010-09-15 Efstathios Chatzikyriakidis <[email protected]>
*
* - initial release of the library.
*
* ---
*
* For the latest version see: http://www.arduino.cc/
*/
// header defining the interface of the source.
#ifndef _STACKARRAY_H
#define _STACKARRAY_H
// include Arduino basic header.
#include <Arduino.h>
// the definition of the stack class.
template<typename T>
class StackArray {
public:
// init the stack (constructor).
StackArray ();
// clear the stack (destructor).
~StackArray ();
// push an item to the stack.
void push (const T i);
// pop an item from the stack.
T pop ();
// get an item from the stack.
T peek () const;
// check if the stack is empty.
bool isEmpty () const;
// get the number of items in the stack.
int count () const;
// check if the stack is full.
bool isFull () const;
// set the printer of the stack.
void setPrinter (Print & p);
void remove(const T i);
// Init iterator on first element
void first();
void next();
bool isDone() const;
T currentItem() const;
private:
// resize the size of the stack.
void resize (const int s);
// exit report method in case of error.
void exit (const char * m) const;
// led blinking method in case of error.
void blink () const;
// the initial size of the stack.
static const int initialSize = 2;
// the pin number of the on-board led.
static const int ledPin = 13;
Print * printer; // the printer of the stack.
T * contents; // the array of the stack.
int size; // the size of the stack.
int top; // the top index of the stack.
int current; // Iterator index
};
// init the stack (constructor).
template<typename T>
StackArray<T>::StackArray () {
size = 0; // set the size of stack to zero.
top = 0; // set the initial top index of the stack.
printer = NULL; // set the printer of stack to point nowhere.
// allocate enough memory for the array.
contents = (T *) malloc (sizeof (T) * initialSize);
// if there is a memory allocation error.
if (contents == NULL)
exit ("STACK: insufficient memory to initialize stack.");
// set the initial size of the stack.
size = initialSize;
}
// clear the stack (destructor).
template<typename T>
StackArray<T>::~StackArray () {
free (contents); // deallocate the array of the stack.
contents = NULL; // set stack's array pointer to nowhere.
printer = NULL; // set the printer of stack to point nowhere.
size = 0; // set the size of stack to zero.
top = 0; // set the initial top index of the stack.
}
// resize the size of the stack.
template<typename T>
void StackArray<T>::resize (const int s) {
// defensive issue.
if (s <= 0)
exit ("STACK: error due to undesirable size for stack size.");
// reallocate enough memory for the array.
contents = (T *) realloc (contents, sizeof (T) * s);
// if there is a memory allocation error.
if (contents == NULL)
exit ("STACK: insufficient memory to resize stack.");
// set the new size of the stack.
size = s;
}
// push an item to the stack.
template<typename T>
void StackArray<T>::push (const T i) {
// check if the stack is full.
if (isFull ())
// double size of array.
resize (size * 2);
// store the item to the array.
contents[top++] = i;
}
// pop an item from the stack.
template<typename T>
T StackArray<T>::pop () {
// check if the stack is empty.
if (isEmpty ())
exit ("STACK: can't pop item from stack: stack is empty.");
// fetch the top item from the array.
T item = contents[--top];
// shrink size of array if necessary.
if (!isEmpty () && (top <= size / 4))
resize (size / 2);
// return the top item from the array.
return item;
}
// get an item from the stack.
template<typename T>
T StackArray<T>::peek () const {
// check if the stack is empty.
if (isEmpty ())
exit ("STACK: can't peek item from stack: stack is empty.");
// get the top item from the array.
return contents[top - 1];
}
// check if the stack is empty.
template<typename T>
bool StackArray<T>::isEmpty () const {
return top == 0;
}
// check if the stack is full.
template<typename T>
bool StackArray<T>::isFull () const {
return top == size;
}
// get the number of items in the stack.
template<typename T>
int StackArray<T>::count () const {
return top;
}
// set the printer of the stack.
template<typename T>
void StackArray<T>::setPrinter (Print & p) {
printer = &p;
}
// exit report method in case of error.
template<typename T>
void StackArray<T>::exit (const char * m) const {
// print the message if there is a printer.
if (printer)
printer->println (m);
// loop blinking until hardware reset.
blink ();
}
// led blinking method in case of error.
template<typename T>
void StackArray<T>::blink () const {
// set led pin as output.
pinMode (ledPin, OUTPUT);
// continue looping until hardware reset.
while (true) {
digitalWrite (ledPin, HIGH); // sets the LED on.
delay (250); // pauses 1/4 of second.
digitalWrite (ledPin, LOW); // sets the LED off.
delay (250); // pauses 1/4 of second.
}
// solution selected due to lack of exit() and assert().
}
template<typename T>
void StackArray<T>::remove(const T o) {
// Find the item
for (int i = 0; i < top; i++) {
if (contents[i] == o) {
// Check if we have items afterwards
if (i < top - 1) {
// Remove this item
memcpy(&contents[i], &contents[i+1], sizeof(T) * (top-i-1));
}
top--;
}
}
// Item not found
exit ("STACK: Item not found in the array.");
}
template<typename T>
void StackArray<T>::first() {
// check if the stack is empty.
if (isEmpty ())
exit ("STACK: can't initialize iterator: stack is empty.");
current = 0;
}
template<typename T>
void StackArray<T>::next() {
// check if the stack is empty.
if (isDone ())
exit ("STACK: no more items in the array.");
current++;
}
template<typename T>
bool StackArray<T>::isDone() const {
return current == top;
}
template<typename T>
T StackArray<T>::currentItem() const {
// check if the stack is empty.
if (isEmpty ())
exit ("STACK: can't get current item from stack: stack is empty.");
// check if the stack is empty.
if (isDone ())
exit ("STACK: no more items in the array.");
// get the top item from the array.
return contents[current];
}
#endif // _STACKARRAY_H