1
0

HashSet.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #if NO_HASHSET
  5. using System;
  6. using System.Collections.Generic;
  7. namespace System.Reactive
  8. {
  9. class HashSet<T>
  10. {
  11. private readonly Dictionary<T, object> _set;
  12. private bool _hasNull;
  13. public HashSet(IEqualityComparer<T> comparer)
  14. {
  15. _set = new Dictionary<T, object>(comparer);
  16. _hasNull = false;
  17. }
  18. public bool Add(T value)
  19. {
  20. //
  21. // Note: The box instruction in the IL will be erased by the JIT in case T is
  22. // a value type. See GroupBy for more information.
  23. //
  24. if (value == null)
  25. {
  26. if (_hasNull)
  27. return false;
  28. _hasNull = true;
  29. return true;
  30. }
  31. else
  32. {
  33. if (_set.ContainsKey(value))
  34. return false;
  35. _set[value] = null;
  36. return true;
  37. }
  38. }
  39. }
  40. }
  41. #endif