AsyncListPartition.cs 6.2 KB

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