libH/Average.cpp

Go to the documentation of this file.
00001 
00012 /*
00013   
00014   Copyright (c) 2007, Tim Burrell
00015   Initial version Copyright (c) 2005, Alexander Kroeller
00016   
00017   Licensed under the Apache License, Version 2.0 (the "License");
00018   you may not use this file except in compliance with the License.
00019   You may obtain a copy of the License at 
00020 
00021         http://www.apache.org/licenses/LICENSE-2.0
00022 
00023   Unless required by applicable law or agreed to in writing, software
00024   distributed under the License is distributed on an "AS IS" BASIS,
00025   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00026   See the License for the specific language governing permissions and 
00027   limitations under the License. 
00028   
00029 */
00030 
00031 #include "Average.hpp"
00032 
00034 // Namespace
00036 
00037 using namespace std;
00038 using namespace H;
00039 
00041 // Typedefs, enums, etc
00043 
00045 // Construction
00047 
00051 Average::Average(int Size) {
00052         mValues.resize(Size);
00053         mHeadIdx = 0;
00054         mSum = 0.0;
00055         mFillState = 0;
00056 }
00057 
00061 Average::~Average() {
00062 }
00063 
00065 // Class Body
00067 
00072 void Average::push(double Value) {
00073         if (mFillState == mValues.size()) {
00074                 mSum += Value - mValues[mHeadIdx];
00075                 mValues[mHeadIdx] = Value;
00076         } else {
00077                 ++ mFillState;
00078                 mSum += Value;
00079                 mValues[mHeadIdx] = Value;
00080         }
00081         mHeadIdx = (mHeadIdx + 1) % mValues.size();
00082 }
00083 
00088 double Average::average(void) {
00089    if (mFillState == 0)
00090       return 0.0;
00091    else
00092       return mSum / double(mFillState);
00093 }

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3