HashSet.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if NO_HASHSET
  3. using System;
  4. using System.Collections.Generic;
  5. namespace System.Reactive
  6. {
  7. class HashSet<T>
  8. {
  9. private readonly Dictionary<T, object> _set;
  10. private bool _hasNull;
  11. public HashSet(IEqualityComparer<T> comparer)
  12. {
  13. _set = new Dictionary<T, object>(comparer);
  14. _hasNull = false;
  15. }
  16. public bool Add(T value)
  17. {
  18. //
  19. // Note: The box instruction in the IL will be erased by the JIT in case T is
  20. // a value type. See GroupBy for more information.
  21. //
  22. if (value == null)
  23. {
  24. if (_hasNull)
  25. return false;
  26. _hasNull = true;
  27. return true;
  28. }
  29. else
  30. {
  31. if (_set.ContainsKey(value))
  32. return false;
  33. _set[value] = null;
  34. return true;
  35. }
  36. }
  37. }
  38. }
  39. #endif