public class CircularBuffer : ICloneable { #region フィールド double[] buf; int top; #endregion #region コンストラクタ public CircularBuffer() : this(0) { } /// /// バッファ長を指定して初期化 /// /// バッファ長 public CircularBuffer(int len) { this.top = 0; this.buf = new double[len]; } #endregion #region 値の挿入・取得 /// /// n サンプル前の値の取得 /// /// 何サンプル前の値を読み書きするか /// n サンプル前の値 public double this[int n] { get { return this.buf[(n + this.top) % this.buf.Length]; } set { this.buf[(n + this.top) % this.buf.Length] = value; } } /// /// 値の挿入 /// /// 挿入したい値 public void Insert(double x) { --this.top; if (this.top < 0) this.top += this.buf.Length; this.buf[this.top] = x; } /// /// 要素数 /// public int Count { get { return this.buf.Length; } } #endregion #region ICloneable メンバ public object Clone() { CircularBuffer cb = new CircularBuffer(this.Count); cb.buf = (double[])this.buf.Clone(); cb.top = this.top; return cb; } #endregion }