-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathImmutableDictionaryEqualityComparer.cs
More file actions
45 lines (36 loc) · 1.32 KB
/
ImmutableDictionaryEqualityComparer.cs
File metadata and controls
45 lines (36 loc) · 1.32 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
using System.Collections.Immutable;
namespace JsonApiDotNetCore.SourceGenerators;
// This type was copied from Roslyn. The implementation looks odd, but is likely a performance tradeoff.
// Beware that the consuming code doesn't adhere to the typical pattern where a dictionary is built once, then queried many times.
internal sealed class ImmutableDictionaryEqualityComparer<TKey, TValue> : IEqualityComparer<ImmutableDictionary<TKey, TValue>?>
where TKey : notnull
{
public static readonly ImmutableDictionaryEqualityComparer<TKey, TValue> Instance = new();
public bool Equals(ImmutableDictionary<TKey, TValue>? x, ImmutableDictionary<TKey, TValue>? y)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (x is null || y is null)
{
return false;
}
if (!Equals(x.KeyComparer, y.KeyComparer) || !Equals(x.ValueComparer, y.ValueComparer))
{
return false;
}
foreach ((TKey key, TValue value) in x)
{
if (!y.TryGetValue(key, out TValue? other) || !x.ValueComparer.Equals(value, other))
{
return false;
}
}
return true;
}
public int GetHashCode(ImmutableDictionary<TKey, TValue>? obj)
{
return obj?.Count ?? 0;
}
}