17 using System.Collections;
18 using System.Collections.Generic;
19 using System.Runtime.CompilerServices;
20 using System.Threading;
33 private readonly List<T> _list;
35 private readonly ReaderWriterLockSlim _listLock =
new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
37 private T _mostRecentlyRemoved;
55 _list =
new List<T>(size);
68 _listLock.EnterReadLock();
73 _listLock.ExitReadLock();
91 _listLock.EnterReadLock();
96 _listLock.ExitReadLock();
110 _listLock.EnterReadLock();
115 _listLock.ExitReadLock();
131 _listLock.EnterReadLock();
133 if (_samples <= _size)
137 return _mostRecentlyRemoved;
141 _listLock.ExitReadLock();
153 public T
this [
int i]
159 _listLock.EnterReadLock();
166 if (i > _list.Count - 1)
170 _listLock.ExitReadLock();
172 _listLock.EnterReadLock();
178 return _list[GetListIndex(i, _list.Count, _tail)];
182 _listLock.ExitReadLock();
189 _listLock.EnterWriteLock();
196 if (i > _list.Count - 1)
203 var count = _list.Count;
204 for (var j = 0; j < i - count + 1; j++)
210 _list[GetListIndex(i, _list.Count, _tail)] = value;
214 _listLock.ExitWriteLock();
229 _listLock.EnterReadLock();
230 return _samples >= _size;
234 _listLock.ExitReadLock();
250 _listLock.EnterReadLock();
254 var count = _list.Count;
255 var temp =
new T[count];
256 for (
int i = 0; i < count; i++)
258 temp[i] = _list[GetListIndex(i, count, _tail)];
261 return ((IEnumerable<T>) temp).GetEnumerator();
265 _listLock.ExitReadLock();
277 IEnumerator IEnumerable.GetEnumerator()
290 _listLock.EnterWriteLock();
293 if (_size == _list.Count)
297 _mostRecentlyRemoved = _list[_tail];
299 _tail = (_tail + 1) % _size;
308 _listLock.ExitWriteLock();
319 _listLock.EnterWriteLock();
327 _listLock.ExitWriteLock();
331 [MethodImpl(MethodImplOptions.AggressiveInlining)]
332 private static int GetListIndex(
int index,
int listCount,
int tail)
334 return (listCount + tail - index - 1) % listCount;
337 private void Resize(
int size)
341 _listLock.EnterWriteLock();
343 _list.EnsureCapacity(size);
344 if (size < _list.Count)
346 _list.RemoveRange(0, _list.Count - size);
352 _listLock.ExitWriteLock();