-
Notifications
You must be signed in to change notification settings - Fork 1
/
RingCollection.cs
208 lines (171 loc) · 4.85 KB
/
RingCollection.cs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (C) 2011 Michael Möller <[email protected]>
*/
using System;
using System.Collections;
using System.Collections.Generic;
namespace OpenHardwareMonitor.Collections {
public class RingCollection<T> : IEnumerable<T> {
private T[] array;
// first item of collection
private int head;
// index after the last item of the collection
private int tail;
// number of items in the collection
private int size;
public RingCollection() : this(0) { }
public RingCollection(int capacity) {
if (capacity < 0)
throw new ArgumentOutOfRangeException("capacity");
this.array = new T[capacity];
this.head = 0;
this.tail = 0;
this.size = 0;
}
public int Capacity {
get {
return array.Length;
}
set {
T[] newArray = new T[value];
if (size > 0) {
if (head < tail) {
Array.Copy(array, head, newArray, 0, size);
} else {
Array.Copy(array, head, newArray, 0, array.Length - head);
Array.Copy(array, 0, newArray, array.Length - head, tail);
}
}
this.array = newArray;
this.head = 0;
this.tail = size == value ? 0 : size;
}
}
public void Clear() {
// remove potential references
if (head < tail) {
Array.Clear(array, head, size);
} else {
Array.Clear(array, 0, tail);
Array.Clear(array, head, array.Length - head);
}
this.head = 0;
this.tail = 0;
this.size = 0;
}
public void Append(T item) {
if (size == array.Length) {
int newCapacity = array.Length * 3 / 2;
if (newCapacity < array.Length + 8)
newCapacity = array.Length + 8;
Capacity = newCapacity;
}
array[tail] = item;
tail = tail + 1 == array.Length ? 0 : tail + 1;
size++;
}
public T Remove() {
if (size == 0)
throw new InvalidOperationException();
T result = array[head];
array[head] = default(T);
head = head + 1 == array.Length ? 0 : head + 1;
size--;
return result;
}
public int Count {
get {
return size;
}
}
public T this[int index] {
get {
if (index < 0 || index >= size)
throw new IndexOutOfRangeException();
int i = head + index;
if (i >= array.Length)
i -= array.Length;
return array[i];
}
set {
if (index < 0 || index >= size)
throw new IndexOutOfRangeException();
int i = head + index;
if (i >= array.Length)
i -= array.Length;
array[i] = value;
}
}
public T First {
get {
if (size == 0)
throw new InvalidOperationException();
return array[head];
}
set {
if (size == 0)
throw new InvalidOperationException();
array[head] = value;
}
}
public T Last {
get {
if (size == 0)
throw new InvalidOperationException();
return array[tail == 0 ? array.Length - 1 : tail - 1];
}
set {
if (size == 0)
throw new InvalidOperationException();
array[tail == 0 ? array.Length - 1 : tail - 1] = value;
}
}
IEnumerator<T> IEnumerable<T>.GetEnumerator() {
return new RingCollection<T>.Enumerator(this);
}
IEnumerator IEnumerable.GetEnumerator() {
return new RingCollection<T>.Enumerator(this);
}
private struct Enumerator : IEnumerator<T>, IEnumerator {
private RingCollection<T> collection;
private int index;
public Enumerator(RingCollection<T> collection) {
this.collection = collection;
this.index = -1;
}
public void Dispose() {
this.index = -2;
}
public void Reset() {
this.index = -1;
}
public T Current {
get {
if (index < 0)
throw new InvalidOperationException();
return collection[index];
}
}
object IEnumerator.Current {
get {
if (index < 0)
throw new InvalidOperationException();
return collection[index];
}
}
public bool MoveNext() {
if (index == -2)
return false;
index++;
if (index == collection.size) {
index = -2;
return false;
}
return true;
}
}
}
}