blob: 18bd36b8f3052a652a1aa7be986d6db3c9717927 [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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
15
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020016#include "absl/types/optional.h"
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020017
18namespace rtc {
19
20// Simple utility class for counting basic statistics (max./avg./variance) on
21// stream of samples.
22class SampleCounter {
23 public:
24 SampleCounter();
25 ~SampleCounter();
26 void Add(int sample);
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020027 absl::optional<int> Avg(int64_t min_required_samples) const;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020028 absl::optional<int> Max() const;
Sergey Silkinbea18ca2018-10-02 16:22:46 +020029 int64_t NumSamples() const;
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020030 void Reset();
31 // Adds all the samples from the |other| SampleCounter as if they were all
32 // individually added using |Add(int)| method.
33 void Add(const SampleCounter& other);
34
Ilya Nikolaevskiy8c688452018-09-11 13:46:22 +020035 protected:
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020036 int64_t sum_ = 0;
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020037 int64_t num_samples_ = 0;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020038 absl::optional<int> max_;
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020039};
40
Ilya Nikolaevskiy8c688452018-09-11 13:46:22 +020041class SampleCounterWithVariance : public SampleCounter {
42 public:
43 SampleCounterWithVariance();
44 ~SampleCounterWithVariance();
45 void Add(int sample);
46 absl::optional<int64_t> Variance(int64_t min_required_samples) const;
47 void Reset();
48 // Adds all the samples from the |other| SampleCounter as if they were all
49 // individually added using |Add(int)| method.
50 void Add(const SampleCounterWithVariance& other);
51
52 private:
53 int64_t sum_squared_ = 0;
54};
55
Ilya Nikolaevskiy0beed5d2018-05-22 10:54:30 +020056} // namespace rtc
57#endif // RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_