add ISafeSerializationData
[mcs.git] / class / System / System.Collections.Specialized / HybridDictionary.cs
blob58fc1ca236f5813307b94e2ecab962b0e8b9135e
1 //
2 // System.Collections.Specialized.HybridDictionary.cs
3 //
4 // Author:
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Raja R Harinath <rharinath@novell.com>
7 //
8 // Copyright (C) 2004, 2005 Novell (http://www.novell.com)
9 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 namespace System.Collections.Specialized {
34 [Serializable]
35 public class HybridDictionary : IDictionary, ICollection, IEnumerable {
37 private const int switchAfter = 10;
39 private IDictionary inner {
40 get { return list == null ? (IDictionary) hashtable : (IDictionary) list; }
43 private bool caseInsensitive;
44 private Hashtable hashtable;
45 private ListDictionary list;
47 // Constructors
49 public HybridDictionary() : this (0, false) { }
51 public HybridDictionary (bool caseInsensitive) : this (0, caseInsensitive) { }
53 public HybridDictionary (int initialSize) : this (initialSize, false) { }
55 public HybridDictionary (int initialSize, bool caseInsensitive)
57 this.caseInsensitive = caseInsensitive;
59 IComparer comparer = caseInsensitive ? CaseInsensitiveComparer.DefaultInvariant : null;
60 IHashCodeProvider hcp = caseInsensitive ? CaseInsensitiveHashCodeProvider.DefaultInvariant : null;
62 if (initialSize <= switchAfter)
63 list = new ListDictionary (comparer);
64 else
65 hashtable = new Hashtable (initialSize, hcp, comparer);
68 // Properties
70 public int Count {
71 get { return inner.Count; }
74 public bool IsFixedSize {
75 get { return false; }
78 public bool IsReadOnly {
79 get { return false; }
82 public bool IsSynchronized {
83 get { return false; }
86 public object this [object key] {
87 get { return inner [key]; }
89 set {
90 inner [key] = value;
91 if (list != null && Count > switchAfter)
92 Switch ();
96 public ICollection Keys {
97 get { return inner.Keys; }
100 public object SyncRoot {
101 get { return this; }
104 public ICollection Values {
105 get { return inner.Values; }
109 // Methods
111 public void Add (object key, object value)
113 inner.Add (key, value);
114 if (list != null && Count > switchAfter)
115 Switch ();
118 public void Clear ()
120 // According to MSDN, this doesn't switch a Hashtable back to a ListDictionary
121 inner.Clear ();
124 public bool Contains (object key)
126 #if NET_2_0
127 return inner.Contains (key);
128 #else
129 // if the dictionary is empty, 'Contains (null)' doesn't throw an ArgumentNullException
130 return Count != 0 && inner.Contains (key);
131 #endif
134 public void CopyTo (Array array, int index)
136 inner.CopyTo (array, index);
139 public IDictionaryEnumerator GetEnumerator ()
141 return inner.GetEnumerator ();
144 IEnumerator IEnumerable.GetEnumerator ()
146 return GetEnumerator ();
149 public void Remove (object key)
151 // According to MSDN, this does not switch a Hashtable back to a ListDictionary
152 // even if Count falls below switchAfter
153 inner.Remove (key);
156 private void Switch ()
158 IComparer comparer = caseInsensitive ? CaseInsensitiveComparer.DefaultInvariant : null;
159 IHashCodeProvider hcp = caseInsensitive ? CaseInsensitiveHashCodeProvider.DefaultInvariant : null;
161 hashtable = new Hashtable (list, hcp, comparer);
162 list.Clear ();
163 list = null;