Distinct.cs 4.7 KB

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