AsyncListPartition.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // Copied from https://github.com/dotnet/corefx/blob/5f1dd8298e4355b63bb760d88d437a91b3ca808c/src/System.Linq/src/System/Linq/Partition.cs
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  12. /// <summary>
  13. /// An iterator that yields the items of part of an <see cref="IList{TSource}"/>.
  14. /// </summary>
  15. /// <typeparam name="TSource">The type of the source list.</typeparam>
  16. internal sealed class AsyncListPartition<TSource> : AsyncIterator<TSource>, IAsyncPartition<TSource>
  17. {
  18. private readonly IList<TSource> _source;
  19. private readonly int _minIndexInclusive;
  20. private readonly int _maxIndexInclusive;
  21. private int _index;
  22. public AsyncListPartition(IList<TSource> source, int minIndexInclusive, int maxIndexInclusive)
  23. {
  24. Debug.Assert(minIndexInclusive >= 0);
  25. Debug.Assert(minIndexInclusive <= maxIndexInclusive);
  26. _source = source;
  27. _minIndexInclusive = minIndexInclusive;
  28. _maxIndexInclusive = maxIndexInclusive;
  29. _index = 0;
  30. }
  31. public override AsyncIteratorBase<TSource> Clone()
  32. {
  33. return new AsyncListPartition<TSource>(_source, _minIndexInclusive, _maxIndexInclusive);
  34. }
  35. protected override ValueTask<bool> MoveNextCore()
  36. {
  37. if ((uint)_index <= (uint)(_maxIndexInclusive - _minIndexInclusive) && _index < _source.Count - _minIndexInclusive)
  38. {
  39. _current = _source[_minIndexInclusive + _index];
  40. ++_index;
  41. return new ValueTask<bool>(true);
  42. }
  43. return Core();
  44. async ValueTask<bool> Core()
  45. {
  46. await DisposeAsync().ConfigureAwait(false);
  47. return false;
  48. }
  49. }
  50. #if NOT_YET
  51. public override IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
  52. {
  53. return new SelectListPartitionIterator<TSource, TResult>(_source, selector, _minIndexInclusive, _maxIndexInclusive);
  54. }
  55. public override IEnumerable<TResult> Select<TResult>(Func<TSource, ValueTask<TResult>> selector)
  56. {
  57. return new SelectListPartitionIterator<TSource, TResult>(_source, selector, _minIndexInclusive, _maxIndexInclusive);
  58. }
  59. #endif
  60. public IAsyncPartition<TSource> Skip(int count)
  61. {
  62. var minIndex = _minIndexInclusive + count;
  63. if ((uint)minIndex > (uint)_maxIndexInclusive)
  64. {
  65. return AsyncEnumerable.EmptyAsyncIterator<TSource>.Instance;
  66. }
  67. else
  68. {
  69. return new AsyncListPartition<TSource>(_source, minIndex, _maxIndexInclusive);
  70. }
  71. }
  72. public IAsyncPartition<TSource> Take(int count)
  73. {
  74. var maxIndex = _minIndexInclusive + count - 1;
  75. if ((uint)maxIndex >= (uint)_maxIndexInclusive)
  76. {
  77. return this;
  78. }
  79. else
  80. {
  81. return new AsyncListPartition<TSource>(_source, _minIndexInclusive, maxIndex);
  82. }
  83. }
  84. public ValueTask<Maybe<TSource>> TryGetElementAtAsync(int index, CancellationToken cancellationToken)
  85. {
  86. if ((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive) && index < _source.Count - _minIndexInclusive)
  87. {
  88. var res = _source[_minIndexInclusive + index];
  89. return new ValueTask<Maybe<TSource>>(new Maybe<TSource>(res));
  90. }
  91. return new ValueTask<Maybe<TSource>>(new Maybe<TSource>());
  92. }
  93. public ValueTask<Maybe<TSource>> TryGetFirstAsync(CancellationToken cancellationToken)
  94. {
  95. if (_source.Count > _minIndexInclusive)
  96. {
  97. var res = _source[_minIndexInclusive];
  98. return new ValueTask<Maybe<TSource>>(new Maybe<TSource>(res));
  99. }
  100. return new ValueTask<Maybe<TSource>>(new Maybe<TSource>());
  101. }
  102. public ValueTask<Maybe<TSource>> TryGetLastAsync(CancellationToken cancellationToken)
  103. {
  104. var lastIndex = _source.Count - 1;
  105. if (lastIndex >= _minIndexInclusive)
  106. {
  107. var res = _source[Math.Min(lastIndex, _maxIndexInclusive)];
  108. return new ValueTask<Maybe<TSource>>(new Maybe<TSource>(res));
  109. }
  110. return new ValueTask<Maybe<TSource>>(new Maybe<TSource>());
  111. }
  112. private int Count
  113. {
  114. get
  115. {
  116. var count = _source.Count;
  117. if (count <= _minIndexInclusive)
  118. {
  119. return 0;
  120. }
  121. return Math.Min(count - 1, _maxIndexInclusive) - _minIndexInclusive + 1;
  122. }
  123. }
  124. public ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  125. {
  126. var count = Count;
  127. if (count == 0)
  128. {
  129. return new ValueTask<TSource[]>([]);
  130. }
  131. var array = new TSource[count];
  132. for (int i = 0, curIdx = _minIndexInclusive; i != array.Length; ++i, ++curIdx)
  133. {
  134. array[i] = _source[curIdx];
  135. }
  136. return new ValueTask<TSource[]>(array);
  137. }
  138. public ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  139. {
  140. var count = Count;
  141. if (count == 0)
  142. {
  143. return new ValueTask<List<TSource>>([]);
  144. }
  145. var list = new List<TSource>(count);
  146. var end = _minIndexInclusive + count;
  147. for (var i = _minIndexInclusive; i != end; ++i)
  148. {
  149. list.Add(_source[i]);
  150. }
  151. return new ValueTask<List<TSource>>(list);
  152. }
  153. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  154. {
  155. return new ValueTask<int>(Count);
  156. }
  157. }
  158. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  159. }