mscorlib(4.0.0.0) API with additions
HybridDictionary.cs
2 {
6  {
7  private const int CutoverPoint = 9;
8 
9  private const int InitialHashtableSize = 13;
10 
11  private const int FixedSizeCutoverPoint = 6;
12 
13  private ListDictionary list;
14 
15  private Hashtable hashtable;
16 
17  private bool caseInsensitive;
18 
24  public object this[object key]
25  {
26  get
27  {
28  ListDictionary listDictionary = list;
29  if (hashtable != null)
30  {
31  return hashtable[key];
32  }
33  if (listDictionary != null)
34  {
35  return listDictionary[key];
36  }
37  if (key == null)
38  {
39  throw new ArgumentNullException("key", SR.GetString("ArgumentNull_Key"));
40  }
41  return null;
42  }
43  set
44  {
45  if (hashtable != null)
46  {
47  hashtable[key] = value;
48  }
49  else if (list != null)
50  {
51  if (list.Count >= 8)
52  {
53  ChangeOver();
54  hashtable[key] = value;
55  }
56  else
57  {
58  list[key] = value;
59  }
60  }
61  else
62  {
63  list = new ListDictionary(caseInsensitive ? StringComparer.OrdinalIgnoreCase : null);
64  list[key] = value;
65  }
66  }
67  }
68 
69  private ListDictionary List
70  {
71  get
72  {
73  if (list == null)
74  {
75  list = new ListDictionary(caseInsensitive ? StringComparer.OrdinalIgnoreCase : null);
76  }
77  return list;
78  }
79  }
80 
83  public int Count
84  {
85  get
86  {
87  ListDictionary listDictionary = list;
88  if (hashtable != null)
89  {
90  return hashtable.Count;
91  }
92  return listDictionary?.Count ?? 0;
93  }
94  }
95 
98  public ICollection Keys
99  {
100  get
101  {
102  if (hashtable != null)
103  {
104  return hashtable.Keys;
105  }
106  return List.Keys;
107  }
108  }
109 
112  public bool IsReadOnly => false;
113 
116  public bool IsFixedSize => false;
117 
120  public bool IsSynchronized => false;
121 
124  public object SyncRoot => this;
125 
128  public ICollection Values
129  {
130  get
131  {
132  if (hashtable != null)
133  {
134  return hashtable.Values;
135  }
136  return List.Values;
137  }
138  }
139 
142  {
143  }
144 
147  public HybridDictionary(int initialSize)
148  : this(initialSize, caseInsensitive: false)
149  {
150  }
151 
154  public HybridDictionary(bool caseInsensitive)
155  {
156  this.caseInsensitive = caseInsensitive;
157  }
158 
162  public HybridDictionary(int initialSize, bool caseInsensitive)
163  {
164  this.caseInsensitive = caseInsensitive;
165  if (initialSize >= 6)
166  {
167  if (caseInsensitive)
168  {
169  hashtable = new Hashtable(initialSize, StringComparer.OrdinalIgnoreCase);
170  }
171  else
172  {
173  hashtable = new Hashtable(initialSize);
174  }
175  }
176  }
177 
178  private void ChangeOver()
179  {
180  IDictionaryEnumerator enumerator = list.GetEnumerator();
181  Hashtable hashtable = (!caseInsensitive) ? new Hashtable(13) : new Hashtable(13, StringComparer.OrdinalIgnoreCase);
182  while (enumerator.MoveNext())
183  {
184  hashtable.Add(enumerator.Key, enumerator.Value);
185  }
186  this.hashtable = hashtable;
187  list = null;
188  }
189 
196  public void Add(object key, object value)
197  {
198  if (hashtable != null)
199  {
200  hashtable.Add(key, value);
201  }
202  else if (list == null)
203  {
204  list = new ListDictionary(caseInsensitive ? StringComparer.OrdinalIgnoreCase : null);
205  list.Add(key, value);
206  }
207  else if (list.Count + 1 >= 9)
208  {
209  ChangeOver();
210  hashtable.Add(key, value);
211  }
212  else
213  {
214  list.Add(key, value);
215  }
216  }
217 
219  public void Clear()
220  {
221  if (this.hashtable != null)
222  {
223  Hashtable hashtable = this.hashtable;
224  this.hashtable = null;
225  hashtable.Clear();
226  }
227  if (list != null)
228  {
229  ListDictionary listDictionary = list;
230  list = null;
231  listDictionary.Clear();
232  }
233  }
234 
241  public bool Contains(object key)
242  {
243  ListDictionary listDictionary = list;
244  if (hashtable != null)
245  {
246  return hashtable.Contains(key);
247  }
248  if (listDictionary != null)
249  {
250  return listDictionary.Contains(key);
251  }
252  if (key == null)
253  {
254  throw new ArgumentNullException("key", SR.GetString("ArgumentNull_Key"));
255  }
256  return false;
257  }
258 
269  public void CopyTo(Array array, int index)
270  {
271  if (hashtable != null)
272  {
273  hashtable.CopyTo(array, index);
274  }
275  else
276  {
277  List.CopyTo(array, index);
278  }
279  }
280 
284  {
285  if (hashtable != null)
286  {
287  return hashtable.GetEnumerator();
288  }
289  if (list == null)
290  {
291  list = new ListDictionary(caseInsensitive ? StringComparer.OrdinalIgnoreCase : null);
292  }
293  return list.GetEnumerator();
294  }
295 
299  {
300  if (hashtable != null)
301  {
302  return hashtable.GetEnumerator();
303  }
304  if (list == null)
305  {
306  list = new ListDictionary(caseInsensitive ? StringComparer.OrdinalIgnoreCase : null);
307  }
308  return list.GetEnumerator();
309  }
310 
315  public void Remove(object key)
316  {
317  if (hashtable != null)
318  {
319  hashtable.Remove(key);
320  }
321  else if (list != null)
322  {
323  list.Remove(key);
324  }
325  else if (key == null)
326  {
327  throw new ArgumentNullException("key", SR.GetString("ArgumentNull_Key"));
328  }
329  }
330  }
331 }
ICollection Values
Gets an T:System.Collections.ICollection containing the values in the T:System.Collections....
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
bool IsSynchronized
Gets a value indicating whether the T:System.Collections.Specialized.HybridDictionary is synchronized...
void CopyTo(Array array, int index)
Copies the T:System.Collections.Specialized.ListDictionary entries to a one-dimensional T:System....
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
bool MoveNext()
Advances the enumerator to the next element of the collection.
void CopyTo(Array array, int index)
Copies the T:System.Collections.Specialized.HybridDictionary entries to a one-dimensional T:System....
HybridDictionary()
Creates an empty case-sensitive T:System.Collections.Specialized.HybridDictionary.
object Key
Gets the key of the current dictionary entry.
int Count
Gets the number of key/value pairs contained in the T:System.Collections.Specialized....
void Remove(object key)
Removes the entry with the specified key from the T:System.Collections.Specialized....
virtual ICollection Values
Gets an T:System.Collections.ICollection containing the values in the T:System.Collections....
Definition: Hashtable.cs:631
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
IDictionaryEnumerator GetEnumerator()
Returns an T:System.Collections.IDictionaryEnumerator that iterates through the T:System....
ICollection Keys
Gets an T:System.Collections.ICollection containing the keys in the T:System.Collections....
virtual void CopyTo(Array array, int arrayIndex)
Copies the T:System.Collections.Hashtable elements to a one-dimensional T:System.Array instance at th...
Definition: Hashtable.cs:1084
object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
bool Contains(object key)
Determines whether the T:System.Collections.Specialized.ListDictionary contains a specific key.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
virtual ICollection Keys
Gets an T:System.Collections.ICollection containing the keys in the T:System.Collections....
Definition: Hashtable.cs:617
HybridDictionary(int initialSize, bool caseInsensitive)
Creates a T:System.Collections.Specialized.HybridDictionary with the specified initial size and case ...
bool Contains(object key)
Determines whether the T:System.Collections.Specialized.HybridDictionary contains a specific key.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
bool IsFixedSize
Gets a value indicating whether the T:System.Collections.Specialized.HybridDictionary has a fixed siz...
int??? Count
Gets the number of key/value pairs contained in the T:System.Collections.Specialized....
HybridDictionary(int initialSize)
Creates a case-sensitive T:System.Collections.Specialized.HybridDictionary with the specified initial...
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
void Clear()
Removes all entries from the T:System.Collections.Specialized.HybridDictionary.
bool IsReadOnly
Gets a value indicating whether the T:System.Collections.Specialized.HybridDictionary is read-only.
ICollection Keys
Gets an T:System.Collections.ICollection containing the keys in the T:System.Collections....
void Add(object key, object value)
Adds an entry with the specified key and value into the T:System.Collections.Specialized....
virtual bool Contains(object key)
Determines whether the T:System.Collections.Hashtable contains a specific key.
Definition: Hashtable.cs:972
Implements IDictionary by using a T:System.Collections.Specialized.ListDictionary while the collectio...
void Clear()
Removes all entries from the T:System.Collections.Specialized.ListDictionary.
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:1349
object Value
Gets the value of the current dictionary entry.
Specifies that the class can be serialized.
Enumerates the elements of a nongeneric dictionary.
static StringComparer OrdinalIgnoreCase
Gets a T:System.StringComparer object that performs a case-insensitive ordinal string comparison.
HybridDictionary(bool caseInsensitive)
Creates an empty T:System.Collections.Specialized.HybridDictionary with the specified case sensitivit...
virtual void Clear()
Removes all elements from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:924
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
Represents a nongeneric collection of key/value pairs.
Definition: IDictionary.cs:8
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
ICollection Values
Gets an T:System.Collections.ICollection containing the values in the T:System.Collections....
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Hashtable.
Definition: Hashtable.cs:658
Implements IDictionary using a singly linked list. Recommended for collections that typically include...
Represents a string comparison operation that uses specific case and culture-based or ordinal compari...