3062: Handle data mutability in pw::image.

https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> Handle data mutability in pw::image. * mln/trait/images.hh: Upgrade doc style. * mln/core/site_set/p_array.hh (operator int, operator unsigned): New in p_indexed_psite. * mln/core/concept/function.hh (is_mutable): New. * mln/fun/l2l/relabel.hh: Likewise. * mln/fun/i2v/array.hh: Likewise. * mln/fun/internal/array_base.hh: Likewise. * mln/pw/image.hh (value_io, pw_io): Allow mutability. * tests/pw/image.cc: Add a test on a mutable image. mln/core/concept/function.hh | 1 + mln/core/site_set/p_array.hh | 18 ++++++++++++++++++ mln/fun/i2v/array.hh | 4 ++++ mln/fun/internal/array_base.hh | 1 + mln/fun/l2l/relabel.hh | 3 +-- mln/pw/image.hh | 26 ++++++++++++++++++-------- mln/trait/images.hh | 18 +++++++++--------- tests/pw/image.cc | 19 ++++++++++++++++++- 8 files changed, 70 insertions(+), 20 deletions(-) Index: mln/trait/images.hh --- mln/trait/images.hh (revision 3061) +++ mln/trait/images.hh (working copy) @@ -1,4 +1,5 @@ // Copyright (C) 2007, 2008 EPITA Research and Development Laboratory +// (LRDE) // // This file is part of the Olena Library. This library is free // software; you can redistribute it and/or modify it under the terms @@ -28,14 +29,13 @@ #ifndef MLN_TRAIT_IMAGES_HH # define MLN_TRAIT_IMAGES_HH -/*! \file mln/trait/images.hh - * - * \brief Some base trait types for images. - * - * \todo Split this file into many. - * - * \todo the 'nature' prop is not set yet in image types. - */ +/// \file mln/trait/images.hh +/// +/// Some base trait types for images. +/// +/// \todo Split this file into many. +/// +/// \todo the 'nature' prop is not set yet in image types. # include <iostream> # include <string> @@ -113,7 +113,7 @@ template <typename T> struct image1d; template <typename T> struct image2d; template <typename T> struct image3d; - namespace pw { template <typename F, typename S> struct image; } + namespace pw { template <typename F, typename S> class image; } template <typename P, typename T> class rle_image; template <typename P, typename T> class sparse_image; Index: mln/core/site_set/p_array.hh --- mln/core/site_set/p_array.hh (revision 3061) +++ mln/core/site_set/p_array.hh (working copy) @@ -1,4 +1,5 @@ // Copyright (C) 2007, 2008 EPITA Research and Development Laboratory +// (LRDE) // // This file is part of the Olena Library. This library is free // software; you can redistribute it and/or modify it under the terms @@ -206,6 +207,8 @@ bool is_valid() const; operator util::index() const; + operator int() const; // To interoperate, e.g., with fun::i2v expecting an int. + operator unsigned() const; // To avoid ambiguity when an unsigned is expected. void update_() const; @@ -586,6 +589,21 @@ return i_; } + template <typename S> + inline + p_indexed_psite<S>::operator int() const + { + return i_; + } + + template <typename S> + inline + p_indexed_psite<S>::operator unsigned() const + { + mln_precondition(i_ >= 0); + return i_; + } + // p_indexed_fwd_piter<S>. Index: mln/core/concept/function.hh --- mln/core/concept/function.hh (revision 3061) +++ mln/core/concept/function.hh (working copy) @@ -100,6 +100,7 @@ struct Function_v2v : public Function<E> { typedef Function_v2v<void> category; + typedef metal::false_ is_mutable; // Meaning: no mutable result by default. protected: Function_v2v(); Function_v2v(const Function_v2v&); Index: mln/pw/image.hh --- mln/pw/image.hh (revision 3061) +++ mln/pw/image.hh (working copy) @@ -44,7 +44,7 @@ { // Fwd decl. - namespace pw { template <typename F, typename S> struct image; } + namespace pw { template <typename F, typename S> class image; } @@ -93,10 +93,14 @@ typedef trait::image::value_storage::disrupted value_storage; typedef trait::image::value_browsing::site_wise_only value_browsing; typedef trait::image::value_alignement::irrelevant value_alignement; - typedef trait::image::value_io::read_only value_io; + typedef mlc_if(typename F::is_mutable, + trait::image::value_io::read_write, + trait::image::value_io::read_only) value_io; // site / domain - typedef trait::image::pw_io::read pw_io; + typedef mlc_if(typename F::is_mutable, + trait::image::pw_io::read_write, + trait::image::pw_io::read) pw_io; typedef /* FIXME: depends on S */ undef localization; typedef /* FIXME: depends on S */ undef dimension; @@ -115,9 +119,11 @@ /// FIXME /// template <typename F, typename S> - struct image : + class image : public internal::image_primary<mln_result(F), S, image<F,S> > { + public: + /// Skeleton. typedef image< tag::function_<F>, tag::pset_<S> > skeleton; @@ -129,7 +135,7 @@ typedef mln_result(F) rvalue; /// Return type of read-write access. - typedef rvalue lvalue; + typedef mlc_if(typename F::is_mutable, mln_result(F)&, mln_result(F)) lvalue; /// Constructor without argument. image(); @@ -149,12 +155,15 @@ /// Read-only access of pixel value at point site \p p. mln_result(F) operator()(const mln_psite(S)& p) const; - /// Read-write access is present but return a temporary value. - mln_result(F) operator()(const mln_psite(S)&); + /// Read-write access returns either a temporary value (copy) or + /// a reference in the case of a mutable function (container). + lvalue operator()(const mln_psite(S)&); }; } // end of namespace mln::pw + + template <typename F, typename S> void init_(tag::function_t, Function_v2v<F>& target, const mln::pw::image<F,S>& model); @@ -163,6 +172,7 @@ void init_(tag::image_t, mln::pw::image<F,S>& target, const J& model); + # ifndef MLN_INCLUDE_ONLY // init_ @@ -264,7 +274,7 @@ template <typename F, typename S> inline - mln_result(F) + typename image<F,S>::lvalue image<F,S>::operator()(const mln_psite(S)& p) { mln_precondition(this->data_->pset_.has(p)); Index: mln/fun/l2l/relabel.hh --- mln/fun/l2l/relabel.hh (revision 3061) +++ mln/fun/l2l/relabel.hh (working copy) @@ -106,8 +106,7 @@ /// Always prefer using from_to instead of this constructor. relabel(const std::vector<L>& from); - /// \} - + typedef metal::true_ is_mutable; }; } // end of namespace mln::fun::l2l Index: mln/fun/i2v/array.hh --- mln/fun/i2v/array.hh (revision 3061) +++ mln/fun/i2v/array.hh (working copy) @@ -31,6 +31,9 @@ /// \file mln/fun/i2v/array.hh /// /// Function mapping an Id i to a value v. +/// +/// \todo Change design so that there is no multiple inheritance: +/// array<T> : internal::array_base<T, E==array<T> > : Function_i2v<E> # include <vector> # include <algorithm> @@ -107,6 +110,7 @@ /// \} + typedef metal::true_ is_mutable; }; } // end of namespace mln::fun::i2v Index: mln/fun/internal/array_base.hh --- mln/fun/internal/array_base.hh (revision 3061) +++ mln/fun/internal/array_base.hh (working copy) @@ -53,6 +53,7 @@ public: typedef T result; + typedef metal::true_ is_mutable; void resize(unsigned n); void resize(unsigned n, const T& val); Index: tests/pw/image.cc --- tests/pw/image.cc (revision 3061) +++ tests/pw/image.cc (working copy) @@ -31,7 +31,9 @@ /// Tests on mln::pw::image. #include <mln/fun/p2b/chess.hh> +#include <mln/fun/i2v/array.hh> #include <mln/core/alias/box2d.hh> +#include <mln/core/site_set/p_array.hh> #include <mln/pw/image.hh> #include <mln/core/var.hh> @@ -41,8 +43,10 @@ using namespace mln; mln_VAR(ima, fun::p2b::chess() | make::box2d(8, 8)); - mln_piter_(ima_t) p(ima.domain()); + // trait::image::print(ima); + unsigned i = 0; + mln_piter_(ima_t) p(ima.domain()); for_all(p) { if (p.row() % 2) @@ -52,4 +56,17 @@ ++i; } mln_assertion(i == 64); + + // A mutable pw::image. + { + p_array<int> arr; // Sites are ints (why not?) + arr.insert(51); // Site 51. + mln_VAR(ima, fun::i2v::array<int>(1) | arr); // An array psite converts to int so that works :-) + // trait::image::print(ima); + + p_array<int>::psite p(ima.domain(), 0); // index 0 means the 1st element of arr + ima(p) = 7; + mln_assertion(ima(p) == 7); + } + }
participants (1)
-
Thierry Geraud