average.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
average.h
Go to the documentation of this file.
1 /* average.h
2 */
3 #ifndef _AVERAGE_H
4 #define _AVERAGE_H
5
6 namespace osl
7 {
8 namespace stat
9 {
13 class Average
14 {
15 double mean;
16 int elements;
17 public:
18 // CREATORS
19 Average() : mean(0), elements(0)
20 {
21 }
22 // MANIPULATORS
27 double add(const double& x)
28 {
29 ++elements;
30 const double diff = x - mean;
31 mean += diff/elements;
32 return diff;
33 }
34 void merge(const Average& r)
35 {
36 if (r.elements == 0)
37 return;
38 const double sum = mean*elements + r.mean*r.elements;
39 elements += r.elements;
40 mean = sum / elements;
41 }
42 void clear(double a=0.0, int e=0)
43 {
44 mean = a;
45 elements = e;
46 }
47 // ACCESSORS
48 double getAverage() const { return mean; }
49 double average() const { return mean; }
50 int numElements() const { return elements; }
51 };
52 } // namespace stat
53 } // namespace osl
54
55
56 #endif /* _AVERAGE_H */
57 // ;;; Local Variables:
58 // ;;; mode:c++
59 // ;;; c-basic-offset:2
60 // ;;; End: