**** Merged from MCS ****
[mono-project.git] / mcs / class / System / System.Collections.Specialized / HybridDictionary.cs
blob3d2c7b8658cd335571415787f3a85ccf7e9c7baf
1 //
2 // System.Collections.Specialized.HybridDictionary.cs
3 //
4 // Author:
5 // Lawrence Pit (loz@cable.a2000.nl)
6 //
7 // Copyright (C) 2004 Novell (http://www.novell.com)
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
31 using System;
32 using System.Collections;
34 namespace System.Collections.Specialized {
36 [Serializable]
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;
45 // Constructors
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)
58 if (caseInsensitive)
59 list = new ListDictionary (CaseInsensitiveComparer.Default);
60 else
61 list = new ListDictionary ();
62 else
63 if (caseInsensitive)
64 hashtable = new Hashtable (initialSize,
65 CaseInsensitiveHashCodeProvider.Default,
66 CaseInsensitiveComparer.Default);
67 else
68 hashtable = new Hashtable (initialSize);
72 // Properties
74 public int Count {
75 get {
76 if (list != null)
77 return list.Count;
78 return hashtable.Count;
82 public bool IsFixedSize {
83 get { return false; }
86 public bool IsReadOnly {
87 get { return false; }
90 public bool IsSynchronized {
91 get { return false; }
94 public object this [object key] {
95 get {
96 if (key == null)
97 throw new ArgumentNullException("key");
98 if (list != null)
99 return list [key];
100 return hashtable [key];
102 set {
103 if (list != null)
104 if (list.Count >= switchAfter)
105 Switch ();
106 else {
107 list [key] = value;
108 return;
110 hashtable [key] = value;
114 public ICollection Keys {
115 get {
116 if (list != null)
117 return list.Keys;
118 return hashtable.Keys;
122 public object SyncRoot {
123 get { return this; }
126 public ICollection Values {
127 get {
128 if (list != null)
129 return list.Values;
130 return hashtable.Values;
135 // Methods
137 public void Add (object key, object value)
139 if (list != null)
140 if (list.Count >= switchAfter)
141 Switch ();
142 else {
143 list.Add (key, value);
144 return;
146 hashtable.Add (key, value);
149 public void Clear ()
151 if (caseInsensitive)
152 list = new ListDictionary (CaseInsensitiveComparer.Default);
153 else
154 list = new ListDictionary ();
155 hashtable = null;
158 public bool Contains (object key)
160 if (key == null) {
161 if (this.Count == 0)
162 return false;
163 else
164 throw new ArgumentNullException ("key");
166 if (list != null)
167 return list.Contains (key);
168 return hashtable.Contains (key);
171 public void CopyTo (Array array, int index)
173 if (list != null)
174 list.CopyTo (array, index);
175 else
176 hashtable.CopyTo (array, index);
179 public IDictionaryEnumerator GetEnumerator ()
181 if (list != null)
182 return list.GetEnumerator ();
183 return hashtable.GetEnumerator ();
186 IEnumerator IEnumerable.GetEnumerator ()
188 return GetEnumerator ();
191 public void Remove (object key)
193 if (list != null)
194 list.Remove (key);
195 else
196 hashtable.Remove (key);
199 private void Switch ()
201 if (caseInsensitive)
202 hashtable = new Hashtable (switchAfter + 1,
203 CaseInsensitiveHashCodeProvider.Default,
204 CaseInsensitiveComparer.Default);
205 else
206 hashtable = new Hashtable (switchAfter + 1);
207 IDictionaryEnumerator e = list.GetEnumerator ();
208 while (e.MoveNext ())
209 hashtable.Add (e.Key, e.Value);
210 list = null;