Distinct.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 CreateEnumerable(
  88. () =>
  89. {
  90. var e = source.GetEnumerator();
  91. var cts = new CancellationTokenDisposable();
  92. var d = Disposable.Create(cts, e);
  93. var currentKey = default(TKey);
  94. var hasCurrentKey = false;
  95. var current = default(TSource);
  96. var f = default(Func<CancellationToken, Task<bool>>);
  97. f = async ct =>
  98. {
  99. if (await e.MoveNext(ct)
  100. .ConfigureAwait(false))
  101. {
  102. var item = e.Current;
  103. var key = default(TKey);
  104. var comparerEquals = false;
  105. key = keySelector(item);
  106. if (hasCurrentKey)
  107. {
  108. comparerEquals = comparer.Equals(currentKey, key);
  109. }
  110. if (!hasCurrentKey || !comparerEquals)
  111. {
  112. hasCurrentKey = true;
  113. currentKey = key;
  114. current = item;
  115. return true;
  116. }
  117. return await f(ct)
  118. .ConfigureAwait(false);
  119. }
  120. return false;
  121. };
  122. return CreateEnumerator(
  123. f,
  124. () => current,
  125. d.Dispose,
  126. e
  127. );
  128. });
  129. }
  130. }
  131. }