3 Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>
\r
4 Permission is hereby granted, free of charge, to any person obtaining a copy
\r
5 of this software and associated documentation files (the "Software"), to deal
\r
6 in the Software without restriction, including without limitation the rights
\r
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
8 copies of the Software, and to permit persons to whom the Software is
\r
9 furnished to do so, subject to the following conditions:
\r
11 The above copyright notice and this permission notice shall be included in
\r
12 all copies or substantial portions of the Software.
\r
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
24 using MSG = System.Collections.Generic;
\r
29 /// A sorted generic dictionary based on a red-black tree set.
\r
31 public class TreeDictionary<K,V>: DictionaryBase<K,V>, IDictionary<K,V>, ISortedDictionary<K,V>
\r
35 private TreeSet<KeyValuePair<K,V>> sortedpairs;
\r
39 #region Constructors
\r
42 /// Create a red-black tree dictionary using the natural comparer for keys.
\r
43 /// <exception cref="ArgumentException"/> if the key type K is not comparable.
\r
45 public TreeDictionary() : this(ComparerBuilder.FromComparable<K>.Examine()) { }
\r
48 /// Create a red-black tree dictionary using an external comparer for keys.
\r
50 /// <param name="c">The external comparer</param>
\r
51 public TreeDictionary(IComparer<K> c)
\r
53 pairs = sortedpairs = new TreeSet<KeyValuePair<K,V>>(new KeyValuePairComparer<K,V>(c));
\r
58 #region ISortedDictionary<K,V> Members
\r
61 /// Get the entry in the dictionary whose key is the
\r
62 /// predecessor of a specified key.
\r
64 /// <param name="key">The key</param>
\r
65 /// <returns>The entry</returns>
\r
67 public KeyValuePair<K,V> Predecessor(K key)
\r
69 KeyValuePair<K,V> p = new KeyValuePair<K,V>(key);
\r
71 return sortedpairs.Predecessor(p);
\r
76 /// Get the entry in the dictionary whose key is the
\r
77 /// weak predecessor of a specified key.
\r
79 /// <param name="key">The key</param>
\r
80 /// <returns>The entry</returns>
\r
82 public KeyValuePair<K,V> WeakPredecessor(K key)
\r
84 KeyValuePair<K,V> p = new KeyValuePair<K,V>(key);
\r
86 return sortedpairs.WeakPredecessor(p);
\r
91 /// Get the entry in the dictionary whose key is the
\r
92 /// successor of a specified key.
\r
94 /// <param name="key">The key</param>
\r
95 /// <returns>The entry</returns>
\r
97 public KeyValuePair<K,V> Successor(K key)
\r
99 KeyValuePair<K,V> p = new KeyValuePair<K,V>(key);
\r
101 return sortedpairs.Successor(p);
\r
106 /// Get the entry in the dictionary whose key is the
\r
107 /// weak successor of a specified key.
\r
109 /// <param name="key">The key</param>
\r
110 /// <returns>The entry</returns>
\r
112 public KeyValuePair<K,V> WeakSuccessor(K key)
\r
114 KeyValuePair<K,V> p = new KeyValuePair<K,V>(key);
\r
116 return sortedpairs.WeakSuccessor(p);
\r
121 //TODO: put in interface
\r
123 /// Make a snapshot of the current state of this dictionary
\r
125 /// <returns>The snapshot</returns>
\r
127 public MSG.IEnumerable<KeyValuePair<K,V>> Snapshot()
\r
129 TreeDictionary<K,V> res = (TreeDictionary<K,V>)MemberwiseClone();
\r
131 res.pairs = (TreeSet<KeyValuePair<K,V>>)sortedpairs.Snapshot();
\r