PageRenderTime 62ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 15ms app.codeStats 1ms

/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 * ***************************************************************************/
15using System; using Microsoft;
16
17
18using System.Collections;
19using System.Collections.Generic;
20
21namespace Microsoft.Scripting.Utils {
22
23    /// <summary>
24    /// A simple hashset, built on Dictionary{K, V}
25    /// </summary>
26    internal sealed class Set<T> : ICollection<T> {
27        private readonly Dictionary<T, object> _data;
28
29        internal Set() {
30            _data = new Dictionary<T, object>();
31        }
32
33        internal Set(IEqualityComparer<T> comparer) {
34            _data = new Dictionary<T, object>(comparer);
35        }
36
37        internal Set(IList<T> list) {
38            _data = new Dictionary<T, object>(list.Count);
39            foreach (T t in list) {
40                _data.Add(t, null);
41            }
42        }
43
44        public void Add(T item) {
45            _data[item] = null;
46        }
47
48        public void Clear() {
49            _data.Clear();
50        }
51
52        public bool Contains(T item) {
53            return _data.ContainsKey(item);
54        }
55
56        public void CopyTo(T[] array, int arrayIndex) {
57            _data.Keys.CopyTo(array, arrayIndex);
58        }
59
60        public int Count {
61            get { return _data.Count; }
62        }
63
64        public bool IsReadOnly {
65            get { return false; }
66        }
67
68        public bool Remove(T item) {
69            return _data.Remove(item);
70        }
71
72        public IEnumerator<T> GetEnumerator() {
73            return _data.Keys.GetEnumerator();
74        }
75
76        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
77            return _data.Keys.GetEnumerator();
78        }
79
80        public void UnionWith(IEnumerable<T> other) {
81            foreach (T t in other) {
82                Add(t);
83            }
84        }
85    }
86}