| 45 | // the vector has a fixed size. |
| 46 | template <typename SampleType, typename SumType, int WindowSize> |
| 47 | class Accumulator { |
| 48 | public: |
| 49 | Accumulator() |
| 50 | : sample_index_(0), |
| 51 | total_samples_(0), |
| 52 | sum_(0), |
| 53 | window_sum_(0), |
| 54 | min_(std::numeric_limits<SampleType>::max()), |
| 55 | max_(std::numeric_limits<SampleType>::lowest()), |
| 56 | most_recent_(0) { |
| 57 | CHECK_GT(WindowSize, 0); |
| 58 | if (WindowSize < kInfiniteWindowSize) { |
| 59 | samples_.reserve(WindowSize); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* ------------------------------------------------------------------------ */ |
| 64 | void Add(SampleType sample) { |
| 65 | most_recent_ = sample; |
| 66 | if (sample_index_ < WindowSize) { |
| 67 | samples_.push_back(sample); |
| 68 | window_sum_ += sample; |
| 69 | ++sample_index_; |
| 70 | } else { |
| 71 | SampleType& oldest = samples_.at(sample_index_++ % WindowSize); |
| 72 | window_sum_ += sample - oldest; |
| 73 | oldest = sample; |
| 74 | } |
| 75 | sum_ += sample; |
| 76 | ++total_samples_; |
| 77 | if (sample > max_) { |
| 78 | max_ = sample; |
| 79 | } |
| 80 | if (sample < min_) { |
| 81 | min_ = sample; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* ------------------------------------------------------------------------ */ |
| 86 | int total_samples() const { return total_samples_; } |
| 87 | |
| 88 | /* ------------------------------------------------------------------------ */ |
| 89 | SumType sum() const { return sum_; } |
| 90 | |
| 91 | /* ------------------------------------------------------------------------ */ |
| 92 | SumType Mean() const { return (total_samples_ < 1) ? 0.0 : sum_ / total_samples_; } |
| 93 | |
| 94 | /* ------------------------------------------------------------------------ */ |
| 95 | // Rolling mean is only used for fixed sized data for now. We don't need this |
| 96 | // function for our infinite accumulator at this point. |
| 97 | SumType RollingMean() const { |
| 98 | if (WindowSize < kInfiniteWindowSize) { |
| 99 | return window_sum_ / std::min(sample_index_, WindowSize); |
| 100 | } else { |
| 101 | return Mean(); |
| 102 | } |
| 103 | } |
| 104 |
nothing calls this directly
no outgoing calls
no test coverage detected