Commit 410de38e authored by edwin.carlinet's avatar edwin.carlinet
Browse files

Some fixes about image expr with new style iterators.

parent f70ff984
......@@ -5,9 +5,8 @@
# include <mln/core/image/zip_image.hpp>
# include <boost/range/iterator_range.hpp>
# include <boost/iterator/transform_iterator.hpp>
# include <mln/core/range/iterator_range.hpp>
# include <mln/core/iterator/transform_iterator.hpp>
# include <mln/core/iterator/transform_pixel_iterator.hpp>
......@@ -136,12 +135,6 @@ namespace mln
template <typename Expr>
struct image_expr : Image<Expr>
{
......@@ -150,47 +143,7 @@ namespace mln
namespace internal
{
template <typename Pixel, typename F>
struct transformed_pixel
{
typedef typename std::result_of<F(typename Pixel::reference)> reference;
typedef typename std::remove_reference<reference>::type value_type;
typedef typename Pixel::image_type image_type;
typedef typename Pixel::point_type point_type;
typedef typename Pixel::site_type site_type;
transform_pixel(const Pixel& x, F fun)
: x_ (x), fun_ (fun)
{
}
auto point() const -> decltype(x_.point())
{
return x_.point();
}
auto site() const -> decltype(x_.point())
{
return x_.point();
}
reference val() const
{
return fun_(x_.val());
}
private:
const Pixel x_;
F fun_;
};
template <typename Pixel, typename F>
inline
transformed_pixel<Pixel, F>
make_transform_pixel(const Pixel& pix, const F& fun)
{
return transformed_pixel<Pixel, F>(pix, fun);
}
template <typename UnaryFunction, typename Image, typename E>
......@@ -209,34 +162,31 @@ namespace mln
typedef typename image_const_value_iterator<image_t>::type ConstVit;
typedef typename image_const_pixel_iterator<image_t>::type ConstPixit;
typedef std::bind2nd< make_transform_pixel<typename Pixit::value_type, UnaryFunction> > PixFun;
typedef std::bind2nd< make_transform_pixel<typename ConstPixit::value_type, UnaryFunction> > ConstPixFun;
public:
typedef typename image_t::point_type point_type;
typedef typename image_t::point_type site_type;
typedef typename image_t::domain_type domain_type;
typedef typename std::result_of< UnaryFunction(arg_t) >::type reference;
typedef typename std::result_of< const UnaryFunction(arg_t) >::type reference;
typedef typename std::result_of< const UnaryFunction(const_arg_t) >::type const_reference;
typedef typename std::decay<reference>::type value_type;
typedef typename std::remove_reference<reference>::type value_type;
typedef boost::transform_iterator< UnaryFunction, Vit > value_iterator;
typedef transformed_pixel_iterator< UnaryFunction, Pixit, self_t> pixel_iterator;
typedef transform_iterator< Vit, UnaryFunction > value_iterator;
typedef transform_pixel_iterator<UnaryFunction, Pixit, E> pixel_iterator;
typedef boost::transform_iterator< UnaryFunction, ConstVit, const_reference> const_value_iterator;
typedef transformed_pixel_iterator< UnaryFunction, ConstPixit, const self_t> const_pixel_iterator;
typedef transform_iterator< ConstVit, UnaryFunction> const_value_iterator;
typedef transform_pixel_iterator< UnaryFunction, ConstPixit, const E> const_pixel_iterator;
//typedef typename const_value_iterator::reference::empty empty;
//static_assert(std::is_const<typename std::remove_reference<typename const_value_iterator::reference>::type>::value, "");
typedef transformed_pixel<UnaryFunction, Pixit, E> pixel_type;
typedef transformed_pixel<UnaryFunction, ConstPixit, const E> const_pixel_type;
typedef typename std::iterator_traits<pixel_iterator>::value_type pixel_type;
typedef typename std::iterator_traits<const_pixel_iterator>::value_type const_pixel_type;
typedef boost::iterator_range<value_iterator> value_range;
typedef boost::iterator_range<pixel_iterator> pixel_range;
typedef boost::iterator_range<const_value_iterator> const_value_range;
typedef boost::iterator_range<const_pixel_iterator> const_pixel_range;
typedef iterator_range<value_iterator> value_range;
typedef iterator_range<pixel_iterator> pixel_range;
typedef iterator_range<const_value_iterator> const_value_range;
typedef iterator_range<const_pixel_iterator> const_pixel_range;
unary_image_expr_base(Image&& ima_, UnaryFunction f_):
ima (std::forward<Image>(ima_)),
......@@ -268,38 +218,30 @@ namespace mln
value_range
values()
{
auto rng = ima.values();
value_iterator a(std::begin(rng), f);
value_iterator b(std::end(rng), f);
return boost::make_iterator_range(a, b);
value_iterator x(ima.values().iter(), f);
return make_iterator_range(x);
}
const_value_range
values() const
{
auto rng = ima.values();
const_value_iterator a(std::begin(rng), f);
const_value_iterator b(std::end(rng), f);
return boost::make_iterator_range(a, b);
const_value_iterator x(ima.values().iter(), f);
return make_iterator_range(x);
}
pixel_range
pixels()
{
auto rng = ima.pixels();
pixel_iterator a(f, std::begin(rng), exact(this));
pixel_iterator b(f, std::end(rng), exact(this));
return boost::make_iterator_range(a, b);
pixel_iterator x(ima.pixels().iter(), f);
return make_iterator_range(x);
}
const_pixel_range
pixels() const
{
auto rng = ima.pixels();
const_pixel_iterator a(f, std::begin(rng), exact(this));
const_pixel_iterator b(f, std::end(rng), exact(this));
return boost::make_iterator_range(a, b);
const_pixel_iterator x(ima.pixels().iter(), f);
return make_iterator_range(x);
}
private:
......
......@@ -4,7 +4,7 @@
/// \FIXME Rename this file
# include <boost/tuple/tuple.hpp>
# include <boost/iterator/zip_iterator.hpp>
# include <mln/core/iterator/zip_iterator.hpp>
# include <mln/core/image_traits.hpp>
# include <mln/core/image/internal/nested_loop_iterator.hpp>
......@@ -74,8 +74,8 @@ namespace mln
{
typedef typename internal::tuple_meta_transform<ImageTuple, image_meta_value_range>::type ValueRangeTuple;
typedef typename internal::tuple_meta_transform<ImageTuple, image_meta_value_iterator>::type IteratorTuple;
typedef boost::zip_iterator<IteratorTuple> iterator;
typedef boost::zip_iterator<const IteratorTuple> const_iterator;
typedef zip_iterator<IteratorTuple> iterator;
typedef zip_iterator<const IteratorTuple> const_iterator;
zip_image_value_range(const ImageTuple& x)
: w (internal::tuple_transform(x, internal::get_image_value_range ()))
......@@ -103,7 +103,7 @@ namespace mln
typedef typename std::remove_reference<typename boost::tuples::element<0, ImageTuple>::type>::type first_image_t;
typedef typename first_image_t::domain_type Domain;
typedef zip_image_value_range<category, ImageTuple> ValueRange;
typedef typename Domain::const_iterator PointIterator;
typedef typename Domain::iterator PointIterator;
typedef typename ValueRange::iterator ValueIterator;
typedef typename ValueRange::const_iterator ConstValueIterator;
......
......@@ -10,8 +10,8 @@ namespace mln
template <typename Iterator>
struct stditerator : iterator_base< stditerator<Iterator>,
typename std::iterator_traits<Iterator>::reference,
typename std::iterator_traits<Iterator>::value_type >
typename std::iterator_traits<Iterator>::value_type,
typename std::iterator_traits<Iterator>::reference >
{
typedef typename std::iterator_traits<Iterator>::reference reference;
......
......@@ -2,7 +2,7 @@
# define TRANSFORMED_PIXEL_ITERATOR_HPP
# include <type_traits>
# include <boost/iterator/iterator_facade.hpp>
# include <mln/core/iterator/iterator_base.hpp>
namespace mln
{
......@@ -10,7 +10,6 @@ namespace mln
template <typename UnaryFunction, typename PixelIterator, typename Image>
struct transformed_pixel;
template <typename UnaryFunction, typename PixelIterator, typename Image>
struct transformed_pixel_iterator;
......@@ -19,107 +18,91 @@ namespace mln
/*** Implementation ****/
/***********************/
template <typename UnaryFunction, typename PixelIterator, typename Image>
struct transformed_pixel
{
private:
typedef typename std::iterator_traits<PixelIterator>::value_type Pixel;
typedef typename PixelIterator::value_type Pixel;
public:
typedef typename std::result_of< UnaryFunction(typename Pixel::reference) >::type reference;
typedef typename std::remove_cv<typename std::remove_reference<reference>::type>::type value_type;
typedef typename Pixel::point_type point_type;
typedef Image image_type;
typedef typename std::result_of<const UnaryFunction(typename Pixel::reference)>::type reference;
typedef typename std::remove_reference<reference>::type value_type;
typedef Image image_type;
typedef typename Pixel::point_type point_type;
typedef typename Pixel::site_type site_type;
transformed_pixel() = default;
transformed_pixel(const UnaryFunction& f, const PixelIterator& pix, Image* ima)
: f_ (f),
pixit_ (pix),
ima_ (ima)
transformed_pixel(const UnaryFunction& fun, const PixelIterator& x, Image& ima)
: x_ (x), fun_ (fun), ima_(&ima)
{
}
template <typename PixelIterator2, typename Image2>
transformed_pixel(const transformed_pixel<UnaryFunction, PixelIterator2, Image2>& other,
typename std::enable_if< std::is_convertible<PixelIterator2, PixelIterator>::value >::type* dummy1 = NULL,
typename std::enable_if< std::is_convertible<Image2*, Image*>::value >::type* dummy2 = NULL)
: f_ (other.f_),
pixit_ (other.pixit_),
ima_ (other.ima_)
typename std::enable_if< std::is_convertible<PixelIterator2, PixelIterator>::value and
std::is_convertible<Image2*, Image*>::value >::type* = NULL)
: x_ (other.x_), fun_ (other.fun_), ima_ (other.ima_)
{
}
point_type point() const
{
return x_->point();
}
point_type point() const
point_type site() const
{
return pixit_->point();
return x_->site();
}
reference val() const
reference val() const
{
return f(pixit_->val());
return fun_(x_->val());
}
image_type& image() const
{
mln_precondition(ima_ != NULL);
return *ima_;
}
template <typename F, typename PixelIterator2, typename Image2>
friend struct transformed_pixel;
friend struct transformed_pixel_iterator<UnaryFunction, PixelIterator, Image>;
private:
template <typename, typename, typename>
friend class transform_pixel_iterator;
template <typename, typename, typename>
friend class transformed_pixel;
private:
UnaryFunction f_;
PixelIterator pixit_;
Image* ima_;
PixelIterator x_;
UnaryFunction fun_;
Image* ima_;
};
template <typename UnaryFunction, typename PixelIterator, typename Image>
struct transformed_pixel_iterator : boost::iterator_facade< transformed_pixel_iterator<UnaryFunction, PixelIterator, Image>,
const transformed_pixel<UnaryFunction, PixelIterator, Image>,
typename boost::iterator_traversal<PixelIterator>::type
>
struct transform_pixel_iterator
: iterator_base< transform_pixel_iterator<UnaryFunction, PixelIterator, Image>,
const transformed_pixel<UnaryFunction, PixelIterator, Image> >
{
transformed_pixel_iterator() = default;
private:
typedef transformed_pixel<UnaryFunction, PixelIterator, Image> pixel_t;
transformed_pixel_iterator(const UnaryFunction& f, const PixelIterator& pix, Image* ima)
: pix_ (f, pix, ima)
{
}
public:
transform_pixel_iterator() = default;
template <typename PixelIterator2, typename Image2>
transformed_pixel_iterator(const transformed_pixel_iterator<UnaryFunction, PixelIterator2, Image2>& other,
typename std::enable_if< std::is_convertible<PixelIterator2, PixelIterator>::value >::type* dummy1 = NULL,
typename std::enable_if< std::is_convertible<Image2*, Image*>::value >::type* dummy2 = NULL)
: pix_ (other.pix_)
transform_pixel_iterator(const UnaryFunction& fun, const PixelIterator& x, Image& ima)
: px_(fun, x, ima)
{
}
private:
typedef transformed_pixel<UnaryFunction, PixelIterator, Image> Pixel;
friend class boost::iterator_core_access;
template <typename F, typename PixelIterator2, typename Image2>
friend class transformed_pixel_iterator;
void increment() { ++(pix_.pixit_); }
void decrement() { --(pix_.pixit_); }
const Pixel& dereference() const { return pix_; }
void init() { px_.x_.init(); }
void next() { px_.x_.next(); }
bool finished() const { return px_.x_.finished(); }
bool equal(const transformed_pixel_iterator& other) const
{
return other.pixit_ == this->pixit_;
}
const pixel_t& dereference() const { return px_; }
private:
Pixel pix_;
pixel_t px_;
};
}
......
......@@ -4,7 +4,7 @@
# include <type_traits>
# include <mln/core/iterator/iterator_base.hpp>
# include <boost/iterator/zip_iterator.hpp>
# include <boost/tuple.hpp>
# include <boost/tuple/tuple.hpp>
namespace mln
{
......@@ -19,15 +19,15 @@ namespace mln
namespace internal
{
using boost::tuple_impl_specific::tuple_meta_transform;
using boost::tuple_impl_specific::tuple_meta_accumulate;
using boost::tuple_impl_specific::tuple_transform;
using boost::tuple_impl_specific::tuple_for_each;
using boost::detail::tuple_impl_specific::tuple_meta_transform;
using boost::detail::tuple_impl_specific::tuple_meta_accumulate;
using boost::detail::tuple_impl_specific::tuple_transform;
using boost::detail::tuple_impl_specific::tuple_for_each;
struct iterator_dereference
{
template <typename Iterator>
struct apply { typedef typename std::remove_reference<Iterator>::type::reference type; }
struct apply { typedef typename std::remove_reference<Iterator>::type::reference type; };
template <typename Iterator>
typename Iterator::reference
......@@ -69,7 +69,7 @@ namespace mln
template <typename OtherIteratorTuple>
zip_iterator(const zip_iterator<OtherIteratorTuple>& other,
typedef std::enable_if< std::is_convertible<OtherIteratorTuple, IteratorTuple>::value >::type* = NULL)
typename std::enable_if< std::is_convertible<OtherIteratorTuple, IteratorTuple>::value >::type* = NULL)
: iterator_tuple_ (other.iterator_tuple_)
{
}
......@@ -77,12 +77,12 @@ namespace mln
void init()
{
internal::tuple_for_each(iterator_tuple_, iterator_init ());
internal::tuple_for_each(iterator_tuple_, internal::iterator_init ());
}
void next()
{
internal::tuple_for_each(iterator_tuple_, iterator_init ());
internal::tuple_for_each(iterator_tuple_, internal::iterator_init ());
}
bool finished() const
......@@ -92,7 +92,7 @@ namespace mln
reference dereference() const
{
return internal::tuple_transform(iterator_tuple_, internal::iterator_dereference) ();
return internal::tuple_transform(iterator_tuple_, internal::iterator_dereference ());
}
private:
......
......@@ -89,8 +89,9 @@ BOOST_AUTO_TEST_CASE(Operators)
iota(ima, 0);
int i = 0;
for (auto& v: ref.values())
v = i--;
mln_viter(v, ref);
mln_forall(v)
*v = i--;
BOOST_CHECK( boost::equal((-ima).values(), ref.values()) );
BOOST_CHECK( all(-ima == ref) );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment