buffer_vector.hxx

Go to the documentation of this file.
00001 #pragma once
00002 #ifndef OPENGM_BUFFER_VECTOR_HXX
00003 #define  OPENGM_BUFFER_VECTOR_HXX
00004 
00005 #include <vector>
00006 #include <algorithm>
00007 
00008 #include "opengm/opengm.hxx"
00009 
00011 
00012 namespace opengm{
00018    template<class T>
00019    class BufferVector{
00020       public:
00021          typedef T ValueType;
00022          typedef T value_type;
00023          typedef T const * ConstIteratorType;
00024          typedef T const * const_iterator;
00025          typedef T * IteratorType;
00026          typedef T * iterator;
00027          BufferVector( );
00028          BufferVector(const size_t );
00029          BufferVector(const size_t ,const T & );
00030          BufferVector(const BufferVector<T> &);
00031          ~BufferVector( );
00032          BufferVector<T> & operator= (const BufferVector<T> &);
00033          size_t size() const ;
00034          T const * begin() const;
00035          T const * end() const;
00036          T * const begin();
00037          T * const end();
00038          
00039          T const * data() const;
00040          T * data();
00041          
00042          T const & operator[](const size_t)const;
00043          T & operator[](const size_t);
00044          void push_back(const T &);
00045          void resize(const size_t );
00046          void reserve(const size_t );
00047          void clear();
00048          bool empty()const;
00049          const T & front()const;
00050          const T & back()const;
00051          T & front();
00052          T & back();
00053          template<class ITERATOR>
00054          void assign(ITERATOR ,ITERATOR);
00055       private:
00056          size_t size_;
00057          size_t capacity_;
00058          T * data_;
00059    };
00061    template<class T>
00062    inline BufferVector<T>::BufferVector( )
00063    :  size_(0),
00064       capacity_(0),
00065       data_(NULL) {
00066    }
00067    
00070    template<class T>
00071    inline BufferVector<T>::BufferVector
00072    (
00073       const size_t size
00074    )
00075    :  size_(size),
00076       capacity_(size) {
00077       if(size_!=0) {
00078       data_ = new T[size];
00079       }
00080    }
00081    
00085    template<class T>
00086    inline BufferVector<T>::BufferVector
00087    (
00088       const size_t size,
00089       const T & value
00090    )
00091    :  size_(size),
00092       capacity_(size) {
00093       data_ = new T[size];
00094       std::fill(data_,data_+size_,value);
00095    }
00096    
00097    
00100    template<class T>
00101    inline BufferVector<T>::BufferVector
00102    (
00103       const  BufferVector<T> & other
00104    )
00105    :  size_(other.size_),
00106       capacity_(other.size_)
00107    {
00108       if(size_!=0) {
00109          data_ = new T[size_];
00110          std::copy(other.data_,other.data_+size_,data_);
00111       }
00112    }
00113    
00115    template<class T>
00116    inline T const * 
00117    BufferVector<T>::data() const{
00118       return data_;
00119    }
00120    
00122    template<class T>
00123    inline T * 
00124    BufferVector<T>::data() {
00125       return data_;
00126    }
00127    
00129    template<class T>
00130    inline BufferVector<T>::~BufferVector( ) {
00131       if(capacity_!=0) {
00132          OPENGM_ASSERT(data_!=NULL);
00133          delete[] data_;
00134       }
00135    }
00136    
00138    template<class T>
00139    inline BufferVector<T> & BufferVector<T>::operator=
00140    (
00141       const  BufferVector<T> & other
00142    )
00143    {
00144       if(&other!=this) {
00145          size_=other.size_;
00146          if(size_>capacity_) {
00147             delete [] data_;
00148             data_ = new T[size_];
00149             capacity_=size_;
00150          }
00151          std::copy(other.data_,other.data_+size_,data_);
00152       }
00153       return *this;
00154    }
00155    
00157    template<class T>
00158    inline size_t
00159    BufferVector<T>::size() const {
00160       OPENGM_ASSERT(data_!=NULL ||size_== 0 );
00161       return size_;
00162    }
00163    
00165    template<class T>
00166    inline T const *
00167    BufferVector<T>::begin() const{
00168       OPENGM_ASSERT(data_!=NULL);
00169       return data_;
00170    }
00171    
00173    template<class T>
00174    inline T const *
00175    BufferVector<T>::end() const{
00176       OPENGM_ASSERT(data_!=NULL);
00177       return data_ + size_;
00178    }
00179    
00181    template<class T>
00182    inline T * const
00183    BufferVector<T>::begin() {
00184       OPENGM_ASSERT(data_!=NULL);
00185       return data_;
00186    }
00187    
00189    template<class T>
00190    inline T * const
00191    BufferVector<T>::end() {
00192       OPENGM_ASSERT(data_!=NULL);
00193       return data_ + size_;
00194    }
00195    
00198    template<class T>
00199    inline T const &
00200    BufferVector<T>::operator[]
00201    (
00202       const size_t index
00203    )const{
00204       OPENGM_ASSERT(data_!=NULL);
00205       OPENGM_ASSERT(index<size_);
00206       return data_[index];
00207    }
00208    
00211    template<class T>
00212    inline T &
00213    BufferVector<T>::operator[]
00214    (
00215       const size_t index
00216    ) {
00217       OPENGM_ASSERT(index<size_);
00218       return data_[index];
00219    }
00220    
00223    template<class T>
00224    inline void
00225    BufferVector<T>::push_back
00226    (
00227       const T & value
00228    ) {
00229       OPENGM_ASSERT(size_<=capacity_);
00230       if(capacity_==size_) {
00231          if(size_!=0) {
00232             T * tmp=new T[capacity_*2];
00233             std::copy(data_,data_+size_,tmp);
00234             delete[] data_;
00235             capacity_*=2;
00236             data_=tmp;
00237          }
00238          else{
00239             T * tmp=new T[2];
00240             capacity_=2;
00241             data_=tmp;
00242          }
00243       }
00244       data_[size_]=value;
00245       ++size_;
00246       OPENGM_ASSERT(size_<=capacity_);
00247    }
00248    
00251    template<class T>
00252    inline void
00253    BufferVector<T>::resize
00254    (
00255       const size_t size
00256    ) {
00257       OPENGM_ASSERT(size_<=capacity_);
00258       if(size>capacity_) {
00259          if(size_!=0) {
00260             T * tmp=new T[size];
00261             std::copy(data_,data_+size_,tmp);
00262             delete[] data_;
00263             capacity_=size;
00264             data_=tmp;
00265          }
00266          else{
00267             data_=new T[size];
00268             capacity_=size;
00269          }
00270       }
00271       size_=size;
00272       OPENGM_ASSERT(size_<=capacity_);
00273    }
00274    
00277    template<class T>
00278    inline void
00279    BufferVector<T>::reserve
00280    (
00281       const size_t size
00282    ) {
00283       OPENGM_ASSERT(size_<=capacity_);
00284       if(size>capacity_) {
00285          if(size_!=0) {
00286             T * tmp=new T[size];
00287             std::copy(data_,data_+size_,tmp);
00288             delete[] data_;
00289             data_=tmp;
00290          }
00291          else{
00292             data_=new T[size];
00293          }
00294          size_=size;
00295          capacity_=size;
00296       }
00297       //size_=size;
00298       OPENGM_ASSERT(size_<=capacity_);
00299    }
00300    
00304    template<class T>
00305    inline void
00306    BufferVector<T>::clear() {
00307       OPENGM_ASSERT(size_<=capacity_);
00308       size_=0;
00309    }
00310    
00312    template<class T>
00313    inline bool
00314    BufferVector<T>::empty()const{
00315       return size_==0 ? true:false;
00316    }
00317    
00321    template<class T>
00322    template<class ITERATOR>
00323    inline void
00324    BufferVector<T>::assign(ITERATOR begin,ITERATOR end) {
00325       this->resize(std::distance(begin,end));
00326       std::copy(begin, end, data_);
00327    }
00328    
00330    template<class T>
00331    inline const T &
00332    BufferVector<T>::back()const{
00333       return data_[size_-1];
00334    }
00335 
00337    template<class T>
00338    inline T &
00339    BufferVector<T>::back() {
00340       return data_[size_-1];
00341    }
00342    
00344    template<class T>
00345    inline const T &
00346    BufferVector<T>::front()const{
00347       return data_[0];
00348    }
00349    
00351    template<class T>
00352    inline T &
00353    BufferVector<T>::front() {
00354       return data_[0];
00355    }
00356 }
00357 
00359 
00360 #endif   //OPENGM_BUFFER_VECTOR_HXX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Jun 17 16:31:01 2013 for OpenGM by  doxygen 1.6.3