File indexing completed on 2024-05-12 15:57:03

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KisRollingMeanAccumulatorWrapper.h"
0008 
0009 #include <boost/accumulators/accumulators.hpp>
0010 #include <boost/accumulators/statistics/stats.hpp>
0011 #include <boost/accumulators/statistics/rolling_mean.hpp>
0012 
0013 using namespace boost::accumulators;
0014 
0015 struct KisRollingMeanAccumulatorWrapper::Private {
0016     Private(int windowSize)
0017         : accumulator(tag::rolling_window::window_size = windowSize)
0018     {
0019     }
0020 
0021     accumulator_set<qreal, stats<tag::lazy_rolling_mean> > accumulator;
0022 };
0023 
0024 
0025 KisRollingMeanAccumulatorWrapper::KisRollingMeanAccumulatorWrapper(int windowSize)
0026     : m_d(new Private(windowSize))
0027 {
0028 }
0029 
0030 KisRollingMeanAccumulatorWrapper::~KisRollingMeanAccumulatorWrapper()
0031 {
0032 }
0033 
0034 void KisRollingMeanAccumulatorWrapper::operator()(qreal value)
0035 {
0036     m_d->accumulator(value);
0037 }
0038 
0039 qreal KisRollingMeanAccumulatorWrapper::rollingMean() const
0040 {
0041     return boost::accumulators::rolling_mean(m_d->accumulator);
0042 }
0043 
0044 qreal KisRollingMeanAccumulatorWrapper::rollingMeanSafe() const
0045 {
0046     return boost::accumulators::rolling_count(m_d->accumulator) > 0 ?
0047         boost::accumulators::rolling_mean(m_d->accumulator) : 0;
0048 }
0049 
0050 int KisRollingMeanAccumulatorWrapper::rollingCount() const
0051 {
0052     return boost::accumulators::rolling_count(m_d->accumulator);
0053 }
0054 
0055 void KisRollingMeanAccumulatorWrapper::reset(int windowSize)
0056 {
0057     m_d->accumulator =
0058         accumulator_set<qreal, stats<tag::lazy_rolling_mean>>(
0059             tag::rolling_window::window_size = windowSize);
0060 }