URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox
ChangeLog:
2009-02-04 Edwin Carlinet <carlinet(a)lrde.epita.fr>
Add card accu and some traits.
* accu.cc: Add traits and fix bugs.
* accu_trait.hh: New.
---
accu.cc | 155 ++++++++++++++++++++++++++++++++++++++++++++++------------
accu_trait.hh | 51 +++++++++++++++++++
2 files changed, 175 insertions(+), 31 deletions(-)
Index: trunk/milena/sandbox/edwin/accu.cc
===================================================================
--- trunk/milena/sandbox/edwin/accu.cc (revision 3283)
+++ trunk/milena/sandbox/edwin/accu.cc (revision 3284)
@@ -1,91 +1,184 @@
-template <typename E>
-struct Accumulator : public object<E>
-{
- typedef mln_vtypes(E, take_with) take_with;
+# include <mln/metal/equal.hh>
+# include <mln/metal/if.hh>
+# include <mln/metal/is_const.hh>
+# include <mln/core/concept/image.hh>
+# include <mln/accu/all.hh>
+# include <mln/util/pix.hh>
+# include <mln/make/pix.hh>
+# include "accu_trait.hh"
-};
+using namespace mln;
+
+namespace mln
+{
+
+ namespace morpho
+ {
namespace accu
{
template <typename T>
- struct card : public Accumulator< card<T> >
+ struct card : public mln::accu::internal::base< unsigned, card<T> >
{
- void take(const T& elt) { ++c_; }
+ typedef T argument;
+ void init () { c_ = 0; };
+ void take (const card<T>& elt) { ++c_; };
+ void take (const T& elt) { ++c_; };
+ void take (const mln_value(T)& v) { ++c_; };
+ unsigned to_result() const { return c_; };
+ operator unsigned () const { return c_; };
+ bool is_valid () const { return true; };
+ card () { init(); };
+
+ private:
unsigned c_;
};
-}
+ } // accu
+ } // morpho
namespace impl
{
- template <typename I, template<typename E> class A>
+ template <typename I, typename A>
void
algebraic(const Image<I>& input,
- const Accumulator< A<point2d> >& acc)
+ Accumulator<A>& acc)
{
const I& ima = exact(input);
+ A& accu = exact (acc);
mln_piter(I) p(ima.domain());
for_all(p)
- acc.take(p);
+ accu.take(p);
}
// fast implementation
- template <typename I, template<typename E> class A>
+ template <typename I, typename A>
void
- leveling (const Image<I>& input,
- const Accumulator< A< pix<I> > >& acc)
+ leveling_fast (const Image<I>& input,
+ Accumulator<A>& acc)
{
const I& ima = exact(input);
+ A& accu = exact (acc);
mln_pixter(const I) px(ima);
for_all(px)
- acc.take(px);
+ accu.take (px.val ());
}
// generic implementation
- template <typename I, template<typename E> class A>
+ template <typename I, typename A>
void
leveling (const Image<I>& input,
- const Accumulator< A<point2d> >& acc)
+ Accumulator<A>& acc)
{
const I& ima = exact(input);
+ A& accu = exact (acc);
mln_piter(I) p(ima.domain());
for_all(p)
- acc.take(p);
+ accu.take (mln::make::pix(ima, p));
}
-
} // impl
namespace internal
{
template <typename I, typename A>
void
- leveling_dispatch(const Image<I>& input,
- const Accumulator<A>& acc)
+ leveling_dispatch(metal::false_,
+ const Image<I>& input,
+ Accumulator<A>& acc)
{
+ impl::leveling(input, acc);
+ }
- //Si when_pix = use_only
- //
-
- algebraic_dispatch(mlc_equal(mln_trait_image_speed(I),
- trait::image::speed::fastest)::value,
- input,
- acc);
+ template <typename I, typename A>
+ inline
+ void
+ leveling_dispatch(metal::true_,
+ const Image<I>& input,
+ Accumulator<A>& acc)
+ {
+ impl::leveling_fast(input, acc);
}
+ template <typename I, typename A>
+ inline
+ void
+ leveling_dispatch(const Image<I>& input,
+ Accumulator<A>& acc)
+ {
+ enum {
+ test = (mlc_equal(mln_trait_image_speed(I),
+ trait::image::speed::fastest)::value &&
+ mlc_equal(mln_trait_accu_when_pix(A),
+ trait::accu::when_pix::use_v)::value)
+ };
+ leveling_dispatch(metal::bool_<test>(), input, acc);
}
+ } // internal
+
+} //mln
+
// Facade.
-template <typename M, typename I>
+template <typename I, typename A>
void
-algebraic(const Image<I>& input, const Meta_Accumulator<M>& acc) //
FIXME: on préfère Meta_Accumulator !
+leveling(const Image<I>& input,
+ Accumulator<A>& acc)
{
- internal::algebraic_dispatch(input, acc);
+ mln::internal::leveling_dispatch(input, acc);
}
+
+
+# include <mln/accu/all.hh>
+# include <mln/core/image/image2d.hh>
+
+# include <mln/debug/iota.hh>
+# include <mln/debug/println.hh>
+# include <mln/core/var.hh>
+# include <mln/util/timer.hh>
+int main()
+{
+ using namespace mln;
+ typedef image2d<int> I;
+
+ I ima(1000, 1000);
+ mln::morpho::accu::card<util::pix<I> > acc;
+
+ float elapsed;
+ mln::util::timer chrono;
+
+ debug::iota(ima);
+ std::cout << "50 mean of a 1000x1000 image2d<int>" <<
std::endl;
+
+ acc.init();
+ chrono.start();
+ for (int i = 0; i < 50; i++)
+ leveling(ima, acc);
+ elapsed = chrono.stop();
+
+ std::cout << "(auto) " << elapsed << "s : "
<< acc.to_result() << std::endl;
+
+ acc.init();
+ chrono.start();
+ for (int i = 0; i < 50; i++)
+ mln::impl::leveling(ima, acc);
+ elapsed = chrono.stop();
+
+ std::cout << "(generic) " << elapsed << "s : "
<< acc.to_result() << std::endl;
+
+ acc.init();
+ chrono.start();
+ for (int i = 0; i < 50; i++)
+ mln::impl::leveling_fast(ima, acc);
+ elapsed = chrono.stop();
+
+ std::cout << "(fast) " << elapsed << "s : "
<< acc.to_result() << std::endl;
+}
+
Index: trunk/milena/sandbox/edwin/accu_trait.hh
===================================================================
--- trunk/milena/sandbox/edwin/accu_trait.hh (revision 0)
+++ trunk/milena/sandbox/edwin/accu_trait.hh (revision 3284)
@@ -0,0 +1,51 @@
+#ifndef ACCU_TRAIT_HH_
+# define ACCU_TRAIT_HH_
+
+# include <mln/trait/undef.hh>
+
+
+# define mln_trait_accu_when_pix(A) typename trait::accu::accu_traits<A>::when_pix
+
+
+namespace mln
+{
+ namespace morpho {
+ namespace accu
+ {
+ template <typename T>
+ struct card;
+ }
+ }
+
+ namespace trait
+ {
+ namespace accu
+ {
+
+ struct when_pix
+ {
+ struct use_v {};
+ struct use_p {};
+ struct use_pix {};
+ struct use_whatever {};
+ struct not_ok {};
+ };
+
+ template <typename A>
+ struct accu_traits
+ {
+ typedef undef when_pix;
+ };
+
+ template <>
+ template <typename T>
+ struct accu_traits< mln::morpho::accu::card <T> >
+ {
+ typedef when_pix::use_v when_pix;
+ };
+ } //accu
+
+ }//trait
+
+} //mln
+#endif /* !ACCU_TRAIT_HH_ */