Go to the documentation of this file.00001
00013 #ifndef LGL_STATS_HPP
00014 #define LGL_STATS_HPP
00015
00016 #include <ostream>
00017
00018 namespace jafar {
00019 namespace lgl {
00020
00021 template < class Class >
00022 class Stats {
00024
00025 private :
00026
00027 unsigned int count ;
00028
00029
00030 Class sum ;
00031
00032
00033 Class mean ;
00034
00035
00036 Class min ;
00037
00038
00039 Class max ;
00040
00041 public :
00043 Stats () : count(0)
00044 { }
00045
00046 ~Stats()
00047 { }
00048
00054 void update ( Class const &newValue )
00055 {
00056 count ++;
00057
00058 if ( count == 1 )
00059 {
00060 sum = newValue ;
00061 mean = newValue ;
00062 min = newValue ;
00063 max = newValue ;
00064 }
00065 else
00066 {
00067 sum += newValue ;
00068 mean = Class ( sum / count ) ;
00069 min = ( min > newValue ? newValue : min ) ;
00070 max = ( max < newValue ? newValue : max ) ;
00071 }
00072
00073 }
00074
00078 void reset()
00079 {
00080 count = 0 ;
00081 }
00082
00085 Class getcount() const { return(count); }
00086 Class getsum() const { return(sum); }
00087 Class getmean() const { return(mean); }
00088 Class getmin() const { return(min); }
00089 Class getmax() const { return(max); }
00090
00091 };
00092
00093
00094 template < class Class >
00095 std::ostream& operator<<(std::ostream& out, Stats < Class > const &rhs)
00096 {
00097
00098 out << "Stats over " << rhs.getcount() << " values : [ " ;
00099 if ( rhs.getcount() > 0 )
00100 out << "Sum = " << rhs.getsum() << " | "
00101 << "Mean = " << rhs.getmean() << " | "
00102 << "Min = " << rhs.getmin() << " | "
00103 << "Max = " << rhs.getmax() << " ]" ;
00104 else
00105 out << " no stats ]" ;
00106
00107 out << std::endl ;
00108
00109 return (out);
00110
00111 }
00112
00113 }
00114 }
00115
00116 #endif