2 // System.Collections.Generic.SortedDictionary
5 // Raja R Harinath <rharinath@novell.com>
7 // Authors of previous (superseded) version:
8 // Kazuki Oikawa (kazuki@panicode.com)
9 // Atsushi Enomoto (atsushi@ximian.com)
13 // Copyright (C) 2007, Novell, Inc.
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.Diagnostics;
39 namespace System.Collections.Generic
42 [DebuggerDisplay ("Count={Count}")]
43 [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
44 public class SortedDictionary<TKey,TValue> : IDictionary<TKey,TValue>, ICollection<KeyValuePair<TKey,TValue>>, IEnumerable<KeyValuePair<TKey,TValue>>, IDictionary, ICollection, IEnumerable
46 class Node : RBTree.Node {
50 public Node (TKey key)
55 public Node (TKey key, TValue value)
61 public override void SwapValue (RBTree.Node other)
63 Node o = (Node) other;
64 TKey k = key; key = o.key; o.key = k;
65 TValue v = value; value = o.value; o.value = v;
68 public KeyValuePair<TKey, TValue> AsKV ()
70 return new KeyValuePair<TKey, TValue> (key, value);
73 public DictionaryEntry AsDE ()
75 return new DictionaryEntry (key, value);
79 class NodeHelper : RBTree.INodeHelper<TKey> {
80 public IComparer<TKey> cmp;
82 public int Compare (TKey key, RBTree.Node node)
84 return cmp.Compare (key, ((Node) node).key);
87 public RBTree.Node CreateNode (TKey key)
89 return new Node (key);
92 private NodeHelper (IComparer<TKey> cmp)
96 static NodeHelper Default = new NodeHelper (Comparer<TKey>.Default);
97 public static NodeHelper GetHelper (IComparer<TKey> cmp)
99 if (cmp == null || cmp == Comparer<TKey>.Default)
101 return new NodeHelper (cmp);
109 public SortedDictionary () : this ((IComparer<TKey>) null)
113 public SortedDictionary (IComparer<TKey> comparer)
115 hlp = NodeHelper.GetHelper (comparer);
116 tree = new RBTree (hlp);
119 public SortedDictionary (IDictionary<TKey,TValue> dic) : this (dic, null)
123 public SortedDictionary (IDictionary<TKey,TValue> dic, IComparer<TKey> comparer) : this (comparer)
126 throw new ArgumentNullException ();
127 foreach (KeyValuePair<TKey, TValue> entry in dic)
128 Add (entry.Key, entry.Value);
132 #region PublicProperty
134 public IComparer<TKey> Comparer {
135 get { return hlp.cmp; }
139 get { return (int) tree.Count; }
142 public TValue this [TKey key] {
144 Node n = (Node) tree.Lookup (key);
146 throw new KeyNotFoundException ();
151 throw new ArgumentNullException ("key");
152 Node n = (Node) tree.Intern (key, null);
157 public KeyCollection Keys {
158 get { return new KeyCollection (this); }
161 public ValueCollection Values {
162 get { return new ValueCollection (this); }
168 public void Add (TKey key, TValue value)
171 throw new ArgumentNullException ("key");
173 RBTree.Node n = new Node (key, value);
174 if (tree.Intern (key, n) != n)
175 throw new ArgumentException ("key already present in dictionary", "key");
183 public bool ContainsKey (TKey key)
185 return tree.Lookup (key) != null;
188 public bool ContainsValue (TValue value)
190 IEqualityComparer<TValue> vcmp = EqualityComparer<TValue>.Default;
191 foreach (Node n in tree)
192 if (vcmp.Equals (value, n.value))
197 public void CopyTo (KeyValuePair<TKey,TValue>[] array, int arrayIndex)
202 throw new ArgumentNullException ();
203 if (arrayIndex < 0 || array.Length <= arrayIndex)
204 throw new ArgumentOutOfRangeException ();
205 if (array.Length - arrayIndex < Count)
206 throw new ArgumentException ();
208 foreach (Node n in tree)
209 array [arrayIndex ++] = n.AsKV ();
212 public Enumerator GetEnumerator ()
214 return new Enumerator (this);
217 public bool Remove (TKey key)
219 return tree.Remove (key) != null;
222 public bool TryGetValue (TKey key, out TValue value)
224 Node n = (Node) tree.Lookup (key);
225 value = n == null ? default (TValue) : n.value;
231 #region PrivateMethod
232 TKey ToKey (object key)
235 throw new ArgumentNullException ("key");
237 throw new ArgumentException (String.Format ("Key \"{0}\" cannot be converted to the key type {1}.", key, typeof (TKey)));
241 TValue ToValue (object value)
243 if (!(value is TValue) && (value != null || typeof (TValue).IsValueType))
244 throw new ArgumentException (String.Format ("Value \"{0}\" cannot be converted to the value type {1}.", value, typeof (TValue)));
245 return (TValue) value;
249 #region IDictionary<TKey,TValue> Member
251 ICollection<TKey> IDictionary<TKey,TValue>.Keys {
252 get { return new KeyCollection (this); }
255 ICollection<TValue> IDictionary<TKey,TValue>.Values {
256 get { return new ValueCollection (this); }
261 #region ICollection<KeyValuePair<TKey,TValue>> Member
263 void ICollection<KeyValuePair<TKey,TValue>>.Add (KeyValuePair<TKey,TValue> item)
265 Add (item.Key, item.Value);
268 bool ICollection<KeyValuePair<TKey,TValue>>.Contains (KeyValuePair<TKey,TValue> item)
271 return TryGetValue (item.Key, out value) &&
272 EqualityComparer<TValue>.Default.Equals (item.Value, value);
275 bool ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly {
276 get { return false; }
279 bool ICollection<KeyValuePair<TKey,TValue>>.Remove (KeyValuePair<TKey,TValue> item)
282 return TryGetValue (item.Key, out value) &&
283 EqualityComparer<TValue>.Default.Equals (item.Value, value) &&
289 #region IDictionary Member
291 void IDictionary.Add (object key, object value)
293 Add (ToKey (key), ToValue (value));
296 bool IDictionary.Contains (object key)
298 return ContainsKey (ToKey (key));
301 IDictionaryEnumerator IDictionary.GetEnumerator ()
303 return new Enumerator (this);
306 bool IDictionary.IsFixedSize {
307 get { return false; }
310 bool IDictionary.IsReadOnly {
311 get { return false; }
314 ICollection IDictionary.Keys {
315 get { return new KeyCollection (this); }
318 void IDictionary.Remove (object key)
320 Remove (ToKey (key));
323 ICollection IDictionary.Values {
324 get { return new ValueCollection (this); }
327 object IDictionary.this [object key] {
328 get { return this [ToKey (key)]; }
329 set { this [ToKey (key)] = ToValue (value); }
334 #region ICollection Member
336 void ICollection.CopyTo (Array array, int index)
341 throw new ArgumentNullException ();
342 if (index < 0 || array.Length <= index)
343 throw new ArgumentOutOfRangeException ();
344 if (array.Length - index < Count)
345 throw new ArgumentException ();
347 foreach (Node n in tree)
348 array.SetValue (n.AsDE (), index++);
351 bool ICollection.IsSynchronized {
352 get { return false; }
355 // TODO:Is this correct? If this is wrong,please fix.
356 object ICollection.SyncRoot {
362 #region IEnumerable Member
364 IEnumerator IEnumerable.GetEnumerator ()
366 return new Enumerator (this);
371 #region IEnumerable<TKey> Member
373 IEnumerator<KeyValuePair<TKey,TValue>> IEnumerable<KeyValuePair<TKey,TValue>>.GetEnumerator ()
375 return new Enumerator (this);
381 [DebuggerDisplay ("Count={Count}")]
382 [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
383 public sealed class ValueCollection : ICollection<TValue>,
384 IEnumerable<TValue>, ICollection, IEnumerable
386 SortedDictionary<TKey,TValue> _dic;
388 public ValueCollection (SortedDictionary<TKey,TValue> dic)
393 void ICollection<TValue>.Add (TValue item)
395 throw new NotSupportedException ();
398 void ICollection<TValue>.Clear ()
400 throw new NotSupportedException ();
403 bool ICollection<TValue>.Contains (TValue item)
405 return _dic.ContainsValue (item);
408 public void CopyTo (TValue [] array, int arrayIndex)
413 throw new ArgumentNullException ();
414 if (arrayIndex < 0 || array.Length <= arrayIndex)
415 throw new ArgumentOutOfRangeException ();
416 if (array.Length - arrayIndex < Count)
417 throw new ArgumentException ();
418 foreach (Node n in _dic.tree)
419 array [arrayIndex++] = n.value;
423 get { return _dic.Count; }
426 bool ICollection<TValue>.IsReadOnly {
430 bool ICollection<TValue>.Remove (TValue item)
432 throw new NotSupportedException ();
435 IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator ()
437 return GetEnumerator ();
440 public Enumerator GetEnumerator ()
442 return new Enumerator (_dic);
445 void ICollection.CopyTo (Array array, int index)
450 throw new ArgumentNullException ();
451 if (index < 0 || array.Length <= index)
452 throw new ArgumentOutOfRangeException ();
453 if (array.Length - index < Count)
454 throw new ArgumentException ();
455 foreach (Node n in _dic.tree)
456 array.SetValue (n.value, index++);
459 bool ICollection.IsSynchronized {
460 get { return false; }
463 object ICollection.SyncRoot {
467 IEnumerator IEnumerable.GetEnumerator ()
469 return new Enumerator (_dic);
472 public struct Enumerator : IEnumerator<TValue>,IEnumerator, IDisposable
474 RBTree.NodeEnumerator host;
478 internal Enumerator (SortedDictionary<TKey,TValue> dic)
481 host = dic.tree.GetEnumerator ();
484 public TValue Current {
485 get { return current; }
488 public bool MoveNext ()
490 if (!host.MoveNext ())
492 current = ((Node) host.Current).value;
496 public void Dispose ()
501 object IEnumerator.Current {
503 host.check_current ();
508 void IEnumerator.Reset ()
516 [DebuggerDisplay ("Count={Count}")]
517 [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
518 public sealed class KeyCollection : ICollection<TKey>,
519 IEnumerable<TKey>, ICollection, IEnumerable
521 SortedDictionary<TKey,TValue> _dic;
523 public KeyCollection (SortedDictionary<TKey,TValue> dic)
528 void ICollection<TKey>.Add (TKey item)
530 throw new NotSupportedException ();
533 void ICollection<TKey>.Clear ()
535 throw new NotSupportedException ();
538 bool ICollection<TKey>.Contains (TKey item)
540 return _dic.ContainsKey (item);
543 IEnumerator<TKey> IEnumerable<TKey>.GetEnumerator ()
545 return GetEnumerator ();
548 public void CopyTo (TKey [] array, int arrayIndex)
553 throw new ArgumentNullException ();
554 if (arrayIndex < 0 || array.Length <= arrayIndex)
555 throw new ArgumentOutOfRangeException ();
556 if (array.Length - arrayIndex < Count)
557 throw new ArgumentException ();
558 foreach (Node n in _dic.tree)
559 array [arrayIndex++] = n.key;
563 get { return _dic.Count; }
566 bool ICollection<TKey>.IsReadOnly {
570 bool ICollection<TKey>.Remove (TKey item)
572 throw new NotSupportedException ();
575 public Enumerator GetEnumerator ()
577 return new Enumerator (_dic);
580 void ICollection.CopyTo (Array array, int index)
585 throw new ArgumentNullException ();
586 if (index < 0 || array.Length <= index)
587 throw new ArgumentOutOfRangeException ();
588 if (array.Length - index < Count)
589 throw new ArgumentException ();
590 foreach (Node n in _dic.tree)
591 array.SetValue (n.key, index++);
594 bool ICollection.IsSynchronized {
595 get { return false; }
598 object ICollection.SyncRoot {
602 IEnumerator IEnumerable.GetEnumerator ()
604 return new Enumerator (_dic);
607 public struct Enumerator : IEnumerator<TKey>, IEnumerator, IDisposable
609 RBTree.NodeEnumerator host;
613 internal Enumerator (SortedDictionary<TKey,TValue> dic)
616 host = dic.tree.GetEnumerator ();
619 public TKey Current {
620 get { return current; }
623 public bool MoveNext ()
625 if (!host.MoveNext ())
627 current = ((Node) host.Current).key;
631 public void Dispose ()
636 object IEnumerator.Current {
638 host.check_current ();
643 void IEnumerator.Reset ()
650 public struct Enumerator : IEnumerator<KeyValuePair<TKey,TValue>>, IDisposable, IDictionaryEnumerator, IEnumerator
652 RBTree.NodeEnumerator host;
654 KeyValuePair<TKey, TValue> current;
656 internal Enumerator (SortedDictionary<TKey,TValue> dic)
659 host = dic.tree.GetEnumerator ();
662 public KeyValuePair<TKey,TValue> Current {
663 get { return current; }
666 public bool MoveNext ()
668 if (!host.MoveNext ())
670 current = ((Node) host.Current).AsKV ();
674 public void Dispose ()
681 host.check_current ();
682 return (Node) host.Current;
686 DictionaryEntry IDictionaryEnumerator.Entry {
687 get { return CurrentNode.AsDE (); }
690 object IDictionaryEnumerator.Key {
691 get { return CurrentNode.key; }
694 object IDictionaryEnumerator.Value {
695 get { return CurrentNode.value; }
698 object IEnumerator.Current {
699 get { return CurrentNode.AsDE (); }
702 void IEnumerator.Reset ()