Repeat.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. /// <summary>
  12. /// Generates an async-enumerable sequence that repeats the given element the specified number of times.
  13. /// </summary>
  14. /// <typeparam name="TResult">The type of the element that will be repeated in the produced sequence.</typeparam>
  15. /// <param name="element">Element to repeat.</param>
  16. /// <param name="count">Number of times to repeat the element.</param>
  17. /// <returns>An async-enumerable sequence that repeats the given element the specified number of times.</returns>
  18. /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero.</exception>
  19. public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element, int count)
  20. {
  21. if (count < 0)
  22. throw Error.ArgumentOutOfRange(nameof(count));
  23. return new RepeatAsyncIterator<TResult>(element, count);
  24. }
  25. private sealed class RepeatAsyncIterator<TResult> : AsyncIterator<TResult>, IAsyncIListProvider<TResult>
  26. {
  27. private readonly TResult _element;
  28. private readonly int _count;
  29. private int _remaining;
  30. public RepeatAsyncIterator(TResult element, int count)
  31. {
  32. _element = element;
  33. _count = count;
  34. }
  35. public override AsyncIteratorBase<TResult> Clone() => new RepeatAsyncIterator<TResult>(_element, _count);
  36. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken) => new ValueTask<int>(_count);
  37. public ValueTask<TResult[]> ToArrayAsync(CancellationToken cancellationToken)
  38. {
  39. cancellationToken.ThrowIfCancellationRequested();
  40. var res = new TResult[_count];
  41. for (var i = 0; i < _count; i++)
  42. {
  43. res[i] = _element;
  44. }
  45. return new ValueTask<TResult[]>(res);
  46. }
  47. public ValueTask<List<TResult>> ToListAsync(CancellationToken cancellationToken)
  48. {
  49. cancellationToken.ThrowIfCancellationRequested();
  50. var res = new List<TResult>(_count);
  51. for (var i = 0; i < _count; i++)
  52. {
  53. res.Add(_element);
  54. }
  55. return new ValueTask<List<TResult>>(res);
  56. }
  57. protected override async ValueTask<bool> MoveNextCore()
  58. {
  59. switch (_state)
  60. {
  61. case AsyncIteratorState.Allocated:
  62. _remaining = _count;
  63. if (_remaining > 0)
  64. {
  65. _current = _element;
  66. }
  67. _state = AsyncIteratorState.Iterating;
  68. goto case AsyncIteratorState.Iterating;
  69. case AsyncIteratorState.Iterating:
  70. if (_remaining-- != 0)
  71. {
  72. return true;
  73. }
  74. break;
  75. }
  76. await DisposeAsync().ConfigureAwait(false);
  77. return false;
  78. }
  79. }
  80. }
  81. }