Chromium Code Reviews| Index: media/base/moving_average.cc |
| diff --git a/media/base/moving_average.cc b/media/base/moving_average.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..22f9b8299e4e37082de7a1c13d96d301a97415a8 |
| --- /dev/null |
| +++ b/media/base/moving_average.cc |
| @@ -0,0 +1,41 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/base/moving_average.h" |
| + |
| +#include <algorithm> |
| + |
| +namespace media { |
| + |
| +MovingAverage::MovingAverage(size_t depth) |
| + : depth_(depth), |
| + count_(0), |
| + samples_(new base::TimeDelta[depth]) { |
| +} |
| + |
| +MovingAverage::~MovingAverage() {} |
| + |
| +void MovingAverage::AddSample(base::TimeDelta sample) { |
| + if (count_ < depth_) { |
| + samples_[count_++] = sample; |
| + total_ += sample; |
| + return; |
| + } |
|
xhwang
2015/04/28 16:01:07
If you initialize |samples_| with all zeros (which
DaleCurtis
2015/04/28 21:45:23
Done.
|
| + |
| + base::TimeDelta& oldest = samples_[count_++ % depth_]; |
| + total_ += sample - oldest; |
| + oldest = sample; |
| +} |
| + |
| +base::TimeDelta MovingAverage::Average() const { |
| + DCHECK_GT(count_, 0u); |
| + return total_ / std::min(depth_, count_); |
|
xhwang
2015/04/28 16:01:07
nit: If we always choose a |depth_| of a power of
DaleCurtis
2015/04/28 21:45:24
Hmm, I feel that's a bit of premature optimization
|
| +} |
| + |
| +void MovingAverage::Reset() { |
| + count_ = 0; |
| + total_ = base::TimeDelta(); |
| +} |
| + |
| +} // namespace media |