8 #ifndef BOOST_GIL_STEP_ITERATOR_HPP
9 #define BOOST_GIL_STEP_ITERATOR_HPP
11 #include <boost/gil/dynamic_step.hpp>
12 #include <boost/gil/pixel_iterator.hpp>
13 #include <boost/gil/pixel_iterator_adaptor.hpp>
14 #include <boost/gil/utilities.hpp>
16 #include <boost/iterator/iterator_facade.hpp>
20 #include <type_traits>
22 namespace boost {
namespace gil {
37 template <
typename Derived,
41 class step_iterator_adaptor :
public iterator_adaptor<Derived, Iterator, use_default, use_default, use_default, typename SFn::difference_type>
44 using parent_t = iterator_adaptor<Derived, Iterator, use_default, use_default, use_default, typename SFn::difference_type>;
45 using base_difference_type =
typename std::iterator_traits<Iterator>::difference_type;
46 using difference_type =
typename SFn::difference_type;
47 using reference =
typename std::iterator_traits<Iterator>::reference;
52 auto step()
const -> difference_type {
return _step_fn.step(); }
57 friend class boost::iterator_core_access;
59 void increment() { _step_fn.advance(this->base_reference(),1); }
60 void decrement() { _step_fn.advance(this->base_reference(),-1); }
61 void advance(base_difference_type d) { _step_fn.advance(this->base_reference(),d); }
65 return _step_fn.difference(this->base_reference(),it.base_reference());
71 template <
typename D,
typename Iterator,
typename SFn>
inline
73 return p1.step()>0 ? p1.base()> p2.base() : p1.base()< p2.base();
76 template <
typename D,
typename Iterator,
typename SFn>
inline
77 bool operator<(
const step_iterator_adaptor<D,Iterator,SFn>& p1,
const step_iterator_adaptor<D,Iterator,SFn>& p2) {
78 return p1.step()>0 ? p1.base()< p2.base() : p1.base()> p2.base();
81 template <
typename D,
typename Iterator,
typename SFn>
inline
82 bool operator>=(
const step_iterator_adaptor<D,Iterator,SFn>& p1,
const step_iterator_adaptor<D,Iterator,SFn>& p2) {
83 return p1.step()>0 ? p1.base()>=p2.base() : p1.base()<=p2.base();
86 template <
typename D,
typename Iterator,
typename SFn>
inline
87 bool operator<=(
const step_iterator_adaptor<D,Iterator,SFn>& p1,
const step_iterator_adaptor<D,Iterator,SFn>& p2) {
88 return p1.step()>0 ? p1.base()<=p2.base() : p1.base()>=p2.base();
91 template <
typename D,
typename Iterator,
typename SFn>
inline
92 bool operator==(
const step_iterator_adaptor<D,Iterator,SFn>& p1,
const step_iterator_adaptor<D,Iterator,SFn>& p2) {
93 return p1.base()==p2.base();
96 template <
typename D,
typename Iterator,
typename SFn>
inline
97 bool operator!=(
const step_iterator_adaptor<D,Iterator,SFn>& p1,
const step_iterator_adaptor<D,Iterator,SFn>& p2) {
98 return p1.base()!=p2.base();
125 template <
typename Iterator>
127 using difference_type = std::ptrdiff_t;
129 memunit_step_fn(difference_type step=memunit_step(Iterator())) : _step(step) {}
131 auto difference(Iterator
const& it1, Iterator
const& it2)
const -> difference_type
133 return memunit_distance(it1,it2)/_step;
136 void advance(Iterator& it, difference_type d)
const { memunit_advance(it,d*_step); }
137 auto step()
const -> difference_type {
return _step; }
139 void set_step(std::ptrdiff_t step) { _step=step; }
142 difference_type _step;
145 template <
typename Iterator>
148 memunit_step_fn<Iterator>>
155 using reference =
typename parent_t::reference;
156 using difference_type =
typename parent_t::difference_type;
157 using x_iterator = Iterator;
161 template <
typename I2>
167 auto operator[](difference_type d)
const -> reference {
return *(*
this+d); }
169 void set_step(std::ptrdiff_t memunit_step) { this->_step_fn.set_step(memunit_step); }
171 auto base() -> x_iterator& {
return parent_t::base_reference(); }
172 auto base() const -> x_iterator const& {
return parent_t::base_reference(); }
175 template <
typename Iterator>
176 struct const_iterator_type<memory_based_step_iterator<Iterator>> {
177 using type = memory_based_step_iterator<typename const_iterator_type<Iterator>::type>;
180 template <
typename Iterator>
181 struct iterator_is_mutable<memory_based_step_iterator<Iterator>> :
public iterator_is_mutable<Iterator> {};
188 template <
typename Iterator>
189 struct is_iterator_adaptor<memory_based_step_iterator<Iterator>> : std::true_type {};
191 template <
typename Iterator>
192 struct iterator_adaptor_get_base<memory_based_step_iterator<Iterator>>
194 using type = Iterator;
197 template <
typename Iterator,
typename NewBaseIterator>
198 struct iterator_adaptor_rebind<memory_based_step_iterator<Iterator>, NewBaseIterator>
200 using type = memory_based_step_iterator<NewBaseIterator>;
207 template <
typename Iterator>
208 struct color_space_type<memory_based_step_iterator<Iterator>> :
public color_space_type<Iterator> {};
210 template <
typename Iterator>
211 struct channel_mapping_type<memory_based_step_iterator<Iterator>> :
public channel_mapping_type<Iterator> {};
213 template <
typename Iterator>
214 struct is_planar<memory_based_step_iterator<Iterator>> :
public is_planar<Iterator> {};
216 template <
typename Iterator>
217 struct channel_type<memory_based_step_iterator<Iterator>> :
public channel_type<Iterator> {};
222 template <
typename Iterator>
223 struct byte_to_memunit<memory_based_step_iterator<Iterator>> :
public byte_to_memunit<Iterator> {};
225 template <
typename Iterator>
226 inline auto memunit_step(memory_based_step_iterator<Iterator>
const& p) -> std::ptrdiff_t {
return p.step(); }
228 template <
typename Iterator>
229 inline auto memunit_distance(memory_based_step_iterator<Iterator>
const& p1, memory_based_step_iterator<Iterator>
const& p2)
232 return memunit_distance(p1.base(),p2.base());
235 template <
typename Iterator>
236 inline void memunit_advance(memory_based_step_iterator<Iterator>& p,
237 std::ptrdiff_t diff) {
238 memunit_advance(p.base(), diff);
241 template <
typename Iterator>
242 inline auto memunit_advanced(
const memory_based_step_iterator<Iterator>& p, std::ptrdiff_t diff)
243 -> memory_based_step_iterator<Iterator>
245 return memory_based_step_iterator<Iterator>(memunit_advanced(p.base(), diff),p.step());
248 template <
typename Iterator>
249 inline auto memunit_advanced_ref(
const memory_based_step_iterator<Iterator>& p, std::ptrdiff_t diff)
250 ->
typename std::iterator_traits<Iterator>::reference
252 return memunit_advanced_ref(p.base(), diff);
259 template <
typename Iterator>
260 struct dynamic_x_step_type<memory_based_step_iterator<Iterator>> {
261 using type = memory_based_step_iterator<Iterator>;
265 template <
typename Iterator,
typename Deref>
266 struct iterator_add_deref<memory_based_step_iterator<Iterator>,Deref> {
267 BOOST_GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
269 using type = memory_based_step_iterator<typename iterator_add_deref<Iterator, Deref>::type>;
271 static type make(
const memory_based_step_iterator<Iterator>& it,
const Deref& d)
273 return type(iterator_add_deref<Iterator, Deref>::make(it.base(),d),it.step());
281 template <
typename I>
typename dynamic_x_step_type<I>::type make_step_iterator(
const I& it, std::ptrdiff_t step);
286 template <
typename I>
287 auto make_step_iterator_impl(I
const& it, std::ptrdiff_t step, std::false_type)
288 ->
typename dynamic_x_step_type<I>::type
290 return memory_based_step_iterator<I>(it, step);
294 template <
typename I>
295 auto make_step_iterator_impl(I
const& it, std::ptrdiff_t step, std::true_type)
296 ->
typename dynamic_x_step_type<I>::type
298 return make_step_iterator(it.base(), step);
302 template <
typename BaseIt>
303 auto make_step_iterator_impl(
304 memory_based_step_iterator<BaseIt>
const& it,
307 -> memory_based_step_iterator<BaseIt>
309 return memory_based_step_iterator<BaseIt>(it.base(), step);
327 template <
typename I>
328 inline auto make_step_iterator(I
const& it, std::ptrdiff_t step)
329 ->
typename dynamic_x_step_type<I>::type
331 return detail::make_step_iterator_impl(it, step,
typename is_iterator_adaptor<I>::type());
An adaptor over an existing iterator that changes the step unit.
Definition: step_iterator.hpp:42
MEMORY-BASED STEP ITERATOR.
Definition: step_iterator.hpp:149
auto operator[](difference_type d) const -> reference
Definition: step_iterator.hpp:167
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
Concept of a random-access iterator that can be advanced in memory units (bytes or bits)
Definition: concepts/pixel_iterator.hpp:237
function object that returns the memory unit distance between two iterators and advances a given iter...
Definition: step_iterator.hpp:126