PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Microsoft.Scripting.Core/Compiler/Set.cs

https://bitbucket.org/stefanrusek/xronos
C# | 97 lines | 60 code | 18 blank | 19 comment | 0 complexity | 51b1eeca3fa4bf50b45748043b283fe7 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. #if CODEPLEX_40
  19. // Note: can't move to Utils because name conflicts with System.Linq.Set
  20. namespace System.Linq.Expressions {
  21. #else
  22. // Note: can't move to Utils because name conflicts with Microsoft.Linq.Set
  23. namespace Microsoft.Linq.Expressions {
  24. #endif
  25. /// <summary>
  26. /// A simple hashset, built on Dictionary{K, V}
  27. /// </summary>
  28. internal sealed class Set<T> : ICollection<T> {
  29. private readonly Dictionary<T, object> _data;
  30. internal Set() {
  31. _data = new Dictionary<T, object>();
  32. }
  33. internal Set(IEqualityComparer<T> comparer) {
  34. _data = new Dictionary<T, object>(comparer);
  35. }
  36. internal Set(IList<T> list) {
  37. _data = new Dictionary<T, object>(list.Count);
  38. foreach (T t in list) {
  39. Add(t);
  40. }
  41. }
  42. internal Set(IEnumerable<T> list) {
  43. _data = new Dictionary<T, object>();
  44. foreach (T t in list) {
  45. Add(t);
  46. }
  47. }
  48. internal Set(int capacity) {
  49. _data = new Dictionary<T, object>(capacity);
  50. }
  51. public void Add(T item) {
  52. _data[item] = null;
  53. }
  54. public void Clear() {
  55. _data.Clear();
  56. }
  57. public bool Contains(T item) {
  58. return _data.ContainsKey(item);
  59. }
  60. public void CopyTo(T[] array, int arrayIndex) {
  61. _data.Keys.CopyTo(array, arrayIndex);
  62. }
  63. public int Count {
  64. get { return _data.Count; }
  65. }
  66. public bool IsReadOnly {
  67. get { return false; }
  68. }
  69. public bool Remove(T item) {
  70. return _data.Remove(item);
  71. }
  72. public IEnumerator<T> GetEnumerator() {
  73. return _data.Keys.GetEnumerator();
  74. }
  75. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
  76. return _data.Keys.GetEnumerator();
  77. }
  78. }
  79. }