1 /*
2 
3 Copyright (c) 2010-2018, Arvid Norberg
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 
10     * Redistributions of source code must retain the above copyright
11       notice, this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in
14       the documentation and/or other materials provided with the distribution.
15     * Neither the name of the author nor the names of its
16       contributors may be used to endorse or promote products derived
17       from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30 
31 */
32 
33 #ifndef TORRENT_SLIDING_AVERAGE_HPP_INCLUDED
34 #define TORRENT_SLIDING_AVERAGE_HPP_INCLUDED
35 
36 #include <cstdint>
37 #include <cstdlib> // for std::abs
38 #include <limits>
39 #include <type_traits> // for is_integral
40 
41 #include "libtorrent/assert.hpp"
42 
43 namespace libtorrent {
44 
45 // an exponential moving average accumulator. Add samples to it and it keeps
46 // track of a moving mean value and an average deviation
47 template <typename Int, Int inverted_gain>
48 struct sliding_average
49 {
50 	static_assert(std::is_integral<Int>::value, "template argument must be integral");
51 
sliding_averagelibtorrent::sliding_average52 	sliding_average(): m_mean(0), m_average_deviation(0), m_num_samples(0) {}
53 	sliding_average(sliding_average const&) = default;
54 	sliding_average& operator=(sliding_average const&) = default;
55 
add_samplelibtorrent::sliding_average56 	void add_sample(Int s)
57 	{
58 		TORRENT_ASSERT(s < std::numeric_limits<Int>::max() / 64);
59 		// fixed point
60 		s *= 64;
61 		Int const deviation = (m_num_samples > 0) ? std::abs(m_mean - s) : 0;
62 
63 		if (m_num_samples < inverted_gain)
64 			++m_num_samples;
65 
66 		m_mean += (s - m_mean) / m_num_samples;
67 
68 		if (m_num_samples > 1) {
69 			// the exact same thing for deviation off the mean except -1 on
70 			// the samples, because the number of deviation samples always lags
71 			// behind by 1 (you need to actual samples to have a single deviation
72 			// sample).
73 			m_average_deviation += (deviation - m_average_deviation) / (m_num_samples - 1);
74 		}
75 	}
76 
meanlibtorrent::sliding_average77 	Int mean() const { return m_num_samples > 0 ? (m_mean + 32) / 64 : 0; }
avg_deviationlibtorrent::sliding_average78 	Int avg_deviation() const { return m_num_samples > 1 ? (m_average_deviation + 32) / 64 : 0; }
num_sampleslibtorrent::sliding_average79 	int num_samples() const { return m_num_samples; }
80 
81 private:
82 	// both of these are fixed point values (* 64)
83 	Int m_mean = 0;
84 	Int m_average_deviation = 0;
85 	// the number of samples we have received, but no more than inverted_gain
86 	// this is the effective inverted_gain
87 	int m_num_samples = 0;
88 };
89 
90 }
91 
92 #endif
93