moving_average.h

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef MOVING_AVERAGE_H_
00013 #define MOVING_AVERAGE_H_
00014 
00015 #include "stdafx.h"
00016 #include "settings_type.h"
00017 #include "core/math_func.hpp"
00018 
00026 template<class Tvalue>
00027 class MovingAverage {
00028 protected:
00029   uint length;
00030 
00031 public:
00036   FORCEINLINE MovingAverage(uint length) : length(length)
00037     {
00038       assert(this->length > 0);
00039     }
00040 
00045   FORCEINLINE uint Length() const
00046     {
00047       return this->length;
00048     }
00049 
00060   FORCEINLINE Tvalue Monthly(const Tvalue &value) const
00061   {
00062     return (value * 30) / (this->length);
00063   }
00064 
00070   FORCEINLINE Tvalue &Decrease(Tvalue &value) const
00071   {
00072     return value = (value * this->length) / (this->length + 1);
00073   }
00074 };
00075 
00076 template<class Titem> void RunAverages();
00077 
00078 #endif /* MOVING_AVERAGE_H_ */