Repository navigation
Expand file tree
/
Copy pathCappedQueue.cs
More file actions
328 lines (268 loc) · 6.11 KB
/
Copy pathCappedQueue.cs
File metadata and controls
328 lines (268 loc) · 6.11 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Sylphe.Utils
{
/// <summary>
/// A queue (first-in-last-out) of limited capacity.
/// Adding an item to a full queue drops out the oldest item.
/// Implementation based on an array; <see cref="Enqueue(T)"/>
/// and <see cref="Dequeue()"/> are O(1) operations.
/// </summary>
/// <typeparam name="T">Type of queue items</typeparam>
[Serializable]
public class CappedQueue<T> : ICollection<T>, ICloneable
{
private int _tail;
private int _head;
private int _size;
private T[] _array;
private int _version;
public CappedQueue(int capacity)
{
if (capacity <= 0)
throw new ArgumentOutOfRangeException(
nameof(capacity), capacity, "Capacity must be at least one");
_head = 0;
_tail = 0;
_size = 0;
_array = new T[capacity];
_version = 0;
}
public T Dequeue()
{
if (_size == 0)
{
throw new InvalidOperationException("Queue is empty");
}
var head = _array[_head];
_array[_head] = default;
_head = (_head + 1) % _array.Length;
_size -= 1; // one less in queue
_version += 1;
return head;
}
public void Enqueue(T item)
{
if (_size == _array.Length)
{
Dequeue(); // drop head to make room for new item
}
_array[_tail] = item;
_tail = (_tail + 1) % _array.Length;
_size += 1; // one more in queue
_version += 1;
}
public T Peek()
{
if (_size == 0)
{
throw new InvalidOperationException("Queue is empty");
}
return _array[_head];
}
public T GetElement(int index)
{
return _array[(_head + index) % _array.Length];
}
public int Capacity
{
get => _array.Length;
set
{
if (value < 1)
{
throw new ArgumentOutOfRangeException(
nameof(value), value, "Capacity must be at least one");
}
SetCapacity(value);
}
}
private void SetCapacity(int capacity)
{
if (_size < 1)
{
_head = 0;
_tail = 0;
_size = 0;
_array = new T[capacity];
_version += 1;
}
else if (capacity > _array.Length)
{
var array = new T[capacity];
// Capacity enlarged: copy queue to beginning of new array
if (_head < _tail)
{
Array.Copy(_array, _head, array, 0, _size);
}
else
{
Array.Copy(_array, _head, array, 0, _array.Length - _head);
Array.Copy(_array, 0, array, _array.Length - _head, _tail);
}
_head = 0;
_tail = _size == capacity ? 0 : _size;
// _size didn't change!
_array = array;
_version += 1;
}
else if (capacity < _array.Length)
{
var array = new T[capacity];
// Capacity shrunk: drop head items, keep tail items
int offset = _array.Length - capacity;
for (int i = 0; i < capacity; i++)
{
array[i] = GetElement(i + offset);
}
_head = 0;
_tail = 0;
_size = capacity;
_array = array;
_version += 1;
}
// else: no change in capacity, nothing to do
}
#region ICollection
public void Add(T item)
{
Enqueue(item);
}
public bool Remove(T item)
{
throw new NotImplementedException(); // could be done in O(n)
}
public bool Contains(T item)
{
return IndexOf(item) >= 0;
}
public int IndexOf(T item)
{
var comparer = EqualityComparer<T>.Default;
for (int i = 0; i < _size; i++)
{
var candidate = GetElement(i);
// Good: comparer treats null and null as equal
if (comparer.Equals(item, candidate))
{
return i;
}
}
return -1; // not found
}
public void Clear()
{
_head = 0;
_tail = 0;
_size = 0;
Array.Clear(_array, 0, _array.Length);
_version += 1;
}
public int Count => _size;
public bool IsReadOnly => false;
public void CopyTo(T[] array, int index)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
if (index < 0 || index > array.Length)
throw new ArgumentOutOfRangeException(nameof(index));
if (array.Length - index < _size)
throw new ArgumentException("Array is too small", nameof(array));
if (_size > 0)
{
if (_head < _tail)
{
Array.Copy(_array, _head, array, index + 0, _array.Length - _head);
}
else
{
Array.Copy(_array, _head, array, index + 0, _array.Length - _head);
Array.Copy(_array, 0, array, index + _array.Length - _head, _tail);
}
}
}
public T[] ToArray()
{
var result = new T[_size];
CopyTo(result, 0);
return result;
}
#region IEnumerable
public IEnumerator<T> GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region ICloneable
/// <summary>Creates a shallow copy.</summary>
public object Clone()
{
var clone = new CappedQueue<T>(_array.Length);
clone._head = _head;
clone._tail = _tail;
clone._size = _size;
Array.Copy(_array, 0, clone._array, 0, _array.Length);
clone._version = _version;
return clone;
}
#endregion
public override string ToString()
{
return $"Count = {Count}";
}
#endregion
#region Nested type: Enumerator
[Serializable, StructLayout(LayoutKind.Sequential)]
private struct Enumerator : IEnumerator<T>
{
private int _index;
private T _current;
private int _version;
private readonly CappedQueue<T> _queue;
internal Enumerator(CappedQueue<T> queue)
{
_index = 0;
_current = default;
_version = queue._version;
_queue = queue;
}
public void Reset()
{
_index = 0;
_version = _queue._version;
_current = default;
}
public bool MoveNext()
{
if (_version != _queue._version)
{
throw new InvalidOperationException("Collection changed while enumerating");
}
if (_index < _queue._size)
{
_current = _queue.GetElement(_index);
_index += 1;
return true;
}
_current = default;
return false;
}
object IEnumerator.Current => Current;
public T Current => _current;
public void Dispose()
{
// Position _index at the end,
// so MoveNext would return false:
_index = _queue._size;
_current = default;
}
}
#endregion
}
}