using System; namespace CRVM.Entity { /// /// 一种环型数组,其长度N在创建后固定,当数组填充满时,继续添加的新元素会覆盖旧元素, /// 数组内始终存储最新添加的N个元素。 /// /// 元素类型 public class RollingArray { /// /// 泛型数组,用于存储各元素 /// private T[] array; /// /// 下一个元素的写入位置 /// private int nextPosition; /// /// 数组大小 /// private int size; /// /// 数组中元素数量 /// public int Count { get; private set; } /// /// 构造一个指定长度的泛型数组 /// /// 数组大小 public RollingArray(int arraySize) { size = arraySize; array = new T[arraySize]; } /// /// 往数组中添加元素,每写入一个元素,下一次写入位置后移一位,当超出边界时又从头开始覆盖写入。 /// /// 待添加元素 public void Add(T element) { array[nextPosition] = element; nextPosition = (nextPosition + 1) % size; if (Count < size) Count++; } /// /// 数组中是否存在元素 /// public bool IsEmpty { get { return Count == 0; } } /// /// 循环数组的头,返回最早添加的元素 /// public T Head { get { if (IsEmpty) throw new IndexOutOfRangeException("当前数组为空!"); if (Count < size) return array[0]; return array[nextPosition]; } } /// /// 循环数组尾,返回最新添加的元素 /// public T Tail { get { if (IsEmpty) throw new IndexOutOfRangeException("当前数组为空!"); if (nextPosition == 0) return array[Count]; return array[nextPosition - 1]; } } /// /// 按照元素的插入顺序输出完整数组 /// public T[] ToArray() { T[] temp = new T[Count]; for (int i = 0; i < Count; i++) { if (Count < size) temp[i] = array[i]; else temp[i] = array[(i + nextPosition) % size]; } return temp; } /// /// 数组重置 /// public void Clear() { nextPosition = 0; Count = 0; } } }