# 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 "settings_type.h"
    00016 #include "core/math_func.hpp"
    00017
    00025 template<class Tvalue>
    00026 class MovingAverage {
    00027 protected:
    00028   uint length;
    00029
    00030 public:
    00035   inline MovingAverage(uint length) : length(length)
    00036   {
    00037     assert(this->length > 0);
    00038   }
    00039
    00044   inline uint Length() const
    00045   {
    00046     return this->length;
    00047   }
    00048
    00059   inline Tvalue Monthly(const Tvalue &value) const
    00060   {
    00061     return (value * 30) / (this->length);
    00062   }
    00063
    00069   inline Tvalue &Decrease(Tvalue &value) const
    00070   {
    00071     return value = (value * this->length) / (this->length + 1);
    00072   }
    00073 };
    00074
    00075 template<class Titem> void RunAverages();
    00076
    00077 #endif /* MOVING_AVERAGE_H_ */
    00078