Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / base / running_average.h
blob633659d19b5bcba0c3ed67041fe657b7cca18f15
1 // Copyright (c) 2011 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.
5 #ifndef REMOTING_BASE_RUNNING_AVERAGE_H_
6 #define REMOTING_BASE_RUNNING_AVERAGE_H_
8 #include <deque>
10 #include "base/basictypes.h"
11 #include "base/synchronization/lock.h"
13 namespace remoting {
15 // Calculates the average of the most recent N recorded samples.
16 // This is typically used to smooth out random variation in point samples
17 // over bandwidth, frame rate, etc.
18 class RunningAverage {
19 public:
20 // Constructs a helper to average over the |window_size| most recent samples.
21 explicit RunningAverage(int window_size);
22 virtual ~RunningAverage();
24 // Records a point sample.
25 void Record(int64 value);
27 // Returns the average over up to |window_size| of the most recent samples.
28 double Average();
30 private:
31 // Stores the desired window size, as size_t to avoid casting when comparing
32 // with the size of |data_points_|.
33 const size_t window_size_;
35 // Protects |data_points_| and |sum_|.
36 base::Lock lock_;
38 // Stores the |window_size| most recently recorded samples.
39 std::deque<int64> data_points_;
41 // Holds the sum of the samples in |data_points_|.
42 int64 sum_;
44 DISALLOW_COPY_AND_ASSIGN(RunningAverage);
47 } // namespace remoting
49 #endif // REMOTING_BASE_RUNNING_AVERAGE_H_