diff options
Diffstat (limited to 'Client/Assembly-CSharp/RingBuffer.cs')
-rw-r--r-- | Client/Assembly-CSharp/RingBuffer.cs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/Client/Assembly-CSharp/RingBuffer.cs b/Client/Assembly-CSharp/RingBuffer.cs new file mode 100644 index 0000000..e12313e --- /dev/null +++ b/Client/Assembly-CSharp/RingBuffer.cs @@ -0,0 +1,75 @@ +using System; + +public class RingBuffer<T> +{ + public int Count { get; private set; } + + public int Capacity + { + get + { + return this.Data.Length; + } + } + + public T this[int i] + { + get + { + int num = (this.startIdx + i) % this.Data.Length; + return this.Data[num]; + } + } + + private readonly T[] Data; + + private int startIdx; + + public RingBuffer(int size) + { + this.Data = new T[size]; + } + + public T First() + { + return this.Data[this.startIdx]; + } + + public T Last() + { + int num = (this.startIdx + this.Count - 1) % this.Data.Length; + return this.Data[num]; + } + + public void Add(T item) + { + int num = (this.startIdx + this.Count) % this.Data.Length; + this.Data[num] = item; + if (this.Count < this.Data.Length) + { + int count = this.Count; + this.Count = count + 1; + return; + } + this.startIdx = (this.startIdx + 1) % this.Data.Length; + } + + public T RemoveFirst() + { + if (this.Count == 0) + { + throw new InvalidOperationException("Data is empty"); + } + T result = this.Data[this.startIdx]; + this.startIdx = (this.startIdx + 1) % this.Data.Length; + int count = this.Count; + this.Count = count - 1; + return result; + } + + public void Clear() + { + this.startIdx = 0; + this.Count = 0; + } +} |