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