| 1 |
// ==++== |
| 2 |
// |
| 3 |
// Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 |
// |
| 5 |
// ==--== |
| 6 |
// <OWNER>Microsoft</OWNER> |
| 7 |
// |
| 8 |
|
| 9 |
using System; |
| 10 |
using System.Collections; |
| 11 |
using System.Collections.Generic; |
| 12 |
using System.Diagnostics.Contracts; |
| 13 |
//using System.Globalization; |
| 14 |
using System.Runtime.CompilerServices; |
| 15 |
|
| 16 |
namespace System.Collections.Generic |
| 17 |
{ |
| 18 |
[Serializable] |
| 19 |
[TypeDependencyAttribute("System.Collections.Generic.ObjectComparer`1")] |
| 20 |
public abstract class Comparer<T> : IComparer, IComparer<T> |
| 21 |
{ |
| 22 |
static volatile Comparer<T> defaultComparer; |
| 23 |
|
| 24 |
public static Comparer<T> Default { |
| 25 |
get { |
| 26 |
Contract.Ensures(Contract.Result<Comparer<T>>() != null); |
| 27 |
|
| 28 |
Comparer<T> comparer = defaultComparer; |
| 29 |
if (comparer == null) { |
| 30 |
comparer = CreateComparer(); |
| 31 |
defaultComparer = comparer; |
| 32 |
} |
| 33 |
return comparer; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public static Comparer<T> Create(Comparison<T> comparison) |
| 38 |
{ |
| 39 |
Contract.Ensures(Contract.Result<Comparer<T>>() != null); |
| 40 |
|
| 41 |
if (comparison == null) |
| 42 |
throw new ArgumentNullException("comparison"); |
| 43 |
|
| 44 |
return new ComparisonComparer<T>(comparison); |
| 45 |
} |
| 46 |
|
| 47 |
// |
| 48 |
// Note that logic in this method is replicated in vm\compile.cpp to ensure that NGen |
| 49 |
// saves the right instantiations |
| 50 |
// |
| 51 |
[System.Security.SecuritySafeCritical] // auto-generated |
| 52 |
private static Comparer<T> CreateComparer() { |
| 53 |
RuntimeType t = (RuntimeType)typeof(T); |
| 54 |
|
| 55 |
// If T implements IComparable<T> return a GenericComparer<T> |
| 56 |
#if FEATURE_LEGACYNETCF |
| 57 |
// Pre-Apollo Windows Phone call the overload that sorts the keys, not values this achieves the same result |
| 58 |
if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) { |
| 59 |
if (t.ImplementInterface(typeof(IComparable<T>))) { |
| 60 |
return (Comparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericComparer<int>), t); |
| 61 |
} |
| 62 |
} |
| 63 |
else |
| 64 |
#endif |
| 65 |
if (typeof(IComparable<T>).IsAssignableFrom(t)) { |
| 66 |
#if MONO |
| 67 |
return (Comparer<T>)RuntimeType.CreateInstanceForAnotherGenericParameter (typeof(GenericComparer<>), t); |
| 68 |
#else |
| 69 |
return (Comparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericComparer<int>), t); |
| 70 |
#endif |
| 71 |
} |
| 72 |
|
| 73 |
// If T is a Nullable<U> where U implements IComparable<U> return a NullableComparer<U> |
| 74 |
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) { |
| 75 |
RuntimeType u = (RuntimeType)t.GetGenericArguments()[0]; |
| 76 |
if (typeof(IComparable<>).MakeGenericType(u).IsAssignableFrom(u)) { |
| 77 |
#if MONO |
| 78 |
return (Comparer<T>)RuntimeType.CreateInstanceForAnotherGenericParameter (typeof(NullableComparer<>), u); |
| 79 |
#else |
| 80 |
return (Comparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableComparer<int>), u); |
| 81 |
#endif |
| 82 |
} |
| 83 |
} |
| 84 |
// Otherwise return an ObjectComparer<T> |
| 85 |
return new ObjectComparer<T>(); |
| 86 |
} |
| 87 |
|
| 88 |
public abstract int Compare(T x, T y); |
| 89 |
|
| 90 |
int IComparer.Compare(object x, object y) { |
| 91 |
if (x == null) return y == null ? 0 : -1; |
| 92 |
if (y == null) return 1; |
| 93 |
if (x is T && y is T) return Compare((T)x, (T)y); |
| 94 |
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArgumentForComparison); |
| 95 |
return 0; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
[Serializable] |
| 100 |
internal class GenericComparer<T> : Comparer<T> where T: IComparable<T> |
| 101 |
{ |
| 102 |
public override int Compare(T x, T y) { |
| 103 |
if (x != null) { |
| 104 |
if (y != null) return x.CompareTo(y); |
| 105 |
return 1; |
| 106 |
} |
| 107 |
if (y != null) return -1; |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
// Equals method for the comparer itself. |
| 112 |
public override bool Equals(Object obj){ |
| 113 |
GenericComparer<T> comparer = obj as GenericComparer<T>; |
| 114 |
return comparer != null; |
| 115 |
} |
| 116 |
|
| 117 |
public override int GetHashCode() { |
| 118 |
return this.GetType().Name.GetHashCode(); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
[Serializable] |
| 123 |
internal class NullableComparer<T> : Comparer<Nullable<T>> where T : struct, IComparable<T> |
| 124 |
{ |
| 125 |
public override int Compare(Nullable<T> x, Nullable<T> y) { |
| 126 |
if (x.HasValue) { |
| 127 |
if (y.HasValue) return x.value.CompareTo(y.value); |
| 128 |
return 1; |
| 129 |
} |
| 130 |
if (y.HasValue) return -1; |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
// Equals method for the comparer itself. |
| 135 |
public override bool Equals(Object obj){ |
| 136 |
NullableComparer<T> comparer = obj as NullableComparer<T>; |
| 137 |
return comparer != null; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public override int GetHashCode() { |
| 142 |
return this.GetType().Name.GetHashCode(); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
[Serializable] |
| 147 |
internal class ObjectComparer<T> : Comparer<T> |
| 148 |
{ |
| 149 |
public override int Compare(T x, T y) { |
| 150 |
return System.Collections.Comparer.Default.Compare(x, y); |
| 151 |
} |
| 152 |
|
| 153 |
// Equals method for the comparer itself. |
| 154 |
public override bool Equals(Object obj){ |
| 155 |
ObjectComparer<T> comparer = obj as ObjectComparer<T>; |
| 156 |
return comparer != null; |
| 157 |
} |
| 158 |
|
| 159 |
public override int GetHashCode() { |
| 160 |
return this.GetType().Name.GetHashCode(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
[Serializable] |
| 165 |
internal class ComparisonComparer<T> : Comparer<T> |
| 166 |
{ |
| 167 |
private readonly Comparison<T> _comparison; |
| 168 |
|
| 169 |
public ComparisonComparer(Comparison<T> comparison) { |
| 170 |
_comparison = comparison; |
| 171 |
} |
| 172 |
|
| 173 |
public override int Compare(T x, T y) { |
| 174 |
return _comparison(x, y); |
| 175 |
} |
| 176 |
} |
| 177 |
} |