mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-10 18:24:53 +00:00
Extension methods for finding distinct items based on a predicate
This commit is contained in:
@@ -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>
|
||||
|
||||
41
ICD.Common.Utils/Extensions/PropertyEqualityComparer.cs
Normal file
41
ICD.Common.Utils/Extensions/PropertyEqualityComparer.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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" />
|
||||
|
||||
Reference in New Issue
Block a user