Repeat.cs 4.5 KB

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