00001 #ifndef RUNNING_AVG_IS_INCLUDED 00002 #define RUNNING_AVG_IS_INCLUDED 00003 00004 #include "std/platform.H" 00005 00006 /***************************************************************** 00007 * RunningAvg: 00008 * 00009 * Convenience class for keeping a running average. 00010 * (E.g. average frame rate). 00011 *****************************************************************/ 00012 template <class T> 00013 class RunningAvg { 00014 public: 00015 RunningAvg(const T& null) : _val(null), _num(0) {} 00016 RunningAvg(const T& null, const T& v) : _val(null), _num(0) { add(v); } 00017 00018 // Average in a new item: 00019 void add(const T& v) { 00020 if (_num == 0) { 00021 _val = v; 00022 _num++; 00023 } else { 00024 _val = interp(_val, v, 1.0/++_num); 00025 } 00026 } 00027 00028 // Get the current value of the average: 00029 const T& val() const { return _val; } 00030 00031 // Get the number of items used in the average: 00032 int num() const { return _num; } 00033 00034 protected: 00035 T _val; // the running average 00036 int _num; // the number of items in the average so far 00037 }; 00038 00039 #endif // RUNNING_AVG_IS_INCLUDED 00040 00041 /* end of file run_avg.H */