From 24d674337ba9b3e142bb2444c309a8fddd428cba Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Wed, 21 Feb 2018 12:17:57 -0500 Subject: [PATCH] Extension methods for finding distinct items based on a predicate --- .../Extensions/EnumerableExtensions.cs | 44 +++++++++++++++---- .../Extensions/PropertyEqualityComparer.cs | 41 +++++++++++++++++ ICD.Common.Utils/FuncComparer.cs | 36 --------------- .../ICD.Common.Utils_SimplSharp.csproj | 2 +- 4 files changed, 78 insertions(+), 45 deletions(-) create mode 100644 ICD.Common.Utils/Extensions/PropertyEqualityComparer.cs delete mode 100644 ICD.Common.Utils/FuncComparer.cs diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index f40fa3f..719d53c 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -653,23 +653,51 @@ namespace ICD.Common.Utils.Extensions } /// - /// Gets distinct elements from the sequence based on given callbacks. + /// Gets distinct elements from the sequence based on given property. /// - /// + /// + /// /// - /// - /// + /// /// [PublicAPI] - public static IEnumerable Distinct(this IEnumerable extends, Func comparer, Func getHashCode) + public static IEnumerable Distinct(this IEnumerable extends, + Func 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(comparer, getHashCode)); + IEqualityComparer comparer = EqualityComparer.Default; + return extends.Distinct(getProperty, comparer); + } + + /// + /// Gets distinct elements from the sequence based on given property. + /// + /// + /// + /// + /// + /// + /// + [PublicAPI] + public static IEnumerable Distinct(this IEnumerable extends, + Func getProperty, + IEqualityComparer 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(propertyComparer, getProperty)); } /// diff --git a/ICD.Common.Utils/Extensions/PropertyEqualityComparer.cs b/ICD.Common.Utils/Extensions/PropertyEqualityComparer.cs new file mode 100644 index 0000000..57ef41a --- /dev/null +++ b/ICD.Common.Utils/Extensions/PropertyEqualityComparer.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; + +namespace ICD.Common.Utils.Extensions +{ + /// + /// Allows for comparing items based on some property. + /// + /// + /// + public sealed class PropertyEqualityComparer : IEqualityComparer + { + private readonly IEqualityComparer m_Comparer; + private readonly Func m_GetProperty; + + /// + /// Constructor. + /// + /// + /// + public PropertyEqualityComparer(IEqualityComparer comparer, Func 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); + } + } +} diff --git a/ICD.Common.Utils/FuncComparer.cs b/ICD.Common.Utils/FuncComparer.cs deleted file mode 100644 index 34b5894..0000000 --- a/ICD.Common.Utils/FuncComparer.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ICD.Common.Utils -{ - /// - /// Simple comparer for comparing items using a callback. - /// - /// - public sealed class FuncComparer : IEqualityComparer - { - private readonly Func m_Comparer; - private readonly Func m_GetHashCode; - - /// - /// Constructor. - /// - /// - /// - public FuncComparer(Func comparer, Func 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); - } - } -} diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index 966b40e..665f60e 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -87,7 +87,7 @@ - +