Main   Class Hierarchy   Classes   Compound List   Files   Compound Members   File Members   Pages  

statistics.cpp

Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 /** @file statistics.cpp
00003     @see statistics.h
00004 
00005     $Id: statistics_8cpp-source.html,v 1.1.1.1 2003/08/07 19:41:39 emarkus Exp $
00006     $Source: /usr/cvsroot/www_pathfind/libpathfind/0.1.0/doc/statistics_8cpp-source.html,v $
00007 */
00008 //-----------------------------------------------------------------------------
00009 
00010 #include "statistics.h"
00011 
00012 #include <sstream>
00013 #include <time.h>
00014 #include "error.h"
00015 
00016 using namespace std;
00017 using namespace PathFind;
00018 
00019 //-----------------------------------------------------------------------------
00020 
00021 Statistics::Statistics()
00022 {
00023     clear();
00024 }
00025 
00026 void Statistics::add(double value)
00027 {
00028     m_count += 1.0;
00029     m_sum += value;
00030     m_sumSq += (value * value);
00031 }
00032 
00033 void Statistics::add(const Statistics& statistics)
00034 {
00035     m_count += statistics.m_count;
00036     m_sum += statistics.m_sum;
00037     m_sumSq += statistics.m_sumSq;
00038 }
00039 
00040 void Statistics::clear()
00041 {
00042     m_count = 0;
00043     m_sum = 0;
00044     m_sumSq = 0;
00045 }
00046 
00047 double Statistics::getVariance() const
00048 {
00049     double mean = getMean();
00050     return m_sumSq / m_count - mean * mean;
00051 }
00052 
00053 void Statistics::print(ostream& o) const
00054 {
00055     o << getMean() << ' ' << getDeviation();
00056 }
00057 
00058 //-----------------------------------------------------------------------------
00059 
00060 void StatisticsCollection::add(const StatisticsCollection& collection)
00061 {
00062     if (m_map.size() != collection.m_map.size())
00063         throw Error("Trying to add incompatible statistics collections");
00064     map<string,Statistics>::iterator p;
00065     for (p = m_map.begin(); p != m_map.end(); ++p)
00066     {
00067         map<string,Statistics>::const_iterator k
00068             = collection.m_map.find(p->first);
00069         if (k == collection.m_map.end())
00070             throw Error("Trying to add incompatible statistics collections");
00071         p->second.add(k->second);
00072     }
00073 }
00074 
00075 void StatisticsCollection::clear()
00076 {
00077     map<string,Statistics>::iterator p;
00078     for (p = m_map.begin(); p != m_map.end(); ++p)
00079         p->second.clear();
00080 }
00081 
00082 void StatisticsCollection::create(const string& name)
00083 {
00084     m_map[name] = Statistics();
00085 }
00086 
00087 const Statistics& StatisticsCollection::get(const string& name) const
00088 {
00089     map<string,Statistics>::const_iterator p = m_map.find(name);
00090     if (p == m_map.end())
00091     {
00092         ostringstream o;
00093         o << "Unknown statistics name " << name << '.';
00094         throw Error(o.str());
00095     }
00096     return p->second;
00097 }
00098 
00099 
00100 Statistics& StatisticsCollection::get(const string& name)
00101 {
00102     map<string,Statistics>::iterator p = m_map.find(name);
00103     if (p == m_map.end())
00104     {
00105         ostringstream o;
00106         o << "Unknown statistics name " << name << '.';
00107         throw Error(o.str());
00108     }
00109     return p->second;
00110 }
00111 
00112 void StatisticsCollection::print(ostream& o) const
00113 {
00114     map<string,Statistics>::const_iterator p;
00115     for (p = m_map.begin(); p != m_map.end(); ++p)
00116     {
00117         string name = p->first;
00118         const Statistics& statistics = p->second;
00119         o << name << ' ';
00120         statistics.print(o);
00121         o << '\n';
00122     }
00123 }
00124 
00125 //-----------------------------------------------------------------------------
00126 
00127 #if 0
00128 
00129 void check(bool expression, int line)
00130 {
00131     if (! expression)
00132         cerr << "Error line " << line << '\n';
00133 }
00134 
00135 int main()
00136 {
00137     Statistics statistics;
00138     statistics.add(1.0);
00139     statistics.add(2.0);
00140     statistics.add(3.0);
00141     check(fabs(statistics.getMean() - 2.000) < 0.001, __LINE__);
00142     check(fabs(statistics.getDeviation() - 0.816) < 0.001, __LINE__);
00143     
00144     Statistics statistics2;
00145     statistics2.add(-1.0);
00146     statistics2.add(2.5);
00147     statistics2.add(2.5);
00148     statistics2.add(2.7);
00149     check(fabs(statistics2.getMean() - 1.675) < 0.001, __LINE__);
00150     check(fabs(statistics2.getDeviation() - 1.547) < 0.001, __LINE__);
00151     
00152     statistics.add(statistics2);
00153     check(fabs(statistics.getMean() - 1.814) < 0.001, __LINE__);
00154     check(fabs(statistics.getDeviation() - 1.296) < 0.001, __LINE__);
00155     return 0;
00156 }
00157 
00158 #endif
00159 
00160 //-----------------------------------------------------------------------------


Generated on Thu Aug 7 13:04:50 2003 by Doxygen1.3.1