Go to the documentation of this file.00001 #ifndef ROLLING_SUM_H
00002 #define ROLLING_SUM_H
00003
00004 #include <boost/circular_buffer.hpp>
00005
00006
00007 template <typename T>
00008 class RollingSum
00009 {
00010 public:
00011 RollingSum(size_t capacity)
00012 : buffer_(capacity), sum_(0)
00013 {
00014 buffer_.push_back(0);
00015 }
00016
00017 void add(T sample)
00018 {
00019 sum_ += sample;
00020 sum_ -= buffer_[0];
00021 buffer_.push_back(sample);
00022 }
00023
00024 T sum()
00025 {
00026 return sum_;
00027 }
00028
00029 private:
00030 boost::circular_buffer<T> buffer_;
00031 T sum_;
00032 };
00033
00034 #endif