Repeat.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element, int count)
  13. {
  14. if (count < 0)
  15. {
  16. throw new ArgumentOutOfRangeException(nameof(count));
  17. }
  18. return Enumerable.Repeat(element, count)
  19. .ToAsyncEnumerable();
  20. }
  21. public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element)
  22. {
  23. return new RepeatElementAsyncIterator<TResult>(element);
  24. }
  25. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source, int count)
  26. {
  27. if (source == null)
  28. {
  29. throw new ArgumentNullException(nameof(source));
  30. }
  31. if (count < 0)
  32. {
  33. throw new ArgumentOutOfRangeException(nameof(count));
  34. }
  35. return new RepeatSequenceAsyncIterator<TSource>(source, count);
  36. }
  37. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source)
  38. {
  39. if (source == null)
  40. {
  41. throw new ArgumentNullException(nameof(source));
  42. }
  43. return new RepeatSequenceAsyncIterator<TSource>(source, -1);
  44. }
  45. private sealed class RepeatElementAsyncIterator<TResult> : AsyncIterator<TResult>
  46. {
  47. private readonly TResult element;
  48. public RepeatElementAsyncIterator(TResult element)
  49. {
  50. this.element = element;
  51. }
  52. public override AsyncIterator<TResult> Clone()
  53. {
  54. return new RepeatElementAsyncIterator<TResult>(element);
  55. }
  56. protected override Task<bool> MoveNextCore(CancellationToken cancellationToken)
  57. {
  58. current = element;
  59. return TaskExt.True;
  60. }
  61. }
  62. private sealed class RepeatSequenceAsyncIterator<TSource> : AsyncIterator<TSource>
  63. {
  64. private readonly int count;
  65. private readonly bool isInfinite;
  66. private readonly IAsyncEnumerable<TSource> source;
  67. private int currentCount;
  68. private IAsyncEnumerator<TSource> enumerator;
  69. public RepeatSequenceAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  70. {
  71. Debug.Assert(source != null);
  72. this.source = source;
  73. this.count = count;
  74. isInfinite = count < 0;
  75. currentCount = count;
  76. }
  77. public override AsyncIterator<TSource> Clone()
  78. {
  79. return new RepeatSequenceAsyncIterator<TSource>(source, count);
  80. }
  81. public override void Dispose()
  82. {
  83. if (enumerator != null)
  84. {
  85. enumerator.Dispose();
  86. enumerator = null;
  87. }
  88. base.Dispose();
  89. }
  90. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  91. {
  92. switch (state)
  93. {
  94. case AsyncIteratorState.Allocated:
  95. if (enumerator != null)
  96. {
  97. enumerator.Dispose();
  98. enumerator = null;
  99. }
  100. if (!isInfinite && currentCount-- == 0)
  101. {
  102. break;
  103. }
  104. enumerator = source.GetEnumerator();
  105. state = AsyncIteratorState.Iterating;
  106. goto case AsyncIteratorState.Iterating;
  107. case AsyncIteratorState.Iterating:
  108. if (await enumerator.MoveNext(cancellationToken)
  109. .ConfigureAwait(false))
  110. {
  111. current = enumerator.Current;
  112. return true;
  113. }
  114. goto case AsyncIteratorState.Allocated;
  115. }
  116. Dispose();
  117. return false;
  118. }
  119. }
  120. }
  121. }