File indexing completed on 2024-05-19 04:25:10

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