OpenGM  2.3.x
Discrete Graphical Model Library
buffer_vector.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_BUFFER_VECTOR_HXX
3 #define OPENGM_BUFFER_VECTOR_HXX
4 
5 #include <vector>
6 #include <algorithm>
7 
8 #include "opengm/opengm.hxx"
9 
11 
12 namespace opengm{
18  template<class T>
19  class BufferVector{
20  public:
21  typedef T ValueType;
22  typedef T value_type;
23  typedef T const * ConstIteratorType;
24  typedef T const * const_iterator;
25  typedef T * IteratorType;
26  typedef T * iterator;
27  BufferVector( );
28  BufferVector(const size_t );
29  BufferVector(const size_t ,const T & );
30  BufferVector(const BufferVector<T> &);
31  ~BufferVector( );
32  BufferVector<T> & operator= (const BufferVector<T> &);
33  size_t size() const ;
34  T const * begin() const;
35  T const * end() const;
36  T * const begin();
37  T * const end();
38 
39  T const * data() const;
40  T * data();
41 
42  T const & operator[](const size_t)const;
43  T & operator[](const size_t);
44  void push_back(const T &);
45  void resize(const size_t );
46  void reserve(const size_t );
47  void clear();
48  bool empty()const;
49  const T & front()const;
50  const T & back()const;
51  T & front();
52  T & back();
53  template<class ITERATOR>
54  void assign(ITERATOR ,ITERATOR);
55  private:
56  size_t size_;
57  size_t capacity_;
58  T * data_;
59  };
61  template<class T>
62  inline BufferVector<T>::BufferVector( )
63  : size_(0),
64  capacity_(0),
65  data_(NULL) {
66  }
67 
70  template<class T>
71  inline BufferVector<T>::BufferVector
72  (
73  const size_t size
74  )
75  : size_(size),
76  capacity_(size) {
77  if(size_!=0) {
78  data_ = new T[size];
79  }
80  }
81 
85  template<class T>
86  inline BufferVector<T>::BufferVector
87  (
88  const size_t size,
89  const T & value
90  )
91  : size_(size),
92  capacity_(size) {
93  data_ = new T[size];
94  std::fill(data_,data_+size_,value);
95  }
96 
97 
100  template<class T>
101  inline BufferVector<T>::BufferVector
102  (
103  const BufferVector<T> & other
104  )
105  : size_(other.size_),
106  capacity_(other.size_)
107  {
108  if(size_!=0) {
109  data_ = new T[size_];
110  std::copy(other.data_,other.data_+size_,data_);
111  }
112  }
113 
115  template<class T>
116  inline T const *
117  BufferVector<T>::data() const{
118  return data_;
119  }
120 
122  template<class T>
123  inline T *
124  BufferVector<T>::data() {
125  return data_;
126  }
127 
129  template<class T>
130  inline BufferVector<T>::~BufferVector( ) {
131  if(capacity_!=0) {
132  OPENGM_ASSERT(data_!=NULL);
133  delete[] data_;
134  }
135  }
136 
138  template<class T>
139  inline BufferVector<T> & BufferVector<T>::operator=
140  (
141  const BufferVector<T> & other
142  )
143  {
144  if(&other!=this) {
145  size_=other.size_;
146  if(size_>capacity_) {
147  delete [] data_;
148  data_ = new T[size_];
149  capacity_=size_;
150  }
151  std::copy(other.data_,other.data_+size_,data_);
152  }
153  return *this;
154  }
155 
157  template<class T>
158  inline size_t
159  BufferVector<T>::size() const {
160  OPENGM_ASSERT(data_!=NULL ||size_== 0 );
161  return size_;
162  }
163 
165  template<class T>
166  inline T const *
167  BufferVector<T>::begin() const{
168  OPENGM_ASSERT(data_!=NULL);
169  return data_;
170  }
171 
173  template<class T>
174  inline T const *
175  BufferVector<T>::end() const{
176  OPENGM_ASSERT(data_!=NULL);
177  return data_ + size_;
178  }
179 
181  template<class T>
182  inline T * const
183  BufferVector<T>::begin() {
184  OPENGM_ASSERT(data_!=NULL);
185  return data_;
186  }
187 
189  template<class T>
190  inline T * const
191  BufferVector<T>::end() {
192  OPENGM_ASSERT(data_!=NULL);
193  return data_ + size_;
194  }
195 
198  template<class T>
199  inline T const &
200  BufferVector<T>::operator[]
201  (
202  const size_t index
203  )const{
204  OPENGM_ASSERT(data_!=NULL);
205  OPENGM_ASSERT(index<size_);
206  return data_[index];
207  }
208 
211  template<class T>
212  inline T &
213  BufferVector<T>::operator[]
214  (
215  const size_t index
216  ) {
217  OPENGM_ASSERT(index<size_);
218  return data_[index];
219  }
220 
223  template<class T>
224  inline void
225  BufferVector<T>::push_back
226  (
227  const T & value
228  ) {
229  OPENGM_ASSERT(size_<=capacity_);
230  if(capacity_==size_) {
231  if(size_!=0) {
232  T * tmp=new T[capacity_*2];
233  std::copy(data_,data_+size_,tmp);
234  delete[] data_;
235  capacity_*=2;
236  data_=tmp;
237  }
238  else{
239  T * tmp=new T[2];
240  capacity_=2;
241  data_=tmp;
242  }
243  }
244  data_[size_]=value;
245  ++size_;
246  OPENGM_ASSERT(size_<=capacity_);
247  }
248 
251  template<class T>
252  inline void
253  BufferVector<T>::resize
254  (
255  const size_t size
256  ) {
257  OPENGM_ASSERT(size_<=capacity_);
258  if(size>capacity_) {
259  if(size_!=0) {
260  T * tmp=new T[size];
261  std::copy(data_,data_+size_,tmp);
262  delete[] data_;
263  capacity_=size;
264  data_=tmp;
265  }
266  else{
267  data_=new T[size];
268  capacity_=size;
269  }
270  }
271  size_=size;
272  OPENGM_ASSERT(size_<=capacity_);
273  }
274 
277  template<class T>
278  inline void
279  BufferVector<T>::reserve
280  (
281  const size_t size
282  ) {
283  OPENGM_ASSERT(size_<=capacity_);
284  if(size>capacity_) {
285  if(size_!=0) {
286  T * tmp=new T[size];
287  std::copy(data_,data_+size_,tmp);
288  delete[] data_;
289  data_=tmp;
290  }
291  else{
292  data_=new T[size];
293  }
294  size_=size;
295  capacity_=size;
296  }
297  //size_=size;
298  OPENGM_ASSERT(size_<=capacity_);
299  }
300 
304  template<class T>
305  inline void
306  BufferVector<T>::clear() {
307  OPENGM_ASSERT(size_<=capacity_);
308  size_=0;
309  }
310 
312  template<class T>
313  inline bool
314  BufferVector<T>::empty()const{
315  return size_==0 ? true:false;
316  }
317 
321  template<class T>
322  template<class ITERATOR>
323  inline void
324  BufferVector<T>::assign(ITERATOR begin,ITERATOR end) {
325  this->resize(std::distance(begin,end));
326  std::copy(begin, end, data_);
327  }
328 
330  template<class T>
331  inline const T &
332  BufferVector<T>::back()const{
333  return data_[size_-1];
334  }
335 
337  template<class T>
338  inline T &
339  BufferVector<T>::back() {
340  return data_[size_-1];
341  }
342 
344  template<class T>
345  inline const T &
346  BufferVector<T>::front()const{
347  return data_[0];
348  }
349 
351  template<class T>
352  inline T &
353  BufferVector<T>::front() {
354  return data_[0];
355  }
356 }
357 
359 
360 #endif //OPENGM_BUFFER_VECTOR_HXX
The OpenGM namespace.
Definition: config.hxx:43
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77