Main Page | Class Hierarchy | Data Structures | File List | Data Fields | Globals

FTVector.h

Go to the documentation of this file.
00001 #ifndef    __FTVector__
00002 #define    __FTVector__
00003 
00004 #include "FTGL.h"
00005 
00009 template <typename FT_VECTOR_ITEM_TYPE>
00010 class FTGL_EXPORT FTVector
00011 {
00012     public:
00013         typedef FT_VECTOR_ITEM_TYPE value_type;
00014         typedef value_type& reference;
00015         typedef const value_type& const_reference;
00016         typedef value_type* iterator;
00017         typedef const value_type* const_iterator;
00018         typedef size_t size_type;
00019         
00020         FTVector()
00021         {
00022             Capacity = Size = 0;
00023             Items = 0;
00024         }
00025 
00026         
00027         virtual ~FTVector()
00028         {
00029             clear();
00030         }
00031         
00032         FTVector& operator =(const FTVector& v)
00033         {
00034             reserve(v.capacity());
00035             
00036             iterator ptr = begin();
00037             const_iterator vbegin = v.begin();
00038             const_iterator vend = v.end();
00039             
00040             while( vbegin != vend)
00041             {
00042                 *ptr++ = *vbegin++;
00043             }
00044             
00045             Size = v.size();
00046             return *this;
00047         }
00048         
00049         size_type size() const
00050         {
00051             return Size;
00052         }
00053         
00054         size_type capacity() const
00055         {
00056             return Capacity;
00057         }
00058         
00059         iterator begin()
00060         {
00061             return Items;
00062         }
00063         
00064         const_iterator begin() const
00065         {
00066             return Items;
00067         }
00068         
00069         iterator end()
00070         {
00071             return begin() + size(); 
00072         }
00073         
00074         const_iterator end() const
00075         {
00076             return begin() + size(); 
00077         }
00078         
00079         bool empty() const 
00080         { 
00081             return size() == 0; 
00082         }
00083 
00084         reference operator [](size_type pos) 
00085         { 
00086             return( *(begin() + pos)); 
00087         }
00088 
00089         const_reference operator []( size_type pos) const 
00090         { 
00091             return( *(begin() + pos)); 
00092         }
00093         
00094         void clear()
00095         {
00096             if( Capacity)
00097             {
00098                 delete [] Items;
00099                 Capacity = Size = 0;
00100                 Items = 0;
00101             }
00102         }
00103 
00104         void reserve( size_type n)
00105         {
00106             if( capacity() < n)
00107             {
00108                 expand(n);
00109             }
00110         }
00111 
00112         void push_back(const value_type& x)
00113         {
00114             if( size() == capacity())
00115             {
00116                 expand();
00117             }
00118            
00119            ( *this)[size()] = x;
00120             ++Size;
00121         }
00122 
00123         void resize(size_type n, value_type x)
00124         {
00125             if( n == size())
00126             {
00127                 return;
00128             }
00129             
00130             reserve(n);
00131             iterator begin, end;
00132             
00133             if( n >= Size)
00134             {
00135                 begin = this->end();
00136                 end = this->begin() + n;
00137             }
00138             else
00139             {
00140                 begin = this->begin() + n;
00141                 end = this->end();
00142             }
00143         
00144             while( begin != end)
00145             {
00146                 *begin++ = x;
00147             }
00148         
00149             Size = n;
00150         }
00151 
00152         
00153     private:
00154         void expand(size_type capacity_hint = 0)
00155         {
00156             size_type new_capacity =( capacity() == 0) ? 256 : capacity()* 2;
00157             if( capacity_hint)
00158             {
00159                 while( new_capacity < capacity_hint)
00160                 {
00161                     new_capacity *= 2;
00162                 }
00163             }
00164             
00165             value_type *new_items = new value_type[new_capacity];
00166             
00167             iterator begin = this->begin();
00168             iterator end = this->end();
00169             value_type *ptr = new_items;
00170             
00171             while( begin != end)
00172             {
00173                 *ptr++ = *begin++;
00174             }
00175             
00176             if( Capacity)
00177             {
00178                 delete [] Items;
00179             }
00180         
00181             Items = new_items;
00182             Capacity = new_capacity;
00183         }
00184 
00185         size_type Capacity;
00186         size_type Size;
00187         value_type* Items;
00188 };
00189 
00190 #endif  //  __FTVector__

Generated on Sun Dec 5 22:24:06 2004 for FTGL by doxygen 1.3.6