00001 /*************************************************************************** 00002 * The contents of this file are subject to the Mozilla Public * 00003 * License Version 1.1 (the "License"); you may not use this file * 00004 * except in compliance with the License. You may obtain a copy of * 00005 * the License at http://www.mozilla.org/MPL/ * 00006 * * 00007 * Software distributed under the License is distributed on an "AS * 00008 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * 00009 * implied. See the License for the specific language governing * 00010 * rights and limitations under the License. * 00011 * * 00012 * The Original Code is Game Network Framework (GaNeF). * 00013 * * 00014 * The Initial Developers of the Original Code are * 00015 * Lars Langer and Emanuel Greisen * 00016 * Copyright (C) 2005. Lars Langer & Emanuel Greisen * 00017 * All Rights Reserved. * 00018 * * 00019 * Contributor(s): * 00020 * none yet.... * 00021 * * 00022 ***************************************************************************/ 00023 #ifndef AVERAGETIMECALCULATOR_H 00024 #define AVERAGETIMECALCULATOR_H 00025 00026 /** 00027 @brief This template-class can summarize values and keep a running average on them. 00028 The precision, min/max, and default are all set at compile-time. 00029 It is intended for use with milliseconds 00030 00031 @author Lars Langer and Emanuel Greisen 00032 */ 00033 00034 template<int precision=16, int default_value=250, int min_value=10, int max_value=3000> 00035 class AverageTimeCalculator 00036 { 00037 /// Contains the last RRT_TIME_CALCULATION_COUNT RRT-times for this client. 00038 unsigned int avg_data[precision]; 00039 /// Contains the average RRT-time for this client. 00040 unsigned int avg_current_sum; 00041 /// Contains the average RRT-time for this client. 00042 unsigned int avg_current; 00043 /// Contains a cyclic counter for the RRT-calculations 00044 unsigned int avg_data_position; 00045 00046 public: 00047 AverageTimeCalculator() 00048 { 00049 reset(); 00050 }; 00051 00052 public: 00053 /// This will initilize/clear the RRT-info 00054 void reset() 00055 { 00056 // Clear the RRT-infomation data 00057 for(int i = 0; i < precision; i++) 00058 { 00059 avg_data[i] = default_value; 00060 } 00061 avg_current = default_value; 00062 avg_current_sum = default_value * precision; 00063 avg_data_position = 0; 00064 } 00065 /// This will add a value to the set and recalculate a new average 00066 void registerValue(unsigned int val) 00067 { 00068 //std::cout << min_value << "," << val << "," << max_value; 00069 if(val >= min_value && val <= max_value) 00070 { 00071 // Replace oldest RRT 00072 avg_current_sum -= avg_data[avg_data_position]; 00073 avg_current_sum += val; 00074 avg_data[avg_data_position] = val; 00075 // Calculate new average 00076 avg_current = avg_current_sum / precision; 00077 // update cyclic position 00078 avg_data_position = (avg_data_position+1) % precision; 00079 } 00080 //std::cout << " [sum:" << avg_current_sum << ",avg:" << avg_current << "]" << std::endl; 00081 } 00082 /// Will return the current average 00083 unsigned int getAvg() const 00084 { 00085 //NONEED return (avg_current > max_value) ? max_value : ((avg_current < min_value) ? min_value : avg_current); 00086 return avg_current; 00087 } 00088 00089 }; 00090 00091 #endif