blob: 6f79ceeb238f9d3ec55cb3cae6bca2a850047600 [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 Titove4ed6ea2019-01-11 11:02:19 +010014#include <vector>
15
Artem Titov89eaf162019-04-25 12:23:40 +020016#include "api/array_view.h"
Artem Titove4ed6ea2019-01-11 11:02:19 +010017#include "rtc_base/checks.h"
Yves Gerey890f62b2019-04-10 17:18:48 +020018#include "rtc_base/numerics/running_statistics.h"
Artem Titove4ed6ea2019-01-11 11:02:19 +010019
20namespace webrtc {
21
Yves Gerey890f62b2019-04-10 17:18:48 +020022// This class extends RunningStatistics by providing GetPercentile() method,
23// while slightly adapting the interface.
Artem Titove4ed6ea2019-01-11 11:02:19 +010024class SamplesStatsCounter {
25 public:
26 SamplesStatsCounter();
27 ~SamplesStatsCounter();
Artem Titove6f6a0c2019-02-07 12:14:35 +010028 SamplesStatsCounter(const SamplesStatsCounter&);
29 SamplesStatsCounter& operator=(const SamplesStatsCounter&);
Artem Titove4ed6ea2019-01-11 11:02:19 +010030 SamplesStatsCounter(SamplesStatsCounter&&);
Artem Titove6f6a0c2019-02-07 12:14:35 +010031 SamplesStatsCounter& operator=(SamplesStatsCounter&&);
Artem Titove4ed6ea2019-01-11 11:02:19 +010032
33 // Adds sample to the stats in amortized O(1) time.
34 void AddSample(double value);
35
Sebastian Janssond93a0042019-04-09 15:38:07 +020036 // Adds samples from another counter.
37 void AddSamples(const SamplesStatsCounter& other);
38
Artem Titove4ed6ea2019-01-11 11:02:19 +010039 // 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 Gerey890f62b2019-04-10 17:18:48 +020046 return *stats_.GetMin();
Artem Titove4ed6ea2019-01-11 11:02:19 +010047 }
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 Gerey890f62b2019-04-10 17:18:48 +020052 return *stats_.GetMax();
Artem Titove4ed6ea2019-01-11 11:02:19 +010053 }
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 Gerey890f62b2019-04-10 17:18:48 +020058 return *stats_.GetMean();
Artem Titove4ed6ea2019-01-11 11:02:19 +010059 }
Artem Titove6f6a0c2019-02-07 12:14:35 +010060 // 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 Gerey890f62b2019-04-10 17:18:48 +020064 return *stats_.GetVariance();
Artem Titove6f6a0c2019-02-07 12:14:35 +010065 }
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 Gerey890f62b2019-04-10 17:18:48 +020070 return *stats_.GetStandardDeviation();
Artem Titove6f6a0c2019-02-07 12:14:35 +010071 }
Artem Titove4ed6ea2019-01-11 11:02:19 +010072 // 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 Titov89eaf162019-04-25 12:23:40 +020079 // 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 Titove4ed6ea2019-01-11 11:02:19 +010084
85 private:
Yves Gerey890f62b2019-04-10 17:18:48 +020086 RunningStatistics<double> stats_;
Artem Titove4ed6ea2019-01-11 11:02:19 +010087 std::vector<double> samples_;
Artem Titove4ed6ea2019-01-11 11:02:19 +010088 bool sorted_ = false;
89};
90
Artem Titov2c5af4f2019-07-03 10:40:16 +020091// Multiply all sample values on |value| and return new SamplesStatsCounter
92// with resulted samples. Doesn't change origin SamplesStatsCounter.
93SamplesStatsCounter operator*(const SamplesStatsCounter& counter, double value);
94inline 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.
100SamplesStatsCounter operator/(const SamplesStatsCounter& counter, double value);
101
Artem Titove4ed6ea2019-01-11 11:02:19 +0100102} // namespace webrtc
103
104#endif // RTC_BASE_NUMERICS_SAMPLES_STATS_COUNTER_H_