mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
Renaming property comparer to predicate comparer
This commit is contained in:
39
ICD.Common.Utils/Comparers/PredicateComparer.cs
Normal file
39
ICD.Common.Utils/Comparers/PredicateComparer.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Comparers
|
||||||
|
{
|
||||||
|
public sealed class PredicateComparer<TParent, TValue> : IComparer<TParent>
|
||||||
|
{
|
||||||
|
private readonly IComparer<TValue> m_Comparer;
|
||||||
|
private readonly Func<TParent, TValue> m_Predicate;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="predicate"></param>
|
||||||
|
public PredicateComparer(Func<TParent, TValue> predicate)
|
||||||
|
: this(Comparer<TValue>.Default, predicate)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="comparer"></param>
|
||||||
|
/// <param name="predicate"></param>
|
||||||
|
public PredicateComparer(IComparer<TValue> comparer, Func<TParent, TValue> predicate)
|
||||||
|
{
|
||||||
|
m_Comparer = comparer;
|
||||||
|
m_Predicate = predicate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Compare(TParent x, TParent y)
|
||||||
|
{
|
||||||
|
TValue a = m_Predicate(x);
|
||||||
|
TValue b = m_Predicate(y);
|
||||||
|
|
||||||
|
return m_Comparer.Compare(a, b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
45
ICD.Common.Utils/Comparers/PredicateEqualityComparer.cs
Normal file
45
ICD.Common.Utils/Comparers/PredicateEqualityComparer.cs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Comparers
|
||||||
|
{
|
||||||
|
public sealed class PredicateEqualityComparer<TParent, TValue> : IEqualityComparer<TParent>
|
||||||
|
{
|
||||||
|
private readonly IEqualityComparer<TValue> m_Comparer;
|
||||||
|
private readonly Func<TParent, TValue> m_Predicate;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="predicate"></param>
|
||||||
|
public PredicateEqualityComparer(Func<TParent, TValue> predicate)
|
||||||
|
: this(EqualityComparer<TValue>.Default, predicate)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="comparer"></param>
|
||||||
|
/// <param name="predicate"></param>
|
||||||
|
public PredicateEqualityComparer(IEqualityComparer<TValue> comparer, Func<TParent, TValue> predicate)
|
||||||
|
{
|
||||||
|
m_Comparer = comparer;
|
||||||
|
m_Predicate = predicate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Equals(TParent x, TParent y)
|
||||||
|
{
|
||||||
|
TValue a = m_Predicate(x);
|
||||||
|
TValue b = m_Predicate(y);
|
||||||
|
|
||||||
|
return m_Comparer.Equals(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetHashCode(TParent parent)
|
||||||
|
{
|
||||||
|
TValue value = m_Predicate(parent);
|
||||||
|
return m_Comparer.GetHashCode(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Comparers
|
|
||||||
{
|
|
||||||
public sealed class PropertyComparer<TParent, TProperty> : IComparer<TParent>
|
|
||||||
{
|
|
||||||
private readonly IComparer<TProperty> m_Comparer;
|
|
||||||
private readonly Func<TParent, TProperty> m_GetProperty;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Constructor.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="comparer"></param>
|
|
||||||
/// <param name="getProperty"></param>
|
|
||||||
public PropertyComparer(IComparer<TProperty> comparer, Func<TParent, TProperty> getProperty)
|
|
||||||
{
|
|
||||||
m_Comparer = comparer;
|
|
||||||
m_GetProperty = getProperty;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Compare(TParent x, TParent y)
|
|
||||||
{
|
|
||||||
TProperty a = m_GetProperty(x);
|
|
||||||
TProperty b = m_GetProperty(y);
|
|
||||||
|
|
||||||
return m_Comparer.Compare(a, b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Comparers
|
|
||||||
{
|
|
||||||
/// <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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -694,7 +694,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
if (propertyComparer == null)
|
if (propertyComparer == null)
|
||||||
throw new ArgumentNullException("propertyComparer");
|
throw new ArgumentNullException("propertyComparer");
|
||||||
|
|
||||||
return extends.Distinct(new PropertyEqualityComparer<TItem, TProperty>(propertyComparer, getProperty));
|
return extends.Distinct(new PredicateEqualityComparer<TItem, TProperty>(propertyComparer, getProperty));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -74,7 +74,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
||||||
<Compile Include="Comparers\PropertyComparer.cs" />
|
<Compile Include="Comparers\PredicateComparer.cs" />
|
||||||
<Compile Include="ConsoleColor.cs" />
|
<Compile Include="ConsoleColor.cs" />
|
||||||
<Compile Include="EventArguments\BoolEventArgs.cs" />
|
<Compile Include="EventArguments\BoolEventArgs.cs" />
|
||||||
<Compile Include="EventArguments\CharEventArgs.cs" />
|
<Compile Include="EventArguments\CharEventArgs.cs" />
|
||||||
@@ -88,7 +88,7 @@
|
|||||||
<None Include="ObfuscationSettings.cs" />
|
<None Include="ObfuscationSettings.cs" />
|
||||||
<Compile Include="Extensions\ByteExtensions.cs" />
|
<Compile Include="Extensions\ByteExtensions.cs" />
|
||||||
<Compile Include="Extensions\ListExtensions.cs" />
|
<Compile Include="Extensions\ListExtensions.cs" />
|
||||||
<Compile Include="Comparers\PropertyEqualityComparer.cs" />
|
<Compile Include="Comparers\PredicateEqualityComparer.cs" />
|
||||||
<Compile Include="ProcessorUtils.SimplSharp.cs" />
|
<Compile Include="ProcessorUtils.SimplSharp.cs" />
|
||||||
<Compile Include="ProcessorUtils.Standard.cs" />
|
<Compile Include="ProcessorUtils.Standard.cs" />
|
||||||
<Compile Include="ProgramUtils.SimplSharp.cs" />
|
<Compile Include="ProgramUtils.SimplSharp.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user