stat.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_UTILS_STAT_HH
00029 # define OLENA_UTILS_STAT_HH
00030 
00031 # include <oln/basics.hh>
00032 # include <ntg/basics.hh>
00033 
00034 namespace oln {
00035 
00036   namespace utils {
00037 
00039     template< class T >
00040     struct f_minmax : std::unary_function< const T&, void >
00041     {
00042       f_minmax()
00043       {
00044         reset();
00045       }
00046 
00047       void
00048       operator()(const T& val)
00049       {
00050         if (! valued())
00051           min_ = max_ = val;
00052         else if (val < min_) // FIXME: use a ftor instead of > and <
00053           min_ = val;
00054         else if (val > max_)
00055           max_ = val;
00056         ++count_;
00057       }
00058 
00059       void
00060       reset()
00061       {
00062         count_ = 0;
00063       }
00064 
00066       bool
00067       valued() const
00068       {
00069         return count_;
00070       }
00071 
00073       size_t
00074       count() const
00075       {
00076         return count_;
00077       }
00078 
00080       const T
00081       min() const
00082       {
00083         assertion(valued());
00084         return min_;
00085       }
00087       const T
00088       max() const
00089       {
00090         assertion(valued());
00091         return max_;
00092       }
00093 
00094     protected:
00095       size_t count_;
00096       T min_;
00097       T max_;
00098     };
00099 
00101     template< class T, class C = ntg::float_s >
00102     struct f_moments : f_minmax<T>
00103     {
00104       typedef f_minmax<T> super;
00105 
00106       void
00107       operator()(const T& val)
00108       {
00109         if (! this->valued())
00110           {
00111             sum1_ = ntg_zero_val(C);
00112             sum2_ = ntg_zero_val(C);
00113           }
00114         else
00115           {
00116             sum1_ += val;
00117             sum2_ += C(val) * val;
00118           }
00119         super::operator()(val);
00120       }
00121 
00123       const C
00124       mean() const
00125       {
00126         assertion(this->valued());
00127         return sum1_ / C(this->count());
00128       }
00130       const C
00131       variance() const
00132       {
00133         assertion(this->valued());
00134         return sum2_ / C(this->count()) - mean() * mean();
00135       }
00136 
00137     protected:
00138       C sum1_;
00139       C sum2_;
00140     };
00141 
00142   } // end of namespace utils
00143 
00144 } // end of namespace oln
00145 
00146 #endif // ! OLENA_STAT_HISTOGRAM_HH

Generated on Thu Apr 15 20:13:14 2004 for Olena by doxygen 1.3.6-20040222