dalecurtis | 3c36fc4 | 2015-05-01 01:48:05 | [diff] [blame] | 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 | #ifndef MEDIA_BASE_MOVING_AVERAGE_H_ |
| 6 | #define MEDIA_BASE_MOVING_AVERAGE_H_ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/time/time.h" |
| 11 | #include "media/base/media_export.h" |
| 12 | |
| 13 | namespace media { |
| 14 | |
| 15 | // Simple class for calculating a moving average of fixed size. |
| 16 | class MEDIA_EXPORT MovingAverage { |
| 17 | public: |
| 18 | // Creates a MovingAverage instance with space for |depth| samples. |
| 19 | explicit MovingAverage(size_t depth); |
| 20 | ~MovingAverage(); |
| 21 | |
| 22 | // Adds a new sample to the average; replaces the oldest sample if |depth_| |
| 23 | // has been exceeded. Updates |total_| to the new sum of values. |
| 24 | void AddSample(base::TimeDelta sample); |
| 25 | |
| 26 | // Returns the current average of all held samples. |
| 27 | base::TimeDelta Average() const; |
| 28 | |
| 29 | // Resets the state of the class to its initial post-construction state. |
| 30 | void Reset(); |
| 31 | |
dalecurtis | 3e75e1da3 | 2015-05-05 00:15:43 | [diff] [blame] | 32 | size_t count() const { return count_; } |
| 33 | |
dalecurtis | 3c36fc4 | 2015-05-01 01:48:05 | [diff] [blame] | 34 | private: |
| 35 | // Maximum number of elements allowed in the average. |
| 36 | const size_t depth_; |
| 37 | |
| 38 | // Number of elements seen thus far. |
dalecurtis | 3e75e1da3 | 2015-05-05 00:15:43 | [diff] [blame] | 39 | uint64_t count_; |
dalecurtis | 3c36fc4 | 2015-05-01 01:48:05 | [diff] [blame] | 40 | |
| 41 | std::vector<base::TimeDelta> samples_; |
| 42 | base::TimeDelta total_; |
| 43 | |
| 44 | DISALLOW_COPY_AND_ASSIGN(MovingAverage); |
| 45 | }; |
| 46 | |
| 47 | } // namespace media |
| 48 | |
| 49 | #endif // MEDIA_BASE_MOVING_AVERAGE_H_ |