/
StructuralComparisons.cs
76 lines (61 loc) · 2.07 KB
/
StructuralComparisons.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
namespace System.Collections
{
public static class StructuralComparisons
{
private static volatile IComparer? s_StructuralComparer;
private static volatile IEqualityComparer? s_StructuralEqualityComparer;
public static IComparer StructuralComparer => s_StructuralComparer ??= new StructuralComparer();
public static IEqualityComparer StructuralEqualityComparer => s_StructuralEqualityComparer ??= new StructuralEqualityComparer();
}
internal sealed class StructuralEqualityComparer : IEqualityComparer
{
public new bool Equals(object? x, object? y)
{
if (x != null)
{
IStructuralEquatable? seObj = x as IStructuralEquatable;
if (seObj != null)
{
return seObj.Equals(y, this);
}
if (y != null)
{
return x.Equals(y);
}
else
{
return false;
}
}
if (y != null) return false;
return true;
}
public int GetHashCode(object obj)
{
if (obj == null) return 0;
IStructuralEquatable? seObj = obj as IStructuralEquatable;
if (seObj != null)
{
return seObj.GetHashCode(this);
}
return obj.GetHashCode();
}
}
internal sealed class StructuralComparer : IComparer
{
public int Compare(object? x, object? y)
{
if (x == null) return y == null ? 0 : -1;
if (y == null) return 1;
IStructuralComparable? scX = x as IStructuralComparable;
if (scX != null)
{
return scX.CompareTo(y, this);
}
return Comparer<object>.Default.Compare(x, y);
}
}
}