* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Mono.C5 / C5 / Comparer.cs
1 #if NET_2_0
2 /*\r
3  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\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 C5;\r
24 using System;\r
25 using System.Reflection;\r
26 using System.Reflection.Emit;\r
27 using System.Diagnostics;\r
28 using SCG = System.Collections.Generic;\r
29 \r
30 namespace C5\r
31\r
32 /// <summary>\r
33 /// A default item comparer for an item type that is either generic (IComparable&lt;T&gt;)\r
34 /// or ordinarily (System.IComparable) comparable.\r
35 /// </summary>\r
36   public static class Comparer<T>\r
37   {\r
38     readonly static Type naturalComparerO = typeof(NaturalComparerO<>);\r
39 \r
40     readonly static Type naturalComparer = typeof(NaturalComparer<>);\r
41 \r
42     static SCG.IComparer<T> cachedComparer = null;\r
43 \r
44     /// <summary>\r
45     /// Create a default comparer. \r
46     /// <para>The IComparer[T] object is constructed when this class is initialised, i.e. \r
47     /// its static constructors called. Thus, the property will be the same object \r
48     /// for the duration of an invocation of the runtime, but a value serialized in \r
49     /// another invocation and deserialized here will not be the same object.</para>\r
50     /// </summary>\r
51     /// <exception cref="NotComparableException">If T is not comparable</exception>\r
52     /// <value>The comparer</value>\r
53     [Tested]\r
54     public static SCG.IComparer<T> Default\r
55     {\r
56       get\r
57       {\r
58         if (cachedComparer != null)\r
59           return cachedComparer;\r
60 \r
61         Type t = typeof(T);\r
62 \r
63         if (t.IsValueType)\r
64         {\r
65           if (t.Equals(typeof(int)))\r
66             return cachedComparer = (SCG.IComparer<T>)(new IntComparer());\r
67 \r
68           if (t.Equals(typeof(double)))\r
69             return cachedComparer = (SCG.IComparer<T>)(new DoubleComparer());\r
70 \r
71           if (t.Equals(typeof(byte)))\r
72             return cachedComparer = (SCG.IComparer<T>)(new ByteComparer());\r
73         }\r
74 \r
75         if (typeof(IComparable<T>).IsAssignableFrom(t))\r
76         {\r
77           Type c = naturalComparer.MakeGenericType(new Type[] { t });\r
78 \r
79           return cachedComparer = (SCG.IComparer<T>)(c.GetConstructor(System.Type.EmptyTypes).Invoke(null));\r
80         }\r
81 \r
82         if (t.GetInterface("System.IComparable") != null)\r
83         {\r
84           Type c = naturalComparerO.MakeGenericType(new Type[] { t });\r
85 \r
86           return cachedComparer = (SCG.IComparer<T>)(c.GetConstructor(System.Type.EmptyTypes).Invoke(null));\r
87         }\r
88 \r
89         throw new NotComparableException(String.Format("Cannot make comparer for type {0}", t));\r
90       }\r
91     }\r
92   }\r
93 \r
94   /// <summary>\r
95   /// A natural generic IComparer for an IComparable&lt;T&gt; item type\r
96   /// </summary>\r
97   /// <typeparam name="T"></typeparam>\r
98   public class NaturalComparer<T> : SCG.IComparer<T>\r
99       where T : IComparable<T>\r
100   {\r
101     /// <summary>\r
102     /// Compare two items\r
103     /// </summary>\r
104     /// <param name="item1">First item</param>\r
105     /// <param name="item2">Second item</param>\r
106     /// <returns>item1 &lt;=&gt; item2</returns>\r
107     [Tested]\r
108     public int Compare(T item1, T item2) { return item1 != null ? item1.CompareTo(item2) : item2 != null ? -1 : 0; }\r
109   }\r
110 \r
111   /// <summary>\r
112   /// A natural generic IComparer for a System.IComparable item type\r
113   /// </summary>\r
114   /// <typeparam name="T"></typeparam>\r
115   public class NaturalComparerO<T> : SCG.IComparer<T>\r
116       where T : System.IComparable\r
117   {\r
118     /// <summary>\r
119     /// Compare two items\r
120     /// </summary>\r
121     /// <param name="item1">First item</param>\r
122     /// <param name="item2">Second item</param>\r
123     /// <returns>item1 &lt;=&gt; item2</returns>\r
124     [Tested]\r
125     public int Compare(T item1, T item2) { return item1 != null ? item1.CompareTo(item2) : item2 != null ? -1 : 0; }\r
126   }\r
127 \r
128   /// <summary>\r
129   /// A generic comparer for type T based on a Comparison[T] delegate\r
130   /// </summary>\r
131   /// <typeparam name="T"></typeparam>\r
132   public class DelegateComparer<T> : SCG.IComparer<T>\r
133   {\r
134     readonly Comparison<T> cmp;\r
135     /// <summary>\r
136     /// \r
137     /// </summary>\r
138     /// <param name="comparison"></param>\r
139     public DelegateComparer(Comparison<T> comparison)\r
140     {\r
141       if (comparison == null)\r
142         throw new NullReferenceException("Comparison cannot be null");\r
143       this.cmp = comparison;\r
144     }\r
145     /// <summary>\r
146     /// \r
147     /// </summary>\r
148     /// <param name="item1">First item</param>\r
149     /// <param name="item2">Second item</param>\r
150     /// <returns>item1 &lt;=&gt; item2</returns>\r
151     public int Compare(T item1, T item2) { return cmp(item1, item2); }\r
152   }\r
153 }
154 #endif