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.

116 lines
3.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_ROLLINGACCUMULATOR_H_
  28. #define TALK_BASE_ROLLINGACCUMULATOR_H_
  29. #include <vector>
  30. #include "talk/base/common.h"
  31. namespace talk_base {
  32. // RollingAccumulator stores and reports statistics
  33. // over N most recent samples.
  34. //
  35. // T is assumed to be an int, long, double or float.
  36. template<typename T>
  37. class RollingAccumulator {
  38. public:
  39. explicit RollingAccumulator(size_t max_count)
  40. : count_(0),
  41. next_index_(0),
  42. sum_(0.0),
  43. sum_2_(0.0),
  44. samples_(max_count) {
  45. }
  46. ~RollingAccumulator() {
  47. }
  48. size_t max_count() const {
  49. return samples_.size();
  50. }
  51. size_t count() const {
  52. return count_;
  53. }
  54. void AddSample(T sample) {
  55. if (count_ == max_count()) {
  56. // Remove oldest sample.
  57. T sample_to_remove = samples_[next_index_];
  58. sum_ -= sample_to_remove;
  59. sum_2_ -= sample_to_remove * sample_to_remove;
  60. } else {
  61. // Increase count of samples.
  62. ++count_;
  63. }
  64. // Add new sample.
  65. samples_[next_index_] = sample;
  66. sum_ += sample;
  67. sum_2_ += sample * sample;
  68. // Update next_index_.
  69. next_index_ = (next_index_ + 1) % max_count();
  70. }
  71. T ComputeSum() const {
  72. return static_cast<T>(sum_);
  73. }
  74. T ComputeMean() const {
  75. if (count_ == 0) {
  76. return static_cast<T>(0);
  77. }
  78. return static_cast<T>(sum_ / count_);
  79. }
  80. // Compute estimated variance. Estimation is more accurate
  81. // as the number of samples grows.
  82. T ComputeVariance() const {
  83. if (count_ == 0) {
  84. return static_cast<T>(0);
  85. }
  86. // Var = E[x^2] - (E[x])^2
  87. double count_inv = 1.0 / count_;
  88. double mean_2 = sum_2_ * count_inv;
  89. double mean = sum_ * count_inv;
  90. return static_cast<T>(mean_2 - (mean * mean));
  91. }
  92. private:
  93. size_t count_;
  94. size_t next_index_;
  95. double sum_; // Sum(x)
  96. double sum_2_; // Sum(x*x)
  97. std::vector<T> samples_;
  98. DISALLOW_COPY_AND_ASSIGN(RollingAccumulator);
  99. };
  100. } // namespace talk_base
  101. #endif // TALK_BASE_ROLLINGACCUMULATOR_H_