2 // System.Collections.Specialized.HybridDictionary.cs
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Raja R Harinath <rharinath@novell.com>
8 // Copyright (C) 2004, 2005 Novell (http://www.novell.com)
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.
33 using System.Collections;
35 namespace System.Collections.Specialized {
38 public class HybridDictionary : IDictionary, ICollection, IEnumerable {
40 private const int switchAfter = 10;
42 private IDictionary inner;
44 private bool caseInsensitive;
48 public HybridDictionary() : this (0, false) { }
50 public HybridDictionary (bool caseInsensitive) : this (0, caseInsensitive) { }
52 public HybridDictionary (int initialSize) : this (initialSize, false) { }
54 public HybridDictionary (int initialSize, bool caseInsensitive)
56 this.caseInsensitive = caseInsensitive;
57 this.is_list = (initialSize <= switchAfter);
59 IComparer comparer = caseInsensitive ? CaseInsensitiveComparer.Default : null;
60 IHashCodeProvider hcp = caseInsensitive ? CaseInsensitiveHashCodeProvider.Default : null;
63 inner = new ListDictionary (comparer);
65 inner = new Hashtable (initialSize, hcp, comparer);
71 get { return inner.Count; }
74 public bool IsFixedSize {
78 public bool IsReadOnly {
82 public bool IsSynchronized {
86 public object this [object key] {
87 get { return inner [key]; }
91 if (is_list && Count > switchAfter)
96 public ICollection Keys {
97 get { return inner.Keys; }
100 public object SyncRoot {
104 public ICollection Values {
105 get { return inner.Values; }
111 public void Add (object key, object value)
113 inner.Add (key, value);
114 if (is_list && Count > switchAfter)
120 // According to MSDN, this doesn't switch a Hashtable back to a ListDictionary
124 public bool Contains (object key)
126 // if the dictionary is empty, 'Contains (null)' doesn't throw an ArgumentNullException
127 return Count != 0 && inner.Contains (key);
130 public void CopyTo (Array array, int index)
132 inner.CopyTo (array, index);
135 public IDictionaryEnumerator GetEnumerator ()
137 return inner.GetEnumerator ();
140 IEnumerator IEnumerable.GetEnumerator ()
142 return GetEnumerator ();
145 public void Remove (object key)
147 // According to MSDN, this does not switch a Hashtable back to a ListDictionary
148 // even if Count falls below switchAfter
152 private void Switch ()
154 IDictionary old = inner;
155 IComparer comparer = caseInsensitive ? CaseInsensitiveComparer.Default : null;
156 IHashCodeProvider hcp = caseInsensitive ? CaseInsensitiveHashCodeProvider.Default : null;
159 inner = new Hashtable (old, hcp, comparer);