blob: 0de6f25f19fa99fedad74e28dbd3d404a53e1190 [file] [log] [blame]
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +02001/*
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_SAMPLE_COUNTER_H_
12#define RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_
13
14#include "api/optional.h"
15
16namespace rtc {
17
18// Simple utility class for counting basic statistics (max./avg./variance) on
19// stream of samples.
20class SampleCounter {
21 public:
22 SampleCounter();
23 ~SampleCounter();
24 void Add(int sample);
25 rtc::Optional<int> Avg(int64_t min_required_samples) const;
26 rtc::Optional<int64_t> Variance(int64_t min_required_samples) const;
27 rtc::Optional<int> Max() const;
28 void Reset();
29 // Adds all the samples from the |other| SampleCounter as if they were all
30 // individually added using |Add(int)| method.
31 void Add(const SampleCounter& other);
32
33 private:
34 int64_t sum_ = 0;
35 int64_t sum_squared_ = 0;
36 int64_t num_samples_ = 0;
37 rtc::Optional<int> max_;
38};
39
40} // namespace rtc
41#endif // RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_