Extension methods for finding distinct items based on a predicate

This commit is contained in:
Chris Cameron
2018-02-21 12:17:57 -05:00
parent cd4791538a
commit 24d674337b
4 changed files with 78 additions and 45 deletions

View File

@@ -653,23 +653,51 @@ namespace ICD.Common.Utils.Extensions
}
/// <summary>
/// Gets distinct elements from the sequence based on given callbacks.
/// Gets distinct elements from the sequence based on given property.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TItem"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="extends"></param>
/// <param name="comparer"></param>
/// <param name="getHashCode"></param>
/// <param name="getProperty"></param>
/// <returns></returns>
[PublicAPI]
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> extends, Func<T, T, bool> comparer, Func<T, int> getHashCode)
public static IEnumerable<TItem> Distinct<TItem, TProperty>(this IEnumerable<TItem> extends,
Func<TItem, TProperty> getProperty)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (comparer == null)
throw new ArgumentNullException("comparer");
if (getProperty == null)
throw new ArgumentNullException("getProperty");
return extends.Distinct(new FuncComparer<T>(comparer, getHashCode));
IEqualityComparer<TProperty> comparer = EqualityComparer<TProperty>.Default;
return extends.Distinct(getProperty, comparer);
}
/// <summary>
/// Gets distinct elements from the sequence based on given property.
/// </summary>
/// <typeparam name="TItem"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="extends"></param>
/// <param name="getProperty"></param>
/// <param name="propertyComparer"></param>
/// <returns></returns>
[PublicAPI]
public static IEnumerable<TItem> Distinct<TItem, TProperty>(this IEnumerable<TItem> extends,
Func<TItem, TProperty> getProperty,
IEqualityComparer<TProperty> propertyComparer)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (getProperty == null)
throw new ArgumentNullException("getProperty");
if (propertyComparer == null)
throw new ArgumentNullException("propertyComparer");
return extends.Distinct(new PropertyEqualityComparer<TItem, TProperty>(propertyComparer, getProperty));
}
/// <summary>

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
namespace ICD.Common.Utils.Extensions
{
/// <summary>
/// Allows for comparing items based on some property.
/// </summary>
/// <typeparam name="TParent"></typeparam>
/// <typeparam name="TProperty"></typeparam>
public sealed class PropertyEqualityComparer<TParent, TProperty> : IEqualityComparer<TParent>
{
private readonly IEqualityComparer<TProperty> m_Comparer;
private readonly Func<TParent, TProperty> m_GetProperty;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="comparer"></param>
/// <param name="getProperty"></param>
public PropertyEqualityComparer(IEqualityComparer<TProperty> comparer, Func<TParent, TProperty> getProperty)
{
m_Comparer = comparer;
m_GetProperty = getProperty;
}
public bool Equals(TParent x, TParent y)
{
TProperty a = m_GetProperty(x);
TProperty b = m_GetProperty(y);
return m_Comparer.Equals(a, b);
}
public int GetHashCode(TParent parent)
{
TProperty property = m_GetProperty(parent);
return m_Comparer.GetHashCode(property);
}
}
}

View File

@@ -1,36 +0,0 @@
using System;
using System.Collections.Generic;
namespace ICD.Common.Utils
{
/// <summary>
/// Simple comparer for comparing items using a callback.
/// </summary>
/// <typeparam name="T"></typeparam>
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>
/// <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)
{
return m_Comparer(x, y);
}
public int GetHashCode(T obj)
{
return m_GetHashCode(obj);
}
}
}

View File

@@ -87,7 +87,7 @@
<None Include="ObfuscationSettings.cs" />
<Compile Include="Extensions\ByteExtensions.cs" />
<Compile Include="Extensions\ListExtensions.cs" />
<Compile Include="FuncComparer.cs" />
<Compile Include="Extensions\PropertyEqualityComparer.cs" />
<Compile Include="ProcessorUtils.SimplSharp.cs" />
<Compile Include="ProcessorUtils.Standard.cs" />
<Compile Include="ProgramUtils.SimplSharp.cs" />