Repeat.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  12. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.repeat?view=net-9.0-pp
  13. /// <summary>
  14. /// Generates an async-enumerable sequence that repeats the given element the specified number of times.
  15. /// </summary>
  16. /// <typeparam name="TResult">The type of the element that will be repeated in the produced sequence.</typeparam>
  17. /// <param name="element">Element to repeat.</param>
  18. /// <param name="count">Number of times to repeat the element.</param>
  19. /// <returns>An async-enumerable sequence that repeats the given element the specified number of times.</returns>
  20. /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero.</exception>
  21. public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element, int count)
  22. {
  23. if (count < 0)
  24. throw Error.ArgumentOutOfRange(nameof(count));
  25. return new RepeatAsyncIterator<TResult>(element, count);
  26. }
  27. private sealed class RepeatAsyncIterator<TResult> : AsyncIterator<TResult>, IAsyncIListProvider<TResult>
  28. {
  29. private readonly TResult _element;
  30. private readonly int _count;
  31. private int _remaining;
  32. public RepeatAsyncIterator(TResult element, int count)
  33. {
  34. _element = element;
  35. _count = count;
  36. }
  37. public override AsyncIteratorBase<TResult> Clone() => new RepeatAsyncIterator<TResult>(_element, _count);
  38. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken) => new(_count);
  39. public ValueTask<TResult[]> ToArrayAsync(CancellationToken cancellationToken)
  40. {
  41. cancellationToken.ThrowIfCancellationRequested();
  42. var res = new TResult[_count];
  43. for (var i = 0; i < _count; i++)
  44. {
  45. res[i] = _element;
  46. }
  47. return new ValueTask<TResult[]>(res);
  48. }
  49. public ValueTask<List<TResult>> ToListAsync(CancellationToken cancellationToken)
  50. {
  51. cancellationToken.ThrowIfCancellationRequested();
  52. var res = new List<TResult>(_count);
  53. for (var i = 0; i < _count; i++)
  54. {
  55. res.Add(_element);
  56. }
  57. return new ValueTask<List<TResult>>(res);
  58. }
  59. protected override async ValueTask<bool> MoveNextCore()
  60. {
  61. switch (_state)
  62. {
  63. case AsyncIteratorState.Allocated:
  64. _remaining = _count;
  65. if (_remaining > 0)
  66. {
  67. _current = _element;
  68. }
  69. _state = AsyncIteratorState.Iterating;
  70. goto case AsyncIteratorState.Iterating;
  71. case AsyncIteratorState.Iterating:
  72. if (_remaining-- != 0)
  73. {
  74. return true;
  75. }
  76. break;
  77. }
  78. await DisposeAsync().ConfigureAwait(false);
  79. return false;
  80. }
  81. }
  82. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  83. }
  84. }