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