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, Inc., 51 Franklin Street, Fifth Floor,
00016 // Boston, MA 02110-1301, 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         // Initialize the members with dummy values to prevent the
00044         // compiler from emitting warnings about uninitialized values.
00045         min_(0), max_(0)
00046       {
00047         reset();
00048       }
00049 
00050       void
00051       operator()(const T& val)
00052       {
00053         if (! valued())
00054           min_ = max_ = val;
00055         else if (val < min_) // FIXME: use a ftor instead of > and <
00056           min_ = val;
00057         else if (val > max_)
00058           max_ = val;
00059         ++count_;
00060       }
00061 
00062       void
00063       reset()
00064       {
00065         count_ = 0;
00066       }
00067 
00069       bool
00070       valued() const
00071       {
00072         return count_;
00073       }
00074 
00076       size_t
00077       count() const
00078       {
00079         return count_;
00080       }
00081 
00083       const T
00084       min() const
00085       {
00086         assertion(valued());
00087         return min_;
00088       }
00090       const T
00091       max() const
00092       {
00093         assertion(valued());
00094         return max_;
00095       }
00096 
00097     protected:
00098       size_t count_;
00099       T min_;
00100       T max_;
00101     };
00102 
00104     template< class T, class C = ntg::float_s >
00105     struct f_moments : f_minmax<T>
00106     {
00107       f_moments() :
00108         // Initialize the members with dummy values to prevent the
00109         // compiler from emitting warnings about uninitialized values.
00110         f_minmax<T>(), sum1_(0), sum2_(0)
00111       {
00112         this->reset();
00113       }
00114 
00115       typedef f_minmax<T> super;
00116 
00117       void
00118       operator()(const T& val)
00119       {
00120         if (! this->valued())
00121           {
00122             sum1_ = ntg_zero_val(C);
00123             sum2_ = ntg_zero_val(C);
00124           }
00125         else
00126           {
00127             sum1_ += val;
00128             sum2_ += C(val) * val;
00129           }
00130         super::operator()(val);
00131       }
00132 
00134       const C
00135       mean() const
00136       {
00137         assertion(this->valued());
00138         return sum1_ / C(this->count());
00139       }
00141       const C
00142       variance() const
00143       {
00144         assertion(this->valued());
00145         return sum2_ / C(this->count()) - mean() * mean();
00146       }
00147 
00148     protected:
00149       C sum1_;
00150       C sum2_;
00151     };
00152 
00153   } // end of namespace utils
00154 
00155 } // end of namespace oln
00156 
00157 #endif // ! OLENA_STAT_HISTOGRAM_HH

Generated on Tue Feb 20 20:20:32 2007 for Olena by  doxygen 1.5.1