blob: ac5f12cdea365d3758120600379bf033add441d6 [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
16#include "rtc_base/checks.h"
Yves Gerey890f62b2019-04-10 17:18:48 +020017#include "rtc_base/numerics/running_statistics.h"
Artem Titove4ed6ea2019-01-11 11:02:19 +010018
19namespace webrtc {
20
Yves Gerey890f62b2019-04-10 17:18:48 +020021// This class extends RunningStatistics by providing GetPercentile() method,
22// while slightly adapting the interface.
Artem Titove4ed6ea2019-01-11 11:02:19 +010023class SamplesStatsCounter {
24 public:
25 SamplesStatsCounter();
26 ~SamplesStatsCounter();
Artem Titove6f6a0c2019-02-07 12:14:35 +010027 SamplesStatsCounter(const SamplesStatsCounter&);
28 SamplesStatsCounter& operator=(const SamplesStatsCounter&);
Artem Titove4ed6ea2019-01-11 11:02:19 +010029 SamplesStatsCounter(SamplesStatsCounter&&);
Artem Titove6f6a0c2019-02-07 12:14:35 +010030 SamplesStatsCounter& operator=(SamplesStatsCounter&&);
Artem Titove4ed6ea2019-01-11 11:02:19 +010031
32 // Adds sample to the stats in amortized O(1) time.
33 void AddSample(double value);
34
Sebastian Janssond93a0042019-04-09 15:38:07 +020035 // Adds samples from another counter.
36 void AddSamples(const SamplesStatsCounter& other);
37
Artem Titove4ed6ea2019-01-11 11:02:19 +010038 // 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 Gerey890f62b2019-04-10 17:18:48 +020045 return *stats_.GetMin();
Artem Titove4ed6ea2019-01-11 11:02:19 +010046 }
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 Gerey890f62b2019-04-10 17:18:48 +020051 return *stats_.GetMax();
Artem Titove4ed6ea2019-01-11 11:02:19 +010052 }
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 Gerey890f62b2019-04-10 17:18:48 +020057 return *stats_.GetMean();
Artem Titove4ed6ea2019-01-11 11:02:19 +010058 }
Artem Titove6f6a0c2019-02-07 12:14:35 +010059 // 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 Gerey890f62b2019-04-10 17:18:48 +020063 return *stats_.GetVariance();
Artem Titove6f6a0c2019-02-07 12:14:35 +010064 }
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 Gerey890f62b2019-04-10 17:18:48 +020069 return *stats_.GetStandardDeviation();
Artem Titove6f6a0c2019-02-07 12:14:35 +010070 }
Artem Titove4ed6ea2019-01-11 11:02:19 +010071 // 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 Gerey890f62b2019-04-10 17:18:48 +020080 RunningStatistics<double> stats_;
Artem Titove4ed6ea2019-01-11 11:02:19 +010081 std::vector<double> samples_;
Artem Titove4ed6ea2019-01-11 11:02:19 +010082 bool sorted_ = false;
83};
84
85} // namespace webrtc
86
87#endif // RTC_BASE_NUMERICS_SAMPLES_STATS_COUNTER_H_