cleanup-2008 2741: Add majority accu.

https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena Index: ChangeLog from Ugo Jardonnet <ugo.jardonnet@lrde.epita.fr> Add majority accu. * mln/accu/maj_h.hh: Add majority accumulator. * mln/accu/histo.hh: Remove bad use of size_t. * mln/accu/mean.hh: Temporary fix. * mln/fun/x2x/rotation.hh: Fix matrix parameters. accu/histo.hh | 24 ++--- accu/maj_h.hh | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++++ accu/mean.hh | 9 ++ fun/x2x/rotation.hh | 2 4 files changed, 235 insertions(+), 13 deletions(-) Index: mln/accu/histo.hh --- mln/accu/histo.hh (revision 2740) +++ mln/accu/histo.hh (working copy) @@ -56,7 +56,7 @@ */ template <typename V> struct histo : - public mln::accu::internal::base<const std::vector<std::size_t>& , + public mln::accu::internal::base<const std::vector<unsigned>& , histo<V> > { histo(); @@ -70,16 +70,16 @@ void untake(const argument& t); void init(); - std::size_t operator()(const argument& t) const; - std::size_t operator[](unsigned i) const; + unsigned operator()(const argument& t) const; + unsigned operator[](unsigned i) const; unsigned nvalues() const; - std::size_t sum() const; + unsigned sum() const; /// \} /// Get the value of the accumulator. /// \{ - const std::vector<std::size_t>& vect() const; - const std::vector<std::size_t>& to_result() const; + const std::vector<unsigned>& vect() const; + const std::vector<unsigned>& to_result() const; /// \} const value::set<V>& vset() const; @@ -91,7 +91,7 @@ protected: mln::histo::data<V> h_; - std::size_t sum_; + unsigned sum_; }; template <typename V> @@ -165,7 +165,7 @@ template <typename V> inline - std::size_t + unsigned histo<V>::operator()(const argument& t) const { return h_[h_.vset().index_of(t)]; @@ -173,7 +173,7 @@ template <typename V> inline - std::size_t + unsigned histo<V>::operator[](unsigned i) const { mln_precondition(i < h_.vset().nvalues()); @@ -190,7 +190,7 @@ template <typename V> inline - std::size_t + unsigned histo<V>::sum() const { return sum_; @@ -198,7 +198,7 @@ template <typename V> inline - const std::vector<std::size_t>& + const std::vector<unsigned>& histo<V>::vect() const { return h_.vect(); @@ -206,7 +206,7 @@ template <typename V> inline - const std::vector<std::size_t>& + const std::vector<unsigned>& histo<V>::to_result() const { return this->vect(); Index: mln/accu/maj_h.hh --- mln/accu/maj_h.hh (revision 0) +++ mln/accu/maj_h.hh (revision 0) @@ -0,0 +1,213 @@ +// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library 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 this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library 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_ACCU_MAJ_H_HH +# define MLN_ACCU_MAJ_H_HH + +/*! \file mln/accu/maj_h.hh + * + * \brief Define an accumulator that computes a maj_h. + */ + +# include <mln/core/concept/meta_accumulator.hh> +# include <mln/accu/internal/base.hh> +# include <mln/trait/value_.hh> +# include <mln/util/pix.hh> +# include <mln/accu/histo.hh> +# include <vector> + + +namespace mln +{ + + namespace accu + { + + + /*! \brief Generic maj_h accumulator class. + * + * The parameter \c T is the type of values. + */ + template <typename T> + struct maj_h : public mln::accu::internal::base< const T& , maj_h<T> > + { + typedef T argument; + + maj_h(); + + /// Manipulators. + /// \{ + void init(); + void take(const argument& t); + void untake(const argument& t); + void take(const maj_h<T>& other); + /// \} + + /// Get the value of the accumulator. + const T& to_result() const; + + //operator T () const; + + /// Check whether this accu is able to return a result. + /// Always true here. + bool is_valid() const; + + void update_() const; + + mutable bool valid_; + + const accu::histo<T>& histo() const; + + protected: + + mutable argument t_; + mutable accu::histo<T> h_; + }; + + + template <typename I> struct maj_h< util::pix<I> >; + + + namespace meta + { + + /// Meta accumulator for maj_h. + + struct maj_h : public Meta_Accumulator< maj_h > + { + template <typename T> + struct with + { + typedef accu::maj_h<T> ret; + }; + }; + + } // end of namespace mln::accu::meta + + + +# ifndef MLN_INCLUDE_ONLY + + template <typename T> + inline + maj_h<T>::maj_h() + { + init(); + valid_ = true; + } + + template <typename T> + inline + void + maj_h<T>::init() + { + h_.init(); + } + + template <typename T> + inline + void + maj_h<T>::take(const argument& t) + { + h_.take(t); + + //update return value + if (h_(t) > h_(t_)) + t_ = t; + } + + template <typename T> + inline + void + maj_h<T>::untake(const argument& t) + { + h_.untake(t); + + if (valid_) + valid_ = false; + } + + template <typename T> + inline + void + maj_h<T>::take(const maj_h<T>& other) + { + h_.take(other.h_); + + //FIXME: t_ is wrong then + } + + template <typename T> + inline + void + maj_h<T>::update_() const + { + const std::vector<unsigned>& v = h_.vect(); + + for(unsigned i = 0; i != v.size(); i++) + { + // if nb referents of occurrence i > nb referents of t_ + if (v[i] > h_(t_)) + t_ = h_.vset()[i]; // t_ <- current + } + valid_ = true; + } + + template <typename T> + inline + const T& + maj_h<T>::to_result() const + { + if (not valid_) + update_(); + return t_; + } + + template <typename T> + inline + bool + maj_h<T>::is_valid() const + { + return true; + } + + template <typename V> + inline + const accu::histo<V>& + maj_h<V>::histo() const + { + return h_; + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::accu + +} // end of namespace mln + + +#endif // ! MLN_ACCU_MAJ_H _HH Index: mln/accu/mean.hh --- mln/accu/mean.hh (revision 2740) +++ mln/accu/mean.hh (working copy) @@ -61,6 +61,7 @@ struct mean : public mln::accu::internal::base< M , mean<T,S,M> > { typedef T argument; + typedef M result; mean(); @@ -73,6 +74,7 @@ /// Get the value of the accumulator. M to_result() const; + operator M () const; /// Check whether this accu is able to return a result. /// Always true here. @@ -153,6 +155,13 @@ template <typename T, typename S, typename M> inline + mean<T,S,M>::operator M() const + { + return M(sum_.to_result() / count_.to_result()); + } + + template <typename T, typename S, typename M> + inline bool mean<T,S,M>::is_valid() const { Index: mln/fun/x2x/rotation.hh --- mln/fun/x2x/rotation.hh (revision 2740) +++ mln/fun/x2x/rotation.hh (working copy) @@ -189,7 +189,7 @@ xy + zw, 1.f - x2 - z2, yz - xw, 0, xz - yw, yz + xw, 1.f - x2 - y2, 0, 0, 0, 0, 1}; - this->m_(make::mat<4,4,16,float>(t)); + this->m_(make::mat<4,4>(t)); } template <unsigned n, typename C>
participants (1)
-
Ugo Jardonnet