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

maxvector.hpp

Go to the documentation of this file.
00001 
00009 #ifndef MAXVECTOR_HPP_050403
00010 #define MAXVECTOR_HPP_050403
00011 
00012 
00013 //#include "common/allocators.cpp"
00014 #include <algorithm>
00027 template<class T> class maxvector {
00028 private:
00029   T* data;
00030   size_t length;
00031 public:
00032   typedef T value_type;
00033 
00034   maxvector() {
00035     length=0;
00036     data=NULL;
00037   }
00038 
00039   maxvector(size_t maxsize) {
00040     data=new T[maxsize];
00041     length=0;
00042   }
00043 
00044   ~maxvector() {
00045     delete [] data;
00046   }
00047 
00048   T& DINLINE  operator[](size_t n) {
00049     return data[n];
00050   }
00051 
00052   const T& DINLINE  operator[](size_t n) const {
00053     return data[n];
00054   }
00055 
00056   typedef T* iterator;
00057   typedef const T* const_iterator;
00058 
00059   iterator DINLINE begin()  {
00060     return data;
00061   }
00062 
00063   const_iterator DINLINE begin() const {
00064     return data;
00065   }
00066 
00067 
00068   iterator DINLINE end()  {
00069     return data+length;
00070   }
00071 
00072   const_iterator DINLINE end() const  {
00073     return data+length;
00074   }
00075 
00076   size_t DINLINE size() const {
00077     return length;
00078   }
00079 
00080   /*void DINLINE push_back(T &d) {
00081     data[length++]=d;
00082     }*/
00083   
00084   /*  template<class U> void DINLINE push_back(const U &u) {
00085     data[length++]=T(u);
00086     }*/
00087 
00088   void DINLINE push_back(const T & d) {
00089     data[length++]=d;
00090   }
00091 
00092   void DINLINE pop_back() {
00093     length--;
00094   }
00095 
00096   bool DINLINE empty() const {
00097     return (!length);
00098   }
00099 
00100   T& DINLINE front() {
00101     return (data[0]);
00102   }
00103   
00104   T& DINLINE back() {
00105     return (data[length-1]);
00106   }
00107 
00108   const T& DINLINE back() const {
00109     return (data[length-1]);
00110   }
00111 
00112 
00113   void DINLINE reserve(size_t n) {
00114     T* newdata = new T[n];
00115     length=std::min(length,n);
00116     for(size_t i=0;i<length;i++) {
00117       newdata[i]=data[i];
00118     }
00119     delete [] data;
00120     data = newdata;
00121   }
00122 
00123   void DINLINE resize(size_t n, const T& d) {
00124     if (!data)
00125       reserve(n);
00126     for (size_t i=length;i<n;++i) {
00127       data[i]=d;
00128     }
00129     length=n;
00130   }
00131 
00132   void DINLINE resize(size_t n) {
00133     if (!data)
00134       reserve(n);
00135     length=n;
00136   }
00137 
00138   void DINLINE clear() {
00139     length=0;
00140   }
00141 
00142         iterator DINLINE erase(iterator first,
00143                                const iterator last)
00144       {
00145          while( first!=last )
00146             erase(first);
00147          return first;
00148       }
00149 
00150   iterator DINLINE erase(iterator pos)
00151   {
00152     T* pos2 = pos;
00153     --length;
00154     while(pos2 != data + length)
00155       {
00156         *pos2 = *(pos2+1);
00157         ++pos2;
00158       }
00159     return pos;
00160   }
00161 
00162 };
00163 
00164 #endif //MAXVECTOR_HPP_050403

Generated on Sun Sep 17 17:50:39 2006 for FIM environment by  doxygen 1.4.4