1 //------------------------------------------------------------------------------
2 // <copyright file="ObjectSet.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
10 * Copyright (c) 1999 Microsoft Corporation
13 // Generics are causing perf regressions, so don't use them for now until we can figure
14 // it out (VSWhidbey 463572)
15 //#define USEGENERICSET
17 namespace System.Web.Util {
19 using System.Reflection;
20 using System.Collections;
21 using System.Collections.Generic;
25 * Holds a set of unique objects of a specific type
27 internal class ObjectSet<T> : ICollection<T>, ICollection {
29 protected const int StartingCapacity = 8;
31 private class EmptyEnumerator : IEnumerator<T> {
32 object IEnumerator.Current { get { return null; } }
33 T IEnumerator<T>.Current { get { return default(T); } }
34 bool IEnumerator.MoveNext() { return false; }
35 void IEnumerator.Reset() { }
36 void IDisposable.Dispose() { }
39 private static EmptyEnumerator _emptyEnumerator = new EmptyEnumerator();
40 private Dictionary<T, object> _objects;
42 protected virtual Dictionary<T, object> CreateDictionary() {
43 return new Dictionary<T, object>(StartingCapacity);
46 public void AddCollection(ICollection c) {
52 public void Add(T o) {
53 if (_objects == null) {
54 _objects = CreateDictionary();
60 public bool Remove(T o) {
64 return _objects.Remove(o);
67 public bool Contains(T o) {
71 return _objects.ContainsKey(o);
74 bool ICollection<T>.IsReadOnly {
85 IEnumerator<T> IEnumerable<T>.GetEnumerator() {
87 return _emptyEnumerator;
89 return _objects.Keys.GetEnumerator();
92 IEnumerator IEnumerable.GetEnumerator() {
94 return _emptyEnumerator;
96 return _objects.Keys.GetEnumerator();
101 if (_objects == null)
103 return _objects.Keys.Count;
107 void ICollection<T>.CopyTo(T[] array, int index) {
108 if (_objects != null)
109 _objects.Keys.CopyTo(array, index);
112 bool ICollection.IsSynchronized {
114 if (_objects == null)
116 return ((ICollection)_objects.Keys).IsSynchronized;
120 object ICollection.SyncRoot {
122 if (_objects == null)
124 return ((ICollection)_objects.Keys).SyncRoot;
128 public void CopyTo(Array array, int index) {
129 if (_objects != null)
130 ((ICollection)_objects.Keys).CopyTo(array, index);
134 internal class StringSet : ObjectSet<String> { }
136 internal class CaseInsensitiveStringSet : StringSet {
137 protected override Dictionary<String, object> CreateDictionary() {
138 return new Dictionary<String, object>(StartingCapacity, StringComparer.InvariantCultureIgnoreCase);
142 internal class VirtualPathSet : ObjectSet<VirtualPath> { }
144 internal class AssemblySet : ObjectSet<Assembly> {
145 internal static AssemblySet Create(ICollection c) {
146 AssemblySet objectSet = new AssemblySet();
147 objectSet.AddCollection(c);
152 internal class BuildProviderSet : ObjectSet<System.Web.Compilation.BuildProvider> { }
154 internal class ControlSet : ObjectSet<System.Web.UI.Control> { }
158 * Holds a set of unique objects
160 internal class ObjectSet: ICollection {
162 private class EmptyEnumerator: IEnumerator {
163 public object Current { get { return null; } }
164 public bool MoveNext() { return false; }
165 public void Reset() {}
168 private static EmptyEnumerator _emptyEnumerator = new EmptyEnumerator();
169 private IDictionary _objects;
171 internal ObjectSet() {}
173 // By default, it's case sensitive
174 protected virtual bool CaseInsensitive { get { return false; } }
176 public void Add(object o) {
177 if (_objects == null)
178 _objects = new System.Collections.Specialized.HybridDictionary(CaseInsensitive);
183 public void AddCollection(ICollection c) {
184 foreach (object o in c) {
189 public void Remove(object o) {
190 if (_objects == null)
196 public bool Contains(object o) {
197 if (_objects == null)
200 return _objects.Contains(o);
203 IEnumerator IEnumerable.GetEnumerator() {
204 if (_objects == null)
205 return _emptyEnumerator;
207 return _objects.Keys.GetEnumerator();
212 if (_objects == null)
214 return _objects.Keys.Count;
218 bool ICollection.IsSynchronized {
220 if (_objects == null)
222 return _objects.Keys.IsSynchronized;
226 object ICollection.SyncRoot {
228 if (_objects == null)
230 return _objects.Keys.SyncRoot;
234 public void CopyTo(Array array, int index) {
235 if (_objects != null)
236 _objects.Keys.CopyTo(array, index);
240 internal class StringSet: ObjectSet {
241 internal StringSet() {}
244 internal class CaseInsensitiveStringSet: StringSet {
245 protected override bool CaseInsensitive { get { return true; } }
248 internal class VirtualPathSet : ObjectSet {
249 internal VirtualPathSet() { }
252 internal class AssemblySet : ObjectSet {
253 internal AssemblySet() { }
255 internal static AssemblySet Create(ICollection c) {
256 AssemblySet objectSet = new AssemblySet();
257 objectSet.AddCollection(c);
262 internal class BuildProviderSet : ObjectSet {
263 internal BuildProviderSet() { }
266 internal class ControlSet : ObjectSet {
267 internal ControlSet() { }