weighted_samples.cc 678 B

123456789101112131415161718192021222324252627
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/base/weighted_samples.h"
  5. namespace remoting {
  6. WeightedSamples::WeightedSamples(double weight_factor)
  7. : weight_factor_(weight_factor) {}
  8. WeightedSamples::~WeightedSamples() = default;
  9. void WeightedSamples::Record(double value) {
  10. weighted_sum_ *= weight_factor_;
  11. weighted_sum_ += value;
  12. weight_ *= weight_factor_;
  13. weight_++;
  14. }
  15. double WeightedSamples::WeightedAverage() const {
  16. if (weight_ == 0) {
  17. return 0;
  18. }
  19. return weighted_sum_ / weight_;
  20. }
  21. } // namespace remoting