mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-17 13:45:05 +00:00
Extension for determining if two sequences are equal without creating a comparer
This commit is contained in:
@@ -80,6 +80,16 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
Assert.AreEqual(true, exists);
|
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]
|
[Test]
|
||||||
public void ScrambledEqualsTest()
|
public void ScrambledEqualsTest()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -286,6 +286,32 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
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)
|
if (extends == other)
|
||||||
return true;
|
return true;
|
||||||
if (other == null)
|
if (other == null)
|
||||||
@@ -298,7 +324,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
TValue secondValue;
|
TValue secondValue;
|
||||||
if (!other.TryGetValue(kvp.Key, out secondValue))
|
if (!other.TryGetValue(kvp.Key, out secondValue))
|
||||||
return false;
|
return false;
|
||||||
if (!valueComparer.Equals(kvp.Value, secondValue))
|
if (!valueComparer(kvp.Value, secondValue))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -120,6 +120,46 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
return true;
|
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>
|
/// <summary>
|
||||||
/// Compares two sequences for identical values, ignoring order.
|
/// Compares two sequences for identical values, ignoring order.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -127,7 +167,8 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
/// <param name="other"></param>
|
/// <param name="other"></param>
|
||||||
/// <returns></returns>
|
/// <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)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|||||||
Reference in New Issue
Block a user