Created
August 31, 2011 14:16
-
-
Save jonpryor/1183642 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ValueComparer<T> : IComparer<T>, IEqualityComparer<T> | |
{ | |
Func<T, T, int> comparer; | |
Func<T, int> getHashCode; | |
public ValueComparer(Func<T, T, int> comparer, Func<T, int> getHashCode = null) | |
{ | |
this.comparer = comparer; | |
this.getHashCode = getHashCode; | |
} | |
public int Compare(T x, T y) | |
{ | |
return comparer(x, y); | |
} | |
public bool Equals(T x, T y) | |
{ | |
return comparer(x, y) == 0; | |
} | |
public int GetHashCode(T value) | |
{ | |
if (getHashCode == null) | |
throw new NotSupportedException (); | |
return getHashCode(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment