PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 2ms 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 * ***************************************************************************/
15using System; using Microsoft;
16
17
18using System.Collections;
19using System.Collections.Generic;
20
21#if CODEPLEX_40
22// Note: can't move to Utils because name conflicts with System.Linq.Set
23namespace System.Linq.Expressions {
24#else
25// Note: can't move to Utils because name conflicts with Microsoft.Linq.Set
26namespace Microsoft.Linq.Expressions {
27#endif
28    
29    /// <summary>
30    /// A simple hashset, built on Dictionary{K, V}
31    /// </summary>
32    internal sealed class Set<T> : ICollection<T> {
33        private readonly Dictionary<T, object> _data;
34
35        internal Set() {
36            _data = new Dictionary<T, object>();
37        }
38
39        internal Set(IEqualityComparer<T> comparer) {
40            _data = new Dictionary<T, object>(comparer);
41        }
42
43        internal Set(IList<T> list) {
44            _data = new Dictionary<T, object>(list.Count);
45            foreach (T t in list) {
46                Add(t);
47            }
48        }
49
50        internal Set(IEnumerable<T> list) {
51            _data = new Dictionary<T, object>();
52            foreach (T t in list) {
53                Add(t);
54            }
55        }
56
57        internal Set(int capacity) {
58            _data = new Dictionary<T, object>(capacity);
59        }
60
61        public void Add(T item) {
62            _data[item] = null;
63        }
64
65        public void Clear() {
66            _data.Clear();
67        }
68
69        public bool Contains(T item) {
70            return _data.ContainsKey(item);
71        }
72
73        public void CopyTo(T[] array, int arrayIndex) {
74            _data.Keys.CopyTo(array, arrayIndex);
75        }
76
77        public int Count {
78            get { return _data.Count; }
79        }
80
81        public bool IsReadOnly {
82            get { return false; }
83        }
84
85        public bool Remove(T item) {
86            return _data.Remove(item);
87        }
88
89        public IEnumerator<T> GetEnumerator() {
90            return _data.Keys.GetEnumerator();
91        }
92
93        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
94            return _data.Keys.GetEnumerator();
95        }
96    }
97}