mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-15 04:35:00 +00:00
Making FuncComparer actually work
This commit is contained in:
@@ -653,14 +653,15 @@ namespace ICD.Common.Utils.Extensions
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shim to
|
||||
/// Gets distinct elements from the sequence based on given callbacks.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <param name="getHashCode"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> extends, Func<T, T, bool> comparer)
|
||||
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> extends, Func<T, T, bool> comparer, Func<T, int> getHashCode)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -668,7 +669,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
|
||||
return extends.Distinct(new FuncComparer<T>(comparer));
|
||||
return extends.Distinct(new FuncComparer<T>(comparer, getHashCode));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -10,14 +10,17 @@ namespace ICD.Common.Utils
|
||||
public sealed class FuncComparer<T> : IEqualityComparer<T>
|
||||
{
|
||||
private readonly Func<T, T, bool> m_Comparer;
|
||||
private readonly Func<T, int> m_GetHashCode;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
public FuncComparer(Func<T, T, bool> comparer)
|
||||
/// <param name="getHashCode"></param>
|
||||
public FuncComparer(Func<T, T, bool> comparer, Func<T, int> getHashCode)
|
||||
{
|
||||
m_Comparer = comparer;
|
||||
m_GetHashCode = getHashCode;
|
||||
}
|
||||
|
||||
public bool Equals(T x, T y)
|
||||
@@ -27,7 +30,7 @@ namespace ICD.Common.Utils
|
||||
|
||||
public int GetHashCode(T obj)
|
||||
{
|
||||
return obj.GetHashCode();
|
||||
return m_GetHashCode(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user