List.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: List.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 __LIST_H_
00019 #define __LIST_H_
00020 
00021 // Includes
00022 #include "Utilities.h"
00023 
00025 template <typename Type> class List
00026 {
00027 public:
00028 
00030         List(int size)
00031         {
00032                 // Create the new list
00033                 listData = NULL;
00034                 listSize = 0;
00035 
00036                 // Initialize the data
00037                 Resize(size);
00038         }
00039 
00041         ~List()
00042         {
00043                 // Remove the data list
00044                 if(listData != NULL)
00045                         delete[] listData;
00046         }
00047 
00049         Type& operator[](int index)
00050         {
00051                 // Exit on empty data error
00052                 Assert(listSize > 0, "Empty data set memory access.");
00053 
00054                 // Exit on out of bounds access
00055                 Assert(index >= 0 && index < listSize, "Out of bounds memory access.");
00056 
00057                 // Return data
00058                 return listData[index];
00059         }
00060 
00062         void Resize(int newSize)
00063         {
00064                 // If new size is invalid, empty the dataset
00065                 if(newSize <= 0)
00066                 {
00067                         listSize = 0;
00068                         if(listData != NULL)
00069                                 delete[] listData;
00070                         listData = NULL;
00071                 }
00072                 else
00073                 {
00074                         // Create the new data array
00075                         Type *newData = new Type[newSize];
00076 
00077                         // Copy over the original data
00078                         //memcpy((void*)newData, (void*)listData, sizeof(Type) * min(newSize, listSize));
00079 
00080                         // Remove the old list, set the new data pointer and size
00081                         if(listData != NULL)
00082                                 delete[] listData;
00083 
00084                         // Save list data and list size
00085                         listData = newData;
00086                         listSize = newSize;
00087                 }
00088         }
00089 
00091         int GetSize()
00092         {
00093                 return listSize;
00094         }
00095 
00096 private:
00097 
00098         Type *listData; 
00099         int listSize;   
00100 };
00101 
00102 #endif

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