![]() |
VR-Forces 4.0.4 Class Documentation
|
00001 /********************************************************************* 00002 ** Copyright (c) 2004 MAK Technologies, Inc. 00003 ** All rights reserved. 00004 *********************************************************************/ 00005 /********************************************************************* 00006 ** $RCSfile: simpleStats.h,v $ $Revision: 1.5 $ $State: Exp $ 00007 *********************************************************************/ 00008 00009 #ifndef simpleStats_H_ 00010 #define simpleStats_H_ 00011 00012 // 00013 // A simple class for maintaining statistics/instrumentation. 00014 // This class intentionally does not provide support for more complex 00015 // computations, nor does it maintain a list of N number of items added. 00016 // This is to ensure that its performance is O(1) and its usage cost low 00017 // enough for use in the most performance critical areas of code. (unlike 00018 // code that has been instrumented by a profiler). 00019 // @note This class is intended ONLY for use with types that support being 00020 // initialized by a constructor taking in 0 as a parameter. Obviously, this 00021 // is intended for use with the basic arithmetic types, such as int and float. 00022 // Additionally, the average must be reportable as a double, as specifying a 00023 // separate template parameter just to specify the return type of the average 00024 // function seemed overly complex. 00025 // 00026 template <typename T> 00027 class DtSimpleStats 00028 { 00029 public: 00030 DtSimpleStats() : myCount(0), mySum(0), myMin(0), myMax(0) {}; 00031 00032 virtual ~DtSimpleStats() {}; 00033 00034 // Adds the specified item. Causes the count, total, and possibly 00035 // the min and max to be updated as well. 00036 void addItem(const T& item); 00037 00038 // @return The current total divided by the current count. 00039 double getAverage() const { return mySum/myCount; } 00040 00041 // @return The current min item added. If no item has been added, 00042 // returns the default for the specified typename T 00043 const T& getMin() const { return myMin; } 00044 00045 // @return The current max item added. If no item has been added, 00046 // returns the default for the specified typename T 00047 const T& getMax() const { return myMax; } 00048 00049 // @return The sum of all items added so far 00050 const T& getSum() const { return mySum; } 00051 00052 // @return The current number of items added. 00053 unsigned int getCount() const { return myCount; } 00054 00055 // Clears the object back to its initial state. 00056 void clear(); 00057 00058 private: 00059 // Not implemented 00060 DtSimpleStats(const DtSimpleStats& original); 00061 DtSimpleStats& operator=(const DtSimpleStats& original); 00062 00063 00064 unsigned int myCount; 00065 T mySum; 00066 T myMin; 00067 T myMax; 00068 }; 00069 00070 00071 //------------------------------------------------------ 00072 // INLINE METHODS 00073 //------------------------------------------------------ 00074 template <typename T> 00075 void DtSimpleStats<T>::addItem(const T& item) 00076 { 00077 ++myCount; 00078 mySum += item; 00079 if (myMax < item || 00080 myCount == 1) 00081 { 00082 myMax = item; 00083 } 00084 if (myMin > item || 00085 myCount == 1) 00086 { 00087 myMin = item; 00088 } 00089 } 00090 00091 00092 template <typename T> 00093 void DtSimpleStats<T>::clear() 00094 { 00095 myCount = 0; 00096 mySum = 0; 00097 myMin = 0; 00098 myMax = 0; 00099 } 00100 00101 #endif