Fix DecayingSample treatment of the window

This commit is contained in:
Vinnie Falco
2014-11-20 10:30:25 -08:00
committed by Nik Bougalis
parent baf0d09455
commit 64d0f7fffd

View File

@@ -20,9 +20,13 @@
#ifndef RIPPLE_BASICS_DECAYINGSAMPLE_H_INCLUDED #ifndef RIPPLE_BASICS_DECAYINGSAMPLE_H_INCLUDED
#define RIPPLE_BASICS_DECAYINGSAMPLE_H_INCLUDED #define RIPPLE_BASICS_DECAYINGSAMPLE_H_INCLUDED
#include <chrono>
namespace ripple { namespace ripple {
/** Sampling function using exponential decay to provide a continuous value. */ /** Sampling function using exponential decay to provide a continuous value.
@tparam The number of seconds in the decay window.
*/
template <int Window, typename Clock> template <int Window, typename Clock>
class DecayingSample class DecayingSample
{ {
@@ -69,26 +73,20 @@ private:
if (m_value != value_type()) if (m_value != value_type())
{ {
typename Clock::duration n (now - m_when); std::size_t elapsed = std::chrono::duration_cast<
std::chrono::seconds>(now - m_when).count();
// A span larger than four times the window decays the // A span larger than four times the window decays the
// value to an insignificant amount so just reset it. // value to an insignificant amount so just reset it.
// //
typename Clock::duration window (Window); if (elapsed > 4 * Window)
if (n > 4 * window)
{ {
m_value = value_type(); m_value = value_type();
} }
else else
{ {
value_type const tick_value = 1; while (elapsed--)
typename Clock::duration const tick (tick_value); m_value -= (m_value + Window - 1) / Window;
typename Clock::duration const zero (0);
while (n > zero)
{
n -= tick;
m_value -= (m_value + Window - tick_value) / Window;
}
} }
} }