Intersect.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace Cysharp.Threading.Tasks.Linq
  6. {
  7. public static partial class UniTaskAsyncEnumerable
  8. {
  9. public static IUniTaskAsyncEnumerable<TSource> Intersect<TSource>(this IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second)
  10. {
  11. Error.ThrowArgumentNullException(first, nameof(first));
  12. Error.ThrowArgumentNullException(second, nameof(second));
  13. return new Intersect<TSource>(first, second, EqualityComparer<TSource>.Default);
  14. }
  15. public static IUniTaskAsyncEnumerable<TSource> Intersect<TSource>(this IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  16. {
  17. Error.ThrowArgumentNullException(first, nameof(first));
  18. Error.ThrowArgumentNullException(second, nameof(second));
  19. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  20. return new Intersect<TSource>(first, second, comparer);
  21. }
  22. }
  23. internal sealed class Intersect<TSource> : IUniTaskAsyncEnumerable<TSource>
  24. {
  25. readonly IUniTaskAsyncEnumerable<TSource> first;
  26. readonly IUniTaskAsyncEnumerable<TSource> second;
  27. readonly IEqualityComparer<TSource> comparer;
  28. public Intersect(IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  29. {
  30. this.first = first;
  31. this.second = second;
  32. this.comparer = comparer;
  33. }
  34. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  35. {
  36. return new _Intersect(first, second, comparer, cancellationToken);
  37. }
  38. class _Intersect : AsyncEnumeratorBase<TSource, TSource>
  39. {
  40. static Action<object> HashSetAsyncCoreDelegate = HashSetAsyncCore;
  41. readonly IEqualityComparer<TSource> comparer;
  42. readonly IUniTaskAsyncEnumerable<TSource> second;
  43. HashSet<TSource> set;
  44. UniTask<HashSet<TSource>>.Awaiter awaiter;
  45. public _Intersect(IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  46. : base(first, cancellationToken)
  47. {
  48. this.second = second;
  49. this.comparer = comparer;
  50. }
  51. protected override bool OnFirstIteration()
  52. {
  53. if (set != null) return false;
  54. awaiter = second.ToHashSetAsync(cancellationToken).GetAwaiter();
  55. if (awaiter.IsCompleted)
  56. {
  57. set = awaiter.GetResult();
  58. SourceMoveNext();
  59. }
  60. else
  61. {
  62. awaiter.SourceOnCompleted(HashSetAsyncCoreDelegate, this);
  63. }
  64. return true;
  65. }
  66. static void HashSetAsyncCore(object state)
  67. {
  68. var self = (_Intersect)state;
  69. if (self.TryGetResult(self.awaiter, out var result))
  70. {
  71. self.set = result;
  72. self.SourceMoveNext();
  73. }
  74. }
  75. protected override bool TryMoveNextCore(bool sourceHasCurrent, out bool result)
  76. {
  77. if (sourceHasCurrent)
  78. {
  79. var v = SourceCurrent;
  80. if (set.Remove(v))
  81. {
  82. Current = v;
  83. result = true;
  84. return true;
  85. }
  86. else
  87. {
  88. result = default;
  89. return false;
  90. }
  91. }
  92. result = false;
  93. return true;
  94. }
  95. }
  96. }
  97. }