Chromium Code Reviews| 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), | |
| 13 count_(0), | |
| 14 samples_(new base::TimeDelta[depth]) { | |
| 15 } | |
| 16 | |
| 17 MovingAverage::~MovingAverage() {} | |
| 18 | |
| 19 void MovingAverage::AddSample(base::TimeDelta sample) { | |
| 20 if (count_ < depth_) { | |
| 21 samples_[count_++] = sample; | |
| 22 total_ += sample; | |
| 23 return; | |
| 24 } | |
|
xhwang
2015/04/28 16:01:07
If you initialize |samples_| with all zeros (which
DaleCurtis
2015/04/28 21:45:23
Done.
| |
| 25 | |
| 26 base::TimeDelta& oldest = samples_[count_++ % depth_]; | |
| 27 total_ += sample - oldest; | |
| 28 oldest = sample; | |
| 29 } | |
| 30 | |
| 31 base::TimeDelta MovingAverage::Average() const { | |
| 32 DCHECK_GT(count_, 0u); | |
| 33 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
| |
| 34 } | |
| 35 | |
| 36 void MovingAverage::Reset() { | |
| 37 count_ = 0; | |
| 38 total_ = base::TimeDelta(); | |
| 39 } | |
| 40 | |
| 41 } // namespace media | |
| OLD | NEW |