Repeat.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 AsyncEnumerableEx
  11. {
  12. public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element)
  13. {
  14. #if USE_ASYNC_ITERATOR
  15. return AsyncEnumerable.Create(Core);
  16. #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
  17. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  18. {
  19. while (true)
  20. {
  21. cancellationToken.ThrowIfCancellationRequested();
  22. yield return element;
  23. }
  24. }
  25. #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
  26. #else
  27. return new RepeatElementAsyncIterator<TResult>(element);
  28. #endif
  29. }
  30. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source)
  31. {
  32. if (source == null)
  33. throw Error.ArgumentNull(nameof(source));
  34. #if USE_ASYNC_ITERATOR
  35. return AsyncEnumerable.Create(Core);
  36. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  37. {
  38. while (true)
  39. {
  40. await foreach (var item in AsyncEnumerableExtensions.WithCancellation(source, cancellationToken).ConfigureAwait(false))
  41. {
  42. yield return item;
  43. }
  44. }
  45. }
  46. #else
  47. return new RepeatSequenceAsyncIterator<TSource>(source, -1);
  48. #endif
  49. }
  50. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source, int count)
  51. {
  52. if (source == null)
  53. throw Error.ArgumentNull(nameof(source));
  54. if (count < 0)
  55. throw Error.ArgumentOutOfRange(nameof(count));
  56. #if USE_ASYNC_ITERATOR
  57. return AsyncEnumerable.Create(Core);
  58. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  59. {
  60. for (var i = 0; i < count; i++)
  61. {
  62. await foreach (var item in AsyncEnumerableExtensions.WithCancellation(source, cancellationToken).ConfigureAwait(false))
  63. {
  64. yield return item;
  65. }
  66. }
  67. }
  68. #else
  69. return new RepeatSequenceAsyncIterator<TSource>(source, count);
  70. #endif
  71. }
  72. #if !USE_ASYNC_ITERATOR
  73. private sealed class RepeatElementAsyncIterator<TResult> : AsyncIterator<TResult>
  74. {
  75. private readonly TResult _element;
  76. public RepeatElementAsyncIterator(TResult element) => _element = element;
  77. public override AsyncIteratorBase<TResult> Clone()
  78. {
  79. return new RepeatElementAsyncIterator<TResult>(_element);
  80. }
  81. protected override ValueTask<bool> MoveNextCore()
  82. {
  83. _cancellationToken.ThrowIfCancellationRequested();
  84. _current = _element;
  85. return new ValueTask<bool>(true);
  86. }
  87. }
  88. private sealed class RepeatSequenceAsyncIterator<TSource> : AsyncIterator<TSource>
  89. {
  90. private readonly int _count;
  91. private readonly bool _isInfinite;
  92. private readonly IAsyncEnumerable<TSource> _source;
  93. private int _currentCount;
  94. private IAsyncEnumerator<TSource> _enumerator;
  95. public RepeatSequenceAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  96. {
  97. Debug.Assert(source != null);
  98. _source = source;
  99. _count = count;
  100. _isInfinite = count < 0;
  101. _currentCount = count;
  102. }
  103. public override AsyncIteratorBase<TSource> Clone()
  104. {
  105. return new RepeatSequenceAsyncIterator<TSource>(_source, _count);
  106. }
  107. public override async ValueTask DisposeAsync()
  108. {
  109. if (_enumerator != null)
  110. {
  111. await _enumerator.DisposeAsync().ConfigureAwait(false);
  112. _enumerator = null;
  113. }
  114. await base.DisposeAsync().ConfigureAwait(false);
  115. }
  116. protected override async ValueTask<bool> MoveNextCore()
  117. {
  118. switch (_state)
  119. {
  120. case AsyncIteratorState.Allocated:
  121. if (_enumerator != null)
  122. {
  123. await _enumerator.DisposeAsync().ConfigureAwait(false);
  124. _enumerator = null;
  125. }
  126. if (!_isInfinite && _currentCount-- == 0)
  127. break;
  128. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  129. _state = AsyncIteratorState.Iterating;
  130. goto case AsyncIteratorState.Iterating;
  131. case AsyncIteratorState.Iterating:
  132. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  133. {
  134. _current = _enumerator.Current;
  135. return true;
  136. }
  137. goto case AsyncIteratorState.Allocated;
  138. }
  139. await DisposeAsync().ConfigureAwait(false);
  140. return false;
  141. }
  142. }
  143. #endif
  144. }
  145. }