|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Reflection; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | + |
| 10 | +#nullable enable |
| 11 | + |
| 12 | +namespace System |
| 13 | +{ |
| 14 | + internal abstract class SZGenericArrayEnumeratorBase : IDisposable |
| 15 | + { |
| 16 | + protected int _index; |
| 17 | + protected readonly int _endIndex; |
| 18 | + |
| 19 | + protected SZGenericArrayEnumeratorBase(int endIndex) |
| 20 | + { |
| 21 | + _index = -1; |
| 22 | + _endIndex = endIndex; |
| 23 | + } |
| 24 | + |
| 25 | + public bool MoveNext() |
| 26 | + { |
| 27 | + int index = _index + 1; |
| 28 | + |
| 29 | + if ((uint)index < (uint)_endIndex) |
| 30 | + { |
| 31 | + _index = index; |
| 32 | + |
| 33 | + return true; |
| 34 | + } |
| 35 | + |
| 36 | + _index = _endIndex; |
| 37 | + |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + public void Reset() => _index = -1; |
| 42 | + |
| 43 | + public void Dispose() |
| 44 | + { |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + internal sealed class SZGenericArrayEnumerator<T> : SZGenericArrayEnumeratorBase, IEnumerator<T> |
| 49 | + { |
| 50 | + private readonly T[]? _array; |
| 51 | + |
| 52 | + /// <summary>Provides an empty enumerator singleton.</summary> |
| 53 | + /// <remarks> |
| 54 | + /// If the consumer is using SZGenericArrayEnumerator elsewhere or is otherwise likely |
| 55 | + /// to be using T[] elsewhere, this singleton should be used. Otherwise, GenericEmptyEnumerator's |
| 56 | + /// singleton should be used instead, as it doesn't reference T[] in order to reduce footprint. |
| 57 | + /// </remarks> |
| 58 | + internal static readonly SZGenericArrayEnumerator<T> Empty = new SZGenericArrayEnumerator<T>(null, 0); |
| 59 | + |
| 60 | + internal SZGenericArrayEnumerator(T[]? array, int endIndex) |
| 61 | + : base(endIndex) |
| 62 | + { |
| 63 | + Debug.Assert(array == null || endIndex == array.Length); |
| 64 | + _array = array; |
| 65 | + } |
| 66 | + |
| 67 | + public T Current |
| 68 | + { |
| 69 | + get |
| 70 | + { |
| 71 | + if ((uint)_index >= (uint)_endIndex) |
| 72 | + { |
| 73 | + throw new InvalidOperationException(); |
| 74 | + } |
| 75 | + |
| 76 | + return _array![_index]; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + object? IEnumerator.Current => Current; |
| 81 | + } |
| 82 | + |
| 83 | + internal abstract class GenericEmptyEnumeratorBase : IDisposable, IEnumerator |
| 84 | + { |
| 85 | +#pragma warning disable CA1822 // https://github.com/dotnet/roslyn-analyzers/issues/5911 |
| 86 | + public bool MoveNext() => false; |
| 87 | + |
| 88 | + public object Current |
| 89 | + { |
| 90 | + get |
| 91 | + { |
| 92 | + return default; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public void Reset() { } |
| 97 | + |
| 98 | + public void Dispose() { } |
| 99 | +#pragma warning restore CA1822 |
| 100 | + } |
| 101 | +} |
0 commit comments