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