Protect everything in #if NET_2_0.
[mono.git] / mcs / class / Mono.C5 / trees / TreeDictionary.cs
1 #if NET_2_0\r
2 /*\r
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
10  \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
13  \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
20  SOFTWARE.\r
21 */\r
22 \r
23 using System;\r
24 using MSG = System.Collections.Generic;\r
25 \r
26 namespace C5\r
27 {\r
28         /// <summary>\r
29         /// A sorted generic dictionary based on a red-black tree set.\r
30         /// </summary>\r
31         public class TreeDictionary<K,V>: DictionaryBase<K,V>, IDictionary<K,V>, ISortedDictionary<K,V>\r
32         {\r
33                 #region Fields\r
34 \r
35                 private TreeSet<KeyValuePair<K,V>> sortedpairs;\r
36 \r
37                 #endregion\r
38 \r
39                 #region Constructors\r
40 \r
41                 /// <summary>\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
44                 /// </summary>\r
45                 public TreeDictionary() : this(ComparerBuilder.FromComparable<K>.Examine()) { }\r
46 \r
47                 /// <summary>\r
48                 /// Create a red-black tree dictionary using an external comparer for keys.\r
49                 /// </summary>\r
50                 /// <param name="c">The external comparer</param>\r
51                 public TreeDictionary(IComparer<K> c)\r
52                 {\r
53                         pairs = sortedpairs = new TreeSet<KeyValuePair<K,V>>(new KeyValuePairComparer<K,V>(c));\r
54                 }\r
55 \r
56                 #endregion\r
57 \r
58                 #region ISortedDictionary<K,V> Members\r
59 \r
60                 /// <summary>\r
61                 /// Get the entry in the dictionary whose key is the\r
62                 /// predecessor of a specified key.\r
63                 /// </summary>\r
64                 /// <param name="key">The key</param>\r
65                 /// <returns>The entry</returns>\r
66                 [Tested]\r
67                 public KeyValuePair<K,V> Predecessor(K key)\r
68                 {\r
69                         KeyValuePair<K,V> p = new KeyValuePair<K,V>(key);\r
70 \r
71                         return sortedpairs.Predecessor(p);\r
72                 }\r
73 \r
74 \r
75                 /// <summary>\r
76                 /// Get the entry in the dictionary whose key is the\r
77                 /// weak predecessor of a specified key.\r
78                 /// </summary>\r
79                 /// <param name="key">The key</param>\r
80                 /// <returns>The entry</returns>\r
81                 [Tested]\r
82                 public KeyValuePair<K,V> WeakPredecessor(K key)\r
83                 {\r
84                         KeyValuePair<K,V> p = new KeyValuePair<K,V>(key);\r
85 \r
86                         return sortedpairs.WeakPredecessor(p);\r
87                 }\r
88 \r
89 \r
90                 /// <summary>\r
91                 /// Get the entry in the dictionary whose key is the\r
92                 /// successor of a specified key.\r
93                 /// </summary>\r
94                 /// <param name="key">The key</param>\r
95                 /// <returns>The entry</returns>\r
96                 [Tested]\r
97                 public KeyValuePair<K,V> Successor(K key)\r
98                 {\r
99                         KeyValuePair<K,V> p = new KeyValuePair<K,V>(key);\r
100 \r
101                         return sortedpairs.Successor(p);\r
102                 }\r
103 \r
104 \r
105                 /// <summary>\r
106                 /// Get the entry in the dictionary whose key is the\r
107                 /// weak successor of a specified key.\r
108                 /// </summary>\r
109                 /// <param name="key">The key</param>\r
110                 /// <returns>The entry</returns>\r
111                 [Tested]\r
112                 public KeyValuePair<K,V> WeakSuccessor(K key)\r
113                 {\r
114                         KeyValuePair<K,V> p = new KeyValuePair<K,V>(key);\r
115 \r
116                         return sortedpairs.WeakSuccessor(p);\r
117                 }\r
118 \r
119                 #endregion\r
120 \r
121                 //TODO: put in interface\r
122                 /// <summary>\r
123                 /// Make a snapshot of the current state of this dictionary\r
124                 /// </summary>\r
125                 /// <returns>The snapshot</returns>\r
126                 [Tested]\r
127                 public MSG.IEnumerable<KeyValuePair<K,V>> Snapshot()\r
128                 {\r
129                         TreeDictionary<K,V> res = (TreeDictionary<K,V>)MemberwiseClone();\r
130 \r
131                         res.pairs = (TreeSet<KeyValuePair<K,V>>)sortedpairs.Snapshot();\r
132                         return res;\r
133                 }\r
134         }\r
135 }\r
136 #endif\r