using System.Collections; namespace MareSynchronos.Utils; public class RollingList : IEnumerable { private readonly Lock _addLock = new(); private readonly LinkedList _list = new(); public RollingList(int maximumCount) { if (maximumCount <= 0) throw new ArgumentException(message: null, nameof(maximumCount)); MaximumCount = maximumCount; } public int Count => _list.Count; public int MaximumCount { get; } public T this[int index] { get { if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index)); return _list.Skip(index).First(); } } public void Add(T value) { lock (_addLock) { if (_list.Count == MaximumCount) { _list.RemoveFirst(); } _list.AddLast(value); } } public IEnumerator GetEnumerator() => _list.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); }