| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | #nullable enable |
| | 4 | |
|
| | 5 | | namespace Ice.Internal; |
| | 6 | |
|
| | 7 | | /// <summary>Extension methods for dictionaries.</summary> |
| | 8 | | public static class DictionaryExtensions |
| | 9 | | { |
| | 10 | | /// <summary>Checks if two dictionaries are equal. The order of the elements in the dictionaries does not |
| | 11 | | /// matter.</summary> |
| | 12 | | /// <typeparam name="TKey">The type of the keys in the dictionaries.</typeparam> |
| | 13 | | /// <typeparam name="TValue">The type of the values in the dictionaries.</typeparam> |
| | 14 | | /// <param name="lhs">The first dictionary to compare.</param> |
| | 15 | | /// <param name="rhs">The second dictionary to compare.</param> |
| | 16 | | /// <param name="valueComparer">The comparer to use to compare the values in the dictionaries or null to use the |
| | 17 | | /// value's default equality comparer.</param> |
| | 18 | | /// <returns>True if the dictionaries are equal; false, otherwise.</returns> |
| | 19 | | public static bool DictionaryEqual<TKey, TValue>( |
| | 20 | | this IReadOnlyDictionary<TKey, TValue> lhs, |
| | 21 | | IReadOnlyDictionary<TKey, TValue> rhs, |
| | 22 | | IEqualityComparer<TValue>? valueComparer = null) |
| | 23 | | { |
| 1 | 24 | | if (rhs.Count != lhs.Count) |
| | 25 | | { |
| 1 | 26 | | return false; |
| | 27 | | } |
| | 28 | |
|
| 1 | 29 | | valueComparer ??= EqualityComparer<TValue>.Default; |
| | 30 | |
|
| 1 | 31 | | foreach ((TKey key, TValue value) in lhs) |
| | 32 | | { |
| 1 | 33 | | if (!rhs.TryGetValue(key, out TValue? otherValue) || !valueComparer.Equals(value, otherValue)) |
| | 34 | | { |
| 1 | 35 | | return false; |
| | 36 | | } |
| | 37 | | } |
| | 38 | |
|
| 1 | 39 | | return true; |
| 1 | 40 | | } |
| | 41 | | } |