OpenGM  2.3.x
Discrete Graphical Model Library
accessor_iterator.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_ACCESSOR_ITERATOR
3 #define OPENGM_ACCESSOR_ITERATOR
4 
5 #include <cstddef>
6 #include <iterator>
7 
8 #include "opengm/opengm.hxx"
10 
11 namespace opengm {
12 
14 
61 template<class A, bool isConst = false>
62 class AccessorIterator {
63 public:
64  typedef A Accessor;
65  typedef typename Accessor::value_type value_type;
66  typedef typename meta::If<isConst, const value_type*, value_type*>::type pointer;
67  typedef typename meta::If
68  <
69  isConst,
70  typename meta::If
71  <
72  meta::IsFundamental<value_type>::value,
73  const value_type,
74  const value_type&
75  >::type,
76  value_type&
77  >::type reference;
78  typedef size_t difference_type;
79  typedef std::random_access_iterator_tag iterator_category;
80 
81  AccessorIterator(const Accessor& = Accessor(), const size_t = 0);
82 
83  template<bool isConstLocal>
84  bool operator==(const AccessorIterator<A, isConstLocal>&) const;
85  template<bool isConstLocal>
86  bool operator!=(const AccessorIterator<A, isConstLocal>&) const;
87  template<bool isConstLocal>
88  bool operator<=(const AccessorIterator<A, isConstLocal>&) const;
89  template<bool isConstLocal>
90  bool operator>=(const AccessorIterator<A, isConstLocal>&) const;
91  template<bool isConstLocal>
92  bool operator<(const AccessorIterator<A, isConstLocal>&) const;
93  template<bool isConstLocal>
94  bool operator>(const AccessorIterator<A, isConstLocal>&) const;
95 
96  pointer operator->();
97  const value_type* operator->() const;
98  reference operator*();
99  const value_type& operator*() const;
100  reference operator[](const size_t);
101  const value_type& operator[](const size_t) const;
102 
103  AccessorIterator<A, isConst>& operator++();
104  AccessorIterator<A, isConst> operator++(int);
105  AccessorIterator<A, isConst>& operator--();
106  AccessorIterator<A, isConst> operator--(int);
107  AccessorIterator<A, isConst> operator+=(const size_t);
108  AccessorIterator<A, isConst> operator-=(const size_t);
109 
110  AccessorIterator<A, isConst> operator+(const size_t) const;
111  AccessorIterator<A, isConst> operator-(const size_t) const;
112  template<bool isConstLocal>
113  size_t operator-(const AccessorIterator<A, isConstLocal>&) const;
114 
115 private:
116  void testInvariant() const;
117 
118  Accessor accessor_;
119  size_t index_;
120 };
121 
122 // binary arithmetic operators
123 template<class A, bool isConst>
124  AccessorIterator<A, isConst> operator+(const size_t, const AccessorIterator<A, isConst>&);
125 
126 // implementation of AccessorIterator
127 
128 template<class A, bool isConst>
129 inline
130 AccessorIterator<A, isConst>::AccessorIterator
131 (
132  const typename AccessorIterator<A, isConst>::Accessor& accessor,
133  const size_t index
134 )
135 : accessor_(accessor),
136  index_(index)
137 {}
138 
139 template<class A, bool isConst>
140 template<bool isConstLocal>
141 inline bool
142 AccessorIterator<A, isConst>::operator==
143 (
144  const AccessorIterator<A, isConstLocal>& it
145 ) const
146 {
147  OPENGM_ASSERT(it.accessor_ == accessor_);
148  return it.index_ == index_;
149 }
150 
151 template<class A, bool isConst>
152 template<bool isConstLocal>
153 inline bool
154 AccessorIterator<A, isConst>::operator!=
155 (
156  const AccessorIterator<A, isConstLocal>& it
157 ) const
158 {
159  OPENGM_ASSERT(it.accessor_ == accessor_);
160  return it.index_ != index_;
161 }
162 
163 template<class A, bool isConst>
164 template<bool isConstLocal>
165 inline bool
166 AccessorIterator<A, isConst>::operator<=
167 (
168  const AccessorIterator<A, isConstLocal>& it
169 ) const
170 {
171  OPENGM_ASSERT(it.accessor_ == accessor_);
172  return index_ <= it.index_;
173 }
174 
175 template<class A, bool isConst>
176 template<bool isConstLocal>
177 inline bool
178 AccessorIterator<A, isConst>::operator>=
179 (
180  const AccessorIterator<A, isConstLocal>& it
181 ) const
182 {
183  OPENGM_ASSERT(it.accessor_ == accessor_);
184  return index_ >= it.index_;
185 }
186 
187 template<class A, bool isConst>
188 template<bool isConstLocal>
189 inline bool
190 AccessorIterator<A, isConst>::operator<
191 (
192  const AccessorIterator<A, isConstLocal>& it
193 ) const
194 {
195  OPENGM_ASSERT(it.accessor_ == accessor_);
196  return index_ < it.index_;
197 }
198 
199 template<class A, bool isConst>
200 template<bool isConstLocal>
201 inline bool
202 AccessorIterator<A, isConst>::operator>
203 (
204  const AccessorIterator<A, isConstLocal>& it
205 ) const
206 {
207  OPENGM_ASSERT(it.accessor_ == accessor_);
208  return index_ > it.index_;
209 }
210 
211 template<class A, bool isConst>
212 inline typename AccessorIterator<A, isConst>::pointer
213 AccessorIterator<A, isConst>::operator->()
214 {
215  OPENGM_ASSERT(index_ < accessor_.size());
216  return &accessor_[index_]; // whether this works depends on the accessor
217 }
218 
219 template<class A, bool isConst>
220 inline const typename AccessorIterator<A, isConst>::value_type*
221 AccessorIterator<A, isConst>::operator->() const
222 {
223  OPENGM_ASSERT(index_ < accessor_.size());
224  return &accessor_[index_]; // whether this works depends on the accessor
225 }
226 
227 template<class A, bool isConst>
228 inline const typename AccessorIterator<A, isConst>::value_type&
230 {
231  OPENGM_ASSERT(index_ < accessor_.size());
232  return accessor_[index_];
233 }
234 
235 template<class A, bool isConst>
236 inline typename AccessorIterator<A, isConst>::reference
238 {
239  OPENGM_ASSERT(index_ < accessor_.size());
240  return accessor_[index_];
241 }
242 
243 template<class A, bool isConst>
244 inline const typename AccessorIterator<A, isConst>::value_type&
245 AccessorIterator<A, isConst>::operator[]
246 (
247  const size_t j
248 ) const
249 {
250  OPENGM_ASSERT(index_ + j < accessor_.size());
251  return accessor_[index_ + j];
252 }
253 
254 template<class A, bool isConst>
255 inline typename AccessorIterator<A, isConst>::reference
256 AccessorIterator<A, isConst>::operator[]
257 (
258  const size_t j
259 )
260 {
261  OPENGM_ASSERT(index_ + j < accessor_.size());
262  return accessor_[index_ + j];
263 }
264 
265 template<class A, bool isConst>
266 inline AccessorIterator<A, isConst>&
268 {
269  if(index_ < accessor_.size()) {
270  ++index_;
271  }
272  testInvariant();
273  return *this;
274 }
275 
276 template<class A, bool isConst>
277 inline AccessorIterator<A, isConst>
279 {
280  if(index_ < accessor_.size()) {
281  AccessorIterator it = *this; // copy
282  ++index_;
283  testInvariant();
284  return it;
285  }
286  else {
287  return *this;
288  }
289 }
290 
291 template<class A, bool isConst>
292 inline AccessorIterator<A, isConst>&
294 {
295  OPENGM_ASSERT(index_ > 0);
296  --index_;
297  testInvariant();
298  return *this;
299 }
300 
301 template<class A, bool isConst>
302 inline AccessorIterator<A, isConst>
304 {
305  OPENGM_ASSERT(index_ > 0);
306  AccessorIterator it = *this; // copy
307  --index_;
308  testInvariant();
309  return it;
310 }
311 
312 template<class A, bool isConst>
313 inline AccessorIterator<A, isConst>
314 AccessorIterator<A, isConst>::operator+=
315 (
316  const size_t j
317 )
318 {
319  if(index_ + j <= accessor_.size()) {
320  index_ += j;
321  }
322  else {
323  index_ = accessor_.size();
324  }
325  testInvariant();
326  return *this;
327 }
328 
329 template<class A, bool isConst>
330 inline AccessorIterator<A, isConst>
331 AccessorIterator<A, isConst>::operator-=
332 (
333  const size_t j
334 )
335 {
336  OPENGM_ASSERT(index_ >= j);
337  index_ -= j;
338  testInvariant();
339  return *this;
340 }
341 
342 template<class A, bool isConst>
343 inline void
344 AccessorIterator<A, isConst>::testInvariant() const
345 {
346  OPENGM_ASSERT(index_ <= accessor_.size());
347 }
348 
349 template<class A, bool isConst>
350 inline AccessorIterator<A, isConst>
351 AccessorIterator<A, isConst>::operator+
352 (
353  const size_t j
354 ) const
355 {
356  AccessorIterator<A, isConst> it = *this; // copy
357  it += j;
358  return it;
359 }
360 
361 template<class A, bool isConst>
362 inline AccessorIterator<A, isConst>
363 AccessorIterator<A, isConst>::operator-
364 (
365  const size_t j
366 ) const
367 {
368  AccessorIterator<A, isConst> it = *this; // copy
369  it -= j;
370  return it;
371 }
372 
373 template<class A, bool isConst>
374 template<bool isConstLocal>
375 inline size_t
376 AccessorIterator<A, isConst>::operator-
377 (
378  const AccessorIterator<A, isConstLocal>& it
379 ) const
380 {
381  typedef std::ptrdiff_t difference_type;
382  OPENGM_ASSERT(this->accessor_ == it.accessor_);
383  return static_cast<difference_type>(index_) - static_cast<difference_type>(it.index_);
384 }
385 
386 // implementation of binary arithmetic operators
387 
388 template<class A, bool isConst>
389 inline AccessorIterator<A, isConst>
390 operator+
391 (
392  const size_t j,
393  const AccessorIterator<A, isConst>& it
394 )
395 {
396  return it + j;
397 }
398 
400 
401 } // namespace opengm
402 
403 #endif // #ifndef OPENGM_ACCESSOR_ITERATOR
404 
bool operator>=(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
The OpenGM namespace.
Definition: config.hxx:43
IndependentFactor< T, I, L > & operator-=(IndependentFactor< T, I, L > &op1, const T &op2)
IndependentFactor< T, I, L > operator*(const IndependentFactor< T, I, L > &op1, const IndependentFactor< T, I, L > &op2)
bool operator!=(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
bool operator==(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
IndependentFactor< T, I, L > operator-(const IndependentFactor< T, I, L > &op1, const IndependentFactor< T, I, L > &op2)
const BinaryViewExpression< E1, T1, E2, T2, marray_detail::Times< T1, T2, typename marray_detail::PromoteType< T1, T2 >::type > > operator*(const ViewExpression< E1, T1 > &expression1, const ViewExpression< E2, T2 > &expression2)
Definition: marray.hxx:3251
bool operator>(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
View< T, false, A > & operator--(View< T, false, A > &v)
Definition: marray.hxx:3120
IndependentFactor< T, I, L > & operator+=(IndependentFactor< T, I, L > &op1, const T &op2)
IndependentFactor< T, I, L > operator+(const IndependentFactor< T, I, L > &op1, const IndependentFactor< T, I, L > &op2)
View< T, false, A > & operator++(View< T, false, A > &v)
Definition: marray.hxx:3082