MicroMouse Control Module  v1.3.2-2-ge2c6882
accumulator.h
[詳解]
1 
8 #pragma once
9 
10 #include <new>
11 
12 namespace ctrl {
13 
19 template <typename T, std::size_t S>
20 class Accumulator {
21  public:
26  Accumulator(const T& value = T()) {
27  buffer = new T[S];
28  head = 0;
29  clear(value);
30  }
34  ~Accumulator() { delete[] buffer; }
39  void clear(const T& value = T()) {
40  for (int i = 0; i < S; i++) buffer[i] = value;
41  }
45  void push(const T& value) {
46  head = (head + 1) % S;
47  buffer[head] = value;
48  }
55  const T& operator[](const std::size_t index) const {
56  return buffer[(S + head - index) % S];
57  }
63  const T average(const int n = S) const {
64  T sum = T();
65  for (int i = 0; i < n; i++) {
66  sum += buffer[(S + head - i) % S];
67  }
68  return sum / n;
69  }
73  std::size_t size() const { return S; }
74 
75  private:
76  T* buffer;
77  std::size_t head;
78 };
79 
80 } // namespace ctrl
データの蓄積器
Definition: accumulator.h:20
Accumulator(const T &value=T())
コンストラクタ
Definition: accumulator.h:26
const T & operator[](const std::size_t index) const
直近 index 番目の値を取得するオペレータ
Definition: accumulator.h:55
~Accumulator()
デストラクタ
Definition: accumulator.h:34
std::size_t size() const
リングバッファのサイズを返す関数
Definition: accumulator.h:73
const T average(const int n=S) const
直近 n 個の平均を取得する関数
Definition: accumulator.h:63
void push(const T &value)
最新のデータを追加する関数
Definition: accumulator.h:45
void clear(const T &value=T())
バッファをクリアする関数
Definition: accumulator.h:39
制御関係の名前空間
Definition: accel_curve.h:54