Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 1 | /* |
| 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 Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 17 | |
| 18 | namespace rtc { |
| 19 | |
| 20 | // Simple utility class for counting basic statistics (max./avg./variance) on |
| 21 | // stream of samples. |
| 22 | class SampleCounter { |
| 23 | public: |
| 24 | SampleCounter(); |
| 25 | ~SampleCounter(); |
| 26 | void Add(int sample); |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 27 | absl::optional<int> Avg(int64_t min_required_samples) const; |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 28 | absl::optional<int> Max() const; |
Sergey Silkin | 0237106 | 2019-01-31 16:45:42 +0100 | [diff] [blame] | 29 | absl::optional<int64_t> Sum(int64_t min_required_samples) const; |
Sergey Silkin | bea18ca | 2018-10-02 16:22:46 +0200 | [diff] [blame] | 30 | int64_t NumSamples() const; |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 31 | void Reset(); |
| 32 | // Adds all the samples from the |other| SampleCounter as if they were all |
| 33 | // individually added using |Add(int)| method. |
| 34 | void Add(const SampleCounter& other); |
| 35 | |
Ilya Nikolaevskiy | 8c68845 | 2018-09-11 13:46:22 +0200 | [diff] [blame] | 36 | protected: |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 37 | int64_t sum_ = 0; |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 38 | int64_t num_samples_ = 0; |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 39 | absl::optional<int> max_; |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
Ilya Nikolaevskiy | 8c68845 | 2018-09-11 13:46:22 +0200 | [diff] [blame] | 42 | class SampleCounterWithVariance : public SampleCounter { |
| 43 | public: |
| 44 | SampleCounterWithVariance(); |
| 45 | ~SampleCounterWithVariance(); |
| 46 | void Add(int sample); |
| 47 | absl::optional<int64_t> Variance(int64_t min_required_samples) const; |
| 48 | void Reset(); |
| 49 | // Adds all the samples from the |other| SampleCounter as if they were all |
| 50 | // individually added using |Add(int)| method. |
| 51 | void Add(const SampleCounterWithVariance& other); |
| 52 | |
| 53 | private: |
| 54 | int64_t sum_squared_ = 0; |
| 55 | }; |
| 56 | |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 57 | } // namespace rtc |
| 58 | #endif // RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_ |