2d/iterator.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef mia_2d_iterator_hh
22 #define mia_2d_iterator_hh
23 
24 #include <mia/2d/vector.hh>
25 
27 
41 template <typename I>
42 class EXPORT_2D range2d_iterator_with_boundary_flag: public std::forward_iterator_tag {
43 public:
45  typedef typename I::reference reference;
47  typedef typename I::pointer pointer;
49  typedef I internal_iterator;
50 
51  typedef typename I::value_type value_type;
52 
60  enum EBoundary {
61  eb_none = 0,
62  eb_xlow = 1,
63  eb_xhigh = 2,
64  eb_x = 3,
65  eb_ylow = 4,
66  eb_yhigh = 8,
67  eb_y = 0xC,
68  };
69 
70 
71 
74 
84  const C2DBounds& start, const C2DBounds& end, I iterator);
85 
92 
95 
98 
100  template <typename AI>
102 
110  template <typename AI>
112 
113 
119  template <typename AI>
121 
122 
127 
129  reference operator *() const;
130 
132  pointer operator ->() const;
133 
137  const C2DBounds& pos() const;
138 
140  template <typename T> friend
143 
144  template <typename T> friend
148 
152  internal_iterator get_point();
153 
155  int get_boundary_flags() const;
156 
157 private:
158 
159  void increment_y();
160  void increment_z();
161 
162  C2DBounds m_pos;
163  C2DBounds m_size;
164  C2DBounds m_begin;
165  C2DBounds m_end;
166  int m_xstride;
167  I m_iterator;
168  int m_boundary;
169 };
170 
171 
172 
173 template <typename I>
174 template <typename AI>
176 {
177  m_pos = other.m_pos;
178  m_size = other.m_size;
179  m_begin = other.m_begin;
180  m_end = other.m_end;
181  m_iterator = other.m_iterator;
182  m_xstride = other.m_xstride;
183  m_boundary = other.m_boundary;
184  return *this;
185 }
186 
187 template <typename I>
188 template <typename AI>
190  m_pos(other.m_pos),
191  m_size(other.m_size),
192  m_begin(other.m_begin),
193  m_end(other.m_end),
194  m_xstride(other.m_xstride),
195  m_iterator(other.m_iterator),
196  m_boundary(other.m_boundary)
197 {
198 }
199 
200 template <typename T>
202 {
203  return left.m_pos == right.m_pos;
204 }
205 
206 template <typename T>
208 {
209  return left.m_pos != right.m_pos;
210 }
211 
212 
213 template <typename I>
214 class EXPORT_2D range2d_iterator: public std::iterator<std::forward_iterator_tag, typename I::value_type>
215 {
216 public:
218  typedef typename I::reference reference;
220  typedef typename I::pointer pointer;
222  typedef I internal_iterator;
223 
224  typedef typename I::value_type value_type;
225 
227  range2d_iterator();
228 
237  range2d_iterator(const C2DBounds& pos, const C2DBounds& size,
238  const C2DBounds& start, const C2DBounds& end, I iterator);
239 
246 
249 
251  range2d_iterator(const range2d_iterator<I>& other);
252 
254  template <typename AI>
255  friend class range2d_iterator;
256 
264  template <typename AI>
265  range2d_iterator(const range2d_iterator<AI>& other);
266 
267 
273  template <typename AI>
275 
276 
281 
283  reference operator *() const;
284 
286  pointer operator ->() const;
287 
291  const C2DBounds& pos() const;
292 
294  template <typename T> friend
295  bool operator == (const range2d_iterator<T>& left, const range2d_iterator<T>& right);
297 
301  internal_iterator get_point();
302 
303 
305 
309  const reference operator[] (int i) const;
310 
311 private:
312 
313  void increment_y();
314  void increment_z();
315 
316  C2DBounds m_pos;
317  C2DBounds m_size;
318  C2DBounds m_begin;
319  C2DBounds m_end;
320  int m_xstride;
321  I m_iterator;
322 };
323 
324 
325 
326 template <typename I>
327 template <typename AI>
329 {
330  m_pos = other.m_pos;
331  m_size = other.m_size;
332  m_begin = other.m_begin;
333  m_end = other.m_end;
334  m_iterator = other.m_iterator;
335  m_xstride = other.m_xstride;
336  return *this;
337 }
338 
339 template <typename I>
340 template <typename AI>
342  m_pos(other.m_pos),
343  m_size(other.m_size),
344  m_begin(other.m_begin),
345  m_end(other.m_end),
346  m_xstride(other.m_xstride),
347  m_iterator(other.m_iterator)
348 {
349 }
350 
351 
352 
353 
358 template <typename I>
359 bool operator == (const range2d_iterator<I>& left, const range2d_iterator<I>& right)
360 {
361  // we really want these two to the same range
362 // assert(left.m_size == right.m_size);
363 // assert(left.m_begin == right.m_begin);
364 // assert(left.m_end == right.m_end);
365 
366  return left.m_pos == right.m_pos;
367 
368 }
369 
373 template <typename I>
375 {
376  return !(a == b);
377 }
378 
379 template <typename I>
380 const typename range2d_iterator<I>::reference
382 {
383  return m_iterator[i];
384 }
385 
386 
388 
389 
390 namespace std {
391 
392 template <typename I>
393 class iterator_traits< mia::range2d_iterator<I> > {
394 public:
395  typedef typename I::difference_type difference_type;
396  typedef typename I::value_type value_type;
397  typedef typename I::pointer pointer;
398  typedef typename I::reference reference;
399  typedef forward_iterator_tag iterator_category;
400 };
401 
402 template <typename I>
403 class iterator_traits< mia::range2d_iterator_with_boundary_flag<I> > {
404 public:
405  typedef typename I::difference_type difference_type;
406  typedef typename I::value_type value_type;
407  typedef typename I::pointer pointer;
408  typedef typename I::reference reference;
409  typedef forward_iterator_tag iterator_category;
410 };
411 
412 }
413 
414 #endif
I::reference reference
data type reference
Definition: 2d/iterator.hh:45
I::pointer pointer
data type pointer
Definition: 2d/iterator.hh:220
I::pointer pointer
data type pointer
Definition: 2d/iterator.hh:47
I internal_iterator
data type for the real iterator in the background
Definition: 2d/iterator.hh:49
const C2DBounds & pos() const
const reference operator[](int i) const
Definition: 2d/iterator.hh:381
a 2D iterator that knows its position in the 2D grid ans supports iterating over sub-ranges ...
Definition: 2d/iterator.hh:42
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
bool operator!=(const range2d_iterator_with_boundary_flag< T > &left, const range2d_iterator_with_boundary_flag< T > &right)
Definition: 2d/iterator.hh:207
range2d_iterator_with_boundary_flag< I > & operator=(const range2d_iterator_with_boundary_flag< I > &other)
assignment operator
#define EXPORT_2D
Definition: defines2d.hh:37
bool operator==(const range2d_iterator_with_boundary_flag< T > &left, const range2d_iterator_with_boundary_flag< T > &right)
Definition: 2d/iterator.hh:201
I internal_iterator
data type for the real iterator in the background
Definition: 2d/iterator.hh:222
range2d_iterator_with_boundary_flag< I > & operator++()
prefix increment
I::reference reference
data type reference
Definition: 2d/iterator.hh:218
range2d_iterator< I > & operator=(const range2d_iterator< I > &other)
assignment operator
I::value_type value_type
Definition: 2d/iterator.hh:224
Matrix EXPORT_GSL operator*(const Matrix &lhs, const Matrix &rhs)
range2d_iterator_with_boundary_flag< I > with_boundary_flag() const
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36