Repeat.cs 4.4 KB

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