- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Containers/DynamicQueue.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #include "DynamicQueue.h"
    00015 #include <stdio.h>
    00016 #include <string.h>
    00017
    00019
    00020 using namespace Mers::Utils::Containers;
    00021 using std::ostream;
    00022
    00024 // Construction/Destruction
    00026
    00027 DynamicQueue::DynamicQueue(DynamicArray::DynamicArrayType queueType,
    00028         bool myOwnershipStatus, int myMaxQueueSize)
    00029 : DynamicArray(queueType, myOwnershipStatus)
    00030 {
    00031         head = 0;
    00032         tail = 0;
    00033         maxQueueSize = myMaxQueueSize;
    00034         // the queue must be able to store at least one element.
    00035         if(maxQueueSize == 0)
    00036                 maxQueueSize = 1;
    00037         if(maxQueueSize > 0){
    00038                 maxQueueSize++;
    00039                 if(FAILED(resize(maxQueueSize))){
    00040                         fprintf(stderr, "Failed to resize the queue to %d elements\n", maxQueueSize - 1);
    00041                 }
    00042         }
    00043 }
    00044
    00046
    00047 DynamicQueue::~DynamicQueue()
    00048 {
    00049 }
    00050
    00052
    00053 unsigned int DynamicQueue::getLength() const
    00054 {
    00055         if(mSize == 0)
    00056                 return 0;
    00057         return ((tail - head) + mSize) % mSize;
    00058 }
    00059
    00061
    00062 bool DynamicQueue::isEmpty() const
    00063 {
    00064         return tail == head;
    00065 }
    00066
    00068
    00069 #ifndef EMBEDDED
    00070
    00071 void DynamicQueue::printArray(ostream &out, char const * seperator) const
    00072 {
    00073         unsigned int x;
    00074
    00075         for(x = head; x != tail;){
    00076                 if(x != head)
    00077                         out << seperator;
    00078                 if(array[x] != NULL)
    00079                         printElement(out, array[x]);
    00080                 else
    00081                         out << "NULL Pointer";
    00082                 x++;
    00083                 x %= mSize;
    00084         }
    00085 }
    00086
    00087 #endif
    00088
    00090
    00091 DynamicQueue::DynamicQueue(DynamicQueue const &other)
    00092 : DynamicArray(other)
    00093 {
    00094         head = other.head;
    00095         tail = other.tail;
    00096 }
    00097
    00099
    00100 DynamicQueue::DynamicQueue(DynamicQueue const * other)
    00101 : DynamicArray(*other)
    00102 {
    00103         head = other->head;
    00104         tail = other->tail;
    00105 }
    00106
    00108
    00109 int DynamicQueue::enqueue(void * element)
    00110 {
    00111         if(mSize == 0 || getLength() == mSize - 1){
    00112                 // we're already full, failed to insert the element
    00113                 if(maxQueueSize > 0){
    00114                         if(mSize == 0){
    00115                                 // our first attempt at growing failed
    00116                                 if(FAILED(resize(maxQueueSize))){
    00117                                         fprintf(stderr, "Failed to resize the queue to fit %d elements\n",
    00118                                                                         maxQueueSize - 1);
    00119                                         return 4;
    00120                                 }
    00121                                 // but we are big enough to fit at least one element now....
    00122                         }else{
    00123                                 // we're really full - we're not suppose to insert anything else
    00124                                 return 3;
    00125                         }
    00126                 }else{
    00127                         unsigned int oldSize = mSize;
    00128
    00129                         //cerr << "Queue Full, Resizing" << endl;
    00130                         //cerr << "Before: [";
    00131                         //printArray(cerr, ", ");
    00132                         //cerr << ']' << endl;
    00133                         if(mSize == 0){
    00134                                 if(FAILED(resize(10))){
    00135                                         fprintf(stderr, "Error, could not resize the queue to fit an "
    00136                                                                         "element\n");
    00137                                         return 1;
    00138                                 }
    00139                         }else{
    00140                                 if(FAILED(resize(mSize * 2))){
    00141                                         fprintf(stderr, "Error, could not resize the queue to fit another "
    00142                                                                         "element\n");
    00143                                         return 2;
    00144                                 }
    00145                         }
    00146                         if(head > tail){
    00147                                 // we need to copy the contents previously at the end of the
    00148                                 // array to the end of the array
    00149                                 memmove(array + mSize + head - oldSize, array + head,
    00150                                                                 (oldSize - head) * sizeof(void *));
    00151                                 head += mSize - oldSize;
    00152                         }
    00153                         //cerr << "After: [";
    00154                         //printArray(cerr, ", ");
    00155                         //cerr << ']' << endl;
    00156                 }
    00157         }
    00158         // we've got room - add the element
    00159         array[tail] = element;
    00160         tail++;
    00161         tail %= mSize;
    00162         return 0;
    00163 }
    00164
    00166
    00167 int DynamicQueue::dequeue(void * &element)
    00168 {
    00169         if(head == tail){
    00170                 element = NULL;
    00171                 return 1;
    00172         }
    00173
    00174         // get the head
    00175         element = array[head];
    00176         // set the head to NULL so it isn't deleted later
    00177         array[head] = NULL;
    00178         head++;
    00179         head %= mSize;
    00180         return 0;
    00181 }
    00182
    00184
    00185 int DynamicQueue::peekQueue(void * &element)
    00186 {
    00187         if(head == tail){
    00188                 element = NULL;
    00189                 return 1;
    00190         }
    00191
    00192         element = array[head];
    00193         return 0;
    00194 }
    00195
    00197