2 // System.Collections.Specialized.HybridDictionary.cs
5 // Lawrence Pit (loz@cable.a2000.nl)
7 // Copyright (C) 2004 Novell (http://www.novell.com)
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
;
34 namespace System
.Collections
.Specialized
{
37 public class HybridDictionary
: IDictionary
, ICollection
, IEnumerable
{
39 private const int switchAfter
= 10;
41 private ListDictionary list
;
42 private Hashtable hashtable
;
43 private bool caseInsensitive
= false;
47 public HybridDictionary() : this (0, false) { }
49 public HybridDictionary (bool caseInsensitive
) : this (0, caseInsensitive
) { }
51 public HybridDictionary (int initialSize
) : this (initialSize
, false) { }
53 public HybridDictionary(int initialSize
, bool caseInsensitive
)
55 this.caseInsensitive
= caseInsensitive
;
57 if (initialSize
<= switchAfter
)
59 list
= new ListDictionary (CaseInsensitiveComparer
.Default
);
61 list
= new ListDictionary ();
64 hashtable
= new Hashtable (initialSize
,
65 CaseInsensitiveHashCodeProvider
.Default
,
66 CaseInsensitiveComparer
.Default
);
68 hashtable
= new Hashtable (initialSize
);
78 return hashtable
.Count
;
82 public bool IsFixedSize
{
86 public bool IsReadOnly
{
90 public bool IsSynchronized
{
94 public object this [object key
] {
97 throw new ArgumentNullException("key");
100 return hashtable
[key
];
104 if (list
.Count
>= switchAfter
)
110 hashtable
[key
] = value;
114 public ICollection Keys
{
118 return hashtable
.Keys
;
122 public object SyncRoot
{
126 public ICollection Values
{
130 return hashtable
.Values
;
137 public void Add (object key
, object value)
140 if (list
.Count
>= switchAfter
)
143 list
.Add (key
, value);
146 hashtable
.Add (key
, value);
152 list
= new ListDictionary (CaseInsensitiveComparer
.Default
);
154 list
= new ListDictionary ();
158 public bool Contains (object key
)
164 throw new ArgumentNullException ("key");
167 return list
.Contains (key
);
168 return hashtable
.Contains (key
);
171 public void CopyTo (Array array
, int index
)
174 list
.CopyTo (array
, index
);
176 hashtable
.CopyTo (array
, index
);
179 public IDictionaryEnumerator
GetEnumerator ()
182 return list
.GetEnumerator ();
183 return hashtable
.GetEnumerator ();
186 IEnumerator IEnumerable
.GetEnumerator ()
188 return GetEnumerator ();
191 public void Remove (object key
)
196 hashtable
.Remove (key
);
199 private void Switch ()
202 hashtable
= new Hashtable (switchAfter
+ 1,
203 CaseInsensitiveHashCodeProvider
.Default
,
204 CaseInsensitiveComparer
.Default
);
206 hashtable
= new Hashtable (switchAfter
+ 1);
207 IDictionaryEnumerator e
= list
.GetEnumerator ();
208 while (e
.MoveNext ())
209 hashtable
.Add (e
.Key
, e
.Value
);