blob: a982b2ebf33ad0476a28c4550b645d0f8c96da6d [file] [log] [blame]
Artem Titove4ed6ea2019-01-11 11:02:19 +01001/*
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 Titove6f6a0c2019-02-07 12:14:35 +010014#include <math.h>
Artem Titove4ed6ea2019-01-11 11:02:19 +010015#include <limits>
16#include <vector>
17
18#include "rtc_base/checks.h"
19
20namespace webrtc {
21
22class SamplesStatsCounter {
23 public:
24 SamplesStatsCounter();
25 ~SamplesStatsCounter();
Artem Titove6f6a0c2019-02-07 12:14:35 +010026 SamplesStatsCounter(const SamplesStatsCounter&);
27 SamplesStatsCounter& operator=(const SamplesStatsCounter&);
Artem Titove4ed6ea2019-01-11 11:02:19 +010028 SamplesStatsCounter(SamplesStatsCounter&&);
Artem Titove6f6a0c2019-02-07 12:14:35 +010029 SamplesStatsCounter& operator=(SamplesStatsCounter&&);
Artem Titove4ed6ea2019-01-11 11:02:19 +010030
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 Titove6f6a0c2019-02-07 12:14:35 +010055 // 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 Titove4ed6ea2019-01-11 11:02:19 +010067 // 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 Titove6f6a0c2019-02-07 12:14:35 +010080 double sum_squared_ = 0;
Artem Titove4ed6ea2019-01-11 11:02:19 +010081 bool sorted_ = false;
82};
83
84} // namespace webrtc
85
86#endif // RTC_BASE_NUMERICS_SAMPLES_STATS_COUNTER_H_