blob: 0a212bc5e239fa32c94fd37f3a86f9f23b654c42 [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
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020014#include "absl/types/optional.h"
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020015
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);
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020025 absl::optional<int> Avg(int64_t min_required_samples) const;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020026 absl::optional<int> Max() const;
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020027 void Reset();
28 // Adds all the samples from the |other| SampleCounter as if they were all
29 // individually added using |Add(int)| method.
30 void Add(const SampleCounter& other);
31
Ilya Nikolaevskiy8c688452018-09-11 13:46:22 +020032 protected:
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020033 int64_t sum_ = 0;
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020034 int64_t num_samples_ = 0;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020035 absl::optional<int> max_;
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020036};
37
Ilya Nikolaevskiy8c688452018-09-11 13:46:22 +020038class SampleCounterWithVariance : public SampleCounter {
39 public:
40 SampleCounterWithVariance();
41 ~SampleCounterWithVariance();
42 void Add(int sample);
43 absl::optional<int64_t> Variance(int64_t min_required_samples) const;
44 void Reset();
45 // Adds all the samples from the |other| SampleCounter as if they were all
46 // individually added using |Add(int)| method.
47 void Add(const SampleCounterWithVariance& other);
48
49 private:
50 int64_t sum_squared_ = 0;
51};
52
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020053} // namespace rtc
54#endif // RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_