|
| 1 | +/* |
| 2 | + * StackArray.h |
| 3 | + * |
| 4 | + * Library implementing a generic, dynamic stack (array version). |
| 5 | + * |
| 6 | + * --- |
| 7 | + * |
| 8 | + * Copyright (C) 2010 Efstathios Chatzikyriakidis ([email protected]) |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License as published by |
| 12 | + * the Free Software Foundation, either version 3 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + * |
| 23 | + * --- |
| 24 | + * |
| 25 | + * Version 1.0 |
| 26 | + * |
| 27 | + * 2010-09-25 Efstathios Chatzikyriakidis <[email protected]> |
| 28 | + * |
| 29 | + * - added resize(): for growing, shrinking the array size. |
| 30 | + * |
| 31 | + * 2010-09-23 Efstathios Chatzikyriakidis <[email protected]> |
| 32 | + * |
| 33 | + * - added exit(), blink(): error reporting and handling methods. |
| 34 | + * |
| 35 | + * 2010-09-20 Alexander Brevig <[email protected]> |
| 36 | + * |
| 37 | + * - added setPrinter(): indirectly reference a Serial object. |
| 38 | + * |
| 39 | + * 2010-09-15 Efstathios Chatzikyriakidis <[email protected]> |
| 40 | + * |
| 41 | + * - initial release of the library. |
| 42 | + * |
| 43 | + * --- |
| 44 | + * |
| 45 | + * For the latest version see: http://www.arduino.cc/ |
| 46 | + */ |
| 47 | + |
| 48 | +// header defining the interface of the source. |
| 49 | +#ifndef _STACKARRAY_H |
| 50 | +#define _STACKARRAY_H |
| 51 | + |
| 52 | +// include Arduino basic header. |
| 53 | +#include <Arduino.h> |
| 54 | + |
| 55 | +// the definition of the stack class. |
| 56 | +template<typename T> |
| 57 | +class StackArray { |
| 58 | + public: |
| 59 | + // init the stack (constructor). |
| 60 | + StackArray (); |
| 61 | + |
| 62 | + // clear the stack (destructor). |
| 63 | + ~StackArray (); |
| 64 | + |
| 65 | + // push an item to the stack. |
| 66 | + void push (const T i); |
| 67 | + |
| 68 | + // pop an item from the stack. |
| 69 | + T pop (); |
| 70 | + |
| 71 | + // get an item from the stack. |
| 72 | + T peek () const; |
| 73 | + |
| 74 | + // check if the stack is empty. |
| 75 | + bool isEmpty () const; |
| 76 | + |
| 77 | + // get the number of items in the stack. |
| 78 | + int count () const; |
| 79 | + |
| 80 | + // check if the stack is full. |
| 81 | + bool isFull () const; |
| 82 | + |
| 83 | + // set the printer of the stack. |
| 84 | + void setPrinter (Print & p); |
| 85 | + |
| 86 | + private: |
| 87 | + // resize the size of the stack. |
| 88 | + void resize (const int s); |
| 89 | + |
| 90 | + // exit report method in case of error. |
| 91 | + void exit (const char * m) const; |
| 92 | + |
| 93 | + // led blinking method in case of error. |
| 94 | + void blink () const; |
| 95 | + |
| 96 | + // the initial size of the stack. |
| 97 | + static const int initialSize = 2; |
| 98 | + |
| 99 | + // the pin number of the on-board led. |
| 100 | + static const int ledPin = 13; |
| 101 | + |
| 102 | + Print * printer; // the printer of the stack. |
| 103 | + T * contents; // the array of the stack. |
| 104 | + int size; // the size of the stack. |
| 105 | + int top; // the top index of the stack. |
| 106 | +}; |
| 107 | + |
| 108 | +// init the stack (constructor). |
| 109 | +template<typename T> |
| 110 | +StackArray<T>::StackArray () { |
| 111 | + size = 0; // set the size of stack to zero. |
| 112 | + top = 0; // set the initial top index of the stack. |
| 113 | + printer = NULL; // set the printer of stack to point nowhere. |
| 114 | + |
| 115 | + // allocate enough memory for the array. |
| 116 | + contents = (T *) malloc (sizeof (T) * initialSize); |
| 117 | + |
| 118 | + // if there is a memory allocation error. |
| 119 | + if (contents == NULL) |
| 120 | + exit ("STACK: insufficient memory to initialize stack."); |
| 121 | + |
| 122 | + // set the initial size of the stack. |
| 123 | + size = initialSize; |
| 124 | +} |
| 125 | + |
| 126 | +// clear the stack (destructor). |
| 127 | +template<typename T> |
| 128 | +StackArray<T>::~StackArray () { |
| 129 | + free (contents); // deallocate the array of the stack. |
| 130 | + |
| 131 | + contents = NULL; // set stack's array pointer to nowhere. |
| 132 | + printer = NULL; // set the printer of stack to point nowhere. |
| 133 | + size = 0; // set the size of stack to zero. |
| 134 | + top = 0; // set the initial top index of the stack. |
| 135 | +} |
| 136 | + |
| 137 | +// resize the size of the stack. |
| 138 | +template<typename T> |
| 139 | +void StackArray<T>::resize (const int s) { |
| 140 | + // defensive issue. |
| 141 | + if (s <= 0) |
| 142 | + exit ("STACK: error due to undesirable size for stack size."); |
| 143 | + |
| 144 | + // reallocate enough memory for the array. |
| 145 | + contents = (T *) realloc (contents, sizeof (T) * s); |
| 146 | + |
| 147 | + // if there is a memory allocation error. |
| 148 | + if (contents == NULL) |
| 149 | + exit ("STACK: insufficient memory to resize stack."); |
| 150 | + |
| 151 | + // set the new size of the stack. |
| 152 | + size = s; |
| 153 | +} |
| 154 | + |
| 155 | +// push an item to the stack. |
| 156 | +template<typename T> |
| 157 | +void StackArray<T>::push (const T i) { |
| 158 | + // check if the stack is full. |
| 159 | + if (isFull ()) |
| 160 | + // double size of array. |
| 161 | + resize (size * 2); |
| 162 | + |
| 163 | + // store the item to the array. |
| 164 | + contents[top++] = i; |
| 165 | +} |
| 166 | + |
| 167 | +// pop an item from the stack. |
| 168 | +template<typename T> |
| 169 | +T StackArray<T>::pop () { |
| 170 | + // check if the stack is empty. |
| 171 | + if (isEmpty ()) |
| 172 | + exit ("STACK: can't pop item from stack: stack is empty."); |
| 173 | + |
| 174 | + // fetch the top item from the array. |
| 175 | + T item = contents[--top]; |
| 176 | + |
| 177 | + // shrink size of array if necessary. |
| 178 | + if (!isEmpty () && (top <= size / 4)) |
| 179 | + resize (size / 2); |
| 180 | + |
| 181 | + // return the top item from the array. |
| 182 | + return item; |
| 183 | +} |
| 184 | + |
| 185 | +// get an item from the stack. |
| 186 | +template<typename T> |
| 187 | +T StackArray<T>::peek () const { |
| 188 | + // check if the stack is empty. |
| 189 | + if (isEmpty ()) |
| 190 | + exit ("STACK: can't peek item from stack: stack is empty."); |
| 191 | + |
| 192 | + // get the top item from the array. |
| 193 | + return contents[top - 1]; |
| 194 | +} |
| 195 | + |
| 196 | +// check if the stack is empty. |
| 197 | +template<typename T> |
| 198 | +bool StackArray<T>::isEmpty () const { |
| 199 | + return top == 0; |
| 200 | +} |
| 201 | + |
| 202 | +// check if the stack is full. |
| 203 | +template<typename T> |
| 204 | +bool StackArray<T>::isFull () const { |
| 205 | + return top == size; |
| 206 | +} |
| 207 | + |
| 208 | +// get the number of items in the stack. |
| 209 | +template<typename T> |
| 210 | +int StackArray<T>::count () const { |
| 211 | + return top; |
| 212 | +} |
| 213 | + |
| 214 | +// set the printer of the stack. |
| 215 | +template<typename T> |
| 216 | +void StackArray<T>::setPrinter (Print & p) { |
| 217 | + printer = &p; |
| 218 | +} |
| 219 | + |
| 220 | +// exit report method in case of error. |
| 221 | +template<typename T> |
| 222 | +void StackArray<T>::exit (const char * m) const { |
| 223 | + // print the message if there is a printer. |
| 224 | + if (printer) |
| 225 | + printer->println (m); |
| 226 | + |
| 227 | + // loop blinking until hardware reset. |
| 228 | + blink (); |
| 229 | +} |
| 230 | + |
| 231 | +// led blinking method in case of error. |
| 232 | +template<typename T> |
| 233 | +void StackArray<T>::blink () const { |
| 234 | + // set led pin as output. |
| 235 | + pinMode (ledPin, OUTPUT); |
| 236 | + |
| 237 | + // continue looping until hardware reset. |
| 238 | + while (true) { |
| 239 | + digitalWrite (ledPin, HIGH); // sets the LED on. |
| 240 | + delay (250); // pauses 1/4 of second. |
| 241 | + digitalWrite (ledPin, LOW); // sets the LED off. |
| 242 | + delay (250); // pauses 1/4 of second. |
| 243 | + } |
| 244 | + |
| 245 | + // solution selected due to lack of exit() and assert(). |
| 246 | +} |
| 247 | + |
| 248 | +#endif // _STACKARRAY_H |
0 commit comments