Extension for determining if two sequences are equal without creating a comparer

This commit is contained in:
Chris Cameron
2017-08-23 12:15:48 -04:00
parent 45623b3f99
commit cd9121592a
3 changed files with 79 additions and 2 deletions

View File

@@ -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()
{

View File

@@ -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);
}
/// <summary>
/// Compares the keys and values of the dictionary to determine equality.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="extends"></param>
/// <param name="other"></param>
/// <param name="valueComparer"></param>
/// <returns></returns>
[PublicAPI]
public static bool DictionaryEqual<TKey, TValue>(this IDictionary<TKey, TValue> extends,
IDictionary<TKey, TValue> other,
Func<TValue, TValue, bool> 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;

View File

@@ -120,6 +120,46 @@ namespace ICD.Common.Utils.Extensions
return true;
}
/// <summary>
/// Compares the two sequences for identical values.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="extends"></param>
/// <param name="other"></param>
/// <param name="comparer"></param>
/// <returns></returns>
public static bool SequenceEqual<T>(this IEnumerable<T> extends, IEnumerable<T> other, Func<T, T, bool> comparer)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (extends == null)
throw new ArgumentNullException("other");
if (comparer == null)
throw new ArgumentNullException("comparer");
using (IEnumerator<T> firstPos = extends.GetEnumerator())
{
using (IEnumerator<T> 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;
}
}
}
/// <summary>
/// Compares two sequences for identical values, ignoring order.
/// </summary>
@@ -127,7 +167,8 @@ namespace ICD.Common.Utils.Extensions
/// <param name="extends"></param>
/// <param name="other"></param>
/// <returns></returns>
public static bool ScrambledEquals<T>(this IEnumerable<T> extends, IEnumerable<T> other)
public static
bool ScrambledEquals<T>(this IEnumerable<T> extends, IEnumerable<T> other)
{
if (extends == null)
throw new ArgumentNullException("extends");