Distinct.cs 4.6 KB

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