Distinct.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 new ArgumentNullException(nameof(source));
  16. return source.Distinct(EqualityComparer<TSource>.Default);
  17. }
  18. public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  19. {
  20. if (source == null)
  21. throw new ArgumentNullException(nameof(source));
  22. if (comparer == null)
  23. throw new ArgumentNullException(nameof(comparer));
  24. return new DistinctAsyncIterator<TSource>(source, comparer);
  25. }
  26. private sealed class DistinctAsyncIterator<TSource> : AsyncIterator<TSource>, IAsyncIListProvider<TSource>
  27. {
  28. private readonly IEqualityComparer<TSource> comparer;
  29. private readonly IAsyncEnumerable<TSource> source;
  30. private IAsyncEnumerator<TSource> enumerator;
  31. private Set<TSource> set;
  32. public DistinctAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  33. {
  34. Debug.Assert(source != null);
  35. this.source = source;
  36. this.comparer = comparer;
  37. }
  38. public async Task<TSource[]> ToArrayAsync(CancellationToken cancellationToken)
  39. {
  40. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  41. return s.ToArray();
  42. }
  43. public async Task<List<TSource>> ToListAsync(CancellationToken cancellationToken)
  44. {
  45. var s = await FillSetAsync(cancellationToken).ConfigureAwait(false);
  46. return s.ToList();
  47. }
  48. public async Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken)
  49. {
  50. return onlyIfCheap ? -1 : (await FillSetAsync(cancellationToken).ConfigureAwait(false)).Count;
  51. }
  52. public override AsyncIterator<TSource> Clone()
  53. {
  54. return new DistinctAsyncIterator<TSource>(source, comparer);
  55. }
  56. public override async Task 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 Task<bool> MoveNextCore()
  67. {
  68. switch (state)
  69. {
  70. case AsyncIteratorState.Allocated:
  71. enumerator = source.GetAsyncEnumerator();
  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 async Task<Set<TSource>> FillSetAsync(CancellationToken cancellationToken)
  99. {
  100. var s = new Set<TSource>(comparer);
  101. await s.UnionWithAsync(source);
  102. return s;
  103. }
  104. }
  105. }
  106. }