Commit e037dab9 authored by Roland Levillain's avatar Roland Levillain
Browse files

Milena: Introduce point-wise function bindings.

	* mln/pw/bind.hh: New.
	* mln/pw/all.hh: Include mln/pw/bind.hh.
	* tests/pw/bind.cc: New.
	* tests/pw/Makefile.am (check_PROGRAMS): Add bind.
	* headers.mk,
	* tests/unit_test/unit-tests.mk:
	Regen.
parent 95c9f696
2013-10-09 Roland Levillain <roland@lrde.epita.fr>
Milena: Introduce point-wise function bindings.
* mln/pw/bind.hh: New.
* mln/pw/all.hh: Include mln/pw/bind.hh.
* tests/pw/bind.cc: New.
* tests/pw/Makefile.am (check_PROGRAMS): Add bind.
* headers.mk,
* tests/unit_test/unit-tests.mk:
Regen.
2013-10-09 Roland Levillain <roland@lrde.epita.fr>
 
Milena: Add support for unary minus on images.
......@@ -1013,6 +1013,7 @@ mln/opt/element.hh \
mln/opt/essential.hh \
mln/opt/value.hh \
mln/pw/all.hh \
mln/pw/bind.hh \
mln/pw/cst.hh \
mln/pw/essential.hh \
mln/pw/image.hh \
......
// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
// Copyright (C) 2007, 2008, 2009, 2013 EPITA Research and Development
// Laboratory (LRDE).
//
// This file is part of Olena.
//
......@@ -26,10 +27,8 @@
#ifndef MLN_PW_ALL_HH
# define MLN_PW_ALL_HH
/*! \file
*
* \brief File that includes all "point-wise" expression tools.
*/
/// \file
/// \brief Inclusion of all "point-wise" expression tools.
namespace mln
......@@ -41,6 +40,7 @@ namespace mln
} // end of namespace mln
# include <mln/pw/bind.hh>
# include <mln/pw/cst.hh>
# include <mln/pw/image.hh>
# include <mln/pw/value.hh>
......
// Copyright (C) 2013 EPITA Research and Development Laboratory (LRDE).
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Olena. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project without restriction. Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License. This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
#ifndef MLN_PW_BIND_HH
# define MLN_PW_BIND_HH
/// \file
/// \brief Definition of point-wise function bindings.
# include <mln/core/concept/function.hh>
# include <mln/fun/internal/selector.hh>
namespace mln
{
namespace pw
{
/*-------------------------.
| Unary function binding. |
`-------------------------*/
/// \brief Point-wise unary function binding.
///
/// This point-wise class is actually a function object embedding
/// a Milena function (functor) and another point-wise expression.
/// Its operator() returns the application of the embedded
/// function to the embedded point-wise expression.
template <typename F, typename PW>
struct unary_bound_fun_
: fun::internal::selector_from_result_< typename F::result,
unary_bound_fun_<F, PW> >::ret
{
typedef typename F::result result;
unary_bound_fun_(const Function_v2v<F>& f, const Function_v2v<PW>& pw);
template <typename P>
result operator()(const P& p) const;
protected:
/// The function to apply.
const F f_;
/// The point-wise expression.
const PW pw_;
};
/// \brief Bind the unary function \a f to the point-wise
/// expression \a pw.
template <typename F, typename PW>
unary_bound_fun_<F, PW>
bind(const Function_v2v<F>& f, const Function_v2v<PW>& pw);
/*--------------------------.
| Binary function binding. |
`--------------------------*/
/// \brief Point-wise binary function binding.
///
/// This point-wise class is actually a function object embedding
/// a Milena function (functor) and another point-wise expression.
/// Its operator() returns the application of the embedded
/// function to the embedded point-wise expression.
template <typename F, typename PW1, typename PW2>
struct binary_bound_fun_
: fun::internal::selector_from_result_< typename F::result,
binary_bound_fun_<F,
PW1,
PW2> >::ret
{
typedef typename F::result result;
binary_bound_fun_(const Function_vv2v<F>& f,
const Function_v2v<PW1>& pw1,
const Function_v2v<PW2>& pw2);
template <typename P>
result operator()(const P& p) const;
protected:
/// The function to apply.
const F f_;
/// The first point-wise expression.
const PW1 pw1_;
/// The second point-wise expression.
const PW2 pw2_;
};
/// \brief Bind the binary function \a f to the point-wise
/// expressions \a pw1 and \a pw2.
template <typename F, typename PW1, typename PW2>
binary_bound_fun_<F, PW1, PW2>
bind(const Function_vv2v<F>& f,
const Function_v2v<PW1>& pw1, const Function_v2v<PW2>& pw2);
# ifndef MLN_INCLUDE_ONLY
// pw::unary_bound_fun_<F, PW>.
template <typename F, typename PW>
inline
unary_bound_fun_<F, PW>::unary_bound_fun_(const Function_v2v<F>& f,
const Function_v2v<PW>& pw)
: f_(exact(f)),
pw_(exact(pw))
{
}
template <typename F, typename PW>
template <typename P>
inline
typename F::result
unary_bound_fun_<F, PW>::operator()(const P& p) const
{
return f_(pw_(p));
}
// pw::bind(f, pw).
template <typename F, typename PW>
inline
unary_bound_fun_<F, PW>
bind(const Function_v2v<F>& f, const Function_v2v<PW>& pw)
{
return unary_bound_fun_<F, PW>(f, pw);
}
// pw::binary_bound_fun_<F, PW1, PW2>.
template <typename F, typename PW1, typename PW2>
inline
binary_bound_fun_<F, PW1, PW2>::binary_bound_fun_(const Function_vv2v<F>& f,
const Function_v2v<PW1>& pw1,
const Function_v2v<PW2>& pw2)
: f_(exact(f)),
pw1_(exact(pw1)),
pw2_(exact(pw2))
{
}
template <typename F, typename PW1, typename PW2>
template <typename P>
inline
typename F::result
binary_bound_fun_<F, PW1, PW2>::operator()(const P& p) const
{
return f_(pw1_(p), pw2_(p));
}
// pw::bind(f, pw1, pw2).
template <typename F, typename PW1, typename PW2>
inline
binary_bound_fun_<F, PW1, PW2>
bind(const Function_vv2v<F>& f,
const Function_v2v<PW1>& pw1, const Function_v2v<PW2>& pw2)
{
return binary_bound_fun_<F, PW1, PW2>(f, pw1, pw2);
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::pw
} // end of namespace mln
#endif // ! MLN_PW_BIND_HH
# Copyright (C) 2007, 2008, 2009, 2010 EPITA Research and Development
# Laboratory (LRDE)
# Copyright (C) 2007, 2008, 2009, 2010, 2013 EPITA Research and
# Development Laboratory (LRDE).
#
# This file is part of Olena.
#
......@@ -18,6 +18,7 @@
include $(top_srcdir)/milena/tests/tests.mk
check_PROGRAMS = \
bind \
image \
value
......
// Copyright (C) 2013 EPITA Research and Development Laboratory (LRDE).
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Olena. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project without restriction. Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License. This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
/// \file
/// \brief Exercise mln::pw::bind with unary and binary functions
/// bound to point-wise expressions.
#include <mln/core/image/image2d.hh>
#include <mln/pw/all.hh>
#include <mln/fun/v2v/abs.hh>
#include <mln/fun/vv2v/max.hh>
#include <mln/arith/minus.hh>
#include <mln/debug/iota.hh>
#include <mln/test/predicate.hh>
int main()
{
using namespace mln;
image2d<int> pos_ints(3, 3);
debug::iota(pos_ints);
image2d<int> neg_ints = -pos_ints;
// Exercise unary point-wise function binding.
mln_assertion(test::predicate(pos_ints.domain(),
pw::bind(fun::v2v::abs<int>(),
pw::value(neg_ints))
== pw::value(pos_ints)));
// Exercise binary point-wise function binding.
mln_assertion(test::predicate(pos_ints.domain(),
pw::bind(fun::vv2v::max<int>(),
pw::value(pos_ints),
pw::value(neg_ints))
== pw::value(pos_ints)));
}
......@@ -1065,6 +1065,7 @@ mln_opt_element \
mln_opt_essential \
mln_opt_value \
mln_pw_all \
mln_pw_bind \
mln_pw_cst \
mln_pw_essential \
mln_pw_image \
......
Supports Markdown
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