-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathSignatureComparerExtensions.cs
More file actions
103 lines (91 loc) · 3.78 KB
/
Copy pathSignatureComparerExtensions.cs
File metadata and controls
103 lines (91 loc) · 3.78 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using AsmResolver.DotNet.Signatures;
namespace WindowsRuntime.InteropGenerator;
/// <summary>
/// Extensions for <see cref="SignatureComparer"/>.
/// </summary>
internal static class SignatureComparerExtensions
{
extension(SignatureComparer comparer)
{
/// <summary>
/// Creates an <see cref="IEqualityComparer{T}"/> instance for a pair of <see cref="TypeSignature"/> values.
/// </summary>
/// <returns>The resulting <see cref="IEqualityComparer{T}"/> instance.</returns>
public IEqualityComparer<(TypeSignature, TypeSignature)> MakeValueTupleComparer()
{
return new SignatureValueTupleComparer(comparer);
}
/// <summary>
/// Creates an <see cref="IEqualityComparer{T}"/> instance for a <see cref="TypeSignature"/> value in a tuple, on the left.
/// </summary>
/// <typeparam name="TRight">The type of the right item in the tuple.</typeparam>
/// <returns>The resulting <see cref="IEqualityComparer{T}"/> instance.</returns>
public IEqualityComparer<(TypeSignature, TRight)> MakeValueTupleLeftComparer<TRight>()
{
return new SignatureValueTupleLeftComparer<TRight>(comparer);
}
}
}
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> for a pair of <see cref="TypeSignature"/> values.
/// </summary>
file sealed class SignatureValueTupleComparer : IEqualityComparer<(TypeSignature, TypeSignature)>
{
/// <summary>
/// The wrapped <see cref="SignatureComparer"/> instance used for comparison.
/// </summary>
private readonly SignatureComparer _comparer;
/// <summary>
/// Creates a new <see cref="SignatureValueTupleComparer"/> instance with the specified parameters.
/// </summary>
/// <param name="comparer">The <see cref="SignatureComparer"/> instance to wrap.</param>
public SignatureValueTupleComparer(SignatureComparer comparer)
{
_comparer = comparer;
}
/// <inheritdoc/>
public bool Equals((TypeSignature, TypeSignature) x, (TypeSignature, TypeSignature) y)
{
return _comparer.Equals(x.Item1, y.Item1) && _comparer.Equals(x.Item2, y.Item2);
}
/// <inheritdoc/>
public int GetHashCode((TypeSignature, TypeSignature) obj)
{
return HashCode.Combine(_comparer.GetHashCode(obj.Item1), _comparer.GetHashCode(obj.Item2));
}
}
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> for a <see cref="TypeSignature"/> value in a tuple, on the left.
/// </summary>
/// <typeparam name="TRight">The type of the right item in the tuple.</typeparam>
file sealed class SignatureValueTupleLeftComparer<TRight> : IEqualityComparer<(TypeSignature, TRight)>
{
/// <summary>
/// The wrapped <see cref="SignatureComparer"/> instance used for comparison.
/// </summary>
private readonly SignatureComparer _comparer;
/// <summary>
/// Creates a new <see cref="SignatureValueTupleLeftComparer{TRight}"/> instance with the specified parameters.
/// </summary>
/// <param name="comparer">The <see cref="SignatureComparer"/> instance to wrap.</param>
public SignatureValueTupleLeftComparer(SignatureComparer comparer)
{
_comparer = comparer;
}
/// <inheritdoc/>
public bool Equals((TypeSignature, TRight) x, (TypeSignature, TRight) y)
{
return _comparer.Equals(x.Item1, y.Item1) && EqualityComparer<TRight>.Default.Equals(x.Item2, y.Item2);
}
/// <inheritdoc/>
public int GetHashCode((TypeSignature, TRight) obj)
{
return HashCode.Combine(
value1: _comparer.GetHashCode(obj.Item1),
value2: obj.Item2 is null ? 0 : EqualityComparer<TRight>.Default.GetHashCode(obj.Item2));
}
}