Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Comparer<T>: IEqualityComparer<T>
- {
- private readonly Func<T, T, bool> _comparer;
- public Comparer(Func<T, T, bool> comparer)
- {
- if (comparer == null)
- throw new ArgumentNullException("comparer");
- _comparer = comparer;
- }
- public bool Equals(T x, T y)
- {
- return _comparer(x, y);
- }
- public int GetHashCode(T obj)
- {
- return obj.ToString().ToLower().GetHashCode();
- }
- }
- ...
- Func<int, int, bool> f = (x, y) => x == y;
- var comparer = new Comparer<int>(f);
- Console.WriteLine(comparer.Equals(1, 1));
- Console.WriteLine(comparer.Equals(1, 2));
- var foo = new List<string> { "abc", "de", "DE" };
- // case-insensitive distinct
- var distinct = foo.Distinct(new KeyEqualityComparer<string>( x => x.ToLower() ) );
- public class KeyEqualityComparer<T> : IEqualityComparer<T>
- {
- private readonly Func<T, object> keyExtractor;
- public KeyEqualityComparer(Func<T,object> keyExtractor)
- {
- this.keyExtractor = keyExtractor;
- }
- public bool Equals(T x, T y)
- {
- return this.keyExtractor(x).Equals(this.keyExtractor(y));
- }
- public int GetHashCode(T obj)
- {
- return this.keyExtractor(obj).GetHashCode();
- }
- }
- class Value
- {
- public string Name { get; private set; }
- public int Number { get; private set; }
- public Value(string name, int number)
- {
- Name = name;
- Number = number;
- }
- public override string ToString()
- {
- return string.Format("{0}: {1}", Name, Number);
- }
- }
- var comparer = new Comparer<Value>((x, y) => x.Name == y.Name);
- var values = new List<Value>();
- var random = new Random();
- for (int i = 0; i < 10; ++i)
- {
- values.Add("x", random.Next());
- }
- var distinct = values.Distinct(comparer);
- foreach (Value x in distinct)
- {
- Console.WriteLine(x);
- }
- GroupBy
- var grouped = values.GroupBy(x => x, comparer);
- foreach (IGrouping<Value> g in grouped)
- {
- Console.WriteLine("[KEY: '{0}']", g);
- foreach (Value x in g)
- {
- Console.WriteLine(x);
- }
- }
- Distinct
- var uniqueValues = new HashSet<Value>(values, comparer);
- foreach (Value x in uniqueValues)
- {
- Console.WriteLine(x);
- }
- GetHashCode
- public class KeyEqualityComparer<T, TKey> : IEqualityComparer<T>
- {
- protected readonly Func<T, TKey> keyExtractor;
- public KeyEqualityComparer(Func<T, TKey> keyExtractor)
- {
- this.keyExtractor = keyExtractor;
- }
- public virtual bool Equals(T x, T y)
- {
- return this.keyExtractor(x).Equals(this.keyExtractor(y));
- }
- public int GetHashCode(T obj)
- {
- return this.keyExtractor(obj).GetHashCode();
- }
- }
- public class StrictKeyEqualityComparer<T, TKey> : KeyEqualityComparer<T, TKey>
- where TKey : IEquatable<TKey>
- {
- public StrictKeyEqualityComparer(Func<T, TKey> keyExtractor)
- : base(keyExtractor)
- { }
- public override bool Equals(T x, T y)
- {
- // This will use the overload that accepts a TKey parameter
- // instead of an object parameter.
- return this.keyExtractor(x).Equals(this.keyExtractor(y));
- }
- }
- public class FuncEqualityComparer<T> : IEqualityComparer<T>
- {
- readonly Func<T, T, bool> _comparer;
- readonly Func<T, int> _hash;
- public FuncEqualityComparer( Func<T, T, bool> comparer )
- : this( comparer, t => t.GetHashCode())
- {
- }
- public FuncEqualityComparer( Func<T, T, bool> comparer, Func<T, int> hash )
- {
- _comparer = comparer;
- _hash = hash;
- }
- public bool Equals( T x, T y )
- {
- return _comparer( x, y );
- }
- public int GetHashCode( T obj )
- {
- return _hash( obj );
- }
- }
- public static class SequenceExtensions
- {
- public static bool SequenceEqual<T>( this IEnumerable<T> first, IEnumerable<T> second, Func<T, T, bool> comparer )
- {
- return first.SequenceEqual( second, new FuncEqualityComparer<T>( comparer ) );
- }
- public static bool SequenceEqual<T>( this IEnumerable<T> first, IEnumerable<T> second, Func<T, T, bool> comparer, Func<T, int> hash )
- {
- return first.SequenceEqual( second, new FuncEqualityComparer<T>( comparer, hash ) );
- }
- }
- class FuncEqualityComparer<T> : IEqualityComparer<T>
- {
- readonly Func<T, T, bool> _comparer;
- readonly Func<T, int> _hash;
- public FuncEqualityComparer( Func<T, T, bool> comparer )
- : this( comparer, t => 0 ) // NB Cannot assume anything about how e.g., t.GetHashCode() interacts with the comparer's behavior
- {
- }
- public FuncEqualityComparer( Func<T, T, bool> comparer, Func<T, int> hash )
- {
- _comparer = comparer;
- _hash = hash;
- }
- public bool Equals( T x, T y )
- {
- return _comparer( x, y );
- }
- public int GetHashCode( T obj )
- {
- return _hash( obj );
- }
- }
- public static IEnumerable<T> Distinct<T>(this IEnumerable<T> list, Func<T, object> keyExtractor)
- {
- return list.Distinct(new KeyEqualityComparer<T>(keyExtractor));
- }
- var distinct = foo.Distinct(x => x.ToLower())
- public class MyComparer<T> : IEqualityComparer<T>
- {
- public bool Equals(T x, T y)
- {
- return EqualityComparer<T>.Default.Equals(x, y);
- }
- public int GetHashCode(T obj)
- {
- return obj.GetHashCode();
- }
- }
- public static Dictionary<TKey, TValue> Distinct<TKey, TValue>(this IEnumerable<TValue> items, Func<TValue, TKey> selector)
- {
- Dictionary<TKey, TValue> result = null;
- ICollection collection = items as ICollection;
- if (collection != null)
- result = new Dictionary<TKey, TValue>(collection.Count);
- else
- result = new Dictionary<TKey, TValue>();
- foreach (TValue item in items)
- result[selector(item)] = item;
- return result;
- }
- public static class Equality<T>
- {
- public static IEqualityComparer<T> CreateComparer<V>(Func<T, V> keySelector)
- {
- return new KeyEqualityComparer<V>(keySelector);
- }
- public static IEqualityComparer<T> CreateComparer<V>(Func<T, V> keySelector, IEqualityCo
- {
- return new KeyEqualityComparer<V>(keySelector, comparer);
- }
- class KeyEqualityComparer<V> : IEqualityComparer<T>
- {
- private Func<T, V> keySelector;
- private IEqualityComparer<V> comparer;
- public KeyEqualityComparer(Func<T, V> keySelector, IEqualityComparer<V> comparer)
- {
- this.keySelector = keySelector;
- this.comparer = comparer;
- }
- public KeyEqualityComparer(Func<T, V> keySelector)
- : this(keySelector, EqualityComparer<V>.Default)
- { }
- public bool Equals(T x, T y)
- {
- return comparer.Equals(keySelector(x), keySelector(y));
- }
- public int GetHashCode(T obj)
- {
- return comparer.GetHashCode(keySelector(obj));
- }
- }
- }
- var comparer1 = Equality<Person>.CreateComparer(p => p.ID);
- var comparer2 = Equality<Person>.CreateComparer(p => p.Name);
- var comparer3 = Equality<Person>.CreateComparer(p => p.Birthday.Year);
- var comparer4 = Equality<Person>.CreateComparer(p => p.Name, StringComparer.CurrentCultureIgnoreCase);
- class Person
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public DateTime Birthday { get; set; }
- }
- public class MyComparer<T> : IEqualityComparer<T>
- {
- private Func<T, T, bool> _compare;
- MyComparer(Func<T, T, bool> compare)
- {
- _compare = compare;
- }
- public bool Equals(T x, Ty)
- {
- return _compare(x, y);
- }
- public int GetHashCode(T obj)
- {
- return obj.GetHashCode();
- }
- }
Add Comment
Please, Sign In to add comment