Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
671 | werner | 1 | /******************************************************************************************** |
2 | ** iLand - an individual based forest landscape and disturbance model |
||
3 | ** http://iland.boku.ac.at |
||
4 | ** Copyright (C) 2009- Werner Rammer, Rupert Seidl |
||
5 | ** |
||
6 | ** This program is free software: you can redistribute it and/or modify |
||
7 | ** it under the terms of the GNU General Public License as published by |
||
8 | ** the Free Software Foundation, either version 3 of the License, or |
||
9 | ** (at your option) any later version. |
||
10 | ** |
||
11 | ** This program is distributed in the hope that it will be useful, |
||
12 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | ** GNU General Public License for more details. |
||
15 | ** |
||
16 | ** You should have received a copy of the GNU General Public License |
||
17 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
18 | ********************************************************************************************/ |
||
19 | |||
214 | werner | 20 | #include "floatingaverage.h" |
21 | #include <QtCore> |
||
22 | |||
23 | FloatingAverage::FloatingAverage() |
||
24 | { |
||
25 | mCurrentAverage=0; |
||
26 | mSize=0; |
||
27 | mInitValue = 0.; |
||
28 | mPos=-1; |
||
29 | } |
||
30 | |||
31 | void FloatingAverage::setup(const int size, const double InitValue) |
||
32 | { |
||
33 | mInitValue = InitValue; |
||
34 | mSize=size; |
||
35 | mData.resize(mSize); |
||
36 | mPos=-1; |
||
37 | mCurrentAverage=0; |
||
38 | mFilled=false; |
||
39 | for (int i=0; i<size; i++) |
||
40 | mData[i]=mInitValue; |
||
41 | } |
||
42 | |||
43 | |||
44 | double FloatingAverage::add(double add_value) |
||
45 | { |
||
46 | |||
47 | mPos++; |
||
48 | if (mPos>=mSize) { |
||
49 | mPos=0; // rollover again |
||
50 | mFilled=true; |
||
51 | } |
||
52 | mData[mPos]=add_value; |
||
53 | |||
54 | int countto=mSize; |
||
55 | if (!mFilled) |
||
56 | countto=mPos+1; |
||
57 | double sum=0; |
||
58 | for (int i=0;i<countto; i++) |
||
59 | sum+=mData[i]; |
||
60 | if (countto) |
||
61 | mCurrentAverage = sum/countto; |
||
62 | else |
||
63 | mCurrentAverage = mInitValue; // kann sein, wenn als erster wert 0 übergeben wird. |
||
64 | return mCurrentAverage; |
||
65 | } |
||
66 | |||
67 | double FloatingAverage::sum() const |
||
68 | { |
||
69 | if (mFilled) |
||
70 | return mCurrentAverage * mSize; |
||
71 | else |
||
72 | return mCurrentAverage * (mPos+1); |
||
73 | } |