1 /* 2 * cRunningAverage.cc 3 * Avida 4 * 5 * Called "running_average.cc" prior to 12/7/05. 6 * Copyright 1999-2011 Michigan State University. All rights reserved. 7 * Copyright 1993-2003 California Institute of Technology. 8 * 9 * 10 * This file is part of Avida. 11 * 12 * Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 13 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 14 * 15 * Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public License along with Avida. 19 * If not, see <http://www.gnu.org/licenses/>. 20 * 21 */ 22 23 #include "cRunningAverage.h" 24 25 #include <cassert> 26 27 cRunningAverage(int window_size)28cRunningAverage::cRunningAverage( int window_size ) : 29 m_values(0), m_s1(0), m_s2(0), m_window_size( window_size ), 30 m_pointer(0), m_n(0) 31 { 32 assert( m_window_size > 1 ); 33 m_values = new double[ m_window_size ]; 34 } 35 36 ~cRunningAverage()37cRunningAverage::~cRunningAverage() { 38 delete [] m_values; 39 } 40 41 Add(double value)42void cRunningAverage::Add( double value ) { 43 m_s1 += value; 44 m_s2 += value*value; 45 if ( m_n < m_window_size ) { 46 m_values[ m_n ] = value; 47 m_n += 1; 48 } else { 49 double out_v = m_values[ m_pointer ]; 50 m_s1 -= out_v; 51 m_s2 -= out_v * out_v; 52 m_values[ m_pointer++ ] = value; 53 if ( m_pointer == m_window_size ) m_pointer = 0; 54 } 55 } 56 57 Clear()58void cRunningAverage::Clear() { 59 m_s1 = 0; 60 m_s2 = 0; 61 m_pointer = 0; 62 m_n = 0; 63 } 64