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