Distinct.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source) => Distinct(source, comparer: null);
  12. public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer)
  13. {
  14. if (source == null)
  15. throw Error.ArgumentNull(nameof(source));
  16. return new DistinctAsyncIterator<TSource>(source, comparer);
  17. }
  18. private sealed class DistinctAsyncIterator<TSource> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
  19. {
  20. private readonly IEqualityComparer<TSource>? _comparer;
  21. private readonly IAsyncEnumerable<TSource> _source;
  22. private IAsyncEnumerator<TSource>? _enumerator;
  23. private Set<TSource>? _set;
  24. public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer)
  25. {
  26. _source = source;
  27. _comparer = comparer;
  28. }
  29. public async ValueTask<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  30. {
  31. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  32. return s.ToArray();
  33. }
  34. public async ValueTask<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  35. {
  36. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  37. return s.ToList();
  38. }
  39. public ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  40. {
  41. if (onlyIfCheap)
  42. {
  43. return new ValueTask<int>(-1);
  44. }
  45. return Core();
  46. async ValueTask<int> Core()
  47. {
  48. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  49. return s.Count;
  50. }
  51. }
  52. public override AsyncIteratorBase<TSource> Clone()
  53. {
  54. return new DistinctAsyncIterator<TSource>(_source, _comparer);
  55. }
  56. public override async ValueTask DisposeAsync()
  57. {
  58. if (_enumerator != null)
  59. {
  60. await _enumerator.DisposeAsync().ConfigureAwait(false);
  61. _enumerator = null;
  62. _set = null;
  63. }
  64. await base.DisposeAsync().ConfigureAwait(false);
  65. }
  66. protected override async ValueTask<bool> MoveNextCore()
  67. {
  68. switch (_state)
  69. {
  70. case AsyncIteratorState.Allocated:
  71. _enumerator = _source.GetAsyncEnumerator(_cancellationToken);
  72. if (!await _enumerator.MoveNextAsync().ConfigureAwait(false))
  73. {
  74. await DisposeAsync().ConfigureAwait(false);
  75. return false;
  76. }
  77. var element = _enumerator.Current;
  78. _set = new Set<TSource>(_comparer);
  79. _set.Add(element);
  80. _current = element;
  81. _state = AsyncIteratorState.Iterating;
  82. return true;
  83. case AsyncIteratorState.Iterating:
  84. while (await _enumerator!.MoveNextAsync().ConfigureAwait(false))
  85. {
  86. element = _enumerator.Current;
  87. if (_set!.Add(element))
  88. {
  89. _current = element;
  90. return true;
  91. }
  92. }
  93. break;
  94. }
  95. await DisposeAsync().ConfigureAwait(false);
  96. return false;
  97. }
  98. private Task<Set<TSource>> FillSetAsync(CancellationToken cancellationToken)
  99. {
  100. return AsyncEnumerableHelpers.ToSet(_source, _comparer, cancellationToken);
  101. }
  102. }
  103. }
  104. }