Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | |
| 11 | #ifndef RTC_BASE_NUMERICS_SAMPLES_STATS_COUNTER_H_ |
| 12 | #define RTC_BASE_NUMERICS_SAMPLES_STATS_COUNTER_H_ |
| 13 | |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Artem Titov | 89eaf16 | 2019-04-25 12:23:40 +0200 | [diff] [blame] | 16 | #include "api/array_view.h" |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 18 | #include "rtc_base/numerics/running_statistics.h" |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 22 | // This class extends RunningStatistics by providing GetPercentile() method, |
| 23 | // while slightly adapting the interface. |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 24 | class SamplesStatsCounter { |
| 25 | public: |
| 26 | SamplesStatsCounter(); |
| 27 | ~SamplesStatsCounter(); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 28 | SamplesStatsCounter(const SamplesStatsCounter&); |
| 29 | SamplesStatsCounter& operator=(const SamplesStatsCounter&); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 30 | SamplesStatsCounter(SamplesStatsCounter&&); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 31 | SamplesStatsCounter& operator=(SamplesStatsCounter&&); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 32 | |
| 33 | // Adds sample to the stats in amortized O(1) time. |
| 34 | void AddSample(double value); |
| 35 | |
Sebastian Jansson | d93a004 | 2019-04-09 15:38:07 +0200 | [diff] [blame] | 36 | // Adds samples from another counter. |
| 37 | void AddSamples(const SamplesStatsCounter& other); |
| 38 | |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 39 | // Returns if there are any values in O(1) time. |
| 40 | bool IsEmpty() const { return samples_.empty(); } |
| 41 | |
| 42 | // Returns min in O(1) time. This function may not be called if there are no |
| 43 | // samples. |
| 44 | double GetMin() const { |
| 45 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 46 | return *stats_.GetMin(); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 47 | } |
| 48 | // Returns max in O(1) time. This function may not be called if there are no |
| 49 | // samples. |
| 50 | double GetMax() const { |
| 51 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 52 | return *stats_.GetMax(); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 53 | } |
| 54 | // Returns average in O(1) time. This function may not be called if there are |
| 55 | // no samples. |
| 56 | double GetAverage() const { |
| 57 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 58 | return *stats_.GetMean(); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 59 | } |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 60 | // Returns variance in O(1) time. This function may not be called if there are |
| 61 | // no samples. |
| 62 | double GetVariance() const { |
| 63 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 64 | return *stats_.GetVariance(); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 65 | } |
| 66 | // Returns standard deviation in O(1) time. This function may not be called if |
| 67 | // there are no samples. |
| 68 | double GetStandardDeviation() const { |
| 69 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 70 | return *stats_.GetStandardDeviation(); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 71 | } |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 72 | // Returns percentile in O(nlogn) on first call and in O(1) after, if no |
| 73 | // additions were done. This function may not be called if there are no |
| 74 | // samples. |
| 75 | // |
| 76 | // |percentile| has to be in [0; 1]. 0 percentile is the min in the array and |
| 77 | // 1 percentile is the max in the array. |
| 78 | double GetPercentile(double percentile); |
Artem Titov | 89eaf16 | 2019-04-25 12:23:40 +0200 | [diff] [blame] | 79 | // Returns array view with all samples added into counter. There are no |
| 80 | // guarantees of order, so samples can be in different order comparing to in |
| 81 | // which they were added into counter. Also return value will be invalidate |
| 82 | // after call to any non const method. |
| 83 | rtc::ArrayView<const double> GetSamples() const { return samples_; } |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 84 | |
| 85 | private: |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 86 | RunningStatistics<double> stats_; |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 87 | std::vector<double> samples_; |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 88 | bool sorted_ = false; |
| 89 | }; |
| 90 | |
Artem Titov | 2c5af4f | 2019-07-03 10:40:16 +0200 | [diff] [blame^] | 91 | // Multiply all sample values on |value| and return new SamplesStatsCounter |
| 92 | // with resulted samples. Doesn't change origin SamplesStatsCounter. |
| 93 | SamplesStatsCounter operator*(const SamplesStatsCounter& counter, double value); |
| 94 | inline SamplesStatsCounter operator*(double value, |
| 95 | const SamplesStatsCounter& counter) { |
| 96 | return counter * value; |
| 97 | } |
| 98 | // Divide all sample values on |value| and return new SamplesStatsCounter with |
| 99 | // resulted samples. Doesn't change origin SamplesStatsCounter. |
| 100 | SamplesStatsCounter operator/(const SamplesStatsCounter& counter, double value); |
| 101 | |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 102 | } // namespace webrtc |
| 103 | |
| 104 | #endif // RTC_BASE_NUMERICS_SAMPLES_STATS_COUNTER_H_ |