Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

IArray.h

Go to the documentation of this file.
00001 /*
00002 
00003 AmnesiaEngine
00004 Copyright (c) 2006 John DiSanti.
00005 
00006 Permission is hereby granted, free of charge,
00007 to any person obtaining a copy of this software and associated
00008 documentation files (the "Software"), to deal in the Software
00009 without restriction, including without limitation the rights to
00010 use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to
00012 whom the Software is furnished to do so, subject to the following
00013 conditions:
00014 
00015 The above copyright notice and this permission notice shall be included
00016 in all copies or substantial portions of the Software.
00017 
00018 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
00019 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
00020 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00021 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00022 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00023 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00024 
00025 */
00026 
00027 #ifndef AMN_IARRAY_H
00028 #define AMN_IARRAY_H
00029 
00030 #include <string.h>
00031 
00032 namespace amn
00033 {
00034 
00036 template<class T>
00037 class IArray
00038 {
00039 protected:
00040         T* elements;
00041         unsigned int allocated, used;
00042 public:
00044         IArray() 
00045         {
00046                 allocated = 5;
00047                 elements = new T[allocated];
00048                 used = 0;               
00049         }
00051         IArray(const int n) 
00052         {
00053                 allocated = n;
00054                 elements = new T[allocated];
00055                 used = 0;
00056         }
00058         IArray(const IArray& a) 
00059         {
00060                 allocated = a.size()+5;
00061                 used = a.size();
00062 
00063                 elements = new T[allocated];
00064                 memcpy(elements, &a[0], used);
00065         }
00068         virtual ~IArray() 
00069         {
00070                 delete [] elements;
00071         }
00072 
00074         virtual T& operator [] (const unsigned int index)
00075         {
00076                 return elements[index];
00077         }
00079         /*virtual const T& operator [] (unsigned int index)
00080         {
00081                 return (const T)elements[index];
00082         }*/
00083 
00085         virtual IArray operator = (IArray& a)
00086         {
00087                 clear();
00088                 allocated = a.size()+5;
00089                 used = a.size();
00090 
00091                 elements = new T[allocated];
00092                 memcpy(elements, &a[0], used);
00093         }
00094 
00096         virtual bool operator == (IArray& a2)
00097         {
00098                 if(size() != a2.size())
00099                         return false;
00100 
00101                 unsigned int i;
00102                 for(i = 0; i < size(); i++)
00103                 {
00104                         if(memcmp(&elements[i], &a2[i], sizeof(T)) != 0)
00105                                 return false;
00106                 } 
00107 
00108                 return true;
00109         }
00111         virtual bool operator != (IArray& a2)
00112         {
00113                 return !(*this == a2);
00114         }
00115 
00117         virtual unsigned int size()
00118         {
00119                 return used;
00120         }
00122         virtual bool empty()
00123         {
00124                 return (used == 0);
00125         }
00126 
00128         virtual void push_back(const T& x)
00129         {
00130                 if(used >= allocated)
00131                 {
00132                         allocated += 10;
00133                         T* elem = new T[allocated];
00134 
00135                         unsigned int i;
00136                         for(i = 0; i < used; i++)
00137                                 elem[i] = elements[i];
00138 
00139                         delete [] elements;
00140                         elements = elem;
00141                 }
00142 
00143                 T e = x;
00144                 elements[used] = e;
00145                 used++;
00146         }
00148         virtual void pop_back()
00149         {
00150                 used--;
00151         }
00152 
00154         virtual void clear()
00155         {
00156                 delete [] elements;
00157                 used = 0;
00158                 allocated = 5;
00159                 elements = new T[allocated];
00160         }
00161 };
00162 
00163 };
00164 
00165 #endif//AMN_IARRAY_H
00166 
00167 /* End of File */

Generated on Wed Feb 1 22:23:26 2006 for AmnesiaEngine by  doxygen 1.4.2