fork download
  1. /*
  2.  * QueueArray.h
  3.  *
  4.  * Library implementing a generic, dynamic queue (array version).
  5.  *
  6.  * ---
  7.  *
  8.  * Copyright (C) 2010 Efstathios Chatzikyriakidis (contact@efxa.org)
  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://w...content-available-to-author-only...u.org/licenses/>.
  22.  *
  23.  * ---
  24.  *
  25.  * Version 1.0
  26.  *
  27.  * 2014-02-03 Brian Fletcher <brian.jf.fletcher@gmail.com>
  28.  *
  29.  * - added enqueue(), dequeue() and front().
  30.  *
  31.  * 2010-09-29 Efstathios Chatzikyriakidis <contact@efxa.org>
  32.  *
  33.  * - added resize(): for growing, shrinking the array size.
  34.  *
  35.  * 2010-09-25 Efstathios Chatzikyriakidis <contact@efxa.org>
  36.  *
  37.  * - added exit(), blink(): error reporting and handling methods.
  38.  *
  39.  * 2010-09-24 Alexander Brevig <alexanderbrevig@gmail.com>
  40.  *
  41.  * - added setPrinter(): indirectly reference a Serial object.
  42.  *
  43.  * 2010-09-20 Efstathios Chatzikyriakidis <contact@efxa.org>
  44.  *
  45.  * - initial release of the library.
  46.  *
  47.  * ---
  48.  *
  49.  * For the latest version see: http://w...content-available-to-author-only...o.cc/
  50.  */
  51.  
  52. // header defining the interface of the source.
  53. #ifndef _QUEUEARRAY_H
  54. #define _QUEUEARRAY_H
  55.  
  56. // include Arduino basic header.
  57. #include <Arduino.h>
  58.  
  59. // the definition of the queue class.
  60. template<typename T>
  61. class QueueArray {
  62. public:
  63. // init the queue (constructor).
  64. QueueArray ();
  65.  
  66. // clear the queue (destructor).
  67. ~QueueArray ();
  68.  
  69. // add an item to the queue.
  70. void enqueue (const T i);
  71.  
  72. // remove an item from the queue.
  73. T dequeue ();
  74.  
  75. // push an item to the queue.
  76. void push (const T i);
  77.  
  78. // pop an item from the queue.
  79. T pop ();
  80.  
  81. // get the front of the queue.
  82. T front () const;
  83.  
  84. // get an item from the queue.
  85. T peek () const;
  86.  
  87. // check if the queue is empty.
  88. bool isEmpty () const;
  89.  
  90. // get the number of items in the queue.
  91. int count () const;
  92.  
  93. // check if the queue is full.
  94. bool isFull () const;
  95.  
  96. // set the printer of the queue.
  97. void setPrinter (Print & p);
  98.  
  99. // resize the size of the queue.
  100.  
  101. void resize (const int s);
  102.  
  103. public:
  104. // exit report method in case of error.
  105. void exit (const char * m) const;
  106.  
  107. // led blinking method in case of error.
  108. void blink () const;
  109.  
  110. // the initial size of the queue.
  111. static const int initialSize = 2;
  112.  
  113. // the pin number of the on-board led.
  114. static const int ledPin = 13;
  115.  
  116. Print * printer; // the printer of the queue.
  117. T * contents; // the array of the queue.
  118.  
  119. int size; // the size of the queue.
  120. int items; // the number of items of the queue.
  121.  
  122. int head; // the head of the queue.
  123. int tail; // the tail of the queue.
  124. };
  125.  
  126. // init the queue (constructor).
  127. template<typename T>
  128. QueueArray<T>::QueueArray () {
  129. size = 0; // set the size of queue to zero.
  130. items = 0; // set the number of items of queue to zero.
  131.  
  132. head = 0; // set the head of the queue to zero.
  133. tail = 0; // set the tail of the queue to zero.
  134.  
  135. printer = NULL; // set the printer of queue to point nowhere.
  136.  
  137. // allocate enough memory for the array.
  138. contents = (T *) malloc (sizeof (T) * initialSize);
  139.  
  140. // if there is a memory allocation error.
  141. if (contents == NULL)
  142. exit ("QUEUE: insufficient memory to initialize queue.");
  143.  
  144. // set the initial size of the queue.
  145. size = initialSize;
  146. }
  147.  
  148. // clear the queue (destructor).
  149. template<typename T>
  150. QueueArray<T>::~QueueArray () {
  151. free (contents); // deallocate the array of the queue.
  152.  
  153. contents = NULL; // set queue's array pointer to nowhere.
  154. printer = NULL; // set the printer of queue to point nowhere.
  155.  
  156. size = 0; // set the size of queue to zero.
  157. items = 0; // set the number of items of queue to zero.
  158.  
  159. head = 0; // set the head of the queue to zero.
  160. tail = 0; // set the tail of the queue to zero.
  161. }
  162.  
  163. // resize the size of the queue.
  164. template<typename T>
  165. void QueueArray<T>::resize (const int s) {
  166. // defensive issue.
  167. if (s <= 0)
  168. exit ("QUEUE: error due to undesirable size for queue size.");
  169.  
  170. // allocate enough memory for the temporary array.
  171. T * temp = (T *) malloc (sizeof (T) * s);
  172.  
  173. // if there is a memory allocation error.
  174. if (temp == NULL)
  175. exit ("QUEUE: insufficient memory to initialize temporary queue.");
  176.  
  177. // copy the items from the old queue to the new one.
  178. for (int i = 0; i < items; i++)
  179. temp[i] = contents[(head + i) % size];
  180.  
  181. // deallocate the old array of the queue.
  182. free (contents);
  183.  
  184. // copy the pointer of the new queue.
  185. contents = temp;
  186.  
  187. // set the head and tail of the new queue.
  188. head = 0; tail = items;
  189.  
  190. // set the new size of the queue.
  191. size = s;
  192. }
  193.  
  194. // add an item to the queue.
  195. template<typename T>
  196. void QueueArray<T>::enqueue (const T i) {
  197. // check if the queue is full.
  198. if (isFull ())
  199. // double size of array.
  200. resize (size * 2);
  201.  
  202. // store the item to the array.
  203. contents[tail++] = i;
  204.  
  205. // wrap-around index.
  206. if (tail == size) tail = 0;
  207.  
  208. // increase the items.
  209. items++;
  210. }
  211.  
  212. // push an item to the queue.
  213. template<typename T>
  214. void QueueArray<T>::push (const T i) {
  215. enqueue(i);
  216. }
  217.  
  218. // remove an item from the queue.
  219. template<typename T>
  220. T QueueArray<T>::dequeue () {
  221. // check if the queue is empty.
  222. if (isEmpty ())
  223. exit ("QUEUE: can't pop item from queue: queue is empty.");
  224.  
  225. // fetch the item from the array.
  226. T item = contents[head++];
  227.  
  228. // decrease the items.
  229. items--;
  230.  
  231. // wrap-around index.
  232. if (head == size) head = 0;
  233.  
  234. // shrink size of array if necessary.
  235. if (!isEmpty () && (items <= size / 4))
  236. resize (size / 2);
  237.  
  238. // return the item from the array.
  239. return item;
  240. }
  241.  
  242. // pop an item from the queue.
  243. template<typename T>
  244. T QueueArray<T>::pop () {
  245. return dequeue();
  246. }
  247.  
  248. // get the front of the queue.
  249. template<typename T>
  250. T QueueArray<T>::front () const {
  251. // check if the queue is empty.
  252. if (isEmpty ())
  253. exit ("QUEUE: can't get the front item of queue: queue is empty.");
  254.  
  255. // get the item from the array.
  256. return contents[head];
  257. }
  258.  
  259. // get an item from the queue.
  260. template<typename T>
  261. T QueueArray<T>::peek () const {
  262. return front();
  263. }
  264.  
  265. // check if the queue is empty.
  266. template<typename T>
  267. bool QueueArray<T>::isEmpty () const {
  268. return items == 0;
  269. }
  270.  
  271. // check if the queue is full.
  272. template<typename T>
  273. bool QueueArray<T>::isFull () const {
  274. return items == size;
  275. }
  276.  
  277. // get the number of items in the queue.
  278. template<typename T>
  279. int QueueArray<T>::count () const {
  280. return items;
  281. }
  282.  
  283. // set the printer of the queue.
  284. template<typename T>
  285. void QueueArray<T>::setPrinter (Print & p) {
  286. printer = &p;
  287. }
  288.  
  289. // exit report method in case of error.
  290. template<typename T>
  291. void QueueArray<T>::exit (const char * m) const {
  292. // print the message if there is a printer.
  293. if (printer)
  294. printer->println (m);
  295.  
  296. // loop blinking until hardware reset.
  297. blink ();
  298. }
  299.  
  300. // led blinking method in case of error.
  301. template<typename T>
  302. void QueueArray<T>::blink () const {
  303. // set led pin as output.
  304. pinMode (ledPin, OUTPUT);
  305.  
  306. // continue looping until hardware reset.
  307. while (true) {
  308. digitalWrite (ledPin, HIGH); // sets the LED on.
  309. delay (250); // pauses 1/4 of second.
  310. digitalWrite (ledPin, LOW); // sets the LED off.
  311. delay (250); // pauses 1/4 of second.
  312. }
  313.  
  314. // solution selected due to lack of exit() and assert().
  315. }
  316.  
  317. #endif // _QUEUEARRAY_H
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:57:10: fatal error: 'Arduino.h' file not found
#include <Arduino.h>
         ^
1 error generated.
stdout
Standard output is empty