SequenceEqual.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 UniTask<Boolean> SequenceEqualAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, CancellationToken cancellationToken = default)
  10. {
  11. return SequenceEqualAsync(first, second, EqualityComparer<TSource>.Default, cancellationToken);
  12. }
  13. public static UniTask<Boolean> SequenceEqualAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken = default)
  14. {
  15. Error.ThrowArgumentNullException(first, nameof(first));
  16. Error.ThrowArgumentNullException(second, nameof(second));
  17. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  18. return SequenceEqual.SequenceEqualAsync(first, second, comparer, cancellationToken);
  19. }
  20. }
  21. internal static class SequenceEqual
  22. {
  23. internal static async UniTask<bool> SequenceEqualAsync<TSource>(IUniTaskAsyncEnumerable<TSource> first, IUniTaskAsyncEnumerable<TSource> second, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  24. {
  25. var e1 = first.GetAsyncEnumerator(cancellationToken);
  26. try
  27. {
  28. var e2 = second.GetAsyncEnumerator(cancellationToken);
  29. try
  30. {
  31. while (true)
  32. {
  33. if (await e1.MoveNextAsync())
  34. {
  35. if (await e2.MoveNextAsync())
  36. {
  37. if (comparer.Equals(e1.Current, e2.Current))
  38. {
  39. continue;
  40. }
  41. else
  42. {
  43. return false;
  44. }
  45. }
  46. else
  47. {
  48. // e2 is finished, but e1 has value
  49. return false;
  50. }
  51. }
  52. else
  53. {
  54. // e1 is finished, e2?
  55. if (await e2.MoveNextAsync())
  56. {
  57. return false;
  58. }
  59. else
  60. {
  61. return true;
  62. }
  63. }
  64. }
  65. }
  66. finally
  67. {
  68. if (e2 != null)
  69. {
  70. await e2.DisposeAsync();
  71. }
  72. }
  73. }
  74. finally
  75. {
  76. if (e1 != null)
  77. {
  78. await e1.DisposeAsync();
  79. }
  80. }
  81. }
  82. }
  83. }