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 | #include <limits> |
| 12 | |
| 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/numerics/safe_conversions.h" |
| 15 | #include "rtc_base/numerics/sample_counter.h" |
| 16 | |
| 17 | namespace rtc { |
| 18 | |
| 19 | SampleCounter::SampleCounter() = default; |
| 20 | SampleCounter::~SampleCounter() = default; |
| 21 | |
| 22 | void SampleCounter::Add(int sample) { |
| 23 | if (sum_ > 0) { |
| 24 | RTC_DCHECK_LE(sample, std::numeric_limits<int64_t>::max() - sum_); |
| 25 | } else { |
| 26 | RTC_DCHECK_GE(sample, std::numeric_limits<int64_t>::min() - sum_); |
| 27 | } |
| 28 | sum_ += sample; |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 29 | ++num_samples_; |
| 30 | if (!max_ || sample > *max_) { |
| 31 | max_ = sample; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void SampleCounter::Add(const SampleCounter& other) { |
| 36 | if (sum_ > 0) { |
| 37 | RTC_DCHECK_LE(other.sum_, std::numeric_limits<int64_t>::max() - sum_); |
| 38 | } else { |
| 39 | RTC_DCHECK_GE(other.sum_, std::numeric_limits<int64_t>::min() - sum_); |
| 40 | } |
| 41 | sum_ += other.sum_; |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 42 | RTC_DCHECK_LE(other.num_samples_, |
| 43 | std::numeric_limits<int64_t>::max() - num_samples_); |
| 44 | num_samples_ += other.num_samples_; |
| 45 | if (other.max_ && (!max_ || *max_ < *other.max_)) |
| 46 | max_ = other.max_; |
| 47 | } |
| 48 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 49 | absl::optional<int> SampleCounter::Avg(int64_t min_required_samples) const { |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 50 | RTC_DCHECK_GT(min_required_samples, 0); |
| 51 | if (num_samples_ < min_required_samples) |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 52 | return absl::nullopt; |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 53 | return rtc::dchecked_cast<int>(sum_ / num_samples_); |
| 54 | } |
| 55 | |
Ilya Nikolaevskiy | 8c68845 | 2018-09-11 13:46:22 +0200 | [diff] [blame] | 56 | absl::optional<int> SampleCounter::Max() const { |
| 57 | return max_; |
| 58 | } |
| 59 | |
Sergey Silkin | 0237106 | 2019-01-31 16:45:42 +0100 | [diff] [blame^] | 60 | absl::optional<int64_t> SampleCounter::Sum(int64_t min_required_samples) const { |
| 61 | RTC_DCHECK_GT(min_required_samples, 0); |
| 62 | if (num_samples_ < min_required_samples) |
| 63 | return absl::nullopt; |
| 64 | return sum_; |
| 65 | } |
| 66 | |
Sergey Silkin | bea18ca | 2018-10-02 16:22:46 +0200 | [diff] [blame] | 67 | int64_t SampleCounter::NumSamples() const { |
| 68 | return num_samples_; |
| 69 | } |
| 70 | |
Ilya Nikolaevskiy | 8c68845 | 2018-09-11 13:46:22 +0200 | [diff] [blame] | 71 | void SampleCounter::Reset() { |
| 72 | *this = {}; |
| 73 | } |
| 74 | |
| 75 | SampleCounterWithVariance::SampleCounterWithVariance() = default; |
| 76 | SampleCounterWithVariance::~SampleCounterWithVariance() = default; |
| 77 | |
| 78 | absl::optional<int64_t> SampleCounterWithVariance::Variance( |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 79 | int64_t min_required_samples) const { |
| 80 | RTC_DCHECK_GT(min_required_samples, 0); |
| 81 | if (num_samples_ < min_required_samples) |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 82 | return absl::nullopt; |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 83 | // E[(x-mean)^2] = E[x^2] - mean^2 |
| 84 | int64_t mean = sum_ / num_samples_; |
| 85 | return sum_squared_ / num_samples_ - mean * mean; |
| 86 | } |
| 87 | |
Ilya Nikolaevskiy | 8c68845 | 2018-09-11 13:46:22 +0200 | [diff] [blame] | 88 | void SampleCounterWithVariance::Add(int sample) { |
| 89 | SampleCounter::Add(sample); |
| 90 | // Prevent overflow in squaring. |
| 91 | RTC_DCHECK_GT(sample, std::numeric_limits<int32_t>::min()); |
| 92 | RTC_DCHECK_LE(int64_t{sample} * sample, |
| 93 | std::numeric_limits<int64_t>::max() - sum_squared_); |
| 94 | sum_squared_ += int64_t{sample} * sample; |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Ilya Nikolaevskiy | 8c68845 | 2018-09-11 13:46:22 +0200 | [diff] [blame] | 97 | void SampleCounterWithVariance::Add(const SampleCounterWithVariance& other) { |
| 98 | SampleCounter::Add(other); |
| 99 | RTC_DCHECK_LE(other.sum_squared_, |
| 100 | std::numeric_limits<int64_t>::max() - sum_squared_); |
| 101 | sum_squared_ += other.sum_squared_; |
| 102 | } |
| 103 | |
| 104 | void SampleCounterWithVariance::Reset() { |
Ilya Nikolaevskiy | 0beed5d | 2018-05-22 10:54:30 +0200 | [diff] [blame] | 105 | *this = {}; |
| 106 | } |
| 107 | |
| 108 | } // namespace rtc |