r3287: Handle fastest version of functor

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox ChangeLog: 2009-02-05 Fabien Freling <freling@lrde.epita.fr> Handle fastest version of functor. * fabien/labeling.hh: . * fabien/regional_maxima.hh: Fastest version of functor --- labeling.hh | 44 ++++++++++++++-------------- regional_maxima.hh | 81 ++++++++++++++++++++--------------------------------- 2 files changed, 54 insertions(+), 71 deletions(-) Index: trunk/milena/sandbox/fabien/regional_maxima.hh =================================================================== --- trunk/milena/sandbox/fabien/regional_maxima.hh (revision 3286) +++ trunk/milena/sandbox/fabien/regional_maxima.hh (revision 3287) @@ -69,74 +69,55 @@ // Generic functor. - template <typename I_, typename N_, typename L_> + template <typename I> struct regional_maxima_functor { - typedef mln_psite(I_) P; + typedef mln_psite(I) P; // requirements from mln::canvas::labeling: - typedef I_ I; - typedef N_ N; - typedef L_ L; - typedef p_array<P> S; - const I& input; - const N& nbh; - S s; + + // Generic implementation void init() { data::fill(attr, true); } bool handles(const P&) const { return true; } - bool labels(const P& p) const { return attr(p); } - bool equiv(const P& n, const P& p) const { return input(n) == - input(p); } - void do_no_union(const P& n, const P& p) { mln_invariant(input(n) > - input(p)); - attr(p) = false; } + bool labels(const P& p) const { return attr.element(p); } + bool equiv(const P& n, const P& p) const { return input.element(n) == + input.element(p); } + void do_no_union(const P& n, const P& p) { mln_invariant(input.element(n) > + input.element(p)); + attr.element(p) = false; } void init_attr(const P&) {} - void merge_attr(const P& r, const P& p) { attr(p) = attr(p) && - attr(r); } + void merge_attr(const P& r, const P& p) { attr.element(p) = attr.element(p) && + attr.element(r); } + + // Fastest implementation + + void init_() { data::fill(attr, true); } + bool handles_(unsigned p) const { return true; } + bool labels_(unsigned p) const { return attr.element(p); } + bool equiv_(unsigned n, unsigned p) const { return input.element(n) == + input.element(p); } + void do_no_union_(unsigned n, unsigned p) { mln_invariant(input.element(n) > + input.element(p)); + attr.element(p) = false; } + void init_attr_(const P&) {} + void merge_attr_(unsigned r, unsigned p) { attr.element(p) = attr.element(p) && + attr.element(r); } // end of requirements mln_ch_value(I, bool) attr; regional_maxima_functor(const I_& input, const N_& nbh) - : input(input), - nbh(nbh), - s(level::sort_psites_decreasing(input)) + : input(input) { initialize(attr, input); } }; - // Generic implementation. - - namespace generic - { - - template <typename I, typename N, typename L> - mln_ch_value(I, L) - regional_maxima(const I& input, const N& nbh, - L& nlabels) - { - trace::entering("labeling::impl::generic::regional_maxima"); - - // FIXME: abort if L is not wide enough to encode the set of - // maxima. - - typedef impl::regional_maxima_functor<I,N,L> F; - F f(exact(input), exact(nbh)); - mln_ch_value(I, L) output = canvas::labeling(input, nbh, f, nlabels); - - trace::exiting("labeling::impl::generic::regional_maxima"); - return output; - } - - } // end of namespace mln::labeling::impl::generic - - } // end of namespace mln::labeling::impl @@ -146,7 +127,7 @@ template <typename I, typename N, typename L> mln_ch_value(I, L) regional_maxima(const Image<I>& input_, const Neighborhood<N>& nbh_, - L& nlabels) + bool increasing, L& nlabels) { trace::entering("labeling::regional_maxima"); @@ -154,8 +135,10 @@ const N& nbh = exact(nbh_); mln_precondition(input.is_valid()); - // Calls the only (generic) impl. - mln_ch_value(I, L) output = impl::generic::regional_maxima(input, nbh, nlabels); + typedef impl::regional_maxima_functor<I> F; + F f(exact(input)); + mln_ch_value(I, L) output = canvas::labeling_sorted(input, nbh, increasing, + f, nlabels); trace::exiting("labeling::regional_maxima"); return output; Index: trunk/milena/sandbox/fabien/labeling.hh =================================================================== --- trunk/milena/sandbox/fabien/labeling.hh (revision 3286) +++ trunk/milena/sandbox/fabien/labeling.hh (revision 3287) @@ -101,10 +101,10 @@ return parent(x) = find_root(parent, parent(x)); } - template <typename I, typename N, typename F, typename L> + template <typename I, typename N, typename S, typename F, typename L> mln_ch_value(I, L) labeling(const Image<I>& input_, const Neighborhood<N>& nbh_, - F& f, L& nlabels) + S& s, F& f, L& nlabels) { trace::entering("canvas::impl::generic::labeling"); @@ -113,8 +113,6 @@ const I& input = exact(input_); const N& nbh = exact(nbh_); - typedef typename F::S S; - // Local type. typedef mln_psite(I) P; @@ -228,8 +226,6 @@ const I& input = exact(input_); const N& nbh = exact(nbh_); - typedef typename F::S S; - // Local type. typedef mln_psite(I) P; @@ -315,7 +311,7 @@ // Fastest sorted version - template <typename I, typename N, typename F, typename L> + template <typename I, typename N, typename S, typename F, typename L> mln_ch_value(I, L) labeling_fastest_sorted(const Image<I>& input_, const Neighborhood<N>& nbh_, @@ -443,8 +439,8 @@ labeling_video(metal::false_, const Image<I>& input, const Neighborhood<N>& nbh, F& functor, L& nlabels) { - // FIXME:s = input.domain() - return impl::generic::labeling(input, nbh, functor, nlabels); + return impl::generic::labeling(input, nbh, input.domain(), + functor, nlabels); } template <typename I, typename N, typename F, typename L> @@ -468,35 +464,37 @@ && mln_is_simple_neighborhood(N)::value }; - return impl::generic::labeling_video(metal::bool_<test>(), input, nbh, - functor, nlabels); + return impl::generic::labeling_video(metal::bool_<test>(), input, + nbh, functor, nlabels); } // Sorted - template <typename I, typename N, typename F, typename L> + template <typename I, typename N, typename S, typename F, typename L> inline mln_ch_value(I, L) labeling_sorted(metal::false_, const Image<I>& input, const Neighborhood<N>& nbh, F& functor, L& nlabels) { - return impl::generic::labeling(input, nbh, functor, nlabels); + return impl::generic::labeling(input, nbh, s, functor, nlabels); } - template <typename I, typename N, typename F, typename L> + template <typename I, typename N, typename S, typename F, typename L> inline mln_ch_value(I, L) labeling_sorted(metal::true_, const Image<I>& input, const Neighborhood<N>& nbh, F& functor, L& nlabels) { - return impl::labeling_fastest_sorted(input, nbh, functor, nlabels); + return impl::labeling_fastest_sorted(input, nbh, s, + functor, nlabels); } - template <typename I, typename N, typename F, typename L> + template <typename I, typename N, typename S, typename F, typename L> inline mln_ch_value(I, L) - labeling_sorted_dispatch(const Image<I>& input, const Neighborhood<N>& nbh, + labeling_sorted_dispatch(const Image<I>& input, + const Neighborhood<N>& nbh, F& functor, L& nlabels) { enum { @@ -505,8 +503,8 @@ && mln_is_simple_neighborhood(N)::value }; - return impl::generic::labeling_video(metal::bool_<test>(), input, nbh, - functor, nlabels); + return impl::generic::labeling_sorted(metal::bool_<test>(), input, + nbh, s, functor, nlabels); } @@ -527,7 +525,8 @@ internal::labeling_tests(input, nbh, functor, nlabels); mln_ch_value(I, L) output; - output = internal::labeling_dispatch(input, nbh, functor, nlabels); + output = internal::labeling_video_dispatch(input, nbh, + functor, nlabels); trace::exiting("canvas::labeling_video"); return output; @@ -537,14 +536,15 @@ inline mln_ch_value(I, L) labeling_sorted(const Image<I>& input, const Neighborhood<N>& nbh, - F& functor, L& nlabels) + bool increasing, F& functor, L& nlabels) { trace::entering("canvas::labeling_sorted"); internal::labeling_tests(input, nbh, functor, nlabels); mln_ch_value(I, L) output; - output = internal::labeling_dispatch(input, nbh, functor, nlabels); + output = internal::labeling_sorted_dispatch(input, nbh, s, + functor, nlabels); trace::exiting("canvas::labeling_sorted"); return output;
participants (1)
-
Fabien Freling