From e9ea621b93fbb58d9edfca8375918791637bbd52 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 30 Dec 2020 20:59:04 +0800 Subject: +init --- Client/Assembly-CSharp/RingBuffer.cs | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Client/Assembly-CSharp/RingBuffer.cs (limited to 'Client/Assembly-CSharp/RingBuffer.cs') 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 +{ + 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; + } +} -- cgit v1.1-26-g67d0