Queue.h

Go to the documentation of this file.
00001 /***************************************************************
00002  
00003  Mini Grand Challenge 2010
00004  Pennsylvania State University - Robotics Club
00005  Learn more at www.psurobotics.org
00006  Protected by the GNU General Public License
00007  
00008  This source file is developed and maintained by:
00009  + Jeremy Bridon jgbridon@gmail.com
00010  
00011  File: Queue.h
00012  Desc: Custom-build data structure for use and support of the
00013  dictionary data structure.
00014  
00015 ***************************************************************/
00016 
00017 // Inclusion guard
00018 #ifndef __QUEUE_H_
00019 #define __QUEUE_H_
00020 
00021 // Includes
00022 #include "Utilities.h"
00023 #include "List.h"
00024 
00026 template <typename Type> class Queue
00027 {
00028 public:
00029 
00031         Queue(int size = DEFAULT_PAGE_SIZE, int PageSize = DEFAULT_PAGE_SIZE)
00032                 : data(size) // Create the list structure
00033         {
00034                 // Set the page size
00035                 pageSize = PageSize;
00036                 if(pageSize <= 0)
00037                         pageSize = DEFAULT_PAGE_SIZE;
00038 
00039                 // Set the default variables
00040                 front = 0;
00041                 back = data.GetSize() - 1;
00042                 count = 0;
00043         }
00044 
00046         ~Queue()
00047         {
00048                 // Nothing to do
00049         }
00050 
00052         Type Enqueue(Type newData)
00053         {
00054                 // If last collides with first
00055                 if(count >= data.GetSize())
00056                 {
00057                         // Grow by a page
00058                         int originalSize = data.GetSize();
00059                         data.Resize(data.GetSize() + pageSize);
00060 
00061                         // Move data only if the front pointer is greater than the back pointer (wrapping)
00062                         if(front > back)
00063                         {
00064                                 // Move all elements from the front index pointer to the end of the current page block one full new page to the right
00065                                 for(int i = front; i < originalSize; i++)
00066                                         data[i + pageSize] = data[i];
00067                                 front += pageSize;
00068                         }
00069                 }
00070 
00071                 // Find the next index
00072                 back = (back + 1) % data.GetSize();
00073 
00074                 // Grow the last index and insert data
00075                 data[back] = newData;
00076                 count++;
00077 
00078                 // Return the element we have added
00079                 return newData;
00080         }
00081 
00083         Type Dequeue()
00084         {
00085                 // Make sure there is data
00086                 Assert(!IsEmpty(), "Attempting to access empty queue.");
00087 
00088                 // Return the data
00089                 Type temp = data[front];
00090                 front = (front + 1) % data.GetSize();
00091                 count--; 
00092 
00093                 // Return temp
00094                 return temp;
00095         }
00096         
00098         int GetSize()
00099         {
00100                 return count;
00101         }
00102         
00104         bool IsEmpty()
00105         {
00106                 if(count <= 0)
00107                         return true;
00108                 return false;
00109         }
00110 
00111 private:
00112 
00114         static const int DEFAULT_PAGE_SIZE = 16;
00115 
00116         List<Type> data;        
00117 
00118         int front;                      
00119         int back;                       
00120         int count;                      
00121 
00122         int pageSize;           
00123 
00124 };
00125 
00126 #endif

Generated on Sun Feb 21 00:00:08 2010 for Penn State Robotics Club: Mini Grand Challenge 2010 by  doxygen 1.5.5