blob: e12313e24c3e9d4d66b4604ad623ae3dc9225cd9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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;
}
}
|