Repeat.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element, int count)
  14. {
  15. if (count < 0)
  16. throw new ArgumentOutOfRangeException(nameof(count));
  17. return Enumerable.Repeat(element, count)
  18. .ToAsyncEnumerable();
  19. }
  20. public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element)
  21. {
  22. return new RepeatElementAsyncIterator<TResult>(element);
  23. }
  24. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source, int count)
  25. {
  26. if (source == null)
  27. throw new ArgumentNullException(nameof(source));
  28. if (count < 0)
  29. throw new ArgumentOutOfRangeException(nameof(count));
  30. return new RepeatSequenceAsyncIterator<TSource>(source, count);
  31. }
  32. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. return new RepeatSequenceAsyncIterator<TSource>(source, -1);
  37. }
  38. private sealed class RepeatElementAsyncIterator<TResult> : AsyncIterator<TResult>
  39. {
  40. private readonly TResult element;
  41. public RepeatElementAsyncIterator(TResult element)
  42. {
  43. this.element = element;
  44. }
  45. public override AsyncIterator<TResult> Clone()
  46. {
  47. return new RepeatElementAsyncIterator<TResult>(element);
  48. }
  49. protected override Task<bool> MoveNextCore(CancellationToken cancellationToken)
  50. {
  51. current = element;
  52. return TaskExt.True;
  53. }
  54. }
  55. private sealed class RepeatSequenceAsyncIterator<TSource> : AsyncIterator<TSource>
  56. {
  57. private readonly int count;
  58. private readonly bool isInfinite;
  59. private readonly IAsyncEnumerable<TSource> source;
  60. private int currentCount;
  61. private IAsyncEnumerator<TSource> enumerator;
  62. public RepeatSequenceAsyncIterator(IAsyncEnumerable<TSource> source, int count)
  63. {
  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. }