average.h
Go to the documentation of this file.
00001 /* average.h
00002  */
00003 #ifndef _AVERAGE_H
00004 #define _AVERAGE_H
00005 
00006 namespace osl
00007 {
00008   namespace stat
00009   {
00013     class Average
00014     {
00015       double average;
00016       int elements;
00017     public:
00018       // CREATORS
00019       Average() : average(0), elements(0)
00020       {
00021       }
00022       // MANIPULATORS
00027       double add(const double& x)
00028       {
00029         ++elements;
00030         const double diff = x - average;
00031         average += diff/elements;
00032         return diff;
00033       }
00034       void merge(const Average& r) 
00035       {
00036         if (r.elements == 0)
00037           return;
00038         const double sum = average*elements + r.average*r.elements;
00039         elements += r.elements;
00040         average = sum / elements;
00041       }
00042       void clear()
00043       {
00044         average = 0.0;
00045         elements = 0;
00046       }
00047       // ACCESSORS
00048       double getAverage() const { return average; }
00049       int numElements() const   { return elements; }
00050     };
00051   } // namespace stat
00052 } // namespace osl
00053 
00054 
00055 #endif /* _AVERAGE_H */
00056 // ;;; Local Variables:
00057 // ;;; mode:c++
00058 // ;;; c-basic-offset:2
00059 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines