1 
2 /* Web Polygraph       http://www.web-polygraph.org/
3  * Copyright 2003-2011 The Measurement Factory
4  * Licensed under the Apache License, Version 2.0 */
5 
6 #ifndef POLYGRAPH__BASE_AGGRSTAT_H
7 #define POLYGRAPH__BASE_AGGRSTAT_H
8 
9 #include "xstd/h/iosfwd.h"
10 #include "xstd/h/stdint.h"
11 #include "base/OLog.h"
12 #include "base/ILog.h"
13 
14 
15 #ifdef min
16 #undef min
17 #endif
18 
19 #ifdef max
20 #undef max
21 #endif
22 
23 // simple aggregate statisitcs
24 class AggrStat {
25 	public:
26 		typedef int64_t Val;
27 
28 	public:
29 		AggrStat();
30 
31 		void reset();
32 		void store(OLog &log) const;
33 		void load(ILog &log);
34 		bool sane() const;
35 
36 		void record(Val val);
37 
38 		AggrStat &operator +=(const AggrStat &s);
39 
known()40 		bool known() const { return count() > 0; }
count()41 		Counter count() const { return theCount; }
min()42 		Val min() const { return theMin; }
max()43 		Val max() const { return theMax; }
sum()44 		double sum() const { return theSum; }
mean()45 		double mean() const { return theCount ? theSum/theCount : -1; }
46 		double stdDev() const;
47 		double relDevp() const;
48 
49 		ostream &print(ostream &os, const String &pfx) const;
50 
51 	protected:
52 		Counter theCount;
53 		Val theMax;
54 		Val theMin;
55 		double theSum;
56 		double theSqSum;
57 };
58 
59 inline
60 ostream &operator <<(ostream &os, const AggrStat &s) { return s.print(os, ""); }
61 
62 inline
63 AggrStat operator +(const AggrStat &s1, const AggrStat &s2) {
64 	AggrStat s(s1);
65 	return s += s2;
66 }
67 
68 inline
69 OLog &operator <<(OLog &log, const AggrStat &s) { s.store(log); return log; }
70 
71 inline
72 ILog &operator >>(ILog &log, AggrStat &s) { s.load(log); return log; }
73 
74 #endif
75