PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Microsoft.Scripting/Utils/Set.cs

https://bitbucket.org/stefanrusek/xronos
C# | 86 lines | 52 code | 17 blank | 17 comment | 0 complexity | 58449de3cbb8860000ad556e59ef2a15 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System; using Microsoft;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. namespace Microsoft.Scripting.Utils {
  19. /// <summary>
  20. /// A simple hashset, built on Dictionary{K, V}
  21. /// </summary>
  22. internal sealed class Set<T> : ICollection<T> {
  23. private readonly Dictionary<T, object> _data;
  24. internal Set() {
  25. _data = new Dictionary<T, object>();
  26. }
  27. internal Set(IEqualityComparer<T> comparer) {
  28. _data = new Dictionary<T, object>(comparer);
  29. }
  30. internal Set(IList<T> list) {
  31. _data = new Dictionary<T, object>(list.Count);
  32. foreach (T t in list) {
  33. _data.Add(t, null);
  34. }
  35. }
  36. public void Add(T item) {
  37. _data[item] = null;
  38. }
  39. public void Clear() {
  40. _data.Clear();
  41. }
  42. public bool Contains(T item) {
  43. return _data.ContainsKey(item);
  44. }
  45. public void CopyTo(T[] array, int arrayIndex) {
  46. _data.Keys.CopyTo(array, arrayIndex);
  47. }
  48. public int Count {
  49. get { return _data.Count; }
  50. }
  51. public bool IsReadOnly {
  52. get { return false; }
  53. }
  54. public bool Remove(T item) {
  55. return _data.Remove(item);
  56. }
  57. public IEnumerator<T> GetEnumerator() {
  58. return _data.Keys.GetEnumerator();
  59. }
  60. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
  61. return _data.Keys.GetEnumerator();
  62. }
  63. public void UnionWith(IEnumerable<T> other) {
  64. foreach (T t in other) {
  65. Add(t);
  66. }
  67. }
  68. }
  69. }