ToHashSet.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  12. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.tohashsetasync?view=net-9.0-pp
  13. // That one overload covers the next two methods, because it supplieds a default comparer.
  14. /// <summary>
  15. /// Creates a hash set from an async-enumerable sequence.
  16. /// </summary>
  17. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  18. /// <param name="source">The source async-enumerable sequence to get a hash set of elements for.</param>
  19. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  20. /// <returns>An async-enumerable sequence containing a single element with a hash set containing all the elements of the source sequence.</returns>
  21. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  22. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  23. public static ValueTask<HashSet<TSource>> ToHashSetAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default) =>
  24. ToHashSetAsync(source, comparer: null, cancellationToken);
  25. /// <summary>
  26. /// Creates a hash set from an async-enumerable sequence.
  27. /// </summary>
  28. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  29. /// <param name="source">The source async-enumerable sequence to get a hash set of elements for.</param>
  30. /// <param name="comparer">An equality comparer to compare elements of the sequence.</param>
  31. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  32. /// <returns>An async-enumerable sequence containing a single element with a hash set containing all the elements of the source sequence.</returns>
  33. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  34. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  35. public static ValueTask<HashSet<TSource>> ToHashSetAsync<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer, CancellationToken cancellationToken = default)
  36. {
  37. if (source == null)
  38. throw Error.ArgumentNull(nameof(source));
  39. return Core(source, comparer, cancellationToken);
  40. static async ValueTask<HashSet<TSource>> Core(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer, CancellationToken cancellationToken)
  41. {
  42. var set = new HashSet<TSource>(comparer);
  43. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  44. {
  45. set.Add(item);
  46. }
  47. return set;
  48. }
  49. }
  50. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  51. }
  52. }