Adding System.Data..., System.ServiceModel..., and System.Web...
[mono.git] / mcs / class / referencesource / System.Web / Util / ObjectSet.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="ObjectSet.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 /*
8  * ObjectSet class
9  * 
10  * Copyright (c) 1999 Microsoft Corporation
11  */
12
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
16
17 namespace System.Web.Util {
18
19 using System.Reflection;
20 using System.Collections;
21 using System.Collections.Generic;
22
23 #if USEGENERICSET
24 /*
25  * Holds a set of unique objects of a specific type
26  */
27 internal class ObjectSet<T> : ICollection<T>, ICollection {
28
29     protected const int StartingCapacity = 8;
30
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() { }
37     }
38
39     private static EmptyEnumerator _emptyEnumerator = new EmptyEnumerator();
40     private Dictionary<T, object> _objects;
41
42     protected virtual Dictionary<T, object> CreateDictionary() {
43         return new Dictionary<T, object>(StartingCapacity);
44     }
45
46     public void AddCollection(ICollection c) {
47         foreach (T o in c) {
48             Add(o);
49         }
50     }
51
52     public void Add(T o) {
53         if (_objects == null) {
54             _objects = CreateDictionary();
55         }
56
57         _objects[o] = null;
58     }
59
60     public bool Remove(T o) {
61         if (_objects == null)
62             return false;
63
64         return _objects.Remove(o);
65     }
66
67     public bool Contains(T o) {
68         if (_objects == null)
69             return false;
70
71         return _objects.ContainsKey(o);
72     }
73
74     bool ICollection<T>.IsReadOnly {
75         get {
76             return true;
77         }
78     }
79
80     public void Clear() {
81         if (_objects != null)
82             _objects.Clear();
83     }
84
85     IEnumerator<T> IEnumerable<T>.GetEnumerator() {
86         if (_objects == null)
87             return _emptyEnumerator;
88
89         return _objects.Keys.GetEnumerator();
90     }
91
92     IEnumerator IEnumerable.GetEnumerator() {
93         if (_objects == null)
94             return _emptyEnumerator;
95
96         return _objects.Keys.GetEnumerator();
97     }
98
99     public int Count {
100         get {
101             if (_objects == null)
102                 return 0;
103             return _objects.Keys.Count;
104         }
105     }
106
107     void ICollection<T>.CopyTo(T[] array, int index) {
108         if (_objects != null)
109             _objects.Keys.CopyTo(array, index);
110     }
111
112     bool ICollection.IsSynchronized {
113         get {
114             if (_objects == null)
115                 return true;
116             return ((ICollection)_objects.Keys).IsSynchronized;
117         }
118     }
119
120     object ICollection.SyncRoot {
121         get {
122             if (_objects == null)
123                 return this;
124             return ((ICollection)_objects.Keys).SyncRoot;
125         }
126     }
127
128     public void CopyTo(Array array, int index) {
129         if (_objects != null)
130             ((ICollection)_objects.Keys).CopyTo(array, index);
131     }
132 }
133
134 internal class StringSet : ObjectSet<String> { }
135
136 internal class CaseInsensitiveStringSet : StringSet {
137     protected override Dictionary<String, object> CreateDictionary() {
138         return new Dictionary<String, object>(StartingCapacity, StringComparer.InvariantCultureIgnoreCase);
139     }
140 }
141
142 internal class VirtualPathSet : ObjectSet<VirtualPath> { }
143
144 internal class AssemblySet : ObjectSet<Assembly> {
145     internal static AssemblySet Create(ICollection c) {
146         AssemblySet objectSet = new AssemblySet();
147         objectSet.AddCollection(c);
148         return objectSet;
149     }
150 }
151
152 internal class BuildProviderSet : ObjectSet<System.Web.Compilation.BuildProvider> { }
153
154 internal class ControlSet : ObjectSet<System.Web.UI.Control> { }
155 #else
156
157 /*
158  * Holds a set of unique objects
159  */
160 internal class ObjectSet: ICollection {
161
162     private class EmptyEnumerator: IEnumerator {
163         public object Current { get { return null; } }
164         public bool MoveNext() { return false; }
165         public void Reset() {}
166     }
167
168     private static EmptyEnumerator _emptyEnumerator = new EmptyEnumerator();
169     private IDictionary _objects;
170
171     internal ObjectSet() {}
172
173     // By default, it's case sensitive
174     protected virtual bool CaseInsensitive { get { return false; } }
175
176     public void Add(object o) {
177         if (_objects == null)
178             _objects = new System.Collections.Specialized.HybridDictionary(CaseInsensitive);
179
180         _objects[o] = null;
181     }
182
183     public void AddCollection(ICollection c) {
184         foreach (object o in c) {
185             Add(o);
186         }
187     }
188
189     public void Remove(object o) {
190         if (_objects == null)
191             return;
192
193         _objects.Remove(o);
194     }
195
196     public bool Contains(object o) {
197         if (_objects == null)
198             return false;
199
200         return _objects.Contains(o);
201     }
202
203     IEnumerator IEnumerable.GetEnumerator() {
204         if (_objects == null)
205             return _emptyEnumerator;
206
207         return _objects.Keys.GetEnumerator();
208     }
209
210     public int Count {
211         get {
212             if (_objects == null)
213                 return 0;
214             return _objects.Keys.Count;
215         }
216     }
217
218     bool ICollection.IsSynchronized {
219         get {
220             if (_objects == null)
221                 return true;
222             return _objects.Keys.IsSynchronized;
223         }
224     }
225
226     object ICollection.SyncRoot {
227         get {
228             if (_objects == null)
229                 return this;
230             return _objects.Keys.SyncRoot;
231         }
232     }
233
234     public void CopyTo(Array array, int index) {
235         if (_objects != null)
236             _objects.Keys.CopyTo(array, index);
237     }
238 }
239
240 internal class StringSet: ObjectSet {
241     internal StringSet() {}
242 }
243
244 internal class CaseInsensitiveStringSet: StringSet {
245     protected override bool CaseInsensitive { get { return true; } }
246 }
247
248 internal class VirtualPathSet : ObjectSet {
249     internal VirtualPathSet() { }
250 }
251
252 internal class AssemblySet : ObjectSet {
253     internal AssemblySet() { }
254
255     internal static AssemblySet Create(ICollection c) {
256         AssemblySet objectSet = new AssemblySet();
257         objectSet.AddCollection(c);
258         return objectSet;
259     }
260 }
261
262 internal class BuildProviderSet : ObjectSet {
263     internal BuildProviderSet() { }
264 }
265
266 internal class ControlSet : ObjectSet {
267     internal ControlSet() { }
268 }
269
270 #endif
271
272 }