generated from gemstone/gemtem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RollingWindow.cs
337 lines (278 loc) · 11.6 KB
/
RollingWindow.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
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
329
330
331
332
333
334
335
336
337
//******************************************************************************************************
// RollingWindow.cs - Gbtc
//
// Copyright © 2015, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 02/02/2015 - Stephen C. Wills
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections;
using System.Collections.Generic;
using Gemstone.Numeric.Analysis;
namespace Gemstone.IO.Collections;
/// <summary>
/// Represents a rolling window of data with FIFO semantics that will
/// automatically truncate the data when the window size is exceeded.
/// </summary>
/// <typeparam name="T">The type of objects to be stored in the rolling window.</typeparam>
public class RollingWindow<T> : IList<T>
{
#region [ Members ]
// Fields
private readonly T[] m_window;
private int m_start;
private int m_version;
#endregion
#region [ Constructors ]
/// <summary>
/// Creates a new instance of the <see cref="RollingWindow{T}"/> class.
/// </summary>
/// <param name="windowSize">The size of the window maintained by the collection.</param>
public RollingWindow(int windowSize)
{
if (windowSize <= 0)
throw new ArgumentOutOfRangeException(nameof(windowSize), "windowSize must be at least 1");
WindowSize = windowSize;
m_window = new T[windowSize];
}
#endregion
#region [ Properties ]
/// <summary>
/// Gets the number of elements contained in the <see cref="RollingWindow{T}"/>.
/// </summary>
public int Count { get; private set; }
/// <summary>
/// Gets the size of the window maintained by the <see cref="RollingWindow{T}"/>.
/// </summary>
public int WindowSize { get; }
/// <summary>
/// Gets a value indicating whether the <see cref="RollingWindow{T}"/> is read-only.
/// </summary>
public bool IsReadOnly => false;
/// <summary>
/// Gets or sets the element at the specified index.
/// </summary>
/// <returns>
/// The element at the specified index.
/// </returns>
/// <param name="index">The zero-based index of the element to get or set.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="RollingWindow{T}"/>.</exception>
public T this[int index]
{
get
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index), "index must be greater than zero and less than the size of the collection");
return m_window[WrapIndex(m_start + index)];
}
set
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index), "index must be greater than zero and less than the size of the collection");
m_window[WrapIndex(m_start + index)] = value;
}
}
#endregion
#region [ Methods ]
/// <summary>
/// Adds an item to the <see cref="RollingWindow{T}"/>.
/// </summary>
/// <param name="item">The object to add to the <see cref="RollingWindow{T}"/>.</param>
public void Add(T item)
{
if (Count < WindowSize)
Count++;
else
m_start = WrapIndex(m_start + 1);
this[Count - 1] = item;
m_version++;
}
/// <summary>
/// Inserts an item to the <see cref="RollingWindow{T}"/> at the specified index.
/// </summary>
/// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
/// <param name="item">The object to insert into the <see cref="RollingWindow{T}"/>.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="RollingWindow{T}"/>.</exception>
/// <exception cref="InvalidOperationException">Rolling window is full (<see cref="Count"/> = <see cref="WindowSize"/>.</exception>
public void Insert(int index, T item)
{
if (index < 0 || index > Count)
throw new ArgumentOutOfRangeException(nameof(index), "index must be greater than zero and less than or equal to the size of the collection");
if (Count >= WindowSize)
throw new InvalidOperationException("Unable to insert item; rolling window is full");
// Increase count first to
// prevent out of range indexes
Count++;
if (index <= WindowSize / 2)
{
// Shift left to make room
m_start = WrapIndex(m_start - 1);
for (int i = 0; i < index; i++)
this[i] = this[i + 1];
}
else
{
// Shift right to make room
for (int i = Count - 1; i > index; i--)
this[i] = this[i - 1];
}
// Insert the item and
// increment the version
this[index] = item;
m_version++;
}
/// <summary>
/// Removes the first occurrence of a specific object from the <see cref="RollingWindow{T}"/>.
/// </summary>
/// <returns>
/// true if <paramref name="item"/> was successfully removed from the <see cref="RollingWindow{T}"/>; otherwise, false.
/// This method also returns false if <paramref name="item"/> is not found in the original <see cref="RollingWindow{T}"/>.
/// </returns>
/// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
public bool Remove(T item)
{
int index = IndexOf(item);
if (index < 0)
return false;
RemoveAt(index);
return true;
}
/// <summary>
/// Removes the <see cref="RollingWindow{T}"/> item at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the item to remove.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="RollingWindow{T}"/>.</exception>
public void RemoveAt(int index)
{
if (index <= WindowSize / 2)
{
// Shift right to fill space
for (int i = index; i > 0; i--)
this[i] = this[i - 1];
this[0] = default!;
m_start++;
}
else
{
// Shift left to fill space
for (int i = index; i < Count - 1; i++)
this[i] = this[i + 1];
this[Count - 1] = default!;
}
Count--;
m_version++;
}
/// <summary>
/// Determines the index of a specific item in the <see cref="RollingWindow{T}"/>.
/// </summary>
/// <returns>
/// The index of <paramref name="item"/> if found in the list; otherwise, -1.
/// </returns>
/// <param name="item">The object to locate in the <see cref="RollingWindow{T}"/>.</param>
public int IndexOf(T item)
{
int i = 0;
foreach (T obj in this)
{
if (Equals(obj, item))
return i;
i++;
}
return -1;
}
/// <summary>
/// Determines whether the <see cref="RollingWindow{T}"/> contains a specific value.
/// </summary>
/// <returns>
/// true if <paramref name="item"/> is found in the <see cref="RollingWindow{T}"/>; otherwise, false.
/// </returns>
/// <param name="item">The object to locate in the <see cref="RollingWindow{T}"/>.</param>
public bool Contains(T item) => IndexOf(item) >= 0;
/// <summary>
/// Removes all items from the <see cref="RollingWindow{T}"/>.
/// </summary>
public void Clear()
{
for (int i = 0; i < WindowSize; i++)
m_window[i] = default!;
Count = 0;
}
/// <summary>
/// Copies the elements of the <see cref="RollingWindow{T}"/> to an <see cref="Array"/>, starting at a particular <see cref="Array"/> index.
/// </summary>
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the elements copied from <see cref="RollingWindow{T}"/>. The <see cref="Array"/> must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
/// <exception cref="ArgumentNullException"><paramref name="array"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.</exception>
/// <exception cref="ArgumentException">The number of elements in the source <see cref="RollingWindow{T}"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.</exception>
public void CopyTo(T[] array, int arrayIndex)
{
if (array is null)
throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0)
throw new ArgumentOutOfRangeException(nameof(arrayIndex), "arrayIndex must be greater than zero");
if (Count > array.Length - arrayIndex)
throw new ArgumentException("Not enough available space in array to copy elements from rolling window");
int i = 0;
foreach (T item in this)
{
array[i] = item;
i++;
}
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>1</filterpriority>
public IEnumerator<T> GetEnumerator()
{
int version = m_version;
int count = 0;
for (int i = m_start; i < WindowSize; i++)
{
if (version != m_version)
throw new InvalidOperationException("Collection was modified; enumeration operation may not execute");
if (count >= Count)
break;
count++;
yield return m_window[i];
}
for (int i = 0; i < m_start; i++)
{
if (version != m_version)
throw new InvalidOperationException("Collection was modified; enumeration operation may not execute");
if (count >= Count)
break;
count++;
yield return m_window[i];
}
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>2</filterpriority>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private int WrapIndex(int index) => (int)Euclidean.Mod(index, WindowSize);
#endregion
}