2 using System
.Runtime
.InteropServices
;
4 namespace Martin
.Collections
.Generic
{
6 public abstract class EqualityComparer
<T
> : IEqualityComparer
<T
> {
8 static EqualityComparer ()
10 if (typeof (IEquatable
<T
>).IsAssignableFrom (typeof (T
)))
11 _default
= (EqualityComparer
<T
>) Activator
.CreateInstance (typeof (IEquatableOfTEqualityComparer
<>).MakeGenericType (typeof (T
)));
13 _default
= new DefaultComparer ();
17 public abstract int GetHashCode (T obj
);
18 public abstract bool Equals (T x
, T y
);
20 static readonly EqualityComparer
<T
> _default
;
22 public static EqualityComparer
<T
> Default
{
29 class DefaultComparer
: EqualityComparer
<T
> {
31 public override int GetHashCode (T obj
)
33 return obj
.GetHashCode ();
36 public override bool Equals (T x
, T y
)
47 class IEquatableOfTEqualityComparer
<T
> : EqualityComparer
<T
> where T
: IEquatable
<T
> {
49 public override int GetHashCode (T obj
)
51 return obj
.GetHashCode ();
54 public override bool Equals (T x
, T y
)
63 public interface IEqualityComparer
<T
> {
64 bool Equals (T x
, T y
);
65 int GetHashCode (T obj
);