Ce patch complète oln::apply pour les fonctions unaires, mais il
s'agit surtout d'un test de vérification que je voulais terminer et
commettre ; en effet, oln::apply ne type pas assez fortement les
fonctions (AdaptableUnaryFun). On va utiliser Metalic pour résoudre
ça.
https://svn.lrde.epita.fr/svn/oln/prototypes/proto-1.0
ChangeLog | 10 ++++++++++
oln/core/apply.hh | 43 ++++++++++++++++++++++++++++++++++++++++---
tests/core/tests/apply | 32 ++++++++++++++++++++++++++++++++
3 files changed, 82 insertions(+), 3 deletions(-)
Index: olena/ChangeLog
from Roland Levillain <roland(a)lrde.epita.fr>
More oln::apply (for Adaptable Unary Functions).
* oln/core/apply.hh: Pass the Adaptable Unary Functions by value
(copy), not by (const) reference.
(apply (AdaptableUnaryFun, const abstract::image<I>&)): Document.
(apply (const abstract::image<I>&)): New functions.
* tests/core/tests/apply: Test them.
Index: olena/tests/core/tests/apply
--- olena/tests/core/tests/apply (revision 108)
+++ olena/tests/core/tests/apply (working copy)
@@ -9,14 +9,46 @@
bool check()
{
+ // oln::apply, with an instantiated Adaptable Unary Function.
+ {
oln::image2d<ntg::int_u8> ima1(10, 10);
oln::level::fill(ima1, 10);
oln::image2d<ntg::int_u8> ima2(10, 10);
oln::level::fill(ima2, 30);
oln::image2d<ntg::int_u8> ima;
+
ima = oln::apply (std::bind2nd(std::multiplies<ntg::int_u8>(), 3), ima1);
+ if (oln::level::is_equal(ima, ima2))
+ return false;
+ }
+ // oln::apply, with a non-instantiated Adaptable Unary Function.
+ {
+ oln::image2d<ntg::int_s8> ima1(10, 10);
+ oln::level::fill(ima1, 10);
+ oln::image2d<ntg::int_s8> ima2(10, 10);
+ oln::level::fill(ima2, -10);
+ oln::image2d<ntg::int_s8> ima;
+
+ typedef std::negate<ntg::int_s8> negate_int_s8;
+ ima = oln::apply<negate_int_s8> (ima1);
if (oln::level::is_equal(ima, ima2))
return false;
+ }
+
+ // oln::apply, with a non-instantiated template Adaptable Unary
+ // Function.
+ {
+ oln::image2d<ntg::int_s8> ima1(10, 10);
+ oln::level::fill(ima1, 10);
+ oln::image2d<ntg::int_s8> ima2(10, 10);
+ oln::level::fill(ima2, -10);
+ oln::image2d<ntg::int_s8> ima;
+
+ ima = oln::apply<std::negate> (ima1);
+ if (oln::level::is_equal(ima, ima2))
+ return false;
+ }
+
return true;
}
Index: olena/oln/core/apply.hh
--- olena/oln/core/apply.hh (revision 108)
+++ olena/oln/core/apply.hh (working copy)
@@ -34,6 +34,10 @@
namespace oln {
+ /*--------.
+ | Unary. |
+ `--------*/
+
// fwd decl
namespace impl {
template <typename AdaptableUnaryFun, typename I> struct apply_type;
@@ -67,10 +71,10 @@
ch_value_type<I, typename AdaptableUnaryFun::result_type>::ret
output_type;
- const AdaptableUnaryFun& f_;
+ AdaptableUnaryFun f_;
box<const I> input_;
- apply_type(const AdaptableUnaryFun& f, const abstract::image<I>&
input) :
+ apply_type(AdaptableUnaryFun f, const abstract::image<I>& input) :
f_ (f),
input_ (input.exact ())
{
@@ -88,15 +92,48 @@
} // end of namespace impl
+
+ /*! \brief Standard unary \a apply procedure.
+ **
+ ** Apply a function \a f to each element of \a input, the function
+ ** is passed as a type and is instantiated.
+ */
template <class AdaptableUnaryFun, typename I>
impl::apply_type<AdaptableUnaryFun, I>
- apply (const AdaptableUnaryFun& f, const abstract::image<I>& input)
+ apply(AdaptableUnaryFun f, const abstract::image<I>& input)
{
impl::apply_type<AdaptableUnaryFun, I> tmp (f, input);
tmp.run ();
return tmp;
}
+ /*! \brief Standard unary \a apply procedure.
+ **
+ ** Apply a function \a f to each element of \a input, the function
+ ** is passed as a type and is instantiated.
+ */
+ template<class AdaptableUnaryFun, class I>
+ impl::apply_type<AdaptableUnaryFun, I>
+ apply(const abstract::image<I>& input)
+ {
+ return apply(AdaptableUnaryFun(), input);
+ }
+
+ /*! \brief Standard unary \a apply procedure.
+ **
+ ** Apply function \a f to each element of \a input, the function is
+ ** passed as a type and is instantiated. For template functions
+ ** passed as template-id, one need to instantiate the function for
+ ** the type of the abstract::image.
+ */
+ template<template<class> class AdaptableUnaryFun, class I>
+ typename impl::apply_type<AdaptableUnaryFun<oln_type_of(I, value)>, I>
+ apply(const abstract::image<I>& input)
+ {
+ AdaptableUnaryFun<oln_type_of(I, value)> tmp;
+ return apply(tmp, input);
+ }
+
} // end of namespace oln
#endif // ! OLENA_CORE_APPLY_HH