5 // Ben Maurer (bmaurer@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.InteropServices;
34 namespace System.Collections.Generic {
36 public abstract class EqualityComparer <T> : IEqualityComparer, IEqualityComparer <T> {
38 static EqualityComparer ()
41 if (t == typeof (string)) {
42 _default = (EqualityComparer<T>) (object) new InternalStringComparer ();
46 if (t == typeof (int)) {
47 _default = (EqualityComparer<T>) (object) new IntEqualityComparer ();
51 if (t.IsEnum && Enum.GetUnderlyingType (t) == typeof (int)) {
52 _default = new EnumIntEqualityComparer<T> ();
56 if (typeof (IEquatable <T>).IsAssignableFrom (t))
57 _default = (EqualityComparer <T>) Activator.CreateInstance (typeof (GenericEqualityComparer <>).MakeGenericType (t));
59 _default = new DefaultComparer<T> ();
62 public abstract int GetHashCode (T obj);
63 public abstract bool Equals (T x, T y);
65 static readonly EqualityComparer <T> _default;
67 public static EqualityComparer <T> Default {
73 int IEqualityComparer.GetHashCode (object obj)
79 throw new ArgumentException ("Argument is not compatible", "obj");
81 return GetHashCode ((T)obj);
84 bool IEqualityComparer.Equals (object x, object y)
89 if (x == null || y == null)
93 throw new ArgumentException ("Argument is not compatible", "x");
95 throw new ArgumentException ("Argument is not compatible", "y");
96 return Equals ((T)x, (T)y);
99 internal virtual int IndexOf (T[] array, T value, int startIndex, int endIndex)
101 for (int i = startIndex; i < endIndex; ++i) {
102 if (Equals (Array.UnsafeLoad (array, i), value))
111 sealed class DefaultComparer<T> : EqualityComparer<T> {
113 public override int GetHashCode (T obj)
117 return obj.GetHashCode ();
120 public override bool Equals (T x, T y)
130 sealed class InternalStringComparer : EqualityComparer<string> {
132 public override int GetHashCode (string obj)
136 return obj.GetHashCode ();
139 public override bool Equals (string x, string y)
144 if ((object) x == (object) y)
150 internal override int IndexOf (string[] array, string value, int startIndex, int endIndex)
152 for (int i = startIndex; i < endIndex; ++i) {
153 if (Array.UnsafeLoad (array, i) == value)
162 sealed class IntEqualityComparer : EqualityComparer<int>
164 public override int GetHashCode (int obj)
169 public override bool Equals (int x, int y)
174 internal override int IndexOf (int[] array, int value, int startIndex, int endIndex)
176 for (int i = startIndex; i < endIndex; ++i) {
177 if (Array.UnsafeLoad (array, i) == value)
186 sealed class EnumIntEqualityComparer<T> : EqualityComparer<T>
188 public override int GetHashCode (T obj)
190 return Array.UnsafeMov<T, int> (obj);
193 public override bool Equals (T x, T y)
195 return Array.UnsafeMov<T, int> (x) == Array.UnsafeMov<T, int> (y);
198 internal override int IndexOf (T[] array, T value, int startIndex, int endIndex)
200 int v = Array.UnsafeMov<T, int> (value);
201 var a = Array.UnsafeMov<T[], int[]> (array);
202 for (int i = startIndex; i < endIndex; ++i) {
203 if (Array.UnsafeLoad (a, i) == v)
212 sealed class GenericEqualityComparer <T> : EqualityComparer <T> where T : IEquatable <T> {
214 public override int GetHashCode (T obj)
218 return obj.GetHashCode ();
221 public override bool Equals (T x, T y)