fix default case-insensitive comparer for 2.0 profile
[mono.git] / mcs / class / System / System.Collections.Specialized / HybridDictionary.cs
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 //
10
11 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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.
30 //
31
32 namespace System.Collections.Specialized {
33
34         [Serializable]
35         public class HybridDictionary : IDictionary, ICollection, IEnumerable {
36
37                 private const int switchAfter = 10;
38
39                 private IDictionary inner {
40                         get { return list == null ? (IDictionary) hashtable : (IDictionary) list; }
41                 }
42
43                 private bool caseInsensitive;
44                 private Hashtable hashtable;
45                 private ListDictionary list;
46
47                 // Constructors
48
49                 public HybridDictionary() : this (0, false) { }
50
51                 public HybridDictionary (bool caseInsensitive) : this (0, caseInsensitive) { }
52
53                 public HybridDictionary (int initialSize) : this (initialSize, false) { }
54
55                 public HybridDictionary (int initialSize, bool caseInsensitive)
56                 {
57                         this.caseInsensitive = caseInsensitive;
58 #if NET_2_0
59                         StringComparer comparer = caseInsensitive ? StringComparer.OrdinalIgnoreCase : null;
60 #else
61                         IComparer comparer = caseInsensitive ? CaseInsensitiveComparer.DefaultInvariant : null;
62                         IHashCodeProvider hcp = caseInsensitive ? CaseInsensitiveHashCodeProvider.DefaultInvariant : null;
63 #endif
64
65                         if (initialSize <= switchAfter)
66                                 list = new ListDictionary (comparer);
67                         else
68 #if NET_2_0
69                                 hashtable = new Hashtable (initialSize, comparer);
70 #else
71                                 hashtable = new Hashtable (initialSize, hcp, comparer);
72 #endif
73                 }
74
75                 // Properties
76
77                 public int Count {
78                         get { return inner.Count; }
79                 }
80
81                 public bool IsFixedSize {
82                         get { return false; }
83                 }
84
85                 public bool IsReadOnly {
86                         get { return false; }
87                 }
88
89                 public bool IsSynchronized {
90                         get { return false; }
91                 }
92
93                 public object this [object key] {
94                         get { return inner [key]; }
95
96                         set {
97                                 inner [key] = value;
98                                 if (list != null && Count > switchAfter)
99                                         Switch ();
100                         }
101                 }
102
103                 public ICollection Keys {
104                         get { return inner.Keys; }
105                 }
106
107                 public object SyncRoot {
108                         get { return this; }
109                 }
110
111                 public ICollection Values {
112                         get { return inner.Values; }
113                 }
114
115
116                 // Methods
117
118                 public void Add (object key, object value)
119                 {
120                         inner.Add (key, value);
121                         if (list != null && Count > switchAfter)
122                                 Switch ();
123                 }
124
125                 public void Clear ()
126                 {
127                         // According to MSDN, this doesn't switch a Hashtable back to a ListDictionary
128                         inner.Clear ();
129                 }
130
131                 public bool Contains (object key)
132                 {
133 #if NET_2_0
134                         return inner.Contains (key);
135 #else
136                         // if the dictionary is empty, 'Contains (null)' doesn't throw an ArgumentNullException
137                         return Count != 0 && inner.Contains (key);
138 #endif
139                 }
140
141                 public void CopyTo (Array array, int index)
142                 {
143                         inner.CopyTo (array, index);
144                 }
145
146                 public IDictionaryEnumerator GetEnumerator ()
147                 {
148                         return inner.GetEnumerator ();
149                 }
150
151                 IEnumerator IEnumerable.GetEnumerator ()
152                 {
153                         return GetEnumerator ();
154                 }
155
156                 public void Remove (object key)
157                 {
158                         // According to MSDN, this does not switch a Hashtable back to a ListDictionary
159                         // even if Count falls below switchAfter
160                         inner.Remove (key);
161                 }
162
163                 private void Switch ()
164                 {
165 #if NET_2_0
166                         StringComparer comparer = caseInsensitive ? StringComparer.OrdinalIgnoreCase : null;
167                         hashtable = new Hashtable (list, comparer);
168 #else
169                         IComparer comparer = caseInsensitive ? CaseInsensitiveComparer.DefaultInvariant : null;
170                         IHashCodeProvider hcp = caseInsensitive ? CaseInsensitiveHashCodeProvider.DefaultInvariant : null;
171
172                         hashtable = new Hashtable (list, hcp, comparer);
173 #endif
174                         list.Clear ();
175                         list = null;
176                 }
177         }
178 }