• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

mean.hh

00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_ACCU_STAT_MEAN_HH
00027 # define MLN_ACCU_STAT_MEAN_HH
00028 
00034 
00035 # include <mln/accu/internal/base.hh>
00036 # include <mln/accu/math/count.hh>
00037 # include <mln/accu/math/sum.hh>
00038 
00039 
00040 namespace mln
00041 {
00042 
00043   namespace accu
00044   {
00045 
00046     namespace stat
00047     {
00048 
00049       // Forward declaration.
00050       template <typename T, typename S, typename M>
00051       struct mean;
00052 
00053     } // end of namespace mln::accu::stat
00054 
00055 
00056     // Meta.
00057 
00058     namespace meta
00059     {
00060 
00061       namespace stat
00062       {
00063 
00065         struct mean : public Meta_Accumulator< mean >
00066         {
00067           template < typename T,
00068                      typename S = mln_sum(T),
00069                      typename M = S >
00070           struct with
00071           {
00072             typedef accu::stat::mean<T,S,M> ret;
00073           };
00074         };
00075 
00076       } // end of namespace mln::accu::meta::stat
00077 
00078     } // end of namespace mln::accu::meta
00079 
00080   } // end of namespace mln::accu
00081 
00082 
00083   // Traits.
00084 
00085   namespace trait
00086   {
00087 
00088     template <typename T, typename S, typename M>
00089     struct accumulator_< accu::stat::mean<T, S, M> >
00090     {
00091       typedef accumulator::has_untake::yes     has_untake;
00092       typedef accumulator::has_set_value::no  has_set_value;
00093       typedef accumulator::has_stop::no       has_stop;
00094       typedef accumulator::when_pix::use_v      when_pix;
00095     };
00096 
00097   } // end of namespace mln::trait
00098 
00099 
00100   namespace accu
00101   {
00102 
00103     namespace stat
00104     {
00105 
00107 
00116       template <typename T,
00117                 typename S = mln_sum(T),
00118                 typename M = S>
00119       struct mean : public mln::accu::internal::base< M , mean<T,S,M> >
00120       {
00121         typedef T argument;
00122         typedef M result;
00123 
00124         mean();
00125 
00128         void init();
00129         void take(const argument& t);
00130         void take(const mean<T,S,M>& other);
00131         void untake(const argument& t);
00132         void untake(const mean<T,S,M>& other);
00134 
00136         M to_result() const;
00137         operator M () const;
00138 
00141         bool is_valid() const;
00142 
00144         mln_result(accu::math::count<T>) count() const;
00145 
00147         mln_result(accu::math::sum<T>) sum() const;
00148 
00149       protected:
00150 
00151         accu::math::count<T> count_;
00152         accu::math::sum<T,S>   sum_;
00153       };
00154 
00155 
00156 
00157       template <typename I, typename S, typename M>
00158       struct mean< util::pix<I>, S,M >;
00159 
00160 
00161 # ifndef MLN_INCLUDE_ONLY
00162 
00163       template <typename T, typename S, typename M>
00164       inline
00165       mean<T,S,M>::mean()
00166       {
00167         init();
00168       }
00169 
00170       template <typename T, typename S, typename M>
00171       inline
00172       void
00173       mean<T,S,M>::init()
00174       {
00175         count_.init();
00176         sum_.init();
00177       }
00178 
00179       template <typename T, typename S, typename M>
00180       inline
00181       void mean<T,S,M>::take(const argument& t)
00182       {
00183         count_.take(t);
00184         sum_.take(t);
00185       }
00186 
00187       template <typename T, typename S, typename M>
00188       inline
00189       void
00190       mean<T,S,M>::take(const mean<T,S,M>& other)
00191       {
00192         count_.take(other.count_);
00193         sum_.take(other.sum_);
00194       }
00195 
00196       template <typename T, typename S, typename M>
00197       inline
00198       void mean<T,S,M>::untake(const argument& t)
00199       {
00200         count_.untake(t);
00201         sum_.untake(t);
00202       }
00203 
00204       template <typename T, typename S, typename M>
00205       inline
00206       void
00207       mean<T,S,M>::untake(const mean<T,S,M>& other)
00208       {
00209         count_.untake(other.count_);
00210         sum_.untake(other.sum_);
00211       }
00212 
00213       template <typename T, typename S, typename M>
00214       inline
00215       M
00216       mean<T,S,M>::to_result() const
00217       {
00218         unsigned n = count_.to_result();
00219         if (n == 0u)
00220           return M(); // Safety.
00221         return static_cast<M>(sum_.to_result() / n);
00222       }
00223 
00224       template <typename T, typename S, typename M>
00225       inline
00226       mean<T,S,M>::operator M() const
00227       {
00228         return to_result();
00229       }
00230 
00231       template <typename T, typename S, typename M>
00232       inline
00233       bool
00234       mean<T,S,M>::is_valid() const
00235       {
00236         return count_.to_result() != 0;
00237       }
00238 
00239       template <typename T, typename S, typename M>
00240       inline
00241       mln_result(accu::math::count<T>)
00242       mean<T,S,M>::count() const
00243       {
00244         return count_.to_result();
00245       }
00246 
00247 
00248       template <typename T, typename S, typename M>
00249       inline
00250       mln_result(accu::math::sum<T>)
00251       mean<T,S,M>::sum() const
00252       {
00253         return sum_.to_result();
00254       }
00255 
00256 # endif // ! MLN_INCLUDE_ONLY
00257 
00258     } // end of namespace mln::accu::stat
00259 
00260   } // end of namespace mln::accu
00261 
00262 } // end of namespace mln
00263 
00264 #endif // ! MLN_ACCU_STAT_MEAN_HH

Generated on Tue Oct 4 2011 15:24:04 for Milena (Olena) by  doxygen 1.7.1