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-2015 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 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 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 private:
306 
307  void increment_y();
308  void increment_z();
309 
310  C2DBounds m_pos;
311  C2DBounds m_size;
312  C2DBounds m_begin;
313  C2DBounds m_end;
314  int m_xstride;
315  I m_iterator;
316 };
317 
318 
319 
320 template <typename I>
321 template <typename AI>
323 {
324  m_pos = other.m_pos;
325  m_size = other.m_size;
326  m_begin = other.m_begin;
327  m_end = other.m_end;
328  m_iterator = other.m_iterator;
329  m_xstride = other.m_xstride;
330  return *this;
331 }
332 
333 template <typename I>
334 template <typename AI>
336  m_pos(other.m_pos),
337  m_size(other.m_size),
338  m_begin(other.m_begin),
339  m_end(other.m_end),
340  m_xstride(other.m_xstride),
341  m_iterator(other.m_iterator)
342 {
343 }
344 
345 
346 
347 
352 template <typename I>
353 bool operator == (const range2d_iterator<I>& left, const range2d_iterator<I>& right)
354 {
355  // we really want these two to the same range
356 // assert(left.m_size == right.m_size);
357 // assert(left.m_begin == right.m_begin);
358 // assert(left.m_end == right.m_end);
359 
360  return left.m_pos == right.m_pos;
361 
362 }
363 
367 template <typename I>
369 {
370  return !(a == b);
371 }
372 
374 
375 
376 namespace std {
377 
378 template <typename I>
379 class iterator_traits< mia::range2d_iterator<I> > {
380 public:
381  typedef typename I::difference_type difference_type;
382  typedef typename I::value_type value_type;
383  typedef typename I::pointer pointer;
384  typedef typename I::reference reference;
385  typedef forward_iterator_tag iterator_category;
386 };
387 
388 template <typename I>
389 class iterator_traits< mia::range2d_iterator_with_boundary_flag<I> > {
390 public:
391  typedef typename I::difference_type difference_type;
392  typedef typename I::value_type value_type;
393  typedef typename I::pointer pointer;
394  typedef typename I::reference reference;
395  typedef forward_iterator_tag iterator_category;
396 };
397 
398 }
399 
400 #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
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:43
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
range2d_iterator_with_boundary_flag< I > with_boundary_flag() const
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
const C2DBounds & pos() const
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
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:46