Distinct.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. 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. /// Returns an async-enumerable sequence that contains only distinct elements.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">An async-enumerable sequence to retain distinct elements for.</param>
  16. /// <returns>An async-enumerable sequence only containing the distinct elements from the source sequence.</returns>
  17. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  18. /// <remarks>Usage of this operator should be considered carefully due to the maintenance of an internal lookup structure which can grow large.</remarks>
  19. public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source) => Distinct(source, comparer: null);
  20. /// <summary>
  21. /// Returns an async-enumerable sequence that contains only distinct elements according to the comparer.
  22. /// </summary>
  23. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  24. /// <param name="source">An async-enumerable sequence to retain distinct elements for.</param>
  25. /// <param name="comparer">Equality comparer for source elements.</param>
  26. /// <returns>An async-enumerable sequence only containing the distinct elements from the source sequence.</returns>
  27. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="comparer"/> is null.</exception>
  28. /// <remarks>Usage of this operator should be considered carefully due to the maintenance of an internal lookup structure which can grow large.</remarks>
  29. public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer)
  30. {
  31. if (source == null)
  32. throw Error.ArgumentNull(nameof(source));
  33. return new DistinctAsyncIterator<TSource>(source, comparer);
  34. }
  35. private sealed class DistinctAsyncIterator<TSource> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
  36. {
  37. private readonly IEqualityComparer<TSource>? _comparer;
  38. private readonly IAsyncEnumerable<TSource> _source;
  39. private IAsyncEnumerator<TSource>? _enumerator;
  40. private Set<TSource>? _set;
  41. public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer)
  42. {
  43. _source = source;
  44. _comparer = comparer;
  45. }
  46. public async ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  47. {
  48. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  49. return s.ToArray();
  50. }
  51. public async ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  52. {
  53. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  54. return s.ToList();
  55. }
  56. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  57. {
  58. if (onlyIfCheap)
  59. {
  60. return new ValueTask<int>(-1);
  61. }
  62. return Core();
  63. async ValueTask<int> Core()
  64. {
  65. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  66. return s.Count;
  67. }
  68. }
  69. public override AsyncIteratorBase<TSource> Clone()
  70. {
  71. return new DistinctAsyncIterator<TSource>(_source, _comparer);
  72. }
  73. public override async ValueTask DisposeAsync()
  74. {
  75. if (_enumerator != null)
  76. {
  77. await _enumerator.DisposeAsync().ConfigureAwait(false);
  78. _enumerator = null;
  79. _set = null;
  80. }
  81. await base.DisposeAsync().ConfigureAwait(false);
  82. }
  83. protected override async ValueTask<bool> MoveNextCore()
  84. {
  85. switch (_state)
  86. {
  87. case AsyncIteratorState.Allocated:
  88. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  89. if (!await _enumerator.MoveNextAsync().ConfigureAwait(false))
  90. {
  91. await DisposeAsync().ConfigureAwait(false);
  92. return false;
  93. }
  94. var element = _enumerator.Current;
  95. _set = new Set<TSource>(_comparer);
  96. _set.Add(element);
  97. _current = element;
  98. _state = AsyncIteratorState.Iterating;
  99. return true;
  100. case AsyncIteratorState.Iterating:
  101. while (await _enumerator!.MoveNextAsync().ConfigureAwait(false))
  102. {
  103. element = _enumerator.Current;
  104. if (_set!.Add(element))
  105. {
  106. _current = element;
  107. return true;
  108. }
  109. }
  110. break;
  111. }
  112. await DisposeAsync().ConfigureAwait(false);
  113. return false;
  114. }
  115. private Task<Set<TSource>> FillSetAsync(CancellationToken cancellationToken)
  116. {
  117. return AsyncEnumerableHelpers.ToSet(_source, _comparer, cancellationToken);
  118. }
  119. }
  120. }
  121. }