2222 *
2323 * ---
2424 *
25+ * 2013-11-05 Marcus Nowotny <interactive-matter.eu>
26+ * - rewritten to use a default size
27+ *
2528 * Version 1.0
2629 *
2730 * 2010-09-29 Efstathios Chatzikyriakidis <contact@efxa.org>
@@ -57,7 +60,7 @@ template<typename T>
5760class QueueArray {
5861 public:
5962 // init the queue (constructor).
60- QueueArray ();
63+ QueueArray (const unsigned char initialSize );
6164
6265 // clear the queue (destructor).
6366 ~QueueArray ();
@@ -84,18 +87,13 @@ class QueueArray {
8487 void setPrinter (Print & p);
8588
8689 private:
87- // resize the size of the queue.
88- void resize (const int s);
89-
90+
9091 // exit report method in case of error.
9192 void exit (const char * m) const ;
9293
9394 // led blinking method in case of error.
9495 void blink () const ;
9596
96- // the initial size of the queue.
97- static const int initialSize = 2 ;
98-
9997 // the pin number of the on-board led.
10098 static const int ledPin = 13 ;
10199
@@ -111,7 +109,7 @@ class QueueArray {
111109
112110// init the queue (constructor).
113111template <typename T>
114- QueueArray<T>::QueueArray () {
112+ QueueArray<T>::QueueArray (const unsigned char initialSize ) {
115113 size = 0 ; // set the size of queue to zero.
116114 items = 0 ; // set the number of items of queue to zero.
117115
@@ -146,44 +144,13 @@ QueueArray<T>::~QueueArray () {
146144 tail = 0 ; // set the tail of the queue to zero.
147145}
148146
149- // resize the size of the queue.
150- template <typename T>
151- void QueueArray<T>::resize (const int s) {
152- // defensive issue.
153- if (s <= 0 )
154- exit (" QUEUE: error due to undesirable size for queue size." );
155-
156- // allocate enough memory for the temporary array.
157- T * temp = (T *) malloc (sizeof (T) * s);
158-
159- // if there is a memory allocation error.
160- if (temp == NULL )
161- exit (" QUEUE: insufficient memory to initialize temporary queue." );
162-
163- // copy the items from the old queue to the new one.
164- for (int i = 0 ; i < items; i++)
165- temp[i] = contents[(head + i) % size];
166-
167- // deallocate the old array of the queue.
168- free (contents);
169-
170- // copy the pointer of the new queue.
171- contents = temp;
172-
173- // set the head and tail of the new queue.
174- head = 0 ; tail = items;
175-
176- // set the new size of the queue.
177- size = s;
178- }
179-
180147// push an item to the queue.
181148template <typename T>
182149void QueueArray<T>::push (const T i) {
183150 // check if the queue is full.
184151 if (isFull ())
185152 // double size of array.
186- resize (size * 2 );
153+ exit ( " todo " );
187154
188155 // store the item to the array.
189156 contents[tail++] = i;
@@ -211,9 +178,6 @@ T QueueArray<T>::pop () {
211178 // wrap-around index.
212179 if (head == size) head = 0 ;
213180
214- // shrink size of array if necessary.
215- if (!isEmpty () && (items <= size / 4 ))
216- resize (size / 2 );
217181
218182 // return the item from the array.
219183 return item;
0 commit comments