You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/numerics/sample_counter.h"
  11. #include <limits>
  12. #include "rtc_base/checks.h"
  13. #include "rtc_base/numerics/safe_conversions.h"
  14. namespace rtc {
  15. SampleCounter::SampleCounter() = default;
  16. SampleCounter::~SampleCounter() = default;
  17. void SampleCounter::Add(int sample) {
  18. if (sum_ > 0) {
  19. RTC_DCHECK_LE(sample, std::numeric_limits<int64_t>::max() - sum_);
  20. } else {
  21. RTC_DCHECK_GE(sample, std::numeric_limits<int64_t>::min() - sum_);
  22. }
  23. sum_ += sample;
  24. ++num_samples_;
  25. if (!max_ || sample > *max_) {
  26. max_ = sample;
  27. }
  28. }
  29. void SampleCounter::Add(const SampleCounter& other) {
  30. if (sum_ > 0) {
  31. RTC_DCHECK_LE(other.sum_, std::numeric_limits<int64_t>::max() - sum_);
  32. } else {
  33. RTC_DCHECK_GE(other.sum_, std::numeric_limits<int64_t>::min() - sum_);
  34. }
  35. sum_ += other.sum_;
  36. RTC_DCHECK_LE(other.num_samples_,
  37. std::numeric_limits<int64_t>::max() - num_samples_);
  38. num_samples_ += other.num_samples_;
  39. if (other.max_ && (!max_ || *max_ < *other.max_))
  40. max_ = other.max_;
  41. }
  42. absl::optional<int> SampleCounter::Avg(int64_t min_required_samples) const {
  43. RTC_DCHECK_GT(min_required_samples, 0);
  44. if (num_samples_ < min_required_samples)
  45. return absl::nullopt;
  46. return rtc::dchecked_cast<int>(sum_ / num_samples_);
  47. }
  48. absl::optional<int> SampleCounter::Max() const {
  49. return max_;
  50. }
  51. absl::optional<int64_t> SampleCounter::Sum(int64_t min_required_samples) const {
  52. RTC_DCHECK_GT(min_required_samples, 0);
  53. if (num_samples_ < min_required_samples)
  54. return absl::nullopt;
  55. return sum_;
  56. }
  57. int64_t SampleCounter::NumSamples() const {
  58. return num_samples_;
  59. }
  60. void SampleCounter::Reset() {
  61. *this = {};
  62. }
  63. SampleCounterWithVariance::SampleCounterWithVariance() = default;
  64. SampleCounterWithVariance::~SampleCounterWithVariance() = default;
  65. absl::optional<int64_t> SampleCounterWithVariance::Variance(
  66. int64_t min_required_samples) const {
  67. RTC_DCHECK_GT(min_required_samples, 0);
  68. if (num_samples_ < min_required_samples)
  69. return absl::nullopt;
  70. // E[(x-mean)^2] = E[x^2] - mean^2
  71. int64_t mean = sum_ / num_samples_;
  72. return sum_squared_ / num_samples_ - mean * mean;
  73. }
  74. void SampleCounterWithVariance::Add(int sample) {
  75. SampleCounter::Add(sample);
  76. // Prevent overflow in squaring.
  77. RTC_DCHECK_GT(sample, std::numeric_limits<int32_t>::min());
  78. RTC_DCHECK_LE(int64_t{sample} * sample,
  79. std::numeric_limits<int64_t>::max() - sum_squared_);
  80. sum_squared_ += int64_t{sample} * sample;
  81. }
  82. void SampleCounterWithVariance::Add(const SampleCounterWithVariance& other) {
  83. SampleCounter::Add(other);
  84. RTC_DCHECK_LE(other.sum_squared_,
  85. std::numeric_limits<int64_t>::max() - sum_squared_);
  86. sum_squared_ += other.sum_squared_;
  87. }
  88. void SampleCounterWithVariance::Reset() {
  89. *this = {};
  90. }
  91. } // namespace rtc