blob: 88495030c9688bf4b9b21062bbb6431562c54514 [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
14#include <limits>
15#include <vector>
16
17#include "rtc_base/checks.h"
18
19namespace webrtc {
20
21class SamplesStatsCounter {
22 public:
23 SamplesStatsCounter();
24 ~SamplesStatsCounter();
25 SamplesStatsCounter(SamplesStatsCounter&);
26 SamplesStatsCounter(SamplesStatsCounter&&);
27
28 // Adds sample to the stats in amortized O(1) time.
29 void AddSample(double value);
30
31 // Returns if there are any values in O(1) time.
32 bool IsEmpty() const { return samples_.empty(); }
33
34 // Returns min in O(1) time. This function may not be called if there are no
35 // samples.
36 double GetMin() const {
37 RTC_DCHECK(!IsEmpty());
38 return min_;
39 }
40 // Returns max in O(1) time. This function may not be called if there are no
41 // samples.
42 double GetMax() const {
43 RTC_DCHECK(!IsEmpty());
44 return max_;
45 }
46 // Returns average in O(1) time. This function may not be called if there are
47 // no samples.
48 double GetAverage() const {
49 RTC_DCHECK(!IsEmpty());
50 return sum_ / samples_.size();
51 }
52 // Returns percentile in O(nlogn) on first call and in O(1) after, if no
53 // additions were done. This function may not be called if there are no
54 // samples.
55 //
56 // |percentile| has to be in [0; 1]. 0 percentile is the min in the array and
57 // 1 percentile is the max in the array.
58 double GetPercentile(double percentile);
59
60 private:
61 std::vector<double> samples_;
62 double min_ = std::numeric_limits<double>::max();
63 double max_ = std::numeric_limits<double>::min();
64 double sum_ = 0;
65 bool sorted_ = false;
66};
67
68} // namespace webrtc
69
70#endif // RTC_BASE_NUMERICS_SAMPLES_STATS_COUNTER_H_