###

#

step_iterator.hpp
//
// Copyright 2005-2007 Adobe Systems Incorporated
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_STEP_ITERATOR_HPP
#define BOOST_GIL_STEP_ITERATOR_HPP
#include <boost/gil/dynamic_step.hpp>
#include <boost/gil/pixel_iterator.hpp>
#include <boost/gil/pixel_iterator_adaptor.hpp>
#include <boost/gil/utilities.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <cstddef>
#include <iterator>
#include <type_traits>
namespace boost { namespace gil {
namespace detail {
template <typename Derived, // type of the derived class
typename Iterator, // Models Iterator
typename SFn> // A policy object that can compute the distance between two iterators of type Iterator
// and can advance an iterator of type Iterator a given number of Iterator's units
class step_iterator_adaptor : public iterator_adaptor<Derived, Iterator, use_default, use_default, use_default, typename SFn::difference_type>
{
public:
using parent_t = iterator_adaptor<Derived, Iterator, use_default, use_default, use_default, typename SFn::difference_type>;
using base_difference_type = typename std::iterator_traits<Iterator>::difference_type;
using difference_type = typename SFn::difference_type;
using reference = typename std::iterator_traits<Iterator>::reference;
step_iterator_adaptor() {}
step_iterator_adaptor(Iterator const& it, SFn step_fn=SFn()) : parent_t(it), _step_fn(step_fn) {}
auto step() const -> difference_type { return _step_fn.step(); }
protected:
SFn _step_fn;
private:
friend class boost::iterator_core_access;
void increment() { _step_fn.advance(this->base_reference(),1); }
void decrement() { _step_fn.advance(this->base_reference(),-1); }
void advance(base_difference_type d) { _step_fn.advance(this->base_reference(),d); }
auto distance_to(step_iterator_adaptor const& it) const -> difference_type
{
return _step_fn.difference(this->base_reference(),it.base_reference());
}
};
// although iterator_adaptor defines these, the default implementation computes distance and compares for zero.
// it is often faster to just apply the relation operator to the base
template <typename D,typename Iterator,typename SFn> inline
bool operator>(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
return p1.step()>0 ? p1.base()> p2.base() : p1.base()< p2.base();
}
template <typename D,typename Iterator,typename SFn> inline
bool operator<(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
return p1.step()>0 ? p1.base()< p2.base() : p1.base()> p2.base();
}
template <typename D,typename Iterator,typename SFn> inline
bool operator>=(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
return p1.step()>0 ? p1.base()>=p2.base() : p1.base()<=p2.base();
}
template <typename D,typename Iterator,typename SFn> inline
bool operator<=(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
return p1.step()>0 ? p1.base()<=p2.base() : p1.base()>=p2.base();
}
template <typename D,typename Iterator,typename SFn> inline
bool operator==(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
return p1.base()==p2.base();
}
template <typename D,typename Iterator,typename SFn> inline
bool operator!=(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
return p1.base()!=p2.base();
}
} // namespace detail
template <typename Iterator>
struct memunit_step_fn {
using difference_type = std::ptrdiff_t;
memunit_step_fn(difference_type step=memunit_step(Iterator())) : _step(step) {}
auto difference(Iterator const& it1, Iterator const& it2) const -> difference_type
{
return memunit_distance(it1,it2)/_step;
}
void advance(Iterator& it, difference_type d) const { memunit_advance(it,d*_step); }
auto step() const -> difference_type { return _step; }
void set_step(std::ptrdiff_t step) { _step=step; }
private:
BOOST_GIL_CLASS_REQUIRE(Iterator, boost::gil, MemoryBasedIteratorConcept)
difference_type _step;
};
template <typename Iterator>
class memory_based_step_iterator : public detail::step_iterator_adaptor<memory_based_step_iterator<Iterator>,
Iterator,
memunit_step_fn<Iterator>>
{
BOOST_GIL_CLASS_REQUIRE(Iterator, boost::gil, MemoryBasedIteratorConcept)
public:
using parent_t = detail::step_iterator_adaptor<memory_based_step_iterator<Iterator>,
Iterator,
memunit_step_fn<Iterator>>;
using reference = typename parent_t::reference;
using difference_type = typename parent_t::difference_type;
using x_iterator = Iterator;
memory_based_step_iterator() : parent_t(Iterator()) {}
memory_based_step_iterator(Iterator it, std::ptrdiff_t memunit_step) : parent_t(it, memunit_step_fn<Iterator>(memunit_step)) {}
template <typename I2>
memory_based_step_iterator(const memory_based_step_iterator<I2>& it)
: parent_t(it.base(), memunit_step_fn<Iterator>(it.step())) {}
auto operator[](difference_type d) const -> reference { return *(*this+d); }
void set_step(std::ptrdiff_t memunit_step) { this->_step_fn.set_step(memunit_step); }
auto base() -> x_iterator& { return parent_t::base_reference(); }
auto base() const -> x_iterator const& { return parent_t::base_reference(); }
};
template <typename Iterator>
struct const_iterator_type<memory_based_step_iterator<Iterator>> {
using type = memory_based_step_iterator<typename const_iterator_type<Iterator>::type>;
};
template <typename Iterator>
struct iterator_is_mutable<memory_based_step_iterator<Iterator>> : public iterator_is_mutable<Iterator> {};
// IteratorAdaptorConcept
template <typename Iterator>
struct is_iterator_adaptor<memory_based_step_iterator<Iterator>> : std::true_type {};
template <typename Iterator>
struct iterator_adaptor_get_base<memory_based_step_iterator<Iterator>>
{
using type = Iterator;
};
template <typename Iterator, typename NewBaseIterator>
struct iterator_adaptor_rebind<memory_based_step_iterator<Iterator>, NewBaseIterator>
{
using type = memory_based_step_iterator<NewBaseIterator>;
};
// PixelBasedConcept
template <typename Iterator>
struct color_space_type<memory_based_step_iterator<Iterator>> : public color_space_type<Iterator> {};
template <typename Iterator>
struct channel_mapping_type<memory_based_step_iterator<Iterator>> : public channel_mapping_type<Iterator> {};
template <typename Iterator>
struct is_planar<memory_based_step_iterator<Iterator>> : public is_planar<Iterator> {};
template <typename Iterator>
struct channel_type<memory_based_step_iterator<Iterator>> : public channel_type<Iterator> {};
// MemoryBasedIteratorConcept
template <typename Iterator>
struct byte_to_memunit<memory_based_step_iterator<Iterator>> : public byte_to_memunit<Iterator> {};
template <typename Iterator>
inline auto memunit_step(memory_based_step_iterator<Iterator> const& p) -> std::ptrdiff_t { return p.step(); }
template <typename Iterator>
inline auto memunit_distance(memory_based_step_iterator<Iterator> const& p1, memory_based_step_iterator<Iterator> const& p2)
-> std::ptrdiff_t
{
return memunit_distance(p1.base(),p2.base());
}
template <typename Iterator>
inline void memunit_advance(memory_based_step_iterator<Iterator>& p,
std::ptrdiff_t diff) {
memunit_advance(p.base(), diff);
}
template <typename Iterator>
inline auto memunit_advanced(const memory_based_step_iterator<Iterator>& p, std::ptrdiff_t diff)
-> memory_based_step_iterator<Iterator>
{
return memory_based_step_iterator<Iterator>(memunit_advanced(p.base(), diff),p.step());
}
template <typename Iterator>
inline auto memunit_advanced_ref(const memory_based_step_iterator<Iterator>& p, std::ptrdiff_t diff)
-> typename std::iterator_traits<Iterator>::reference
{
return memunit_advanced_ref(p.base(), diff);
}
// HasDynamicXStepTypeConcept
template <typename Iterator>
struct dynamic_x_step_type<memory_based_step_iterator<Iterator>> {
using type = memory_based_step_iterator<Iterator>;
};
// For step iterators, pass the function object to the base
template <typename Iterator, typename Deref>
struct iterator_add_deref<memory_based_step_iterator<Iterator>,Deref> {
BOOST_GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
using type = memory_based_step_iterator<typename iterator_add_deref<Iterator, Deref>::type>;
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());
}
};
template <typename I> typename dynamic_x_step_type<I>::type make_step_iterator(const I& it, std::ptrdiff_t step);
namespace detail {
// if the iterator is a plain base iterator (non-adaptor), wraps it in memory_based_step_iterator
template <typename I>
auto make_step_iterator_impl(I const& it, std::ptrdiff_t step, std::false_type)
-> typename dynamic_x_step_type<I>::type
{
return memory_based_step_iterator<I>(it, step);
}
// If the iterator is compound, put the step in its base
template <typename I>
auto make_step_iterator_impl(I const& it, std::ptrdiff_t step, std::true_type)
-> typename dynamic_x_step_type<I>::type
{
return make_step_iterator(it.base(), step);
}
// If the iterator is memory_based_step_iterator, change the step
template <typename BaseIt>
auto make_step_iterator_impl(
memory_based_step_iterator<BaseIt> const& it,
std::ptrdiff_t step,
std::true_type)
-> memory_based_step_iterator<BaseIt>
{
return memory_based_step_iterator<BaseIt>(it.base(), step);
}
} // namespace detail
template <typename I> // Models MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept
inline auto make_step_iterator(I const& it, std::ptrdiff_t step)
-> typename dynamic_x_step_type<I>::type
{
return detail::make_step_iterator_impl(it, step, typename is_iterator_adaptor<I>::type());
}
}} // namespace boost::gil
#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