[corlib] ParseNumber.StringToInt now check for overflows.
[mono.git] / mcs / class / corlib / System.Collections.Generic / EqualityComparer.cs
1 //
2 // EqualityComparer.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@ximian.com)
6 //      Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System;
32 using System.Runtime.InteropServices;
33
34 namespace System.Collections.Generic {
35         [Serializable]
36         public abstract class EqualityComparer <T> : IEqualityComparer, IEqualityComparer <T> {
37                 
38                 static EqualityComparer ()
39                 {
40                         var t = typeof (T);
41                         if (t == typeof (string)) {
42                                 _default = (EqualityComparer<T>) (object) new InternalStringComparer ();
43                                 return;
44                         }
45
46                         if (t == typeof (int)) {
47                                 _default = (EqualityComparer<T>) (object) new IntEqualityComparer ();
48                                 return;
49                         }
50
51                         if (t.IsEnum && Enum.GetUnderlyingType (t) == typeof (int)) {
52                                 _default = new EnumIntEqualityComparer<T> ();
53                                 return;
54                         }
55
56                         if (typeof (IEquatable <T>).IsAssignableFrom (t))
57                                 _default = (EqualityComparer <T>) Activator.CreateInstance (typeof (GenericEqualityComparer <>).MakeGenericType (t));
58                         else
59                                 _default = new DefaultComparer<T> ();
60                 }
61                 
62                 public abstract int GetHashCode (T obj);
63                 public abstract bool Equals (T x, T y);
64         
65                 static readonly EqualityComparer <T> _default;
66                 
67                 public static EqualityComparer <T> Default {
68                         get {
69                                 return _default;
70                         }
71                 }
72
73                 int IEqualityComparer.GetHashCode (object obj)
74                 {
75                         if (obj == null)
76                                 return 0;
77
78                         if (!(obj is T))
79                                 throw new ArgumentException ("Argument is not compatible", "obj");
80
81                         return GetHashCode ((T)obj);
82                 }
83
84                 bool IEqualityComparer.Equals (object x, object y)
85                 {
86                         if (x == y)
87                                 return true;
88
89                         if (x == null || y == null)
90                                 return false;
91
92                         if (!(x is T))
93                                 throw new ArgumentException ("Argument is not compatible", "x");
94                         if (!(y is T))
95                                 throw new ArgumentException ("Argument is not compatible", "y");
96                         return Equals ((T)x, (T)y);
97                 }
98                 
99                 internal virtual int IndexOf (T[] array, T value, int startIndex, int endIndex)
100                 {
101                         for (int i = startIndex; i < endIndex; ++i) {
102                                 if (Equals (Array.UnsafeLoad (array, i), value))
103                                         return i;
104                         }
105
106                         return -1;
107                 }
108         }
109
110         [Serializable]
111         sealed class DefaultComparer<T> : EqualityComparer<T> {
112         
113                 public override int GetHashCode (T obj)
114                 {
115                         if (obj == null)
116                                 return 0;
117                         return obj.GetHashCode ();
118                 }
119         
120                 public override bool Equals (T x, T y)
121                 {
122                         if (x == null)
123                                 return y == null;
124
125                         return x.Equals (y);
126                 }
127         }
128
129         [Serializable]
130         sealed class InternalStringComparer : EqualityComparer<string> {
131         
132                 public override int GetHashCode (string obj)
133                 {
134                         if (obj == null)
135                                 return 0;
136                         return obj.GetHashCode ();
137                 }
138         
139                 public override bool Equals (string x, string y)
140                 {
141                         if (x == null)
142                                 return y == null;
143
144                         if ((object) x == (object) y)
145                                 return true;
146                                 
147                         return x.Equals (y);
148                 }
149
150                 internal override int IndexOf (string[] array, string value, int startIndex, int endIndex)
151                 {
152                         for (int i = startIndex; i < endIndex; ++i) {
153                                 if (Array.UnsafeLoad (array, i) == value)
154                                         return i;
155                         }
156
157                         return -1;
158                 }
159         }
160
161         [Serializable]
162         sealed class IntEqualityComparer : EqualityComparer<int>
163         {
164                 public override int GetHashCode (int obj)
165                 {
166                         return obj;
167                 }
168
169                 public override bool Equals (int x, int y)
170                 {
171                         return x == y;
172                 }
173
174                 internal override int IndexOf (int[] array, int value, int startIndex, int endIndex)
175                 {
176                         for (int i = startIndex; i < endIndex; ++i) {
177                                 if (Array.UnsafeLoad (array, i) == value)
178                                         return i;
179                         }
180
181                         return -1;
182                 }
183         }
184
185         [Serializable]
186         sealed class EnumIntEqualityComparer<T> : EqualityComparer<T>
187         {
188                 public override int GetHashCode (T obj)
189                 {
190                         return Array.UnsafeMov<T, int> (obj);
191                 }
192
193                 public override bool Equals (T x, T y)
194                 {
195                         return Array.UnsafeMov<T, int> (x) == Array.UnsafeMov<T, int> (y);
196                 }
197
198                 internal override int IndexOf (T[] array, T value, int startIndex, int endIndex)
199                 {
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)
204                                         return i;
205                         }
206
207                         return -1;
208                 }
209         }
210
211         [Serializable]
212         sealed class GenericEqualityComparer <T> : EqualityComparer <T> where T : IEquatable <T> {
213
214                 public override int GetHashCode (T obj)
215                 {
216                         if (obj == null)
217                                 return 0;
218                         return obj.GetHashCode ();
219                 }
220
221                 public override bool Equals (T x, T y)
222                 {
223                         if (x == null)
224                                 return y == null;
225                         
226                         return x.Equals (y);
227                 }
228         }
229 }