• 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::no     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);
00132 
00134         M to_result() const;
00135         operator M () const;
00136 
00139         bool is_valid() const;
00140 
00141       protected:
00142 
00143         accu::math::count<T> count_;
00144         accu::math::sum<T,S>   sum_;
00145       };
00146 
00147 
00148 
00149       template <typename I, typename S, typename M>
00150       struct mean< util::pix<I>, S,M >;
00151 
00152 
00153 # ifndef MLN_INCLUDE_ONLY
00154 
00155       template <typename T, typename S, typename M>
00156       inline
00157       mean<T,S,M>::mean()
00158       {
00159         init();
00160       }
00161 
00162       template <typename T, typename S, typename M>
00163       inline
00164       void
00165       mean<T,S,M>::init()
00166       {
00167         count_.init();
00168         sum_.init();
00169       }
00170 
00171       template <typename T, typename S, typename M>
00172       inline
00173       void mean<T,S,M>::take(const argument& t)
00174       {
00175         count_.take(t);
00176         sum_.take(t);
00177       }
00178 
00179       template <typename T, typename S, typename M>
00180       inline
00181       void
00182       mean<T,S,M>::take(const mean<T,S,M>& other)
00183       {
00184         count_.take(other.count_);
00185         sum_.take(other.sum_);
00186       }
00187 
00188       template <typename T, typename S, typename M>
00189       inline
00190       M
00191       mean<T,S,M>::to_result() const
00192       {
00193         unsigned n = count_.to_result();
00194         if (n == 0u)
00195           return M(); // Safety.
00196         return static_cast<M>(sum_.to_result() / n);
00197       }
00198 
00199       template <typename T, typename S, typename M>
00200       inline
00201       mean<T,S,M>::operator M() const
00202       {
00203         return to_result();
00204       }
00205 
00206       template <typename T, typename S, typename M>
00207       inline
00208       bool
00209       mean<T,S,M>::is_valid() const
00210       {
00211         return count_.to_result() != 0;
00212       }
00213 
00214 # endif // ! MLN_INCLUDE_ONLY
00215 
00216     } // end of namespace mln::accu::stat
00217 
00218   } // end of namespace mln::accu
00219 
00220 } // end of namespace mln
00221 
00222 #endif // ! MLN_ACCU_STAT_MEAN_HH

Generated on Thu Sep 8 2011 18:32:05 for Milena (Olena) by  doxygen 1.7.1