VR-Forces 5.0.1 Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
simpleStats.h
Go to the documentation of this file.
1 /*********************************************************************
2 ** Copyright (c) 2004 MAK Technologies, Inc.
3 ** All rights reserved.
4 *********************************************************************/
5 /*********************************************************************
6 ** $RCSfile: simpleStats.h,v $ $Revision: 1.5 $ $State: Exp $
7 *********************************************************************/
8 
9 #ifndef simpleStats_H_
10 #define simpleStats_H_
11 
12 //
13 // A simple class for maintaining statistics/instrumentation.
14 // This class intentionally does not provide support for more complex
15 // computations, nor does it maintain a list of N number of items added.
16 // This is to ensure that its performance is O(1) and its usage cost low
17 // enough for use in the most performance critical areas of code. (unlike
18 // code that has been instrumented by a profiler).
19 // @note This class is intended ONLY for use with types that support being
20 // initialized by a constructor taking in 0 as a parameter. Obviously, this
21 // is intended for use with the basic arithmetic types, such as int and float.
22 // Additionally, the average must be reportable as a double, as specifying a
23 // separate template parameter just to specify the return type of the average
24 // function seemed overly complex.
25 //
26 template <typename T>
28 {
29 public:
30  DtSimpleStats() : myCount(0), mySum(0), myMin(0), myMax(0) {};
31 
32  virtual ~DtSimpleStats() {};
33 
34  // Adds the specified item. Causes the count, total, and possibly
35  // the min and max to be updated as well.
36  void addItem(const T& item);
37 
38  // @return The current total divided by the current count.
39  double getAverage() const { return mySum/myCount; }
40 
41  // @return The current min item added. If no item has been added,
42  // returns the default for the specified typename T
43  const T& getMin() const { return myMin; }
44 
45  // @return The current max item added. If no item has been added,
46  // returns the default for the specified typename T
47  const T& getMax() const { return myMax; }
48 
49  // @return The sum of all items added so far
50  const T& getSum() const { return mySum; }
51 
52  // @return The current number of items added.
53  unsigned int getCount() const { return myCount; }
54 
55  // Clears the object back to its initial state.
56  void clear();
57 
58 private:
59  // Not implemented
60  DtSimpleStats(const DtSimpleStats& original);
61  DtSimpleStats& operator=(const DtSimpleStats& original);
62 
63 
64  unsigned int myCount;
65  T mySum;
66  T myMin;
67  T myMax;
68 };
69 
70 
71 //------------------------------------------------------
72 // INLINE METHODS
73 //------------------------------------------------------
74 template <typename T>
75 void DtSimpleStats<T>::addItem(const T& item)
76 {
77  ++myCount;
78  mySum += item;
79  if (myMax < item ||
80  myCount == 1)
81  {
82  myMax = item;
83  }
84  if (myMin > item ||
85  myCount == 1)
86  {
87  myMin = item;
88  }
89 }
90 
91 
92 template <typename T>
94 {
95  myCount = 0;
96  mySum = 0;
97  myMin = 0;
98  myMax = 0;
99 }
100 
101 #endif
void addItem(const T &item)
Definition: simpleStats.h:75
T myMin
Definition: simpleStats.h:66
const T & getSum() const
Definition: simpleStats.h:50
const T & getMax() const
Definition: simpleStats.h:47
virtual ~DtSimpleStats()
Definition: simpleStats.h:32
unsigned int myCount
Definition: simpleStats.h:64
DtSimpleStats()
Definition: simpleStats.h:30
void clear()
Definition: simpleStats.h:93
T mySum
Definition: simpleStats.h:65
const T & getMin() const
Definition: simpleStats.h:43
T myMax
Definition: simpleStats.h:67
DtSimpleStats & operator=(const DtSimpleStats &original)
Definition: simpleStats.h:27
double getAverage() const
Definition: simpleStats.h:39
unsigned int getCount() const
Definition: simpleStats.h:53

Document ID: Generated on Mon Jun 20 00:38:30 EDT 2022 from SVN revision 244029
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)