Boost GIL


position_iterator.hpp
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #ifndef BOOST_GIL_POSITION_ITERATOR_HPP
9 #define BOOST_GIL_POSITION_ITERATOR_HPP
10 
11 #include <boost/gil/locator.hpp>
12 
13 #include <boost/iterator/iterator_facade.hpp>
14 
15 #include <type_traits>
16 
17 namespace boost { namespace gil {
18 
23 
30 template <typename Deref, int Dim>
31 struct position_iterator : public iterator_facade<position_iterator<Deref,Dim>,
32  typename Deref::value_type,
33  std::random_access_iterator_tag,
34  typename Deref::reference,
35  typename Deref::argument_type::template axis<Dim>::coord_t> {
36  using parent_t = iterator_facade<position_iterator<Deref,Dim>,
37  typename Deref::value_type,
38  std::random_access_iterator_tag,
39  typename Deref::reference,
40  typename Deref::argument_type::template axis<Dim>::coord_t>;
41  using difference_type = typename parent_t::difference_type;
42  using reference = typename parent_t::reference;
43  using point_t = typename Deref::argument_type;
44 
46  position_iterator(point_t const& p, point_t const& step, Deref const& d) : _p(p), _step(step), _d(d) {}
47 
48  position_iterator(position_iterator const& p) : _p(p._p), _step(p._step), _d(p._d) {}
49 
50  template <typename D>
51  position_iterator(position_iterator<D,Dim> const& p) : _p(p._p), _step(p._step), _d(p._d) {}
52 
53  auto operator=(position_iterator const& p) -> position_iterator&
54  {
55  _p=p._p;
56  _d=p._d;
57  _step=p._step;
58  return *this;
59  }
60 
61  auto pos() const -> point_t const& { return _p; }
62  auto step() const -> point_t const& { return _step; }
63  auto deref_fn() const -> Deref const& { return _d; }
64 
65  void set_step(difference_type s) { _step[Dim]=s; }
68  auto operator[](difference_type d) const -> reference
69  {
70  point_t p=_p;
71  p[Dim]+=d*_step[Dim];
72  return _d(p);
73  }
74 
75 private:
76  point_t _p, _step;
77  Deref _d;
78 
79  template <typename DE, int DI> friend struct position_iterator;
80  friend class boost::iterator_core_access;
81  reference dereference() const { return _d(_p); }
82  void increment() { _p[Dim]+=_step[Dim]; }
83  void decrement() { _p[Dim]-=_step[Dim]; }
84  void advance(difference_type d) { _p[Dim]+=d*_step[Dim]; }
85 
86  difference_type distance_to(const position_iterator& it) const { return (it._p[Dim]-_p[Dim])/_step[Dim]; }
87  bool equal(const position_iterator& it) const { return _p==it._p; }
88 };
89 
90 template <typename Deref,int Dim>
91 struct const_iterator_type<position_iterator<Deref,Dim> > {
92  using type = position_iterator<typename Deref::const_t,Dim>;
93 };
94 
95 template <typename Deref, int Dim>
96 struct iterator_is_mutable<position_iterator<Deref, Dim>>
97  : std::integral_constant<bool, Deref::is_mutable>
98 {
99 };
100 
102 // PixelBasedConcept
104 
105 template <typename Deref,int Dim>
106 struct color_space_type<position_iterator<Deref,Dim> > : public color_space_type<typename Deref::value_type> {};
107 
108 template <typename Deref,int Dim>
109 struct channel_mapping_type<position_iterator<Deref,Dim> > : public channel_mapping_type<typename Deref::value_type> {};
110 
111 template <typename Deref,int Dim>
112 struct is_planar<position_iterator<Deref, Dim>> : std::false_type {};
113 
114 template <typename Deref,int Dim>
115 struct channel_type<position_iterator<Deref,Dim> > : public channel_type<typename Deref::value_type> {};
116 
118 // HasDynamicXStepTypeConcept
120 
121 template <typename Deref,int Dim>
122 struct dynamic_x_step_type<position_iterator<Deref,Dim> > {
123  using type = position_iterator<Deref,Dim>;
124 };
125 
126 } } // namespace boost::gil
127 
128 #endif
BOOST_FORCEINLINE bool equal(boost::gil::iterator_from_2d< Loc1 > first, boost::gil::iterator_from_2d< Loc1 > last, boost::gil::iterator_from_2d< Loc2 > first2)
std::equal(I1,I1,I2) with I1 and I2 being a iterator_from_2d
Definition: algorithm.hpp:1087
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
An iterator that remembers its current X,Y position and invokes a function object with it upon derefe...
Definition: position_iterator.hpp:35
auto operator[](difference_type d) const -> reference
Definition: position_iterator.hpp:68