Distinct.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (keySelector == null)
  18. throw new ArgumentNullException(nameof(keySelector));
  19. if (comparer == null)
  20. throw new ArgumentNullException(nameof(comparer));
  21. return Defer(() =>
  22. {
  23. var set = new HashSet<TKey>(comparer);
  24. return source.Where(item => set.Add(keySelector(item)));
  25. });
  26. }
  27. public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (keySelector == null)
  32. throw new ArgumentNullException(nameof(keySelector));
  33. return source.Distinct(keySelector, EqualityComparer<TKey>.Default);
  34. }
  35. public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. if (comparer == null)
  40. throw new ArgumentNullException(nameof(comparer));
  41. return Defer(() =>
  42. {
  43. var set = new HashSet<TSource>(comparer);
  44. return source.Where(set.Add);
  45. });
  46. }
  47. public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source)
  48. {
  49. if (source == null)
  50. throw new ArgumentNullException(nameof(source));
  51. return source.Distinct(EqualityComparer<TSource>.Default);
  52. }
  53. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source)
  54. {
  55. if (source == null)
  56. throw new ArgumentNullException(nameof(source));
  57. return source.DistinctUntilChanged_(x => x, EqualityComparer<TSource>.Default);
  58. }
  59. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  60. {
  61. if (source == null)
  62. throw new ArgumentNullException(nameof(source));
  63. if (comparer == null)
  64. throw new ArgumentNullException(nameof(comparer));
  65. return source.DistinctUntilChanged_(x => x, comparer);
  66. }
  67. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  68. {
  69. if (source == null)
  70. throw new ArgumentNullException(nameof(source));
  71. if (keySelector == null)
  72. throw new ArgumentNullException(nameof(keySelector));
  73. return source.DistinctUntilChanged_(keySelector, EqualityComparer<TKey>.Default);
  74. }
  75. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  76. {
  77. if (source == null)
  78. throw new ArgumentNullException(nameof(source));
  79. if (keySelector == null)
  80. throw new ArgumentNullException(nameof(keySelector));
  81. if (comparer == null)
  82. throw new ArgumentNullException(nameof(comparer));
  83. return source.DistinctUntilChanged_(keySelector, comparer);
  84. }
  85. private static IAsyncEnumerable<TSource> DistinctUntilChanged_<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  86. {
  87. return Create(() =>
  88. {
  89. var e = source.GetEnumerator();
  90. var cts = new CancellationTokenDisposable();
  91. var d = Disposable.Create(cts, e);
  92. var currentKey = default(TKey);
  93. var hasCurrentKey = false;
  94. var current = default(TSource);
  95. var f = default(Func<CancellationToken, Task<bool>>);
  96. f = async ct =>
  97. {
  98. if (await e.MoveNext(ct)
  99. .ConfigureAwait(false))
  100. {
  101. var item = e.Current;
  102. var key = default(TKey);
  103. var comparerEquals = false;
  104. key = keySelector(item);
  105. if (hasCurrentKey)
  106. {
  107. comparerEquals = comparer.Equals(currentKey, key);
  108. }
  109. if (!hasCurrentKey || !comparerEquals)
  110. {
  111. hasCurrentKey = true;
  112. currentKey = key;
  113. current = item;
  114. return true;
  115. }
  116. return await f(ct)
  117. .ConfigureAwait(false);
  118. }
  119. return false;
  120. };
  121. return Create(
  122. f,
  123. () => current,
  124. d.Dispose,
  125. e
  126. );
  127. });
  128. }
  129. }
  130. }