Repeat.cs 4.3 KB

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