Skip.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static IAsyncEnumerable<TSource> Skip<TSource>(this IAsyncEnumerable<TSource> source, int count)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (count <= 0)
  16. {
  17. // Return source if not actually skipping, but only if it's a type from here, to avoid
  18. // issues if collections are used as keys or otherwise must not be aliased.
  19. if (source is AsyncIterator<TSource> || source is IAsyncPartition<TSource>)
  20. {
  21. return source;
  22. }
  23. count = 0;
  24. }
  25. else if (source is IAsyncPartition<TSource> partition)
  26. {
  27. return partition.Skip(count);
  28. }
  29. return new SkipAsyncIterator<TSource>(source, count);
  30. }
  31. private sealed class SkipAsyncIterator<TSource> : AsyncIterator<TSource>
  32. {
  33. private readonly int count;
  34. private readonly IAsyncEnumerable<TSource> source;
  35. private int currentCount;
  36. private IAsyncEnumerator<TSource> enumerator;
  37. public SkipAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  38. {
  39. Debug.Assert(source != null);
  40. this.source = source;
  41. this.count = count;
  42. currentCount = count;
  43. }
  44. public override AsyncIterator<TSource> Clone()
  45. {
  46. return new SkipAsyncIterator<TSource>(source, count);
  47. }
  48. public override async Task DisposeAsync()
  49. {
  50. if (enumerator != null)
  51. {
  52. await enumerator.DisposeAsync().ConfigureAwait(false);
  53. enumerator = null;
  54. }
  55. await base.DisposeAsync().ConfigureAwait(false);
  56. }
  57. protected override async Task<bool> MoveNextCore()
  58. {
  59. switch (state)
  60. {
  61. case AsyncIteratorState.Allocated:
  62. enumerator = source.GetAsyncEnumerator();
  63. // skip elements as requested
  64. while (currentCount > 0 && await enumerator.MoveNextAsync().ConfigureAwait(false))
  65. {
  66. currentCount--;
  67. }
  68. if (currentCount <= 0)
  69. {
  70. state = AsyncIteratorState.Iterating;
  71. goto case AsyncIteratorState.Iterating;
  72. }
  73. break;
  74. case AsyncIteratorState.Iterating:
  75. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  76. {
  77. current = enumerator.Current;
  78. return true;
  79. }
  80. break;
  81. }
  82. await DisposeAsync().ConfigureAwait(false);
  83. return false;
  84. }
  85. }
  86. }
  87. }