OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 |
| 5 #include "media/base/moving_average.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 namespace media { |
| 10 |
| 11 MovingAverage::MovingAverage(size_t depth) |
| 12 : depth_(depth), count_(0), samples_(depth_) { |
| 13 } |
| 14 |
| 15 MovingAverage::~MovingAverage() { |
| 16 } |
| 17 |
| 18 void MovingAverage::AddSample(base::TimeDelta sample) { |
| 19 // |samples_| is zero-initialized, so |oldest| is also zero before |count_| |
| 20 // exceeds |depth_|. |
| 21 base::TimeDelta& oldest = samples_[count_++ % depth_]; |
| 22 total_ += sample - oldest; |
| 23 oldest = sample; |
| 24 } |
| 25 |
| 26 base::TimeDelta MovingAverage::Average() const { |
| 27 DCHECK_GT(count_, 0u); |
| 28 |
| 29 // TODO(dalecurtis): Consider limiting |depth| to powers of two so that we can |
| 30 // replace the integer divide with a bit shift operation. |
| 31 |
| 32 return total_ / std::min(depth_, count_); |
| 33 } |
| 34 |
| 35 void MovingAverage::Reset() { |
| 36 count_ = 0; |
| 37 total_ = base::TimeDelta(); |
| 38 std::fill(samples_.begin(), samples_.end(), base::TimeDelta()); |
| 39 } |
| 40 |
| 41 } // namespace media |
OLD | NEW |