- add sources.
[platform/framework/web/crosswalk.git] / src / remoting / base / running_average.h
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.
4
5 #ifndef REMOTING_BASE_RUNNING_AVERAGE_H_
6 #define REMOTING_BASE_RUNNING_AVERAGE_H_
7
8 #include <deque>
9
10 #include "base/basictypes.h"
11 #include "base/synchronization/lock.h"
12
13 namespace remoting {
14
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();
23
24   // Records a point sample.
25   void Record(int64 value);
26
27   // Returns the average over up to |window_size| of the most recent samples.
28   double Average();
29
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_;
34
35   // Protects |data_points_| and |sum_|.
36   base::Lock lock_;
37
38   // Stores the |window_size| most recently recorded samples.
39   std::deque<int64> data_points_;
40
41   // Holds the sum of the samples in |data_points_|.
42   int64 sum_;
43
44   DISALLOW_COPY_AND_ASSIGN(RunningAverage);
45 };
46
47 }  // namespace remoting
48
49 #endif  // REMOTING_BASE_RUNNING_AVERAGE_H_