ToHashSet.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. /// <summary>
  12. /// Creates a hash set from an async-enumerable sequence.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">The source async-enumerable sequence to get a hash set of elements for.</param>
  16. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  17. /// <returns>An async-enumerable sequence containing a single element with a hash set containing all the elements of the source sequence.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  19. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  20. public static ValueTask<HashSet<TSource>> ToHashSetAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default) =>
  21. ToHashSetAsync(source, comparer: null, cancellationToken);
  22. /// <summary>
  23. /// Creates a hash set from an async-enumerable sequence.
  24. /// </summary>
  25. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  26. /// <param name="source">The source async-enumerable sequence to get a hash set of elements for.</param>
  27. /// <param name="comparer">An equality comparer to compare elements of the sequence.</param>
  28. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  29. /// <returns>An async-enumerable sequence containing a single element with a hash set containing all the elements of the source sequence.</returns>
  30. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  31. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  32. public static ValueTask<HashSet<TSource>> ToHashSetAsync<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer, CancellationToken cancellationToken = default)
  33. {
  34. if (source == null)
  35. throw Error.ArgumentNull(nameof(source));
  36. return Core(source, comparer, cancellationToken);
  37. static async ValueTask<HashSet<TSource>> Core(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer, CancellationToken cancellationToken)
  38. {
  39. var set = new HashSet<TSource>(comparer);
  40. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  41. {
  42. set.Add(item);
  43. }
  44. return set;
  45. }
  46. }
  47. }
  48. }