]> AND Private Git Repository - loba.git/blobdiff - statistics.h
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Use static instead of volatile for variables that may be clobbered by a TRY..CATCH.
[loba.git] / statistics.h
index 2978747901e4d7ea5f3e4008f5ff307a92718206..78f93e367da93b8e52d2578ca1277b565026d155 100644 (file)
@@ -1,29 +1,47 @@
 #ifndef STATISTICS_H
 #define STATISTICS_H
 
-#include <vector>
+#include <cmath>
+#include <limits>
 
 class statistics {
 public:
-    statistics();
-
-    void reset();
-    void push(double value);
-
-    size_t get_count();
-    double get_sum();
-    double get_avg();
-    double get_stddev();
+    statistics()
+        : count(0)
+        , min(std::numeric_limits<double>::infinity())
+        , max(-std::numeric_limits<double>::infinity())
+        , sum(0.0)
+        , mean(0.0)
+        , sqdiff_sum(0.0)
+    { }
+
+    void push(double x) {
+        double delta = x - mean;
+        ++count;
+        if (x < min)
+            min = x;
+        if (x > max)
+            max = x;
+        sum += x;
+        mean = sum / count;
+        sqdiff_sum += delta * (x - mean);
+    }
+
+    unsigned get_count() const  { return count;                     }
+    double get_min() const      { return min;                       }
+    double get_max() const      { return max;                       }
+    double get_sum() const      { return sum;                       }
+    double get_mean() const     { return mean;                      }
+    double get_variance() const { return sqdiff_sum / count;        }
+    double get_stddev() const   { return std::sqrt(get_variance()); }
 
 private:
-    bool up2date;
-
-    std::vector<double> values;
-    double sum;
-    double avg;
-    double stddev;
-
-    void update();
+    int count;
+    double min;                 // min of x_i
+    double max;                 // max of x_i
+    double sum;                 // sum of x_i
+    double mean;                // mean of x_i
+    double sqdiff_sum;          // sum of (x_i - mean)^2
 };
 
 #endif // !STATISTICS_H