Range.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  13. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.range?view=net-9.0-pp
  14. /// <summary>
  15. /// Generates an async-enumerable sequence of integral numbers within a specified range.
  16. /// </summary>
  17. /// <param name="start">The value of the first integer in the sequence.</param>
  18. /// <param name="count">The number of sequential integers to generate.</param>
  19. /// <returns>An async-enumerable sequence that contains a range of sequential integral numbers.</returns>
  20. /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero. -or- <paramref name="start"/> + <paramref name="count"/> - 1 is larger than <see cref="int.MaxValue"/>.</exception>
  21. public static IAsyncEnumerable<int> Range(int start, int count)
  22. {
  23. if (count < 0)
  24. throw Error.ArgumentOutOfRange(nameof(count));
  25. var end = (long)start + count - 1L;
  26. if (end > int.MaxValue)
  27. throw Error.ArgumentOutOfRange(nameof(count));
  28. if (count == 0)
  29. return Empty<int>();
  30. return new RangeAsyncIterator(start, count);
  31. }
  32. private sealed class RangeAsyncIterator : AsyncIterator<int>, IAsyncPartition<int>
  33. {
  34. private readonly int _start;
  35. private readonly int _end;
  36. public RangeAsyncIterator(int start, int count)
  37. {
  38. Debug.Assert(count > 0);
  39. _start = start;
  40. _end = start + count;
  41. }
  42. public override AsyncIteratorBase<int> Clone() => new RangeAsyncIterator(_start, _end - _start);
  43. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken) => new(_end - _start);
  44. public IAsyncPartition<int> Skip(int count)
  45. {
  46. var n = _end - _start;
  47. if (count >= n)
  48. {
  49. return EmptyAsyncIterator<int>.Instance;
  50. }
  51. return new RangeAsyncIterator(_start + count, n - count);
  52. }
  53. public IAsyncPartition<int> Take(int count)
  54. {
  55. var n = _end - _start;
  56. if (count >= n)
  57. {
  58. return this;
  59. }
  60. return new RangeAsyncIterator(_start, count);
  61. }
  62. public ValueTask<int[]> ToArrayAsync(CancellationToken cancellationToken)
  63. {
  64. var res = new int[_end - _start];
  65. var value = _start;
  66. for (var i = 0; i < res.Length; i++)
  67. {
  68. res[i] = value++;
  69. }
  70. return new ValueTask<int[]>(res);
  71. }
  72. public ValueTask<List<int>> ToListAsync(CancellationToken cancellationToken)
  73. {
  74. var res = new List<int>(_end - _start);
  75. for (var value = _start; value < _end; value++)
  76. {
  77. res.Add(value);
  78. }
  79. return new ValueTask<List<int>>(res);
  80. }
  81. public ValueTask<Maybe<int>> TryGetElementAtAsync(int index, CancellationToken cancellationToken)
  82. {
  83. if ((uint)index < (uint)(_end - _start))
  84. {
  85. return new ValueTask<Maybe<int>>(new Maybe<int>(_start + index));
  86. }
  87. return new ValueTask<Maybe<int>>(new Maybe<int>());
  88. }
  89. public ValueTask<Maybe<int>> TryGetFirstAsync(CancellationToken cancellationToken) => new(new Maybe<int>(_start));
  90. public ValueTask<Maybe<int>> TryGetLastAsync(CancellationToken cancellationToken) => new(new Maybe<int>(_end - 1));
  91. protected override ValueTask<bool> MoveNextCore()
  92. {
  93. switch (_state)
  94. {
  95. case AsyncIteratorState.Allocated:
  96. _current = _start;
  97. _state = AsyncIteratorState.Iterating;
  98. return new ValueTask<bool>(true);
  99. case AsyncIteratorState.Iterating:
  100. _current++;
  101. if (_current != _end)
  102. {
  103. return new ValueTask<bool>(true);
  104. }
  105. break;
  106. }
  107. return Core();
  108. async ValueTask<bool> Core()
  109. {
  110. await DisposeAsync().ConfigureAwait(false);
  111. return false;
  112. }
  113. }
  114. }
  115. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  116. }
  117. }