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 difference_type step()
const {
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); }
62 difference_type distance_to(
const step_iterator_adaptor& it)
const {
return _step_fn.difference(this->base_reference(),it.base_reference()); }
67 template <
typename D,
typename Iterator,
typename SFn>
inline 69 return p1.step()>0 ? p1.base()> p2.base() : p1.base()< p2.base();
72 template <
typename D,
typename Iterator,
typename SFn>
inline 73 bool operator<(
const step_iterator_adaptor<D,Iterator,SFn>& p1,
const step_iterator_adaptor<D,Iterator,SFn>& p2) {
74 return p1.step()>0 ? p1.base()< p2.base() : p1.base()> p2.base();
77 template <
typename D,
typename Iterator,
typename SFn>
inline 78 bool operator>=(
const step_iterator_adaptor<D,Iterator,SFn>& p1,
const step_iterator_adaptor<D,Iterator,SFn>& p2) {
79 return p1.step()>0 ? p1.base()>=p2.base() : p1.base()<=p2.base();
82 template <
typename D,
typename Iterator,
typename SFn>
inline 83 bool operator<=(
const step_iterator_adaptor<D,Iterator,SFn>& p1,
const step_iterator_adaptor<D,Iterator,SFn>& p2) {
84 return p1.step()>0 ? p1.base()<=p2.base() : p1.base()>=p2.base();
87 template <
typename D,
typename Iterator,
typename SFn>
inline 88 bool operator==(
const step_iterator_adaptor<D,Iterator,SFn>& p1,
const step_iterator_adaptor<D,Iterator,SFn>& p2) {
89 return p1.base()==p2.base();
92 template <
typename D,
typename Iterator,
typename SFn>
inline 93 bool operator!=(
const step_iterator_adaptor<D,Iterator,SFn>& p1,
const step_iterator_adaptor<D,Iterator,SFn>& p2) {
94 return p1.base()!=p2.base();
121 template <
typename Iterator>
123 using difference_type = std::ptrdiff_t;
125 memunit_step_fn(difference_type step=memunit_step(Iterator())) : _step(step) {}
127 difference_type difference(
const Iterator& it1,
const Iterator& it2)
const {
return memunit_distance(it1,it2)/_step; }
128 void advance(Iterator& it, difference_type d)
const { memunit_advance(it,d*_step); }
129 difference_type step()
const {
return _step; }
131 void set_step(std::ptrdiff_t step) { _step=step; }
134 difference_type _step;
137 template <
typename Iterator>
140 memunit_step_fn<Iterator>>
147 using reference =
typename parent_t::reference;
148 using difference_type =
typename parent_t::difference_type;
149 using x_iterator = Iterator;
152 memory_based_step_iterator(Iterator it, std::ptrdiff_t memunit_step) : parent_t(it, memunit_step_fn<Iterator>(memunit_step)) {}
153 template <
typename I2>
154 memory_based_step_iterator(
const memory_based_step_iterator<I2>& it)
155 : parent_t(it.base(), memunit_step_fn<Iterator>(it.step())) {}
159 reference
operator[](difference_type d)
const {
return *(*
this+d); }
161 void set_step(std::ptrdiff_t memunit_step) { this->_step_fn.set_step(memunit_step); }
163 x_iterator& base() {
return parent_t::base_reference(); }
164 x_iterator
const& base()
const {
return parent_t::base_reference(); }
167 template <
typename Iterator>
168 struct const_iterator_type<memory_based_step_iterator<Iterator>> {
169 using type = memory_based_step_iterator<typename const_iterator_type<Iterator>::type>;
172 template <
typename Iterator>
173 struct iterator_is_mutable<memory_based_step_iterator<Iterator>> :
public iterator_is_mutable<Iterator> {};
180 template <
typename Iterator>
181 struct is_iterator_adaptor<memory_based_step_iterator<Iterator>> : std::true_type {};
183 template <
typename Iterator>
184 struct iterator_adaptor_get_base<memory_based_step_iterator<Iterator>>
186 using type = Iterator;
189 template <
typename Iterator,
typename NewBaseIterator>
190 struct iterator_adaptor_rebind<memory_based_step_iterator<Iterator>, NewBaseIterator>
192 using type = memory_based_step_iterator<NewBaseIterator>;
199 template <
typename Iterator>
200 struct color_space_type<memory_based_step_iterator<Iterator>> :
public color_space_type<Iterator> {};
202 template <
typename Iterator>
203 struct channel_mapping_type<memory_based_step_iterator<Iterator>> :
public channel_mapping_type<Iterator> {};
205 template <
typename Iterator>
206 struct is_planar<memory_based_step_iterator<Iterator>> :
public is_planar<Iterator> {};
208 template <
typename Iterator>
209 struct channel_type<memory_based_step_iterator<Iterator>> :
public channel_type<Iterator> {};
214 template <
typename Iterator>
215 struct byte_to_memunit<memory_based_step_iterator<Iterator>> :
public byte_to_memunit<Iterator> {};
217 template <
typename Iterator>
218 inline std::ptrdiff_t memunit_step(
const memory_based_step_iterator<Iterator>& p) {
return p.step(); }
220 template <
typename Iterator>
221 inline std::ptrdiff_t memunit_distance(
const memory_based_step_iterator<Iterator>& p1,
222 const memory_based_step_iterator<Iterator>& p2) {
223 return memunit_distance(p1.base(),p2.base());
226 template <
typename Iterator>
227 inline void memunit_advance(memory_based_step_iterator<Iterator>& p,
228 std::ptrdiff_t diff) {
229 memunit_advance(p.base(), diff);
232 template <
typename Iterator>
233 inline memory_based_step_iterator<Iterator>
234 memunit_advanced(
const memory_based_step_iterator<Iterator>& p,
235 std::ptrdiff_t diff) {
236 return memory_based_step_iterator<Iterator>(memunit_advanced(p.base(), diff),p.step());
239 template <
typename Iterator>
240 inline typename std::iterator_traits<Iterator>::reference
241 memunit_advanced_ref(
const memory_based_step_iterator<Iterator>& p,
242 std::ptrdiff_t diff) {
243 return memunit_advanced_ref(p.base(), diff);
250 template <
typename Iterator>
251 struct dynamic_x_step_type<memory_based_step_iterator<Iterator>> {
252 using type = memory_based_step_iterator<Iterator>;
256 template <
typename Iterator,
typename Deref>
257 struct iterator_add_deref<memory_based_step_iterator<Iterator>,Deref> {
258 BOOST_GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
260 using type = memory_based_step_iterator<typename iterator_add_deref<Iterator, Deref>::type>;
262 static type make(
const memory_based_step_iterator<Iterator>& it,
const Deref& d) {
return type(iterator_add_deref<Iterator, Deref>::make(it.base(),d),it.step()); }
269 template <
typename I>
typename dynamic_x_step_type<I>::type make_step_iterator(
const I& it, std::ptrdiff_t step);
274 template <
typename I>
275 auto make_step_iterator_impl(I
const& it, std::ptrdiff_t step, std::false_type)
276 ->
typename dynamic_x_step_type<I>::type
278 return memory_based_step_iterator<I>(it, step);
282 template <
typename I>
283 auto make_step_iterator_impl(I
const& it, std::ptrdiff_t step, std::true_type)
284 ->
typename dynamic_x_step_type<I>::type
286 return make_step_iterator(it.base(), step);
290 template <
typename BaseIt>
291 auto make_step_iterator_impl(
292 memory_based_step_iterator<BaseIt>
const& it,
295 -> memory_based_step_iterator<BaseIt>
297 return memory_based_step_iterator<BaseIt>(it.base(), step);
315 template <
typename I>
316 typename dynamic_x_step_type<I>::type make_step_iterator(
const I& it, std::ptrdiff_t step) {
317 return detail::make_step_iterator_impl(it, step,
typename is_iterator_adaptor<I>::type());
Concept of a random-access iterator that can be advanced in memory units (bytes or bits)
Definition: concepts/pixel_iterator.hpp:236
reference operator[](difference_type d) const
Definition: step_iterator.hpp:159
function object that returns the memory unit distance between two iterators and advances a given iter...
Definition: step_iterator.hpp:122
An adaptor over an existing iterator that changes the step unit.
Definition: step_iterator.hpp:41
MEMORY-BASED STEP ITERATOR.
Definition: algorithm.hpp:36