Skip to content

Instantly share code, notes, and snippets.

@kjellski
Created August 19, 2013 09:52
Show Gist options
  • Save kjellski/6267515 to your computer and use it in GitHub Desktop.
Save kjellski/6267515 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RLUtils
{
/// <summary>
/// Creates an Comparer for a type
/// </summary>
/// <typeparam name="T"></typeparam>
public class Comparer<T> : IComparer<T>
{
private Func<T, T, int> _func;
/// <summary>
/// Constructor to hide the funtion
/// </summary>
/// <param name="func"></param>
public Comparer(Func<T, T, int> func)
{
_func = func;
}
/// <summary>
/// Compares two types of type T
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int Compare(T x, T y)
{
return _func(x, y);
}
}
/// <summary>
/// This creates a concrete comparer for lambdas
/// </summary>
public static class Comparer
{
/// <summary>
/// Creates a Comparer
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="func"></param>
/// <returns></returns>
public static Comparer<T> Create<T>(Func<T, T, int> func)
{
return new Comparer<T>(func);
}
/// <summary>
/// Creates a comparer on an Enumerable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable"></param>
/// <param name="func"></param>
/// <returns></returns>
public static Comparer<T> CreateComparerForElements<T>(this IEnumerable<T> enumerable, Func<T, T, int> func)
{
return new Comparer<T>(func);
}
}
/// <summary>
/// This is an Extension to the Enumerables Comparing methods
/// </summary>
public static class LambdaComparerExtensionMethods
{
/// <summary>
/// Extension method to hide it's usage
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <param name="first"></param>
/// <param name="second"></param>
/// <param name="comparer"></param>
/// <returns></returns>
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first,
IEnumerable<TSource> second, Func<TSource, TSource, bool> comparer)
{
return first.Except(second, new LambdaComparer<TSource>(comparer));
}
}
/// <summary>
/// This class could be used to write delegate comparers.
/// Especially useful for linq queries
/// Like this:
/// List OF MyObject x = myCollection.Except(otherCollection, new LambdaComparer OF MyObject((x, y) => x.Id == y.Id)).ToList();
/// </summary>
/// <typeparam name="T"></typeparam>
public class LambdaComparer<T> : IEqualityComparer<T>
{
private readonly Func<T, T, bool> _lambdaComparer;
private readonly Func<T, int> _lambdaHash;
/// <summary>
/// The comparer itself, initialized with
/// </summary>
/// <param name="lambdaComparer"></param>
public LambdaComparer(Func<T, T, bool> lambdaComparer) :
this(lambdaComparer, o => 0)
{
}
/// <summary>
/// Constructor for generating the Comparer itself
/// </summary>
/// <param name="lambdaComparer"></param>
/// <param name="lambdaHash"></param>
public LambdaComparer(Func<T, T, bool> lambdaComparer, Func<T, int> lambdaHash)
{
if (lambdaComparer == null)
throw new ArgumentNullException("lambdaComparer");
if (lambdaHash == null)
throw new ArgumentNullException("lambdaHash");
_lambdaComparer = lambdaComparer;
_lambdaHash = lambdaHash;
}
/// <summary>
/// Equals with _labdaComparer
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public bool Equals(T x, T y)
{
return _lambdaComparer(x, y);
}
/// <summary>
/// GetHashCode with _labdaHash
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int GetHashCode(T obj)
{
return _lambdaHash(obj);
}
}
}
if (virtualMachineRefs.All(
r => this.vapp.Vm.Contains(r,
new LambdaComparer<ManagedObjectReference>((a, b) => a.Type == b.Type && a.Value == b.Value))))
{
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment