1
|
/*
|
2
|
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
|
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
of this software and associated documentation files (the "Software"), to deal
|
5
|
in the Software without restriction, including without limitation the rights
|
6
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
copies of the Software, and to permit persons to whom the Software is
|
8
|
furnished to do so, subject to the following conditions:
|
9
|
|
10
|
The above copyright notice and this permission notice shall be included in
|
11
|
all copies or substantial portions of the Software.
|
12
|
|
13
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
SOFTWARE.
|
20
|
*/
|
21
|
|
22
|
using C5;
|
23
|
using System;
|
24
|
using System.Reflection;
|
25
|
using System.Reflection.Emit;
|
26
|
using System.Diagnostics;
|
27
|
using SCG = System.Collections.Generic;
|
28
|
|
29
|
namespace C5
|
30
|
{
|
31
|
/// <summary>
|
32
|
/// A default item comparer for an item type that is either generic (IComparable<T>)
|
33
|
/// or ordinarily (System.IComparable) comparable.
|
34
|
/// </summary>
|
35
|
public static class Comparer<T>
|
36
|
{
|
37
|
readonly static Type naturalComparerO = typeof(NaturalComparerO<>);
|
38
|
|
39
|
readonly static Type naturalComparer = typeof(NaturalComparer<>);
|
40
|
|
41
|
static SCG.IComparer<T> cachedComparer = null;
|
42
|
|
43
|
/// <summary>
|
44
|
/// Create a default comparer.
|
45
|
/// <para>The IComparer[T] object is constructed when this class is initialised, i.e.
|
46
|
/// its static constructors called. Thus, the property will be the same object
|
47
|
/// for the duration of an invocation of the runtime, but a value serialized in
|
48
|
/// another invocation and deserialized here will not be the same object.</para>
|
49
|
/// </summary>
|
50
|
/// <exception cref="NotComparableException">If T is not comparable</exception>
|
51
|
/// <value>The comparer</value>
|
52
|
[Tested]
|
53
|
public static SCG.IComparer<T> Default
|
54
|
{
|
55
|
get
|
56
|
{
|
57
|
if (cachedComparer != null)
|
58
|
return cachedComparer;
|
59
|
|
60
|
Type t = typeof(T);
|
61
|
|
62
|
if (t.IsValueType)
|
63
|
{
|
64
|
if (t.Equals(typeof(char)))
|
65
|
return cachedComparer = (SCG.IComparer<T>)(new CharComparer());
|
66
|
|
67
|
if (t.Equals(typeof(sbyte)))
|
68
|
return cachedComparer = (SCG.IComparer<T>)(new SByteComparer());
|
69
|
|
70
|
if (t.Equals(typeof(byte)))
|
71
|
return cachedComparer = (SCG.IComparer<T>)(new ByteComparer());
|
72
|
|
73
|
if (t.Equals(typeof(short)))
|
74
|
return cachedComparer = (SCG.IComparer<T>)(new ShortComparer());
|
75
|
|
76
|
if (t.Equals(typeof(ushort)))
|
77
|
return cachedComparer = (SCG.IComparer<T>)(new UShortComparer());
|
78
|
|
79
|
if (t.Equals(typeof(int)))
|
80
|
return cachedComparer = (SCG.IComparer<T>)(new IntComparer());
|
81
|
|
82
|
if (t.Equals(typeof(uint)))
|
83
|
return cachedComparer = (SCG.IComparer<T>)(new UIntComparer());
|
84
|
|
85
|
if (t.Equals(typeof(long)))
|
86
|
return cachedComparer = (SCG.IComparer<T>)(new LongComparer());
|
87
|
|
88
|
if (t.Equals(typeof(ulong)))
|
89
|
return cachedComparer = (SCG.IComparer<T>)(new ULongComparer());
|
90
|
|
91
|
if (t.Equals(typeof(float)))
|
92
|
return cachedComparer = (SCG.IComparer<T>)(new FloatComparer());
|
93
|
|
94
|
if (t.Equals(typeof(double)))
|
95
|
return cachedComparer = (SCG.IComparer<T>)(new DoubleComparer());
|
96
|
|
97
|
if (t.Equals(typeof(decimal)))
|
98
|
return cachedComparer = (SCG.IComparer<T>)(new DecimalComparer());
|
99
|
}
|
100
|
|
101
|
if (typeof(IComparable<T>).IsAssignableFrom(t))
|
102
|
{
|
103
|
Type c = naturalComparer.MakeGenericType(new Type[] { t });
|
104
|
|
105
|
return cachedComparer = (SCG.IComparer<T>)(c.GetConstructor(System.Type.EmptyTypes).Invoke(null));
|
106
|
}
|
107
|
|
108
|
if (t.GetInterface("System.IComparable") != null)
|
109
|
{
|
110
|
Type c = naturalComparerO.MakeGenericType(new Type[] { t });
|
111
|
|
112
|
return cachedComparer = (SCG.IComparer<T>)(c.GetConstructor(System.Type.EmptyTypes).Invoke(null));
|
113
|
}
|
114
|
|
115
|
throw new NotComparableException(String.Format("Cannot make comparer for type {0}", t));
|
116
|
}
|
117
|
}
|
118
|
}
|
119
|
|
120
|
/// <summary>
|
121
|
/// A natural generic IComparer for an IComparable<T> item type
|
122
|
/// </summary>
|
123
|
/// <typeparam name="T"></typeparam>
|
124
|
[Serializable]
|
125
|
public class NaturalComparer<T> : SCG.IComparer<T>
|
126
|
where T : IComparable<T>
|
127
|
{
|
128
|
/// <summary>
|
129
|
/// Compare two items
|
130
|
/// </summary>
|
131
|
/// <param name="item1">First item</param>
|
132
|
/// <param name="item2">Second item</param>
|
133
|
/// <returns>item1 <=> item2</returns>
|
134
|
[Tested]
|
135
|
public int Compare(T item1, T item2) { return item1 != null ? item1.CompareTo(item2) : item2 != null ? -1 : 0; }
|
136
|
}
|
137
|
|
138
|
/// <summary>
|
139
|
/// A natural generic IComparer for a System.IComparable item type
|
140
|
/// </summary>
|
141
|
/// <typeparam name="T"></typeparam>
|
142
|
[Serializable]
|
143
|
public class NaturalComparerO<T> : SCG.IComparer<T> where T : System.IComparable
|
144
|
{
|
145
|
/// <summary>
|
146
|
/// Compare two items
|
147
|
/// </summary>
|
148
|
/// <param name="item1">First item</param>
|
149
|
/// <param name="item2">Second item</param>
|
150
|
/// <returns>item1 <=> item2</returns>
|
151
|
[Tested]
|
152
|
public int Compare(T item1, T item2) { return item1 != null ? item1.CompareTo(item2) : item2 != null ? -1 : 0; }
|
153
|
}
|
154
|
|
155
|
/// <summary>
|
156
|
/// A generic comparer for type T based on a Comparison[T] delegate
|
157
|
/// </summary>
|
158
|
/// <typeparam name="T"></typeparam>
|
159
|
[Serializable]
|
160
|
public class DelegateComparer<T> : SCG.IComparer<T>
|
161
|
{
|
162
|
readonly Comparison<T> cmp;
|
163
|
/// <summary>
|
164
|
///
|
165
|
/// </summary>
|
166
|
/// <param name="comparison"></param>
|
167
|
public DelegateComparer(Comparison<T> comparison)
|
168
|
{
|
169
|
if (comparison == null)
|
170
|
throw new NullReferenceException("Comparison cannot be null");
|
171
|
this.cmp = comparison;
|
172
|
}
|
173
|
/// <summary>
|
174
|
///
|
175
|
/// </summary>
|
176
|
/// <param name="item1">First item</param>
|
177
|
/// <param name="item2">Second item</param>
|
178
|
/// <returns>item1 <=> item2</returns>
|
179
|
public int Compare(T item1, T item2) { return cmp(item1, item2); }
|
180
|
}
|
181
|
}
|