forked from pulumi/pulumi-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryContentsComparer.cs
More file actions
58 lines (52 loc) · 1.79 KB
/
Copy pathDictionaryContentsComparer.cs
File metadata and controls
58 lines (52 loc) · 1.79 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
// Copyright 2016-2019, Pulumi Corporation
using System.Collections.Generic;
namespace Pulumi.Automation.Collections
{
/// Compares two dictionaries for equality by content, as F# maps would.
internal sealed class DictionaryContentsComparer<TKey, TValue> : IEqualityComparer<IDictionary<TKey, TValue>> where TKey : notnull
{
private readonly IEqualityComparer<TKey> _keyComparer;
private readonly IEqualityComparer<TValue> _valueComparer;
public DictionaryContentsComparer(IEqualityComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer)
{
this._keyComparer = keyComparer;
this._valueComparer = valueComparer;
}
bool IEqualityComparer<IDictionary<TKey, TValue>>.Equals(IDictionary<TKey, TValue>? x, IDictionary<TKey, TValue>? y)
{
if (x == null)
{
return y == null;
}
if (y == null)
{
return false;
}
if (ReferenceEquals(x, y))
{
return true;
}
if (x.Count != y.Count)
{
return false;
}
var y2 = new Dictionary<TKey, TValue>(y, this._keyComparer);
foreach (var pair in x)
{
if (!y2.TryGetValue(pair.Key, out TValue? value))
{
return false;
}
if (!this._valueComparer.Equals(pair.Value, value))
{
return false;
}
}
return true;
}
int IEqualityComparer<IDictionary<TKey, TValue>>.GetHashCode(IDictionary<TKey, TValue> obj)
{
return 0; // inefficient but correct
}
}
}