KrisLibrary  1.0.0
StatCollector.h
1 #ifndef UTILS_STAT_COLLECTOR_H
2 #define UTILS_STAT_COLLECTOR_H
3 
4 #include <KrisLibrary/Logger.h>
5 #include <KrisLibrary/utils.h>
6 #include <math.h>
7 #include <ostream>
8 #include <map>
9 #include <vector>
10 #include <string>
11 
28 {
29  StatCollector();
30  inline void operator << (double x) { collect(x); }
31  void collect(double x);
32  void weightedCollect(double x,double weight);
33 
34  int number() const { return (int)n; }
35  double minimum() const { return xmin; }
36  double maximum() const { return xmax; }
37  double average() const { return sum/n; }
38  double variance() const {
39  double avg=average();
40  return Max(sumsquared/n - avg*avg,0.0); //may have numerical errors...
41  }
42  double stddev() const { return sqrt(variance()); }
43  void clear();
44 
45  bool Load(std::istream& in);
46  bool Save(std::ostream& out) const;
47  void Print(std::ostream& out) const;
48 
49  double n;
50  double xmin,xmax;
51  double sum,sumsquared;
52 };
53 
59 {
60  struct Data
61  {
62  Data() : count(0) {}
63 
64  int count;
65  StatCollector value;
66  std::map<std::string,Data> children;
67  };
68 
69  StatDatabase();
70  void Clear();
71  void Increment(const std::string& name,int inc=1);
72  void AddValue(const std::string& name,double val);
73  int GetCount(const std::string& name) const;
74  const StatCollector& GetValue(const std::string& name) const;
75 
76  Data& AddData(const std::string& name);
77  const Data* GetData(const std::string& name) const;
78 
79  bool Load(std::istream& in);
80  bool Save(std::ostream& out) const;
81  void Print(std::ostream& out) const;
82 
83  //convenience functions
84  void Increment(const std::string& name,const std::string& child,int inc=1) { Increment(Concat(name,child),inc); }
85  void Increment(const std::string& name,const std::string& child1,const std::string& child2,int inc=1) { Increment(Concat(name,child1,child2),inc); }
86  void Increment(const std::string& n,const std::string& c1,const std::string& c2,const std::string& c3,int inc=1) { Increment(Concat(n,c1,c2,c3),inc); }
87  void AddValue(const std::string& name,const std::string& child,double val) { AddValue(Concat(name,child),val); }
88  void AddValue(const std::string& name,const std::string& child1,const std::string& child2,double val) { AddValue(Concat(name,child1,child2),val); }
89  void AddValue(const std::string& n,const std::string& c1,const std::string& c2,const std::string& c3,double val) { AddValue(Concat(n,c1,c2,c3),val); }
90 
91 
92  //helpers
93  void NameToPath(const std::string& name,std::vector<std::string>& path) const;
94  std::string Concat(const std::string& s1,const std::string& s2) const;
95  std::string Concat(const std::string& s1,const std::string& s2,const std::string& s3) const;
96  std::string Concat(const std::string& s1,const std::string& s2,const std::string& s3,const std::string& s4) const;
97  std::string Concat(const std::vector<std::string>& s) const;
98 
99  unsigned char delim;
100  Data root;
101 };
102 
103 #endif
Common math typedefs, constants, functions.
A heirarchical database of statistical data (counts and StatCollectors). Useful for performance testi...
Definition: StatCollector.h:58
Utilities commonly used throughout a program.
The logging system used in KrisLibrary.
Definition: StatCollector.h:60
Collects statistics (min,max,mean,stddev,etc) on floating-point data.
Definition: StatCollector.h:27