Stack.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: Stack.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 __STACK_H_
00019 #define __STACK_H_
00020 
00021 // Includes
00022 #include "Utilities.h"
00023 #include "List.h"
00024 #include "Queue.h"
00025 
00027 template <typename Type> class Stack
00028 {
00029 public:
00030 
00032         Stack(int size = DEFAULT_PAGE_SIZE, int PageSize = DEFAULT_PAGE_SIZE)
00033                 : data(size) // Create the list structure
00034         {
00035                 // Set the page size
00036                 pageSize = PageSize;
00037                 if(pageSize <= 0)
00038                         pageSize = DEFAULT_PAGE_SIZE;
00039 
00040                 // Set the default variables
00041                 top = 0;
00042         }
00043 
00045         ~Stack()
00046         {
00047                 // Nothing to do
00048         }
00049 
00051         void Push(Type newData)
00052         {
00053                 // Resize if needed
00054                 if(top >= data.GetSize())
00055                 {
00056                         // Grow by a page
00057                         data.Resize(data.GetSize() + pageSize);
00058                 }
00059 
00060                 // Insert data
00061                 memcpy((void*)&data[top++], (void*)&newData, sizeof(Type));
00062         }
00063 
00065         Type Pop()
00066         {
00067                 // Return the top of the stack as well as recudes the count index
00068                 Assert(top - 1 >= 0, "Attemted to pop off empty stack.");
00069                 return data[--top];
00070         }
00071 
00073         Type Peek()
00074         {
00075                 // Returns the top of the stack
00076                 Assert(top >= 0, "Attemted to peek empty stack.");
00077                 return data[top];
00078         }
00079 
00081         int GetSize()
00082         {
00083                 return top;
00084         }
00085 
00087         bool IsEmpty()
00088         {
00089                 if(top <= 0)
00090                         return true;
00091                 else
00092                         return false;
00093         }
00094 
00096         void Flip()
00097         {
00098                 // Flip data
00099                 for(int i = 0; i < top / 2; i++)
00100                 {
00101                         // Swap i with top - i
00102                         Type temp = data[i];
00103                         data[i] = data[top - i - 1];
00104                         data[top - i - 1] = temp;
00105                 }
00106         }
00107 
00108 private:
00109 
00111         static const int DEFAULT_PAGE_SIZE = 16;
00112 
00113         List< Type > data;      
00114         int top;                        
00115         int pageSize;           
00116 };
00117 
00118 #endif

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