Generic Image Library: step_iterator.hpp Source File

###  | #  |

step_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_STEP_ITERATOR_HPP
9#define BOOST_GIL_STEP_ITERATOR_HPP
10
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>
15
16#include <boost/iterator/iterator_facade.hpp>
17
18#include <cstddef>
19#include <iterator>
20#include <type_traits>
21
22namespace boost { namespace gil {
23
27
28namespace detail {
29
36
37template <typename Derived, // type of the derived class
38 typename Iterator, // Models Iterator
39 typename SFn> // A policy object that can compute the distance between two iterators of type Iterator
40 // and can advance an iterator of type Iterator a given number of Iterator's units
41 class step_iterator_adaptor : public iterator_adaptor<Derived, Iterator, use_default, use_default, use_default, typename SFn::difference_type>
42{
43public:
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;
48
49 step_iterator_adaptor() {}
50 step_iterator_adaptor(Iterator const& it, SFn step_fn=SFn()) : parent_t(it), _step_fn(step_fn) {}
51
52 auto step() const -> difference_type { return _step_fn.step(); }
53
54protected:
55 SFn _step_fn;
56private:
57 friend class boost::iterator_core_access;
58
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); }
62
63 auto distance_to(step_iterator_adaptor const& it) const -> difference_type
64 {
65 return _step_fn.difference(this->base_reference(),it.base_reference());
66 }
67};
68
69// although iterator_adaptor defines these, the default implementation computes distance and compares for zero.
70// it is often faster to just apply the relation operator to the base
71template <typename D,typename Iterator,typename SFn> inline
72bool operator>(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
73 return p1.step()>0 ? p1.base()> p2.base() : p1.base()< p2.base();
74}
75
76template <typename D,typename Iterator,typename SFn> inline
77bool 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();
79}
80
81template <typename D,typename Iterator,typename SFn> inline
82bool 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();
84}
85
86template <typename D,typename Iterator,typename SFn> inline
87bool 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();
89}
90
91template <typename D,typename Iterator,typename SFn> inline
92bool operator==(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
93 return p1.base()==p2.base();
94}
95
96template <typename D,typename Iterator,typename SFn> inline
97bool operator!=(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
98 return p1.base()!=p2.base();
99}
100
101} // namespace detail
102
106
122
125template <typename Iterator>
126 struct memunit_step_fn {
127 using difference_type = std::ptrdiff_t;
128
129 memunit_step_fn(difference_type step=memunit_step(Iterator())) : _step(step) {}
130
131 auto difference(Iterator const& it1, Iterator const& it2) const -> difference_type
132 {
133 return memunit_distance(it1,it2)/_step;
134 }
135
136 void advance(Iterator& it, difference_type d) const { memunit_advance(it,d*_step); }
137 auto step() const -> difference_type { return _step; }
138
139 void set_step(std::ptrdiff_t step) { _step=step; }
140private:
141 BOOST_GIL_CLASS_REQUIRE(Iterator, boost::gil, MemoryBasedIteratorConcept)
142 difference_type _step;
143};
144
145template <typename Iterator>
146 class memory_based_step_iterator : public detail::step_iterator_adaptor<memory_based_step_iterator<Iterator>,
147 Iterator,
148 memunit_step_fn<Iterator>>
149{
150 BOOST_GIL_CLASS_REQUIRE(Iterator, boost::gil, MemoryBasedIteratorConcept)
151public:
152 using parent_t = detail::step_iterator_adaptor<memory_based_step_iterator<Iterator>,
153 Iterator,
154 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;
158
159 memory_based_step_iterator() : parent_t(Iterator()) {}
160 memory_based_step_iterator(Iterator it, std::ptrdiff_t memunit_step) : parent_t(it, memunit_step_fn<Iterator>(memunit_step)) {}
161 template <typename I2>
162 memory_based_step_iterator(const memory_based_step_iterator<I2>& it)
163 : parent_t(it.base(), memunit_step_fn<Iterator>(it.step())) {}
164
167 auto operator[](difference_type d) const -> reference { return *(*this+d); }
168
169 void set_step(std::ptrdiff_t memunit_step) { this->_step_fn.set_step(memunit_step); }
170
171 auto base() -> x_iterator& { return parent_t::base_reference(); }
172 auto base() const -> x_iterator const& { return parent_t::base_reference(); }
173};
174
175template <typename Iterator>
176struct const_iterator_type<memory_based_step_iterator<Iterator>> {
177 using type = memory_based_step_iterator<typename const_iterator_type<Iterator>::type>;
178};
179
180template <typename Iterator>
181struct iterator_is_mutable<memory_based_step_iterator<Iterator>> : public iterator_is_mutable<Iterator> {};
182
183
185// IteratorAdaptorConcept
187
188template <typename Iterator>
189struct is_iterator_adaptor<memory_based_step_iterator<Iterator>> : std::true_type {};
190
191template <typename Iterator>
192struct iterator_adaptor_get_base<memory_based_step_iterator<Iterator>>
193{
194 using type = Iterator;
195};
196
197template <typename Iterator, typename NewBaseIterator>
198struct iterator_adaptor_rebind<memory_based_step_iterator<Iterator>, NewBaseIterator>
199{
200 using type = memory_based_step_iterator<NewBaseIterator>;
201};
202
204// PixelBasedConcept
206
207template <typename Iterator>
208struct color_space_type<memory_based_step_iterator<Iterator>> : public color_space_type<Iterator> {};
209
210template <typename Iterator>
211struct channel_mapping_type<memory_based_step_iterator<Iterator>> : public channel_mapping_type<Iterator> {};
212
213template <typename Iterator>
214struct is_planar<memory_based_step_iterator<Iterator>> : public is_planar<Iterator> {};
215
216template <typename Iterator>
217struct channel_type<memory_based_step_iterator<Iterator>> : public channel_type<Iterator> {};
218
220// MemoryBasedIteratorConcept
222template <typename Iterator>
223struct byte_to_memunit<memory_based_step_iterator<Iterator>> : public byte_to_memunit<Iterator> {};
224
225template <typename Iterator>
226inline auto memunit_step(memory_based_step_iterator<Iterator> const& p) -> std::ptrdiff_t { return p.step(); }
227
228template <typename Iterator>
229inline auto memunit_distance(memory_based_step_iterator<Iterator> const& p1, memory_based_step_iterator<Iterator> const& p2)
230 -> std::ptrdiff_t
231{
232 return memunit_distance(p1.base(),p2.base());
233}
234
235template <typename Iterator>
236inline void memunit_advance(memory_based_step_iterator<Iterator>& p,
237 std::ptrdiff_t diff) {
238 memunit_advance(p.base(), diff);
239}
240
241template <typename Iterator>
242inline auto memunit_advanced(const memory_based_step_iterator<Iterator>& p, std::ptrdiff_t diff)
243 -> memory_based_step_iterator<Iterator>
244{
245 return memory_based_step_iterator<Iterator>(memunit_advanced(p.base(), diff),p.step());
246}
247
248template <typename Iterator>
249inline auto memunit_advanced_ref(const memory_based_step_iterator<Iterator>& p, std::ptrdiff_t diff)
250 -> typename std::iterator_traits<Iterator>::reference
251{
252 return memunit_advanced_ref(p.base(), diff);
253}
254
256// HasDynamicXStepTypeConcept
258
259template <typename Iterator>
260struct dynamic_x_step_type<memory_based_step_iterator<Iterator>> {
261 using type = memory_based_step_iterator<Iterator>;
262};
263
264// For step iterators, pass the function object to the base
265template <typename Iterator, typename Deref>
266struct iterator_add_deref<memory_based_step_iterator<Iterator>,Deref> {
267 BOOST_GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
268
269 using type = memory_based_step_iterator<typename iterator_add_deref<Iterator, Deref>::type>;
270
271 static type make(const memory_based_step_iterator<Iterator>& it, const Deref& d)
272 {
273 return type(iterator_add_deref<Iterator, Deref>::make(it.base(),d),it.step());
274 }
275};
276
280
281template <typename I> typename dynamic_x_step_type<I>::type make_step_iterator(const I& it, std::ptrdiff_t step);
282
283namespace detail {
284
285// if the iterator is a plain base iterator (non-adaptor), wraps it in memory_based_step_iterator
286template <typename I>
287auto make_step_iterator_impl(I const& it, std::ptrdiff_t step, std::false_type)
288 -> typename dynamic_x_step_type<I>::type
289{
290 return memory_based_step_iterator<I>(it, step);
291}
292
293// If the iterator is compound, put the step in its base
294template <typename I>
295auto make_step_iterator_impl(I const& it, std::ptrdiff_t step, std::true_type)
296 -> typename dynamic_x_step_type<I>::type
297{
298 return make_step_iterator(it.base(), step);
299}
300
301// If the iterator is memory_based_step_iterator, change the step
302template <typename BaseIt>
303auto make_step_iterator_impl(
304 memory_based_step_iterator<BaseIt> const& it,
305 std::ptrdiff_t step,
306 std::true_type)
307 -> memory_based_step_iterator<BaseIt>
308{
309 return memory_based_step_iterator<BaseIt>(it.base(), step);
310}
311
312} // namespace detail
313
327template <typename I> // Models MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept
328inline auto make_step_iterator(I const& it, std::ptrdiff_t step)
329 -> typename dynamic_x_step_type<I>::type
330{
331 return detail::make_step_iterator_impl(it, step, typename is_iterator_adaptor<I>::type());
332}
333
334}} // namespace boost::gil
335
336#endif
boost::gil::detail::step_iterator_adaptor
An adaptor over an existing iterator that changes the step unit.
Definition step_iterator.hpp:42
boost::gil::memory_based_step_iterator
MEMORY-BASED STEP ITERATOR.
Definition step_iterator.hpp:149
boost::gil::memory_based_step_iterator::operator[]
auto operator[](difference_type d) const -> reference
Definition step_iterator.hpp:167
boost
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition algorithm.hpp:36
boost::gil::MemoryBasedIteratorConcept
Concept of a random-access iterator that can be advanced in memory units (bytes or bits)
Definition concepts/pixel_iterator.hpp:237
boost::gil::memunit_step_fn
function object that returns the memory unit distance between two iterators and advances a given iter...
Definition step_iterator.hpp:126