Repeat.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 AsyncEnumerableEx
  10. {
  11. public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element)
  12. {
  13. return new RepeatElementAsyncIterator<TResult>(element);
  14. }
  15. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source)
  16. {
  17. if (source == null)
  18. throw new ArgumentNullException(nameof(source));
  19. return new RepeatSequenceAsyncIterator<TSource>(source, -1);
  20. }
  21. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source, int count)
  22. {
  23. if (source == null)
  24. throw new ArgumentNullException(nameof(source));
  25. if (count < 0)
  26. throw new ArgumentOutOfRangeException(nameof(count));
  27. return new RepeatSequenceAsyncIterator<TSource>(source, count);
  28. }
  29. private sealed class RepeatElementAsyncIterator<TResult> : AsyncIterator<TResult>
  30. {
  31. private readonly TResult element;
  32. public RepeatElementAsyncIterator(TResult element)
  33. {
  34. this.element = element;
  35. }
  36. public override AsyncIterator<TResult> Clone()
  37. {
  38. return new RepeatElementAsyncIterator<TResult>(element);
  39. }
  40. protected override Task<bool> MoveNextCore()
  41. {
  42. current = element;
  43. return TaskExt.True;
  44. }
  45. }
  46. private sealed class RepeatSequenceAsyncIterator<TSource> : AsyncIterator<TSource>
  47. {
  48. private readonly int count;
  49. private readonly bool isInfinite;
  50. private readonly IAsyncEnumerable<TSource> source;
  51. private int currentCount;
  52. private IAsyncEnumerator<TSource> enumerator;
  53. public RepeatSequenceAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  54. {
  55. Debug.Assert(source != null);
  56. this.source = source;
  57. this.count = count;
  58. isInfinite = count < 0;
  59. currentCount = count;
  60. }
  61. public override AsyncIterator<TSource> Clone()
  62. {
  63. return new RepeatSequenceAsyncIterator<TSource>(source, count);
  64. }
  65. public override async Task DisposeAsync()
  66. {
  67. if (enumerator != null)
  68. {
  69. await enumerator.DisposeAsync().ConfigureAwait(false);
  70. enumerator = null;
  71. }
  72. await base.DisposeAsync().ConfigureAwait(false);
  73. }
  74. protected override async Task<bool> MoveNextCore()
  75. {
  76. switch (state)
  77. {
  78. case AsyncIteratorState.Allocated:
  79. if (enumerator != null)
  80. {
  81. await enumerator.DisposeAsync().ConfigureAwait(false);
  82. enumerator = null;
  83. }
  84. if (!isInfinite && currentCount-- == 0)
  85. break;
  86. enumerator = source.GetAsyncEnumerator();
  87. state = AsyncIteratorState.Iterating;
  88. goto case AsyncIteratorState.Iterating;
  89. case AsyncIteratorState.Iterating:
  90. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  91. {
  92. current = enumerator.Current;
  93. return true;
  94. }
  95. goto case AsyncIteratorState.Allocated;
  96. }
  97. await DisposeAsync().ConfigureAwait(false);
  98. return false;
  99. }
  100. }
  101. }
  102. }