Pairwise.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static IUniTaskAsyncEnumerable<(TSource, TSource)> Pairwise<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return new Pairwise<TSource>(source);
  12. }
  13. }
  14. internal sealed class Pairwise<TSource> : IUniTaskAsyncEnumerable<(TSource, TSource)>
  15. {
  16. readonly IUniTaskAsyncEnumerable<TSource> source;
  17. public Pairwise(IUniTaskAsyncEnumerable<TSource> source)
  18. {
  19. this.source = source;
  20. }
  21. public IUniTaskAsyncEnumerator<(TSource, TSource)> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  22. {
  23. return new _Pairwise(source, cancellationToken);
  24. }
  25. sealed class _Pairwise : MoveNextSource, IUniTaskAsyncEnumerator<(TSource, TSource)>
  26. {
  27. static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
  28. readonly IUniTaskAsyncEnumerable<TSource> source;
  29. CancellationToken cancellationToken;
  30. IUniTaskAsyncEnumerator<TSource> enumerator;
  31. UniTask<bool>.Awaiter awaiter;
  32. TSource prev;
  33. bool isFirst;
  34. public _Pairwise(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  35. {
  36. this.source = source;
  37. this.cancellationToken = cancellationToken;
  38. TaskTracker.TrackActiveTask(this, 3);
  39. }
  40. public (TSource, TSource) Current { get; private set; }
  41. public UniTask<bool> MoveNextAsync()
  42. {
  43. cancellationToken.ThrowIfCancellationRequested();
  44. if (enumerator == null)
  45. {
  46. isFirst = true;
  47. enumerator = source.GetAsyncEnumerator(cancellationToken);
  48. }
  49. completionSource.Reset();
  50. SourceMoveNext();
  51. return new UniTask<bool>(this, completionSource.Version);
  52. }
  53. void SourceMoveNext()
  54. {
  55. try
  56. {
  57. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  58. if (awaiter.IsCompleted)
  59. {
  60. MoveNextCore(this);
  61. }
  62. else
  63. {
  64. awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. completionSource.TrySetException(ex);
  70. }
  71. }
  72. static void MoveNextCore(object state)
  73. {
  74. var self = (_Pairwise)state;
  75. if (self.TryGetResult(self.awaiter, out var result))
  76. {
  77. if (result)
  78. {
  79. if (self.isFirst)
  80. {
  81. self.isFirst = false;
  82. self.prev = self.enumerator.Current;
  83. self.SourceMoveNext(); // run again. okay to use recursive(only one more).
  84. }
  85. else
  86. {
  87. var p = self.prev;
  88. self.prev = self.enumerator.Current;
  89. self.Current = (p, self.prev);
  90. self.completionSource.TrySetResult(true);
  91. }
  92. }
  93. else
  94. {
  95. self.completionSource.TrySetResult(false);
  96. }
  97. }
  98. }
  99. public UniTask DisposeAsync()
  100. {
  101. TaskTracker.RemoveTracking(this);
  102. if (enumerator != null)
  103. {
  104. return enumerator.DisposeAsync();
  105. }
  106. return default;
  107. }
  108. }
  109. }
  110. }