using System; using System.Security.Policy; using System.Linq; namespace sharp.extensions { public class HistoryValue { T[] values; int p; public HistoryValue() { this.values = new T[0]; this.p = 0; } public HistoryValue(int len) { this.values = new T[len]; } public void Add(T value) { this.p = (++this.p) % this.values.Length; this.values[p] = value; } public T this[int i] { get { if (i < 0) { i += this.values.Length; } if ((i < 0) || (i >= this.values.Length)) { throw new IndexOutOfRangeException(); } i += p; i %= this.values.Length; return this.values[i]; } set { if (i < 0) { i += this.values.Length; } if ((i < 0) || (i >= this.values.Length)) { throw new IndexOutOfRangeException(); } i += p; i %= this.values.Length; this.values[i] = value; } } public int Count { get { return this.values.Length; } } public T[] History { get { T[] pre, post; if (p == this.values.Length-1){ return this.values.Segment(0); } else { pre = this.values.Segment(p + 1); post = this.values.Segment(0, p + 1); return pre.Combine(post); } } set { this.values = value.Segment(0); this.p = this.values.Length - 1; } } public HistoryValue Clone(){ HistoryValue clone = new HistoryValue(); clone.p = this.p; clone.values = this.values.Segment(0); return clone; } public static HistoryValue operator +(HistoryValue hvalue,T value){ hvalue.Add(value); return hvalue; } } }