diff --git a/ICD.Common.Utils.Tests/Extensions/EnumerableExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/EnumerableExtensionsTest.cs index 072b140..7562592 100644 --- a/ICD.Common.Utils.Tests/Extensions/EnumerableExtensionsTest.cs +++ b/ICD.Common.Utils.Tests/Extensions/EnumerableExtensionsTest.cs @@ -80,6 +80,16 @@ namespace ICD.Common.Utils.Tests.Extensions Assert.AreEqual(true, exists); } + [Test] + public void SequenceEqualTest() + { + int[] a = new int[] { 1, 2, 3, 4}; + int[] b = new int[] { 1, 4, 9, 16}; + + Assert.IsFalse(a.SequenceEqual(b, (x, y) => x == y)); + Assert.IsTrue(a.SequenceEqual(b, (x, y) => x * x == y)); + } + [Test] public void ScrambledEqualsTest() { diff --git a/ICD.Common.Utils/Extensions/DictionaryExtensions.cs b/ICD.Common.Utils/Extensions/DictionaryExtensions.cs index 622ae11..d21049e 100644 --- a/ICD.Common.Utils/Extensions/DictionaryExtensions.cs +++ b/ICD.Common.Utils/Extensions/DictionaryExtensions.cs @@ -286,6 +286,32 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); + if (valueComparer == null) + throw new ArgumentNullException("valueComparer"); + + return extends.DictionaryEqual(other, valueComparer.Equals); + } + + /// + /// Compares the keys and values of the dictionary to determine equality. + /// + /// + /// + /// + /// + /// + /// + [PublicAPI] + public static bool DictionaryEqual(this IDictionary extends, + IDictionary other, + Func valueComparer) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (valueComparer == null) + throw new ArgumentNullException("valueComparer"); + if (extends == other) return true; if (other == null) @@ -298,7 +324,7 @@ namespace ICD.Common.Utils.Extensions TValue secondValue; if (!other.TryGetValue(kvp.Key, out secondValue)) return false; - if (!valueComparer.Equals(kvp.Value, secondValue)) + if (!valueComparer(kvp.Value, secondValue)) return false; } return true; diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index 66cc2bd..37ee21d 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -120,6 +120,46 @@ namespace ICD.Common.Utils.Extensions return true; } + /// + /// Compares the two sequences for identical values. + /// + /// + /// + /// + /// + /// + public static bool SequenceEqual(this IEnumerable extends, IEnumerable other, Func comparer) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (extends == null) + throw new ArgumentNullException("other"); + + if (comparer == null) + throw new ArgumentNullException("comparer"); + + using (IEnumerator firstPos = extends.GetEnumerator()) + { + using (IEnumerator secondPos = other.GetEnumerator()) + { + bool hasFirst = firstPos.MoveNext(); + bool hasSecond = secondPos.MoveNext(); + + while (hasFirst && hasSecond) + { + if (!comparer(firstPos.Current, secondPos.Current)) + return false; + + hasFirst = firstPos.MoveNext(); + hasSecond = secondPos.MoveNext(); + } + + return !hasFirst && !hasSecond; + } + } + } + /// /// Compares two sequences for identical values, ignoring order. /// @@ -127,7 +167,8 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static bool ScrambledEquals(this IEnumerable extends, IEnumerable other) + public static + bool ScrambledEquals(this IEnumerable extends, IEnumerable other) { if (extends == null) throw new ArgumentNullException("extends");