blob: 84d791edd13ab13b1a7622751f148d8f3e9dd559 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_ROLLING_ACCUMULATOR_H_
12#define RTC_BASE_ROLLING_ACCUMULATOR_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <algorithm>
17#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
Yves Gerey3dfb6802019-05-13 17:01:32 +020020#include "rtc_base/numerics/running_statistics.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021
22namespace rtc {
23
24// RollingAccumulator stores and reports statistics
25// over N most recent samples.
26//
27// T is assumed to be an int, long, double or float.
Yves Gerey665174f2018-06-19 15:03:05 +020028template <typename T>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029class RollingAccumulator {
30 public:
Yves Gerey665174f2018-06-19 15:03:05 +020031 explicit RollingAccumulator(size_t max_count) : samples_(max_count) {
Yves Gerey3dfb6802019-05-13 17:01:32 +020032 RTC_DCHECK(max_count > 0);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033 Reset();
34 }
Yves Gerey665174f2018-06-19 15:03:05 +020035 ~RollingAccumulator() {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020036
Byoungchan Lee14af7622022-01-12 05:24:58 +090037 RollingAccumulator(const RollingAccumulator&) = delete;
38 RollingAccumulator& operator=(const RollingAccumulator&) = delete;
39
Yves Gerey665174f2018-06-19 15:03:05 +020040 size_t max_count() const { return samples_.size(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041
Yves Gerey3dfb6802019-05-13 17:01:32 +020042 size_t count() const { return static_cast<size_t>(stats_.Size()); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043
44 void Reset() {
Artem Titov9d777622020-09-18 18:23:08 +020045 stats_ = webrtc::webrtc_impl::RunningStatistics<T>();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020046 next_index_ = 0U;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047 max_ = T();
48 max_stale_ = false;
49 min_ = T();
50 min_stale_ = false;
51 }
52
53 void AddSample(T sample) {
Yves Gerey3dfb6802019-05-13 17:01:32 +020054 if (count() == max_count()) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 // Remove oldest sample.
56 T sample_to_remove = samples_[next_index_];
Yves Gerey3dfb6802019-05-13 17:01:32 +020057 stats_.RemoveSample(sample_to_remove);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058 if (sample_to_remove >= max_) {
59 max_stale_ = true;
60 }
61 if (sample_to_remove <= min_) {
62 min_stale_ = true;
63 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064 }
65 // Add new sample.
66 samples_[next_index_] = sample;
Yves Gerey3dfb6802019-05-13 17:01:32 +020067 if (count() == 0 || sample >= max_) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068 max_ = sample;
69 max_stale_ = false;
70 }
Yves Gerey3dfb6802019-05-13 17:01:32 +020071 if (count() == 0 || sample <= min_) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072 min_ = sample;
73 min_stale_ = false;
74 }
Yves Gerey3dfb6802019-05-13 17:01:32 +020075 stats_.AddSample(sample);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076 // Update next_index_.
77 next_index_ = (next_index_ + 1) % max_count();
78 }
79
Yves Gerey3dfb6802019-05-13 17:01:32 +020080 double ComputeMean() const { return stats_.GetMean().value_or(0); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081
82 T ComputeMax() const {
83 if (max_stale_) {
Yves Gerey3dfb6802019-05-13 17:01:32 +020084 RTC_DCHECK(count() > 0)
85 << "It shouldn't be possible for max_stale_ && count() == 0";
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086 max_ = samples_[next_index_];
Yves Gerey3dfb6802019-05-13 17:01:32 +020087 for (size_t i = 1u; i < count(); i++) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088 max_ = std::max(max_, samples_[(next_index_ + i) % max_count()]);
89 }
90 max_stale_ = false;
91 }
92 return max_;
93 }
94
95 T ComputeMin() const {
96 if (min_stale_) {
Yves Gerey3dfb6802019-05-13 17:01:32 +020097 RTC_DCHECK(count() > 0)
98 << "It shouldn't be possible for min_stale_ && count() == 0";
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020099 min_ = samples_[next_index_];
Yves Gerey3dfb6802019-05-13 17:01:32 +0200100 for (size_t i = 1u; i < count(); i++) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101 min_ = std::min(min_, samples_[(next_index_ + i) % max_count()]);
102 }
103 min_stale_ = false;
104 }
105 return min_;
106 }
107
108 // O(n) time complexity.
109 // Weights nth sample with weight (learning_rate)^n. Learning_rate should be
110 // between (0.0, 1.0], otherwise the non-weighted mean is returned.
111 double ComputeWeightedMean(double learning_rate) const {
Yves Gerey3dfb6802019-05-13 17:01:32 +0200112 if (count() < 1 || learning_rate <= 0.0 || learning_rate >= 1.0) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113 return ComputeMean();
114 }
115 double weighted_mean = 0.0;
116 double current_weight = 1.0;
117 double weight_sum = 0.0;
118 const size_t max_size = max_count();
Yves Gerey3dfb6802019-05-13 17:01:32 +0200119 for (size_t i = 0; i < count(); ++i) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200120 current_weight *= learning_rate;
121 weight_sum += current_weight;
122 // Add max_size to prevent underflow.
123 size_t index = (next_index_ + max_size - i - 1) % max_size;
124 weighted_mean += current_weight * samples_[index];
125 }
126 return weighted_mean / weight_sum;
127 }
128
129 // Compute estimated variance. Estimation is more accurate
130 // as the number of samples grows.
Yves Gerey3dfb6802019-05-13 17:01:32 +0200131 double ComputeVariance() const { return stats_.GetVariance().value_or(0); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132
133 private:
Artem Titov9d777622020-09-18 18:23:08 +0200134 webrtc::webrtc_impl::RunningStatistics<T> stats_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200135 size_t next_index_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200136 mutable T max_;
137 mutable bool max_stale_;
138 mutable T min_;
139 mutable bool min_stale_;
140 std::vector<T> samples_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200141};
142
143} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144
Steve Anton10542f22019-01-11 09:11:00 -0800145#endif // RTC_BASE_ROLLING_ACCUMULATOR_H_