Renaming property comparer to predicate comparer

This commit is contained in:
Chris Cameron
2018-03-26 15:38:54 -04:00
parent 30febd4a73
commit 60137254da
6 changed files with 87 additions and 74 deletions

View 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);
}
}
}

View 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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -694,7 +694,7 @@ namespace ICD.Common.Utils.Extensions
if (propertyComparer == null)
throw new ArgumentNullException("propertyComparer");
return extends.Distinct(new PropertyEqualityComparer<TItem, TProperty>(propertyComparer, getProperty));
return extends.Distinct(new PredicateEqualityComparer<TItem, TProperty>(propertyComparer, getProperty));
}
/// <summary>

View File

@@ -74,7 +74,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
<Compile Include="Comparers\PropertyComparer.cs" />
<Compile Include="Comparers\PredicateComparer.cs" />
<Compile Include="ConsoleColor.cs" />
<Compile Include="EventArguments\BoolEventArgs.cs" />
<Compile Include="EventArguments\CharEventArgs.cs" />
@@ -88,7 +88,7 @@
<None Include="ObfuscationSettings.cs" />
<Compile Include="Extensions\ByteExtensions.cs" />
<Compile Include="Extensions\ListExtensions.cs" />
<Compile Include="Comparers\PropertyEqualityComparer.cs" />
<Compile Include="Comparers\PredicateEqualityComparer.cs" />
<Compile Include="ProcessorUtils.SimplSharp.cs" />
<Compile Include="ProcessorUtils.Standard.cs" />
<Compile Include="ProgramUtils.SimplSharp.cs" />