2 Star 0 Fork 0

mirrors_beltoforion/Wator-Screensaver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
auStatBuffer.h 7.43 KB
一键复制 编辑 原始数据 按行查看 历史
Ingo Berg 提交于 2015-01-24 00:12 . first commit
/*
WorldOfWator
Copyright 2009 Ingo Berg
This program 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; either version 3 of the License, or
(at your option) any later version.
This program 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef STAT_BUFFER_H
#define STAT_BUFFER_H
#include "auCircularBuffer.h"
namespace au
{
//---------------------------------------------------------------------------
//
// Deklaration: StatisticBuffer
//
// Zweck: Stellt eine Kontainerklasse dar, ber deren Inhalt
// statistische Gren, wie Mittelwert und Varianz
// berechnet werden knnen.
// Da sie auf einem Ringpuffer basiert ist sie ideal fr
// Berechnungen ber ein bewegtes Fenster.
//
//---------------------------------------------------------------------------
template<typename TData>
class StatBuffer : private CircularBuffer<TData>
{
private:
typedef StatBuffer<TData> self_type;
typedef CircularBuffer<TData> parent_type;
public:
using parent_type::Size;
using parent_type::operator[];
using parent_type::ElementAt;
//--- Konstruktor / Destruktor -------------------------------------
StatBuffer(unsigned size=0);
StatBuffer(const self_type &buf);
virtual ~StatBuffer();
virtual void Reset(unsigned a_iSize = 0, const TData &val = TData());
//--- Gre des Puffers ndern -------------------------------------
virtual void Resize(unsigned size);
virtual void Clear();
//--- Hinzufgen von Werten (vorne im Puffer) ----------------------
virtual void Push(const TData& val);
//--- Entfernen (letzten Wert) -------------------------------------
virtual TData Pop();
//--- Abfragen der Statistikdaten ----------------------------------
const TData& MeanValue();
const TData& Variance();
const TData& Min() const;
const TData& Max() const;
private:
TData m_meanValue; // Mittelwert ber den Ringpuffer
TData m_sum;
TData m_sumOfSquares;
TData m_variance;
mutable TData m_min;
mutable TData m_max;
mutable bool m_bUpdateMax;
mutable bool m_bUpdateMin;
void ReCalculate();
};
//---------------------------------------------------------------------------
//
// Implementation: StatisticBuffer
//
//---------------------------------------------------------------------------
template<typename TData>
StatBuffer<TData>::StatBuffer(unsigned size)
:CircularBuffer<TData>(size)
,m_meanValue()
,m_sum()
,m_sumOfSquares()
,m_variance()
,m_min()
,m_max()
,m_bUpdateMax(false)
,m_bUpdateMin(false)
{}
//---------------------------------------------------------------------------
template<typename TData>
StatBuffer<TData>::StatBuffer(const self_type &obj)
:CircularBuffer<TData>(obj)
,m_meanValue(obj.m_meanValue)
,m_sum(obj.m_sum)
,m_sumOfSquares(obj.m_sumOfSquares)
,m_variance(obj.m_variance)
,m_min()
,m_max()
,m_MinBuf(obj.m_MinBuf)
,m_MaxBuf(obj.m_MaxBuf)
,m_bUpdateMax(obj.m_bUpdate)
,m_bUpdateMin(obj.m_bUpdate)
{}
//---------------------------------------------------------------------------
template<typename TData>
StatBuffer<TData>::~StatBuffer()
{}
//---------------------------------------------------------------------------
// Hinzufgen / Entfernen von Werten zum Puffer
//---------------------------------------------------------------------------
template<typename TData>
void StatBuffer<TData>::Push(const TData& val)
{
TData oldVal(Back()); // oldVal: Wert, der aus dem Puffer fallen wird.
parent_type::Push(val);
// Beitrag des neuen Wertes zu den Statistikdaten
m_sum += val;
m_sumOfSquares += val*val;
// Beitrag des berschriebenen zu den Statistikdaten
m_sum -= oldVal;
m_sumOfSquares -= oldVal*oldVal;
m_bUpdateMin = true;
m_bUpdateMax = true;
}
//---------------------------------------------------------------------------
template<typename TData>
TData StatBuffer<TData>::Pop()
{
TData val = parent_type::Pop();
m_bUpdateMin = (val==m_min);
m_bUpdateMax = (val==m_max);
m_sum -= val;
m_sumOfSquares -= val*val;
return val;
}
//---------------------------------------------------------------------------
// Managen des Puffers
//---------------------------------------------------------------------------
/** \brief Reset the buffer */
template<typename TData>
void StatBuffer<TData>::Reset(unsigned a_iSize, const TData &val)
{
parent_type::Reset(a_iSize, val);
ReCalculate();
}
template<typename TData>
void StatBuffer<TData>::Resize(unsigned newSize)
{
parent_type::Resize(newSize);
ReCalculate();
}
//---------------------------------------------------------------------------
template<typename TData>
void StatBuffer<TData>::Clear()
{
parent_type::Clear();
m_meanValue = 0;
m_variance = 0;
m_sum = 0;
m_sumOfSquares = 0;
m_min = 0;
m_max = 0;
m_bUpdateMin = true;
m_bUpdateMax = true;
}
//---------------------------------------------------------------------------
// Neuberechnen der Statistikdaten
//---------------------------------------------------------------------------
template<typename TData>
void StatBuffer<TData>::ReCalculate()
{
if (!Size())
throw InsufficientBufferSize();
m_sum = TData();
m_sumOfSquares = TData();
m_max = m_min = this->operator[](0);
for (unsigned i=0, size = Size(); i<size; ++i)
{
TData val(this->operator[](i));
m_sum += val;
m_sumOfSquares += val*val;
m_min = std::min(m_min, val);
m_max = std::max(m_max, val);
}
m_bUpdateMin = false;
m_bUpdateMax = false;
}
//---------------------------------------------------------------------------
//
//---------------------------------------------------------------------------
template<typename TData>
const TData& StatBuffer<TData>::MeanValue()
{
unsigned s = Size();
if (!s) throw InsufficientBufferSize();
m_meanValue = m_sum / s;
return m_meanValue;
}
//---------------------------------------------------------------------------
template<typename TData>
const TData& StatBuffer<TData>::Variance()
{
unsigned int s = Size();
if (s<2)
throw InsufficientBufferSize();
m_variance = (m_sumOfSquares - (m_sum*m_sum / s)) / (s-1);
return m_variance;
}
//---------------------------------------------------------------------------
template<typename TData>
const TData& StatBuffer<TData>::Min() const
{
if (m_bUpdateMin)
{
m_min = *std::min_element(m_Data.begin(), m_Data.end());
m_bUpdateMin = false;
}
return m_min;
}
//---------------------------------------------------------------------------
template<typename TData>
const TData& StatBuffer<TData>::Max() const
{
if (m_bUpdateMax)
{
m_max = *std::max_element(m_Data.begin(), m_Data.end());
m_bUpdateMax = false;
}
return m_max;
}
} // end of namespace
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_beltoforion/Wator-Screensaver.git
git@gitee.com:mirrors_beltoforion/Wator-Screensaver.git
mirrors_beltoforion
Wator-Screensaver
Wator-Screensaver
master

搜索帮助