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 | |
| 16 | #include "rtc_base/checks.h" |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 17 | #include "rtc_base/numerics/running_statistics.h" |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 21 | // This class extends RunningStatistics by providing GetPercentile() method, |
| 22 | // while slightly adapting the interface. |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 23 | class SamplesStatsCounter { |
| 24 | public: |
| 25 | SamplesStatsCounter(); |
| 26 | ~SamplesStatsCounter(); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 27 | SamplesStatsCounter(const SamplesStatsCounter&); |
| 28 | SamplesStatsCounter& operator=(const SamplesStatsCounter&); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 29 | SamplesStatsCounter(SamplesStatsCounter&&); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 30 | SamplesStatsCounter& operator=(SamplesStatsCounter&&); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 31 | |
| 32 | // Adds sample to the stats in amortized O(1) time. |
| 33 | void AddSample(double value); |
| 34 | |
Sebastian Jansson | d93a004 | 2019-04-09 15:38:07 +0200 | [diff] [blame] | 35 | // Adds samples from another counter. |
| 36 | void AddSamples(const SamplesStatsCounter& other); |
| 37 | |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 38 | // Returns if there are any values in O(1) time. |
| 39 | bool IsEmpty() const { return samples_.empty(); } |
| 40 | |
| 41 | // Returns min in O(1) time. This function may not be called if there are no |
| 42 | // samples. |
| 43 | double GetMin() const { |
| 44 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 45 | return *stats_.GetMin(); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 46 | } |
| 47 | // Returns max in O(1) time. This function may not be called if there are no |
| 48 | // samples. |
| 49 | double GetMax() const { |
| 50 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 51 | return *stats_.GetMax(); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 52 | } |
| 53 | // Returns average in O(1) time. This function may not be called if there are |
| 54 | // no samples. |
| 55 | double GetAverage() const { |
| 56 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 57 | return *stats_.GetMean(); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 58 | } |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 59 | // Returns variance in O(1) time. This function may not be called if there are |
| 60 | // no samples. |
| 61 | double GetVariance() const { |
| 62 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 63 | return *stats_.GetVariance(); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 64 | } |
| 65 | // Returns standard deviation in O(1) time. This function may not be called if |
| 66 | // there are no samples. |
| 67 | double GetStandardDeviation() const { |
| 68 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 69 | return *stats_.GetStandardDeviation(); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 70 | } |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 71 | // Returns percentile in O(nlogn) on first call and in O(1) after, if no |
| 72 | // additions were done. This function may not be called if there are no |
| 73 | // samples. |
| 74 | // |
| 75 | // |percentile| has to be in [0; 1]. 0 percentile is the min in the array and |
| 76 | // 1 percentile is the max in the array. |
| 77 | double GetPercentile(double percentile); |
| 78 | |
| 79 | private: |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 80 | RunningStatistics<double> stats_; |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 81 | std::vector<double> samples_; |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 82 | bool sorted_ = false; |
| 83 | }; |
| 84 | |
| 85 | } // namespace webrtc |
| 86 | |
| 87 | #endif // RTC_BASE_NUMERICS_SAMPLES_STATS_COUNTER_H_ |