Distinct.cs 1.9 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. namespace System.Reactive.Linq.ObservableImpl
  6. {
  7. internal sealed class Distinct<TSource, TKey> : Producer<TSource, Distinct<TSource, TKey>._>
  8. {
  9. private readonly IObservable<TSource> _source;
  10. private readonly Func<TSource, TKey> _keySelector;
  11. private readonly IEqualityComparer<TKey> _comparer;
  12. public Distinct(IObservable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  13. {
  14. _source = source;
  15. _keySelector = keySelector;
  16. _comparer = comparer;
  17. }
  18. protected override _ CreateSink(IObserver<TSource> observer) => new _(this, observer);
  19. protected override void Run(_ sink) => sink.Run(_source);
  20. internal sealed class _ : IdentitySink<TSource>
  21. {
  22. private readonly Func<TSource, TKey> _keySelector;
  23. private readonly HashSet<TKey> _hashSet;
  24. public _(Distinct<TSource, TKey> parent, IObserver<TSource> observer)
  25. : base(observer)
  26. {
  27. _keySelector = parent._keySelector;
  28. _hashSet = new HashSet<TKey>(parent._comparer);
  29. }
  30. public override void OnNext(TSource value)
  31. {
  32. TKey key;
  33. bool hasAdded;
  34. try
  35. {
  36. key = _keySelector(value);
  37. hasAdded = _hashSet.Add(key);
  38. }
  39. catch (Exception exception)
  40. {
  41. ForwardOnError(exception);
  42. return;
  43. }
  44. if (hasAdded)
  45. {
  46. ForwardOnNext(value);
  47. }
  48. }
  49. }
  50. }
  51. }