2 // System.Collections.Specialized.HybridDictionary.cs
\r
5 // Lawrence Pit (loz@cable.a2000.nl)
7 // Copyright (C) 2004 Novell (http://www.novell.com)
\r
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
\r
34 namespace System.Collections.Specialized {
\r
37 public class HybridDictionary : IDictionary, ICollection, IEnumerable {
\r
39 private const int switchAfter = 10;
\r
41 private ListDictionary list;
\r
42 private Hashtable hashtable;
\r
43 private bool caseInsensitive = false;
\r
47 public HybridDictionary() : this (0, false) { }
\r
49 public HybridDictionary (bool caseInsensitive) : this (0, caseInsensitive) { }
\r
51 public HybridDictionary (int initialSize) : this (initialSize, false) { }
\r
53 public HybridDictionary(int initialSize, bool caseInsensitive)
\r
55 this.caseInsensitive = caseInsensitive;
\r
57 if (initialSize <= switchAfter)
\r
58 if (caseInsensitive)
\r
59 list = new ListDictionary (CaseInsensitiveComparer.Default);
\r
61 list = new ListDictionary ();
\r
63 if (caseInsensitive)
\r
64 hashtable = new Hashtable (initialSize,
\r
65 CaseInsensitiveHashCodeProvider.Default,
\r
66 CaseInsensitiveComparer.Default);
\r
68 hashtable = new Hashtable (initialSize);
\r
78 return hashtable.Count;
\r
82 public bool IsFixedSize {
\r
83 get { return false; }
\r
86 public bool IsReadOnly {
\r
87 get { return false; }
\r
90 public bool IsSynchronized {
\r
91 get { return false; }
\r
94 public object this [object key] {
\r
97 throw new ArgumentNullException("key");
\r
100 return hashtable [key];
\r
104 if (list.Count >= switchAfter)
\r
107 list [key] = value;
\r
110 hashtable [key] = value;
\r
114 public ICollection Keys {
\r
118 return hashtable.Keys;
\r
122 public object SyncRoot {
\r
123 get { return this; }
\r
126 public ICollection Values {
\r
129 return list.Values;
\r
130 return hashtable.Values;
\r
137 public void Add (object key, object value)
\r
140 if (list.Count >= switchAfter)
\r
143 list.Add (key, value);
\r
146 hashtable.Add (key, value);
\r
149 public void Clear ()
\r
151 if (caseInsensitive)
\r
152 list = new ListDictionary (CaseInsensitiveComparer.Default);
\r
154 list = new ListDictionary ();
\r
158 public bool Contains (object key)
\r
164 throw new ArgumentNullException ("key");
\r
167 return list.Contains (key);
\r
168 return hashtable.Contains (key);
\r
171 public void CopyTo (Array array, int index)
\r
174 list.CopyTo (array, index);
\r
176 hashtable.CopyTo (array, index);
\r
179 public IDictionaryEnumerator GetEnumerator ()
\r
182 return list.GetEnumerator ();
\r
183 return hashtable.GetEnumerator ();
\r
186 IEnumerator IEnumerable.GetEnumerator ()
\r
188 return GetEnumerator ();
\r
191 public void Remove (object key)
\r
196 hashtable.Remove (key);
\r
199 private void Switch ()
\r
201 if (caseInsensitive)
\r
202 hashtable = new Hashtable (switchAfter + 1,
\r
203 CaseInsensitiveHashCodeProvider.Default,
\r
204 CaseInsensitiveComparer.Default);
\r
206 hashtable = new Hashtable (switchAfter + 1);
\r
207 IDictionaryEnumerator e = list.GetEnumerator ();
\r
208 while (e.MoveNext ())
\r
209 hashtable.Add (e.Key, e.Value);
\r