| | 1 | | using System.Collections; |
| | 2 | | using System.Diagnostics.CodeAnalysis; |
| | 3 | |
|
| | 4 | | namespace Pozitron.QuerySpecification; |
| | 5 | |
|
| | 6 | | internal abstract class Iterator<TSource> : IEnumerable<TSource>, IEnumerator<TSource> |
| | 7 | | { |
| 219 | 8 | | private readonly int _threadId = Environment.CurrentManagedThreadId; |
| | 9 | |
|
| | 10 | | private protected int _state; |
| | 11 | | private protected TSource _current = default!; |
| | 12 | |
|
| | 13 | | public Iterator<TSource> GetEnumerator() |
| | 14 | | { |
| 219 | 15 | | var enumerator = _state == 0 && _threadId == Environment.CurrentManagedThreadId ? this : Clone(); |
| 219 | 16 | | enumerator._state = 1; |
| 219 | 17 | | return enumerator; |
| | 18 | | } |
| | 19 | |
|
| | 20 | | public abstract Iterator<TSource> Clone(); |
| | 21 | | public abstract bool MoveNext(); |
| | 22 | |
|
| 378 | 23 | | public TSource Current => _current; |
| 2 | 24 | | object? IEnumerator.Current => Current; |
| 218 | 25 | | IEnumerator<TSource> IEnumerable<TSource>.GetEnumerator() => GetEnumerator(); |
| 1 | 26 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | 27 | |
|
| | 28 | | [ExcludeFromCodeCoverage] |
| | 29 | | void IEnumerator.Reset() => throw new NotSupportedException(); |
| | 30 | |
|
| | 31 | | public virtual void Dispose() |
| | 32 | | { |
| 342 | 33 | | _current = default!; |
| 342 | 34 | | _state = -1; |
| 342 | 35 | | } |
| | 36 | | } |