
URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox ChangeLog: 2009-03-24 Frederic Bour <bour@lrde.epita.fr> Correct new functions. * fred/mln/core/image/thru_morpher.hh, * fred/mln/fun/math/inf.hh, * fred/mln/fun/math/sup.hh: Correct typo * fred/mln/fun/spe/unary.hh, * fred/mln/fun/unary.hh: Add composition with lresult. * fred/tests/Makefile, * fred/tests/thru.cc: Removed unnecessary include. --- mln/core/image/thru_morpher.hh | 1 mln/fun/math/inf.hh | 2 mln/fun/math/sup.hh | 4 - mln/fun/spe/unary.hh | 93 ++++++++++++++++++++++++++++++++++++++--- mln/fun/unary.hh | 38 +++++++++++++++- tests/Makefile | 13 ++--- tests/thru.cc | 9 +-- 7 files changed, 137 insertions(+), 23 deletions(-) Index: trunk/milena/sandbox/fred/tests/thru.cc =================================================================== --- trunk/milena/sandbox/fred/tests/thru.cc (revision 3563) +++ trunk/milena/sandbox/fred/tests/thru.cc (revision 3564) @@ -1,5 +1,5 @@ // Meta functions test -#include <mln/fun/essential.hh> +#include <mln/fun/v2v/convert.hh> #include <mln/fun/math/cos.hh> #include <mln/core/image/thru_morpher.hh> #include <mln/fun/compose.hh> @@ -28,10 +28,9 @@ debug::println(ima); debug::println(thru(cos, ima)); - thru_image<I, fun::cos::with<fun::cos>::ret::result::with<float>::ret > ima2; - ima2 = thru(cos(cos), ima); + mln_VAR(ima2, thru(cos(cos), ima)); - data::fill_with_image(ima2, (pw::value(tmp) - pw::cst(13.0f)) / pw::cst(12.0f) | tmp.domain()); + data::fill_with_image(ima2, tmp); - debug::println(ima); + debug::println(ima2); } \ No newline at end of file Index: trunk/milena/sandbox/fred/tests/Makefile =================================================================== --- trunk/milena/sandbox/fred/tests/Makefile (revision 3563) +++ trunk/milena/sandbox/fred/tests/Makefile (revision 3564) @@ -1,17 +1,16 @@ -TARGET=fun -OBJS=$(TARGET).o +TARGETS=fun.bin thru.bin cos.bin OLENADIR=../../../.. MILENADIR=$(OLENADIR)/milena -CXXFLAGS=-I$(MILENADIR) -I../ -DNDEBUG -O1 -ffast-math +CXXFLAGS=-I$(MILENADIR) -I../ -DNDEBUG -O1 CXX=g++ LD=g++ LDFLAGS= RM=rm -$(TARGET): $(OBJS) - $(LD) $(LDFLAGS) -o $@ $(OBJS) +%.bin: %.o + $(LD) $(LDFLAGS) -o $@ $< %.o: %.cc $(CXX) $(CXXFLAGS) -c $< @@ -19,7 +18,7 @@ %.o: %.hh $(CXX) $(CXXFLAGS) -c $< -all: $(TARGET) +all: $(TARGETS) clean: - $(RM) -f $(TARGET) *.o *~ + $(RM) -f $(TARGETS) *.o *~ Index: trunk/milena/sandbox/fred/mln/core/image/thru_morpher.hh =================================================================== --- trunk/milena/sandbox/fred/mln/core/image/thru_morpher.hh (revision 3563) +++ trunk/milena/sandbox/fred/mln/core/image/thru_morpher.hh (revision 3564) @@ -72,7 +72,6 @@ data(I& ima, const F& f); I ima_; - // FIXME: value or pointer or whatever ? F f_; }; Index: trunk/milena/sandbox/fred/mln/fun/spe/unary.hh =================================================================== --- trunk/milena/sandbox/fred/mln/fun/spe/unary.hh (revision 3563) +++ trunk/milena/sandbox/fred/mln/fun/spe/unary.hh (revision 3564) @@ -44,7 +44,8 @@ namespace internal { - template <typename Impl> + // Wrapper for bijective functions + template <typename Impl, typename T> struct unary_modifier { typedef typename Impl::result result; @@ -52,7 +53,43 @@ typedef typename Impl::lvalue lvalue; typedef unary_modifier lresult; - // FIXME: "argument& x" or "lvalue x" directly? ~~~ + unary_modifier(const Impl& impl, T& x) + : x_(&x), impl_(&impl) + { + } + + result to_result() const + { + return impl_->read(*x_); + }; + + operator result() const + { + return to_result(); + }; + + const result& operator = (const result& r) const + { + argument x(*x_); + impl_->write(x, r); + *x_ = x; + + return r; + } + + private: + T *x_; + const Impl *impl_; + }; + + template <typename Impl> + struct unary_modifier<Impl, typename Impl::argument> + { + typedef typename Impl::result result; + typedef typename Impl::argument argument; + typedef typename Impl::lvalue lvalue; + typedef unary_modifier lresult; + unary_modifier(const Impl& impl, argument& x) : x_(&x), impl_(&impl) { @@ -71,6 +108,7 @@ const result& operator = (const result& r) const { impl_->write(*x_, r); + return r; } @@ -79,6 +117,44 @@ const Impl *impl_; }; + template <typename Impl, typename Impl2, typename T> + struct unary_modifier<Impl, unary_modifier<Impl2, T> > + { + typedef typename Impl::result result; + typedef typename Impl::argument argument; + typedef typename Impl::lvalue lvalue; + typedef unary_modifier lresult; + + // FIXME: "argument& x" or "lvalue x" directly? ~~~ + unary_modifier(const Impl& impl, const unary_modifier<Impl2, T>& m) + : m_(m), impl_(&impl) + { + } + + result to_result() const + { + return impl_->read(m_.to_result()); + }; + + operator result() const + { + return to_result(); + }; + + const result& operator = (const result& r) const + { + argument m(m_); + impl_->write(m, r); + m_ = m; + + return r; + } + + private: + const unary_modifier<Impl2, T> m_; + const Impl *impl_; + }; + } // end of namespace mln::fun::internal // Forward declaration (defined in mln/fun/unary.hh) @@ -123,7 +199,14 @@ typedef typename impl::argument argument; typedef typename impl::result result; typedef typename impl::lvalue lvalue; - typedef mln::fun::spe::internal::unary_modifier<impl> lresult; + + template <typename U> + struct lresult_with + { + typedef mln::fun::spe::internal::unary_modifier<impl, U> ret; + }; + + typedef typename lresult_with<argument>::ret lresult; unary_impl_set() {} unary_impl_set(const typename super::init_param& p) : super(p) {} @@ -190,8 +273,8 @@ } // end of namespace mln::fun - template <typename Impl> - std::ostream& operator << (std::ostream& o, const mln::fun::spe::internal::unary_modifier<Impl>& m) + template <typename Impl, typename T> + std::ostream& operator << (std::ostream& o, const mln::fun::spe::internal::unary_modifier<Impl, T>& m) { return o << m.to_result(); } Index: trunk/milena/sandbox/fred/mln/fun/math/sup.hh =================================================================== --- trunk/milena/sandbox/fred/mln/fun/math/sup.hh (revision 3563) +++ trunk/milena/sandbox/fred/mln/fun/math/sup.hh (revision 3564) @@ -46,8 +46,8 @@ namespace next { - template <typename T, typename T> - struct set_binary_<mln::fun::inf, mln::Object, T, mln::Object, T> + template <typename T> + struct set_binary_<mln::fun::sup, mln::Object, T, mln::Object, T> { typedef set_binary_ ret; typedef T result; Index: trunk/milena/sandbox/fred/mln/fun/math/inf.hh =================================================================== --- trunk/milena/sandbox/fred/mln/fun/math/inf.hh (revision 3563) +++ trunk/milena/sandbox/fred/mln/fun/math/inf.hh (revision 3564) @@ -46,7 +46,7 @@ namespace next { - template <typename T, typename T> + template <typename T> struct set_binary_<mln::fun::inf, mln::Object, T, mln::Object, T> { typedef set_binary_ ret; Index: trunk/milena/sandbox/fred/mln/fun/unary.hh =================================================================== --- trunk/milena/sandbox/fred/mln/fun/unary.hh (revision 3563) +++ trunk/milena/sandbox/fred/mln/fun/unary.hh (revision 3564) @@ -101,7 +101,7 @@ typedef unary_with_lvalue_helper<false, M, T> super; typedef typename super::function function; - static typename function::lresult lcall(const M& m, T& x) + static typename function::template lresult_with<T>::ret lcall(const M& m, T& x) { function f(super::inst(m)); return f(x); @@ -114,6 +114,33 @@ } }; + template <typename MF, typename MG, typename T> + struct unary_with_lvalue_helper<true, MF, mln::fun::spe::internal::unary_modifier<MG, T> > + : unary_with_lvalue_helper<false, MF, mln::fun::spe::internal::unary_modifier<MG, T> > + { + typedef unary_with_lvalue_helper<false, MF, typename MG::result > super; + typedef typename super::function function; + + static typename function::template lresult_with<typename MG::result>::ret lcall(const MF& m, T& x) + { + function f(super::inst(m)); + return f(x); + } + + static typename function::template lresult_with< mln::fun::spe::internal::unary_modifier<MG, T> >::ret + lcall(const MF& m, const mln::fun::spe::internal::unary_modifier<MG, T>& x) + { + function f(super::inst(m)); + return f(x); + } + + static void set(const MF& m, typename function::lvalue v, const T& x) + { + function f(super::inst(m)); + f.set(v, x); + } + }; + template <typename M, typename T> struct unary_with_helper : unary_with_lvalue_helper<mln_trait_fun_is_assignable__1comma_(mln::fun::spe::unary<M, T>)::value, M, T> @@ -139,11 +166,18 @@ } template <typename T> - typename with<T>::ret::lresult operator()(T& v) const + typename with<T>::ret::template lresult_with<T>::ret operator()(T& v) const { return with<T>::impl::lcall(exact(*this), v); } + template <typename G, typename U> + typename with< mln::fun::spe::internal::unary_modifier<G, U> >::ret::lresult + operator()(const mln::fun::spe::internal::unary_modifier<G, U>& v) const + { + return with< mln::fun::spe::internal::unary_modifier<G, U> >::impl::lcall(exact(*this), v); + } + template <typename T, typename R> void set(T& v, const R& r) const {