1 /*
2 * Copyright (c) 2017 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "KisRollingMeanAccumulatorWrapper.h"
20
21 #include <boost/accumulators/accumulators.hpp>
22 #include <boost/accumulators/statistics/stats.hpp>
23 #include <boost/accumulators/statistics/rolling_mean.hpp>
24
25 using namespace boost::accumulators;
26
27 struct KisRollingMeanAccumulatorWrapper::Private {
PrivateKisRollingMeanAccumulatorWrapper::Private28 Private(int windowSize)
29 : accumulator(tag::rolling_window::window_size = windowSize)
30 {
31 }
32
33 accumulator_set<qreal, stats<tag::lazy_rolling_mean> > accumulator;
34 };
35
36
KisRollingMeanAccumulatorWrapper(int windowSize)37 KisRollingMeanAccumulatorWrapper::KisRollingMeanAccumulatorWrapper(int windowSize)
38 : m_d(new Private(windowSize))
39 {
40 }
41
~KisRollingMeanAccumulatorWrapper()42 KisRollingMeanAccumulatorWrapper::~KisRollingMeanAccumulatorWrapper()
43 {
44 }
45
operator ()(qreal value)46 void KisRollingMeanAccumulatorWrapper::operator()(qreal value)
47 {
48 m_d->accumulator(value);
49 }
50
rollingMean() const51 qreal KisRollingMeanAccumulatorWrapper::rollingMean() const
52 {
53 return boost::accumulators::rolling_mean(m_d->accumulator);
54 }
55
rollingMeanSafe() const56 qreal KisRollingMeanAccumulatorWrapper::rollingMeanSafe() const
57 {
58 return boost::accumulators::rolling_count(m_d->accumulator) > 0 ?
59 boost::accumulators::rolling_mean(m_d->accumulator) : 0;
60 }
61
rollingCount() const62 int KisRollingMeanAccumulatorWrapper::rollingCount() const
63 {
64 return boost::accumulators::rolling_count(m_d->accumulator);
65 }
66
reset(int windowSize)67 void KisRollingMeanAccumulatorWrapper::reset(int windowSize)
68 {
69 m_d->accumulator =
70 accumulator_set<qreal, stats<tag::lazy_rolling_mean>>(
71 tag::rolling_window::window_size = windowSize);
72 }
73