1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_ 12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_ 13*d9f75844SAndroid Build Coastguard Worker 14*d9f75844SAndroid Build Coastguard Worker #include <stdint.h> 15*d9f75844SAndroid Build Coastguard Worker 16*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h" 17*d9f75844SAndroid Build Coastguard Worker 18*d9f75844SAndroid Build Coastguard Worker namespace rtc { 19*d9f75844SAndroid Build Coastguard Worker 20*d9f75844SAndroid Build Coastguard Worker // Simple utility class for counting basic statistics (max./avg./variance) on 21*d9f75844SAndroid Build Coastguard Worker // stream of samples. 22*d9f75844SAndroid Build Coastguard Worker class SampleCounter { 23*d9f75844SAndroid Build Coastguard Worker public: 24*d9f75844SAndroid Build Coastguard Worker SampleCounter(); 25*d9f75844SAndroid Build Coastguard Worker ~SampleCounter(); 26*d9f75844SAndroid Build Coastguard Worker void Add(int sample); 27*d9f75844SAndroid Build Coastguard Worker absl::optional<int> Avg(int64_t min_required_samples) const; 28*d9f75844SAndroid Build Coastguard Worker absl::optional<int> Max() const; 29*d9f75844SAndroid Build Coastguard Worker absl::optional<int64_t> Sum(int64_t min_required_samples) const; 30*d9f75844SAndroid Build Coastguard Worker int64_t NumSamples() const; 31*d9f75844SAndroid Build Coastguard Worker void Reset(); 32*d9f75844SAndroid Build Coastguard Worker // Adds all the samples from the `other` SampleCounter as if they were all 33*d9f75844SAndroid Build Coastguard Worker // individually added using `Add(int)` method. 34*d9f75844SAndroid Build Coastguard Worker void Add(const SampleCounter& other); 35*d9f75844SAndroid Build Coastguard Worker 36*d9f75844SAndroid Build Coastguard Worker protected: 37*d9f75844SAndroid Build Coastguard Worker int64_t sum_ = 0; 38*d9f75844SAndroid Build Coastguard Worker int64_t num_samples_ = 0; 39*d9f75844SAndroid Build Coastguard Worker absl::optional<int> max_; 40*d9f75844SAndroid Build Coastguard Worker }; 41*d9f75844SAndroid Build Coastguard Worker 42*d9f75844SAndroid Build Coastguard Worker class SampleCounterWithVariance : public SampleCounter { 43*d9f75844SAndroid Build Coastguard Worker public: 44*d9f75844SAndroid Build Coastguard Worker SampleCounterWithVariance(); 45*d9f75844SAndroid Build Coastguard Worker ~SampleCounterWithVariance(); 46*d9f75844SAndroid Build Coastguard Worker void Add(int sample); 47*d9f75844SAndroid Build Coastguard Worker absl::optional<int64_t> Variance(int64_t min_required_samples) const; 48*d9f75844SAndroid Build Coastguard Worker void Reset(); 49*d9f75844SAndroid Build Coastguard Worker // Adds all the samples from the `other` SampleCounter as if they were all 50*d9f75844SAndroid Build Coastguard Worker // individually added using `Add(int)` method. 51*d9f75844SAndroid Build Coastguard Worker void Add(const SampleCounterWithVariance& other); 52*d9f75844SAndroid Build Coastguard Worker 53*d9f75844SAndroid Build Coastguard Worker private: 54*d9f75844SAndroid Build Coastguard Worker int64_t sum_squared_ = 0; 55*d9f75844SAndroid Build Coastguard Worker }; 56*d9f75844SAndroid Build Coastguard Worker 57*d9f75844SAndroid Build Coastguard Worker } // namespace rtc 58*d9f75844SAndroid Build Coastguard Worker #endif // RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_ 59