Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ComparerHelpers.cs « IntrinsicSupport « Internal « src « System.Private.CoreLib « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f88223917077b82e7feec887106575e0298e29af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//
// These helper methods are known to a NUTC intrinsic used to implement the Comparer<T> class.

// the compiler will instead replace the IL code within get_Default to call one of GetUnknownComparer, GetKnownGenericComparer,
// GetKnownNullableComparer, GetKnownEnumComparer or GetKnownObjectComparer based on what sort of
// type is being compared.

using System;
using System.Collections.Generic;
using System.Runtime;

using Internal.IntrinsicSupport;
using Internal.Runtime.Augments;

namespace Internal.IntrinsicSupport
{
    internal static class ComparerHelpers
    {
        private static bool ImplementsIComparable(RuntimeTypeHandle t)
        {
            EETypePtr objectType = t.ToEETypePtr();
            EETypePtr icomparableType = typeof(IComparable<>).TypeHandle.ToEETypePtr();
            int interfaceCount = objectType.Interfaces.Count;
            for (int i = 0; i < interfaceCount; i++)
            {
                EETypePtr interfaceType = objectType.Interfaces[i];

                if (!interfaceType.IsGeneric)
                    continue;

                if (interfaceType.GenericDefinition == icomparableType)
                {
                    var instantiation = interfaceType.Instantiation;
                    if (instantiation.Length != 1)
                        continue;

                    if (objectType.IsValueType)
                    {
                        if (instantiation[0] == objectType)
                        {
                            return true;
                        }
                    }
                    else if (RuntimeImports.AreTypesAssignable(objectType, instantiation[0]))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        private static object GetComparer(RuntimeTypeHandle t)
        {
            RuntimeTypeHandle comparerType;
            RuntimeTypeHandle openComparerType = default(RuntimeTypeHandle);
            RuntimeTypeHandle comparerTypeArgument = default(RuntimeTypeHandle);

            if (RuntimeAugments.IsNullable(t))
            {
                RuntimeTypeHandle nullableType = RuntimeAugments.GetNullableType(t);
                if (ImplementsIComparable(nullableType))
                {
                    openComparerType = typeof(NullableComparer<>).TypeHandle;
                    comparerTypeArgument = nullableType;
                }
            }

            if (openComparerType.Equals(default(RuntimeTypeHandle)))
            {
                if (ImplementsIComparable(t))
                {
                    openComparerType = typeof(GenericComparer<>).TypeHandle;
                    comparerTypeArgument = t;
                }
                else
                {
                    openComparerType = typeof(ObjectComparer<>).TypeHandle;
                    comparerTypeArgument = t;
                }
            }

            bool success = RuntimeAugments.TypeLoaderCallbacks.TryGetConstructedGenericTypeForComponents(openComparerType, new RuntimeTypeHandle[] { comparerTypeArgument }, out comparerType);
            if (!success)
            {
                Environment.FailFast("Unable to create comparer");
            }

            return RuntimeAugments.NewObject(comparerType);
        }

        internal static Comparer<T> GetUnknownComparer<T>()
        {
            return (Comparer<T>)GetComparer(typeof(T).TypeHandle);
        }

        private static Comparer<T> GetKnownGenericComparer<T>() where T : IComparable<T>
        {
            return new GenericComparer<T>();
        }

        private static Comparer<Nullable<U>> GetKnownNullableComparer<U>() where U : struct, IComparable<U>
        {
            return new NullableComparer<U>();
        }

        private static Comparer<T> GetKnownObjectComparer<T>()
        {
            return new ObjectComparer<T>();
        }

        // This routine emulates System.Collection.Comparer.Default.Compare(), which lives in the System.Collections.NonGenerics contract.
        // To avoid adding a reference to that contract just for this hack, we'll replicate the implementation here.
        internal static int CompareObjects(object x, object y)
        {
            if (x == y)
                return 0;

            if (x == null)
                return -1;

            if (y == null)
                return 1;

            {
                // System.Collection.Comparer.Default.Compare() compares strings using the CurrentCulture.
                string sx = x as string;
                string sy = y as string;
                if (sx != null && sy != null)
                    return string.Compare(sx, sy, StringComparison.CurrentCulture);
            }

            IComparable ix = x as IComparable;
            if (ix != null)
                return ix.CompareTo(y);

            IComparable iy = y as IComparable;
            if (iy != null)
                return -iy.CompareTo(x);

            throw new ArgumentException(SR.Argument_ImplementIComparable);
        }
    }
}

namespace System.Collections.Generic
{ 
    //-----------------------------------------------------------------------
    // Implementations of EqualityComparer<T> for the various possible scenarios
    // Because these are serializable, they must not be renamed
    //-----------------------------------------------------------------------
    [Serializable]
    [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
    public sealed class GenericComparer<T> : Comparer<T> where T : IComparable<T>
    {
        public sealed override int Compare(T x, T y)
        {
            if (x != null)
            {
                if (y != null)
                    return x.CompareTo(y);

                return 1;
            }

            if (y != null)
                return -1;

            return 0;
        }

        // Equals method for the comparer itself. 
        public sealed override bool Equals(object obj) => obj != null && GetType() == obj.GetType();

        public sealed override int GetHashCode() => GetType().GetHashCode();
    }

    [Serializable]
    [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
    public sealed class NullableComparer<T> : Comparer<Nullable<T>> where T : struct, IComparable<T>
    {
        public sealed override int Compare(Nullable<T> x, Nullable<T> y)
        {
            if (x.HasValue)
            {
                if (y.HasValue)
                    return x.Value.CompareTo(y.Value);

                return 1;
            }

            if (y.HasValue)
                return -1;

            return 0;
        }

        // Equals method for the comparer itself. 
        public sealed override bool Equals(object obj) => obj != null && GetType() == obj.GetType();

        public sealed override int GetHashCode() => GetType().GetHashCode();
    }

    [Serializable]
    [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
    public sealed class ObjectComparer<T> : Comparer<T>
    {
        public sealed override int Compare(T x, T y)
        {
            return ComparerHelpers.CompareObjects(x, y);
        }

        // Equals method for the comparer itself. 
        public sealed override bool Equals(object obj) => obj != null && GetType() == obj.GetType();

        public sealed override int GetHashCode() => GetType().GetHashCode();
    }
}