Contains.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. return ContainsCore(source, value, CancellationToken.None);
  16. }
  17. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken)
  18. {
  19. if (source == null)
  20. throw Error.ArgumentNull(nameof(source));
  21. return ContainsCore(source, value, cancellationToken);
  22. }
  23. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer)
  24. {
  25. if (source == null)
  26. throw Error.ArgumentNull(nameof(source));
  27. return ContainsCore(source, value, comparer, CancellationToken.None);
  28. }
  29. public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  30. {
  31. if (source == null)
  32. throw Error.ArgumentNull(nameof(source));
  33. return ContainsCore(source, value, comparer, cancellationToken);
  34. }
  35. private static Task<bool> ContainsCore<TSource>(IAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken)
  36. {
  37. if (source is ICollection<TSource> collection)
  38. {
  39. return Task.FromResult(collection.Contains(value));
  40. }
  41. return ContainsCore(source, value, comparer: null, cancellationToken);
  42. }
  43. private static async Task<bool> ContainsCore<TSource>(IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  44. {
  45. var e = source.GetAsyncEnumerator(cancellationToken);
  46. try
  47. {
  48. //
  49. // See https://github.com/dotnet/corefx/pull/25097 for the optimization here.
  50. //
  51. if (comparer == null)
  52. {
  53. while (await e.MoveNextAsync().ConfigureAwait(false))
  54. {
  55. if (EqualityComparer<TSource>.Default.Equals(e.Current, value))
  56. {
  57. return true;
  58. }
  59. }
  60. }
  61. else
  62. {
  63. while (await e.MoveNextAsync().ConfigureAwait(false))
  64. {
  65. if (comparer.Equals(e.Current, value))
  66. {
  67. return true;
  68. }
  69. }
  70. }
  71. }
  72. finally
  73. {
  74. await e.DisposeAsync().ConfigureAwait(false);
  75. }
  76. return false;
  77. }
  78. }
  79. }