OpenGM  2.3.x
Discrete Graphical Model Library
fast_sequence.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_FAST_SEQUENCE_HXX
3 #define OPENGM_FAST_SEQUENCE_HXX
4 
5 #include <vector>
6 #include <algorithm>
7 
8 #include "opengm/opengm.hxx"
9 
10 namespace opengm {
11 
20 template<class T, size_t MAX_STACK=opengm::USUAL_MAX_FACTOR_ORDER>
22 public:
23  typedef T ValueType;
24  typedef T value_type;
25  typedef T const * ConstIteratorType;
26  typedef T const * const_iterator;
27  typedef T* IteratorType;
28  typedef T* iterator;
29 
30  FastSequence( );
31  FastSequence(const size_t );
32  FastSequence(const size_t , const T & );
34  ~FastSequence( );
36  template<class ITERATOR> void assign(ITERATOR , ITERATOR);
37 
38  size_t size() const ;
39  T const * begin() const;
40  T const * end() const;
41  T* const begin();
42  T* const end();
43  T const & operator[](const size_t) const;
44  T& operator[](const size_t);
45  void push_back(const T &);
46  void resize(const size_t );
47  void reserve(const size_t );
48  void clear();
49  bool empty() const;
50  const T& front() const;
51  const T& back() const;
52  T& front();
53  T& back();
54 
55 private:
56  size_t size_;
57  size_t capacity_;
58  T stackSequence_[MAX_STACK];
59  T* pointerToSequence_;
60 };
61 
63 template<class T, size_t MAX_STACK>
65 : size_(0),
66  capacity_(MAX_STACK),
67  pointerToSequence_(stackSequence_) {
68 
69 }
70 
73 template<class T, size_t MAX_STACK>
75 (
76  const size_t size
77 )
78 : size_(size),
79  capacity_(size>MAX_STACK?size:MAX_STACK) {
80  OPENGM_ASSERT(size_<=capacity_);
81  OPENGM_ASSERT(capacity_>=MAX_STACK);
82  if(size_>MAX_STACK) {
83  pointerToSequence_ = new T[size];
84  }
85  else{
86  pointerToSequence_=stackSequence_;
87  }
88 }
89 
93 template<class T, size_t MAX_STACK>
95 (
96  const size_t size,
97  const T & value
98 )
99 : size_(size),
100  capacity_(size>MAX_STACK?size:MAX_STACK) {
101  OPENGM_ASSERT(size_<=capacity_);
102  OPENGM_ASSERT(capacity_>=MAX_STACK);
103  if(size_>MAX_STACK) {
104  pointerToSequence_ = new T[size_];
105  }
106  else{
107  pointerToSequence_=stackSequence_;
108  }
109  std::fill(pointerToSequence_, pointerToSequence_+size_, value);
110 }
111 
114 template<class T, size_t MAX_STACK>
116 (
117  const FastSequence<T, MAX_STACK> & other
118 )
119 : size_(other.size_),
120  capacity_(other.capacity_)
121 {
122  OPENGM_ASSERT(size_<=capacity_);
123  OPENGM_ASSERT(capacity_>=MAX_STACK);
124  if(size_>MAX_STACK) {
125  pointerToSequence_ = new T[size_];
126  }
127  else{
128  pointerToSequence_=stackSequence_;
129  }
130  std::copy(other.pointerToSequence_, other.pointerToSequence_+size_, pointerToSequence_);
131 }
132 
134 template<class T, size_t MAX_STACK>
136  if(size_>MAX_STACK) {
137  OPENGM_ASSERT(pointerToSequence_!=NULL);
138  delete[] pointerToSequence_;
139  }
140 }
141 
144 template<class T, size_t MAX_STACK>
146 (
147  const FastSequence<T, MAX_STACK> & other
148 )
149 {
150  if(&other != this) {
151  // size_=other.size_;
152  // capacity_=other.capacity_;
153  // OPENGM_ASSERT(size_<=capacity_);
154  // OPENGM_ASSERT(capacity_>=MAX_STACK);
155  if(other.size_>MAX_STACK) {
156  // delete old sequence
157  if(size_>MAX_STACK) {
158  delete [] pointerToSequence_;
159  pointerToSequence_ = new T[other.size_];
160  }
161  // nothing to delete
162  else{
163  pointerToSequence_ = new T[other.size_];
164  }
165  size_=other.size_;
166  capacity_=size_;
167  }
168  else{
169  pointerToSequence_=stackSequence_;
170  size_=other.size_;
171  }
172  std::copy(other.pointerToSequence_, other.pointerToSequence_+size_, pointerToSequence_);
173  }
174  return *this;
175 }
176 
178 template<class T, size_t MAX_STACK>
179 inline size_t
181  OPENGM_ASSERT(pointerToSequence_!=NULL ||size_== 0 );
182  return size_;
183 }
184 
186 template<class T, size_t MAX_STACK>
187 inline T const *
189  OPENGM_ASSERT(pointerToSequence_!=NULL);
190  return pointerToSequence_;
191 }
192 
194 template<class T, size_t MAX_STACK>
195 inline T const *
197  OPENGM_ASSERT(pointerToSequence_!=NULL);
198  return pointerToSequence_ + size_;
199 }
200 
202 template<class T, size_t MAX_STACK>
203 inline T * const
205  OPENGM_ASSERT(pointerToSequence_!=NULL);
206  return pointerToSequence_;
207 }
208 
210 template<class T, size_t MAX_STACK>
211 inline T * const
213  OPENGM_ASSERT(pointerToSequence_!=NULL);
214  return pointerToSequence_ + size_;
215 }
216 
218 template<class T, size_t MAX_STACK>
219 inline T const &
220 FastSequence<T, MAX_STACK>::operator[]
221 (
222  const size_t index
223 ) const {
224  OPENGM_ASSERT(pointerToSequence_!=NULL);
225  OPENGM_ASSERT(index<size_);
226  return pointerToSequence_[index];
227 }
228 
230 template<class T, size_t MAX_STACK>
231 inline T &
232 FastSequence<T, MAX_STACK>::operator[]
233 (
234  const size_t index
235 ) {
236  OPENGM_ASSERT(index<size_);
237  return pointerToSequence_[index];
238 }
239 
242 template<class T, size_t MAX_STACK>
243 inline void
245 (
246  const T & value
247 ) {
248  OPENGM_ASSERT(capacity_ >= MAX_STACK);
249  OPENGM_ASSERT(size_ <= capacity_);
250  if(capacity_ == size_) {
251  T * tmp=new T[capacity_ * 2];
252  std::copy(pointerToSequence_, pointerToSequence_ + size_, tmp);
253  if(capacity_ > MAX_STACK) {
254  delete[] pointerToSequence_;
255  }
256  capacity_ *= 2;
257  pointerToSequence_ = tmp;
258  }
259  pointerToSequence_[size_]=value;
260  ++size_;
261  OPENGM_ASSERT(size_<=capacity_);
262  OPENGM_ASSERT(capacity_>=MAX_STACK);
263 }
264 
267 template<class T, size_t MAX_STACK>
268 inline void
270 (
271  const size_t size
272 ) {
273  OPENGM_ASSERT(capacity_>=MAX_STACK);
274  OPENGM_ASSERT(size_<=capacity_);
275  if(size>capacity_) {
276  T * tmp=new T[size];
277  std::copy(pointerToSequence_, pointerToSequence_+size_, tmp);
278  if(capacity_>MAX_STACK) {
279  delete[] pointerToSequence_;
280  }
281  capacity_=size;
282  pointerToSequence_=tmp;
283  }
284  size_=size;
285  OPENGM_ASSERT(size_<=capacity_);
286  OPENGM_ASSERT(capacity_>=MAX_STACK);
287 }
288 
291 template<class T, size_t MAX_STACK>
292 inline void
294 (
295  const size_t size
296 ) {
297  OPENGM_ASSERT(capacity_>=MAX_STACK);
298  OPENGM_ASSERT(size_<=capacity_);
299  if(size>capacity_) {
300  T * tmp=new T[size];
301  std::copy(pointerToSequence_, pointerToSequence_+size_, tmp);
302  if(capacity_>MAX_STACK) {
303  delete[] pointerToSequence_;
304  }
305  capacity_=size;
306  pointerToSequence_=tmp;
307  }
308  // size_ = size;
309  OPENGM_ASSERT(size_<=capacity_);
310  OPENGM_ASSERT(capacity_>=MAX_STACK);
311 }
312 
314 template<class T, size_t MAX_STACK>
315 inline void
317  OPENGM_ASSERT(capacity_>=MAX_STACK);
318  OPENGM_ASSERT(size_<=capacity_);
319  if(capacity_>MAX_STACK) {
320  delete[] pointerToSequence_;
321  }
322  pointerToSequence_=stackSequence_;
323  capacity_=MAX_STACK;
324  size_=0;
325  OPENGM_ASSERT(size_<=capacity_);
326  OPENGM_ASSERT(capacity_>=MAX_STACK);
327 }
328 
330 template<class T, size_t MAX_STACK>
331 inline bool
333  return (size_ == 0);
334 }
335 
339 template<class T, size_t MAX_STACK>
340 template<class ITERATOR>
341 inline void
342 FastSequence<T, MAX_STACK>::assign(ITERATOR begin, ITERATOR end) {
343  this->resize(std::distance(begin, end));
344  std::copy(begin, end, pointerToSequence_);
345 }
346 
348 template<class T, size_t MAX_STACK>
349 inline const T &
351  return pointerToSequence_[size_-1];
352 }
353 
355 template<class T, size_t MAX_STACK>
356 inline T &
358  return pointerToSequence_[size_-1];
359 }
360 
362 template<class T, size_t MAX_STACK>
363 inline const T &
365  return pointerToSequence_[0];
366 }
367 
369 template<class T, size_t MAX_STACK>
370 inline T &
372  return pointerToSequence_[0];
373 }
374 
375 } // namespace opengm
376 
377 #endif // OPENGM_FAST_SEQUENCE_HXX
FastSequence()
constructor
The OpenGM namespace.
Definition: config.hxx:43
void assign(ITERATOR, ITERATOR)
assign values
size_t size() const
size
~FastSequence()
destructor
const T & front() const
reference to the first entry
T const & operator[](const size_t) const
access entries
void push_back(const T &)
append a value
Vector that stores values on the stack if size is smaller than MAX_STACK.
T const * end() const
end iterator
void clear()
clear the sequence
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
void reserve(const size_t)
reserve memory
bool empty() const
query if the sequence is empty
void resize(const size_t)
resize the sequence
T const * begin() const
begin iterator
FastSequence< T, MAX_STACK > & operator=(const FastSequence< T, MAX_STACK > &)
assignment operator
const T & back() const
reference to the last entry