blob: 015229b04c7aa95392cae2980c88a844c2ca3331 [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"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/constructor_magic.h"
Yves Gerey3dfb6802019-05-13 17:01:32 +020021#include "rtc_base/numerics/running_statistics.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022
23namespace rtc {
24
25// RollingAccumulator stores and reports statistics
26// over N most recent samples.
27//
28// T is assumed to be an int, long, double or float.
Yves Gerey665174f2018-06-19 15:03:05 +020029template <typename T>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030class RollingAccumulator {
31 public:
Yves Gerey665174f2018-06-19 15:03:05 +020032 explicit RollingAccumulator(size_t max_count) : samples_(max_count) {
Yves Gerey3dfb6802019-05-13 17:01:32 +020033 RTC_DCHECK(max_count > 0);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034 Reset();
35 }
Yves Gerey665174f2018-06-19 15:03:05 +020036 ~RollingAccumulator() {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037
Yves Gerey665174f2018-06-19 15:03:05 +020038 size_t max_count() const { return samples_.size(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039
Yves Gerey3dfb6802019-05-13 17:01:32 +020040 size_t count() const { return static_cast<size_t>(stats_.Size()); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041
42 void Reset() {
Yves Gerey3dfb6802019-05-13 17:01:32 +020043 stats_ = webrtc::RunningStatistics<T>();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044 next_index_ = 0U;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045 max_ = T();
46 max_stale_ = false;
47 min_ = T();
48 min_stale_ = false;
49 }
50
51 void AddSample(T sample) {
Yves Gerey3dfb6802019-05-13 17:01:32 +020052 if (count() == max_count()) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053 // Remove oldest sample.
54 T sample_to_remove = samples_[next_index_];
Yves Gerey3dfb6802019-05-13 17:01:32 +020055 stats_.RemoveSample(sample_to_remove);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056 if (sample_to_remove >= max_) {
57 max_stale_ = true;
58 }
59 if (sample_to_remove <= min_) {
60 min_stale_ = true;
61 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062 }
63 // Add new sample.
64 samples_[next_index_] = sample;
Yves Gerey3dfb6802019-05-13 17:01:32 +020065 if (count() == 0 || sample >= max_) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066 max_ = sample;
67 max_stale_ = false;
68 }
Yves Gerey3dfb6802019-05-13 17:01:32 +020069 if (count() == 0 || sample <= min_) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070 min_ = sample;
71 min_stale_ = false;
72 }
Yves Gerey3dfb6802019-05-13 17:01:32 +020073 stats_.AddSample(sample);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074 // Update next_index_.
75 next_index_ = (next_index_ + 1) % max_count();
76 }
77
Yves Gerey3dfb6802019-05-13 17:01:32 +020078 double ComputeMean() const { return stats_.GetMean().value_or(0); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079
80 T ComputeMax() const {
81 if (max_stale_) {
Yves Gerey3dfb6802019-05-13 17:01:32 +020082 RTC_DCHECK(count() > 0)
83 << "It shouldn't be possible for max_stale_ && count() == 0";
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084 max_ = samples_[next_index_];
Yves Gerey3dfb6802019-05-13 17:01:32 +020085 for (size_t i = 1u; i < count(); i++) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086 max_ = std::max(max_, samples_[(next_index_ + i) % max_count()]);
87 }
88 max_stale_ = false;
89 }
90 return max_;
91 }
92
93 T ComputeMin() const {
94 if (min_stale_) {
Yves Gerey3dfb6802019-05-13 17:01:32 +020095 RTC_DCHECK(count() > 0)
96 << "It shouldn't be possible for min_stale_ && count() == 0";
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097 min_ = samples_[next_index_];
Yves Gerey3dfb6802019-05-13 17:01:32 +020098 for (size_t i = 1u; i < count(); i++) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020099 min_ = std::min(min_, samples_[(next_index_ + i) % max_count()]);
100 }
101 min_stale_ = false;
102 }
103 return min_;
104 }
105
106 // O(n) time complexity.
107 // Weights nth sample with weight (learning_rate)^n. Learning_rate should be
108 // between (0.0, 1.0], otherwise the non-weighted mean is returned.
109 double ComputeWeightedMean(double learning_rate) const {
Yves Gerey3dfb6802019-05-13 17:01:32 +0200110 if (count() < 1 || learning_rate <= 0.0 || learning_rate >= 1.0) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111 return ComputeMean();
112 }
113 double weighted_mean = 0.0;
114 double current_weight = 1.0;
115 double weight_sum = 0.0;
116 const size_t max_size = max_count();
Yves Gerey3dfb6802019-05-13 17:01:32 +0200117 for (size_t i = 0; i < count(); ++i) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118 current_weight *= learning_rate;
119 weight_sum += current_weight;
120 // Add max_size to prevent underflow.
121 size_t index = (next_index_ + max_size - i - 1) % max_size;
122 weighted_mean += current_weight * samples_[index];
123 }
124 return weighted_mean / weight_sum;
125 }
126
127 // Compute estimated variance. Estimation is more accurate
128 // as the number of samples grows.
Yves Gerey3dfb6802019-05-13 17:01:32 +0200129 double ComputeVariance() const { return stats_.GetVariance().value_or(0); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200130
131 private:
Yves Gerey3dfb6802019-05-13 17:01:32 +0200132 webrtc::RunningStatistics<T> stats_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200133 size_t next_index_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200134 mutable T max_;
135 mutable bool max_stale_;
136 mutable T min_;
137 mutable bool min_stale_;
138 std::vector<T> samples_;
139
140 RTC_DISALLOW_COPY_AND_ASSIGN(RollingAccumulator);
141};
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_