DistinctUntilChanged.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return DistinctUntilChangedCore(source, EqualityComparer<TSource>.Default);
  16. }
  17. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. if (comparer == null)
  22. throw new ArgumentNullException(nameof(comparer));
  23. return DistinctUntilChangedCore(source, comparer);
  24. }
  25. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  26. {
  27. if (source == null)
  28. throw new ArgumentNullException(nameof(source));
  29. if (keySelector == null)
  30. throw new ArgumentNullException(nameof(keySelector));
  31. return DistinctUntilChangedCore(source, keySelector, EqualityComparer<TKey>.Default);
  32. }
  33. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  34. {
  35. if (source == null)
  36. throw new ArgumentNullException(nameof(source));
  37. if (keySelector == null)
  38. throw new ArgumentNullException(nameof(keySelector));
  39. if (comparer == null)
  40. throw new ArgumentNullException(nameof(comparer));
  41. return DistinctUntilChangedCore(source, keySelector, comparer);
  42. }
  43. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector)
  44. {
  45. if (source == null)
  46. throw new ArgumentNullException(nameof(source));
  47. if (keySelector == null)
  48. throw new ArgumentNullException(nameof(keySelector));
  49. return DistinctUntilChangedCore(source, keySelector, EqualityComparer<TKey>.Default);
  50. }
  51. public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  52. {
  53. if (source == null)
  54. throw new ArgumentNullException(nameof(source));
  55. if (keySelector == null)
  56. throw new ArgumentNullException(nameof(keySelector));
  57. if (comparer == null)
  58. throw new ArgumentNullException(nameof(comparer));
  59. return DistinctUntilChangedCore(source, keySelector, comparer);
  60. }
  61. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource>(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  62. {
  63. return new DistinctUntilChangedAsyncIterator<TSource>(source, comparer);
  64. }
  65. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  66. {
  67. return new DistinctUntilChangedAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  68. }
  69. private static IAsyncEnumerable<TSource> DistinctUntilChangedCore<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  70. {
  71. return new DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  72. }
  73. private sealed class DistinctUntilChangedAsyncIterator<TSource> : AsyncIterator<TSource>
  74. {
  75. private readonly IEqualityComparer<TSource> comparer;
  76. private readonly IAsyncEnumerable<TSource> source;
  77. private TSource currentValue;
  78. private IAsyncEnumerator<TSource> enumerator;
  79. private bool hasCurrentValue;
  80. public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  81. {
  82. Debug.Assert(comparer != null);
  83. Debug.Assert(source != null);
  84. this.source = source;
  85. this.comparer = comparer;
  86. }
  87. public override AsyncIterator<TSource> Clone()
  88. {
  89. return new DistinctUntilChangedAsyncIterator<TSource>(source, comparer);
  90. }
  91. public override async Task DisposeAsync()
  92. {
  93. if (enumerator != null)
  94. {
  95. await enumerator.DisposeAsync().ConfigureAwait(false);
  96. enumerator = null;
  97. currentValue = default(TSource);
  98. }
  99. await base.DisposeAsync().ConfigureAwait(false);
  100. }
  101. protected override async Task<bool> MoveNextCore()
  102. {
  103. switch (state)
  104. {
  105. case AsyncIteratorState.Allocated:
  106. enumerator = source.GetAsyncEnumerator();
  107. state = AsyncIteratorState.Iterating;
  108. goto case AsyncIteratorState.Iterating;
  109. case AsyncIteratorState.Iterating:
  110. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  111. {
  112. var item = enumerator.Current;
  113. var comparerEquals = false;
  114. if (hasCurrentValue)
  115. {
  116. comparerEquals = comparer.Equals(currentValue, item);
  117. }
  118. if (!hasCurrentValue || !comparerEquals)
  119. {
  120. hasCurrentValue = true;
  121. currentValue = item;
  122. current = item;
  123. return true;
  124. }
  125. }
  126. break;
  127. }
  128. await DisposeAsync().ConfigureAwait(false);
  129. return false;
  130. }
  131. }
  132. private sealed class DistinctUntilChangedAsyncIterator<TSource, TKey> : AsyncIterator<TSource>
  133. {
  134. private readonly IEqualityComparer<TKey> comparer;
  135. private readonly Func<TSource, TKey> keySelector;
  136. private readonly IAsyncEnumerable<TSource> source;
  137. private TKey currentKeyValue;
  138. private IAsyncEnumerator<TSource> enumerator;
  139. private bool hasCurrentKey;
  140. public DistinctUntilChangedAsyncIterator(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  141. {
  142. this.source = source;
  143. this.keySelector = keySelector;
  144. this.comparer = comparer;
  145. }
  146. public override AsyncIterator<TSource> Clone()
  147. {
  148. return new DistinctUntilChangedAsyncIterator<TSource, TKey>(source, keySelector, comparer);
  149. }
  150. public override async Task DisposeAsync()
  151. {
  152. if (enumerator != null)
  153. {
  154. await enumerator.DisposeAsync().ConfigureAwait(false);
  155. enumerator = null;
  156. currentKeyValue = default(TKey);
  157. }
  158. await base.DisposeAsync().ConfigureAwait(false);
  159. }
  160. protected override async Task<bool> MoveNextCore()
  161. {
  162. switch (state)
  163. {
  164. case AsyncIteratorState.Allocated:
  165. enumerator = source.GetAsyncEnumerator();
  166. state = AsyncIteratorState.Iterating;
  167. goto case AsyncIteratorState.Iterating;
  168. case AsyncIteratorState.Iterating:
  169. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  170. {
  171. var item = enumerator.Current;
  172. var key = keySelector(item);
  173. var comparerEquals = false;
  174. if (hasCurrentKey)
  175. {
  176. comparerEquals = comparer.Equals(currentKeyValue, key);
  177. }
  178. if (!hasCurrentKey || !comparerEquals)
  179. {
  180. hasCurrentKey = true;
  181. currentKeyValue = key;
  182. current = item;
  183. return true;
  184. }
  185. }
  186. break; // case
  187. }
  188. await DisposeAsync().ConfigureAwait(false);
  189. return false;
  190. }
  191. }
  192. private sealed class DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey> : AsyncIterator<TSource>
  193. {
  194. private readonly IEqualityComparer<TKey> comparer;
  195. private readonly Func<TSource, Task<TKey>> keySelector;
  196. private readonly IAsyncEnumerable<TSource> source;
  197. private TKey currentKeyValue;
  198. private IAsyncEnumerator<TSource> enumerator;
  199. private bool hasCurrentKey;
  200. public DistinctUntilChangedAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  201. {
  202. this.source = source;
  203. this.keySelector = keySelector;
  204. this.comparer = comparer;
  205. }
  206. public override AsyncIterator<TSource> Clone()
  207. {
  208. return new DistinctUntilChangedAsyncIteratorWithTask<TSource, TKey>(source, keySelector, comparer);
  209. }
  210. public override async Task DisposeAsync()
  211. {
  212. if (enumerator != null)
  213. {
  214. await enumerator.DisposeAsync().ConfigureAwait(false);
  215. enumerator = null;
  216. currentKeyValue = default(TKey);
  217. }
  218. await base.DisposeAsync().ConfigureAwait(false);
  219. }
  220. protected override async Task<bool> MoveNextCore()
  221. {
  222. switch (state)
  223. {
  224. case AsyncIteratorState.Allocated:
  225. enumerator = source.GetAsyncEnumerator();
  226. state = AsyncIteratorState.Iterating;
  227. goto case AsyncIteratorState.Iterating;
  228. case AsyncIteratorState.Iterating:
  229. while (await enumerator.MoveNextAsync().ConfigureAwait(false))
  230. {
  231. var item = enumerator.Current;
  232. var key = await keySelector(item).ConfigureAwait(false);
  233. var comparerEquals = false;
  234. if (hasCurrentKey)
  235. {
  236. comparerEquals = comparer.Equals(currentKeyValue, key);
  237. }
  238. if (!hasCurrentKey || !comparerEquals)
  239. {
  240. hasCurrentKey = true;
  241. currentKeyValue = key;
  242. current = item;
  243. return true;
  244. }
  245. }
  246. break; // case
  247. }
  248. await DisposeAsync().ConfigureAwait(false);
  249. return false;
  250. }
  251. }
  252. }
  253. }