Merge pull request #4198 from vkargov/vk-prevbb
[mono.git] / mcs / class / Mono.C5 / C5 / Builtin.cs
1 /*
2  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
3  Permission is hereby granted, free of charge, to any person obtaining a copy
4  of this software and associated documentation files (the "Software"), to deal
5  in the Software without restriction, including without limitation the rights
6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  copies of the Software, and to permit persons to whom the Software is
8  furnished to do so, subject to the following conditions:
9  
10  The above copyright notice and this permission notice shall be included in
11  all copies or substantial portions of the Software.
12  
13  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19  SOFTWARE.
20 */
21 using System;
22 using System.Diagnostics;
23 using SCG = System.Collections.Generic;
24 namespace C5
25 {
26   #region char comparer and equality comparer
27   class CharComparer : SCG.IComparer<char>
28   {
29     public int Compare(char item1, char item2) { 
30       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0; 
31     }
32   }
33
34   /// <summary>
35   /// An equality comparer for type char, also known as System.Char.
36   /// </summary>
37   public class CharEqualityComparer : SCG.IEqualityComparer<char>
38   {
39     static CharEqualityComparer cached = new CharEqualityComparer();
40     CharEqualityComparer() { }
41     /// <summary>
42     /// 
43     /// </summary>
44     /// <value></value>
45     public static CharEqualityComparer Default { get { return cached ?? (cached = new CharEqualityComparer()); } }
46
47     /// <summary>
48     /// Get the hash code of this char
49     /// </summary>
50     /// <param name="item">The char</param>
51     /// <returns>The same</returns>
52     public int GetHashCode(char item) { return item.GetHashCode(); }
53
54
55     /// <summary>
56     /// Check if two chars are equal
57     /// </summary>
58     /// <param name="item1">first char</param>
59     /// <param name="item2">second char</param>
60     /// <returns>True if equal</returns>
61     public bool Equals(char item1, char item2) { return item1 == item2; }
62   }
63   #endregion
64   
65   #region sbyte comparer and equality comparer
66   [Serializable]
67   class SByteComparer : SCG.IComparer<sbyte>
68   {
69     [Tested]
70     public int Compare(sbyte item1, sbyte item2) { 
71       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0; 
72     }
73   }
74
75   /// <summary>
76   /// An equality comparer for type sbyte, also known as System.SByte. 
77   /// <para>This class is a singleton and the instance can be accessed
78   /// via the static <see cref="P:C5.SByteEqualityComparer.Default"/> property</para>
79   /// </summary>
80   [Serializable]
81   public class SByteEqualityComparer : SCG.IEqualityComparer<sbyte>
82   {
83     static SByteEqualityComparer cached;
84     SByteEqualityComparer() { }
85
86     /// <summary>
87     /// 
88     /// </summary>
89     /// <value></value>
90     [Tested]
91     public static SByteEqualityComparer Default { get { return cached ?? (cached = new SByteEqualityComparer()); } }
92     /// <summary>
93     /// Get the hash code of this sbyte, that is, itself
94     /// </summary>
95     /// <param name="item">The sbyte</param>
96     /// <returns>The same</returns>
97     [Tested]
98     public int GetHashCode(sbyte item) { return item.GetHashCode(); }
99
100
101     /// <summary>
102     /// Determine whether two sbytes are equal
103     /// </summary>
104     /// <param name="item1">first sbyte</param>
105     /// <param name="item2">second sbyte</param>
106     /// <returns>True if equal</returns>
107     [Tested]
108     public bool Equals(sbyte item1, sbyte item2) { return item1 == item2; }
109   }
110
111   #endregion
112
113   #region byte comparer and equality comparer
114   class ByteComparer : SCG.IComparer<byte>
115   {
116     public int Compare(byte item1, byte item2) { 
117       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0; 
118     }
119   }
120
121   /// <summary>
122   /// An equality comparer for type byte, also known as System.Byte.
123   /// <para>This class is a singleton and the instance can be accessed
124   /// via the <see cref="P:C5.ByteEqualityComparer.Default"/> property</para>
125   /// </summary>
126   public class ByteEqualityComparer : SCG.IEqualityComparer<byte>
127   {
128     static ByteEqualityComparer cached = new ByteEqualityComparer();
129     ByteEqualityComparer() { }
130     /// <summary>
131     /// 
132     /// </summary>
133     /// <value></value>
134     public static ByteEqualityComparer Default { get { return cached ?? (cached = new ByteEqualityComparer()); } }
135     /// <summary>
136     /// Get the hash code of this byte, i.e. itself
137     /// </summary>
138     /// <param name="item">The byte</param>
139     /// <returns>The same</returns>
140     public int GetHashCode(byte item) { return item.GetHashCode(); }
141
142     /// <summary>
143     /// Check if two bytes are equal
144     /// </summary>
145     /// <param name="item1">first byte</param>
146     /// <param name="item2">second byte</param>
147     /// <returns>True if equal</returns>
148     public bool Equals(byte item1, byte item2) { return item1 == item2; }
149   }
150   #endregion
151
152   #region short comparer and equality comparer
153   [Serializable]
154   class ShortComparer : SCG.IComparer<short>
155   {
156     [Tested]
157     public int Compare(short item1, short item2) { 
158       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0; 
159     }
160   }
161
162   /// <summary>
163   /// An equality comparer for type short, also known as System.Int16. 
164   /// <para>This class is a singleton and the instance can be accessed
165   /// via the static <see cref="P:C5.ShortEqualityComparer.Default"/> property</para>
166   /// </summary>
167   [Serializable]
168   public class ShortEqualityComparer : SCG.IEqualityComparer<short>
169   {
170     static ShortEqualityComparer cached;
171     ShortEqualityComparer() { }
172
173     /// <summary>
174     /// 
175     /// </summary>
176     /// <value></value>
177     [Tested]
178     public static ShortEqualityComparer Default { get { return cached ?? (cached = new ShortEqualityComparer()); } }
179     /// <summary>
180     /// Get the hash code of this short, that is, itself
181     /// </summary>
182     /// <param name="item">The short</param>
183     /// <returns>The same</returns>
184     [Tested]
185     public int GetHashCode(short item) { return item.GetHashCode(); }
186
187
188     /// <summary>
189     /// Determine whether two shorts are equal
190     /// </summary>
191     /// <param name="item1">first short</param>
192     /// <param name="item2">second short</param>
193     /// <returns>True if equal</returns>
194     [Tested]
195     public bool Equals(short item1, short item2) { return item1 == item2; }
196   }
197
198   #endregion
199
200   #region ushort comparer and equality comparer
201   [Serializable]
202   class UShortComparer : SCG.IComparer<ushort>
203   {
204     [Tested]
205     public int Compare(ushort item1, ushort item2)
206     {
207       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
208     }
209   }
210
211   /// <summary>
212   /// An equality comparer for type ushort, also known as System.UInt16. 
213   /// <para>This class is a singleton and the instance can be accessed
214   /// via the static <see cref="P:C5.UShortEqualityComparer.Default"/> property</para>
215   /// </summary>
216   [Serializable]
217   public class UShortEqualityComparer : SCG.IEqualityComparer<ushort>
218   {
219     static UShortEqualityComparer cached;
220     UShortEqualityComparer() { }
221
222     /// <summary>
223     /// 
224     /// </summary>
225     /// <value></value>
226     [Tested]
227     public static UShortEqualityComparer Default { get { return cached ?? (cached = new UShortEqualityComparer()); } }
228     /// <summary>
229     /// Get the hash code of this ushort, that is, itself
230     /// </summary>
231     /// <param name="item">The ushort</param>
232     /// <returns>The same</returns>
233     [Tested]
234     public int GetHashCode(ushort item) { return item.GetHashCode(); }
235
236
237     /// <summary>
238     /// Determine whether two ushorts are equal
239     /// </summary>
240     /// <param name="item1">first ushort</param>
241     /// <param name="item2">second ushort</param>
242     /// <returns>True if equal</returns>
243     [Tested]
244     public bool Equals(ushort item1, ushort item2) { return item1 == item2; }
245   }
246
247   #endregion
248
249   #region int comparer and equality comparer
250   [Serializable]
251   class IntComparer : SCG.IComparer<int>
252   {
253     [Tested]
254     public int Compare(int item1, int item2) { 
255       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0; 
256     }
257   }
258
259   /// <summary>
260   /// An equality comparer for type int, also known as System.Int32. 
261   /// <para>This class is a singleton and the instance can be accessed
262   /// via the static <see cref="P:C5.IntEqualityComparer.Default"/> property</para>
263   /// </summary>
264   [Serializable]
265   public class IntEqualityComparer : SCG.IEqualityComparer<int>
266   {
267     static IntEqualityComparer cached;
268     IntEqualityComparer() { }
269     /// <summary>
270     /// 
271     /// </summary>
272     /// <value></value>
273     [Tested]
274     public static IntEqualityComparer Default { get { return cached ?? (cached = new IntEqualityComparer()); } }
275     /// <summary>
276     /// Get the hash code of this integer, that is, itself
277     /// </summary>
278     /// <param name="item">The integer</param>
279     /// <returns>The same</returns>
280     [Tested]
281     public int GetHashCode(int item) { return item; }
282
283
284     /// <summary>
285     /// Determine whether two integers are equal
286     /// </summary>
287     /// <param name="item1">first integer</param>
288     /// <param name="item2">second integer</param>
289     /// <returns>True if equal</returns>
290     [Tested]
291     public bool Equals(int item1, int item2) { return item1 == item2; }
292   }
293
294   #endregion
295
296   #region uint comparer and equality comparer
297   [Serializable]
298   class UIntComparer : SCG.IComparer<uint>
299   {
300     [Tested]
301     public int Compare(uint item1, uint item2)
302     {
303       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
304     }
305   }
306
307   /// <summary>
308   /// An equality comparer for type uint, also known as System.UInt32. 
309   /// <para>This class is a singleton and the instance can be accessed
310   /// via the static <see cref="P:C5.UIntEqualityComparer.Default"/> property</para>
311   /// </summary>
312   [Serializable]
313   public class UIntEqualityComparer : SCG.IEqualityComparer<uint>
314   {
315     static UIntEqualityComparer cached;
316     UIntEqualityComparer() { }
317     /// <summary>
318     /// 
319     /// </summary>
320     /// <value></value>
321     [Tested]
322     public static UIntEqualityComparer Default { get { return cached ?? (cached = new UIntEqualityComparer()); } }
323     /// <summary>
324     /// Get the hash code of this unsigned integer
325     /// </summary>
326     /// <param name="item">The integer</param>
327     /// <returns>The same bit pattern as a signed integer</returns>
328     [Tested]
329     public int GetHashCode(uint item) { return item.GetHashCode(); }
330
331
332     /// <summary>
333     /// Determine whether two unsigned integers are equal
334     /// </summary>
335     /// <param name="item1">first unsigned integer</param>
336     /// <param name="item2">second unsigned integer</param>
337     /// <returns>True if equal</returns>
338     [Tested]
339     public bool Equals(uint item1, uint item2) { return item1 == item2; }
340   }
341
342   #endregion
343
344   #region long comparer and equality comparer
345   [Serializable]
346   class LongComparer : SCG.IComparer<long>
347   {
348     [Tested]
349     public int Compare(long item1, long item2)
350     {
351       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
352     }
353   }
354
355   /// <summary>
356   /// An equality comparer for type long, also known as System.Int64. 
357   /// <para>This class is a singleton and the instance can be accessed
358   /// via the static <see cref="P:C5.LongEqualityComparer.Default"/> property</para>
359   /// </summary>
360   [Serializable]
361   public class LongEqualityComparer : SCG.IEqualityComparer<long>
362   {
363     static LongEqualityComparer cached;
364     LongEqualityComparer() { }
365     /// <summary>
366     /// 
367     /// </summary>
368     /// <value></value>
369     [Tested]
370     public static LongEqualityComparer Default { get { return cached ?? (cached = new LongEqualityComparer()); } }
371     /// <summary>
372     /// Get the hash code of this long integer
373     /// </summary>
374     /// <param name="item">The long integer</param>
375     /// <returns>The hash code</returns>
376     [Tested]
377     public int GetHashCode(long item) { return item.GetHashCode(); }
378
379
380     /// <summary>
381     /// Determine whether two long integers are equal
382     /// </summary>
383     /// <param name="item1">first long integer</param>
384     /// <param name="item2">second long integer</param>
385     /// <returns>True if equal</returns>
386     [Tested]
387     public bool Equals(long item1, long item2) { return item1 == item2; }
388   }
389
390   #endregion
391
392   #region ulong comparer and equality comparer
393   [Serializable]
394   class ULongComparer : SCG.IComparer<ulong>
395   {
396     [Tested]
397     public int Compare(ulong item1, ulong item2)
398     {
399       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
400     }
401   }
402
403   /// <summary>
404   /// An equality comparer for type uint, also known as System.UInt64. 
405   /// <para>This class is a singleton and the instance can be accessed
406   /// via the static <see cref="P:C5.ULongEqualityComparer.Default"/> property</para>
407   /// </summary>
408   [Serializable]
409   public class ULongEqualityComparer : SCG.IEqualityComparer<ulong>
410   {
411     static ULongEqualityComparer cached;
412     ULongEqualityComparer() { }
413     /// <summary>
414     /// 
415     /// </summary>
416     /// <value></value>
417     [Tested]
418     public static ULongEqualityComparer Default { get { return cached ?? (cached = new ULongEqualityComparer()); } }
419     /// <summary>
420     /// Get the hash code of this unsigned long integer
421     /// </summary>
422     /// <param name="item">The unsigned long integer</param>
423     /// <returns>The hash code</returns>
424     [Tested]
425     public int GetHashCode(ulong item) { return item.GetHashCode(); }
426
427
428     /// <summary>
429     /// Determine whether two unsigned long integers are equal
430     /// </summary>
431     /// <param name="item1">first unsigned long integer</param>
432     /// <param name="item2">second unsigned long integer</param>
433     /// <returns>True if equal</returns>
434     [Tested]
435     public bool Equals(ulong item1, ulong item2) { return item1 == item2; }
436   }
437
438   #endregion
439
440   #region float comparer and equality comparer
441   class FloatComparer : SCG.IComparer<float>
442   {
443     public int Compare(float item1, float item2)
444     {
445       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
446     }
447   }
448
449   /// <summary>
450   /// An equality comparer for type float, also known as System.Single. 
451   /// <para>This class is a singleton and the instance can be accessed
452   /// via the static <see cref="P:C5.FloatEqualityComparer.Default"/> property</para>
453   /// </summary>
454   public class FloatEqualityComparer : SCG.IEqualityComparer<float>
455   {
456     static FloatEqualityComparer cached;
457     FloatEqualityComparer() { }
458     /// <summary>
459     /// 
460     /// </summary>
461     /// <value></value>
462     [Tested]
463     public static FloatEqualityComparer Default { get { return cached ?? (cached = new FloatEqualityComparer()); } }
464     /// <summary>
465     /// Get the hash code of this float
466     /// </summary>
467     /// <param name="item">The float</param>
468     /// <returns>The same</returns>
469     [Tested]
470     public int GetHashCode(float item) { return item.GetHashCode(); }
471
472
473     /// <summary>
474     /// Check if two floats are equal
475     /// </summary>
476     /// <param name="item1">first float</param>
477     /// <param name="item2">second float</param>
478     /// <returns>True if equal</returns>
479     [Tested]
480     public bool Equals(float item1, float item2) { return item1 == item2; }
481   }
482   #endregion
483
484   #region double comparer and equality comparer
485   class DoubleComparer : SCG.IComparer<double>
486   {
487     public int Compare(double item1, double item2) { 
488       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0; 
489     }
490   }
491
492   /// <summary>
493   /// An equality comparer for type double, also known as System.Double.
494   /// <para>This class is a singleton and the instance can be accessed
495   /// via the static <see cref="P:C5.DoubleEqualityComparer.Default"/> property</para>
496   /// </summary>
497   public class DoubleEqualityComparer : SCG.IEqualityComparer<double>
498   {
499     static DoubleEqualityComparer cached;
500     DoubleEqualityComparer() { }
501     /// <summary>
502     /// 
503     /// </summary>
504     /// <value></value>
505     [Tested]
506     public static DoubleEqualityComparer Default { get { return cached ?? (cached = new DoubleEqualityComparer()); } }
507     /// <summary>
508     /// Get the hash code of this double
509     /// </summary>
510     /// <param name="item">The double</param>
511     /// <returns>The same</returns>
512     [Tested]
513     public int GetHashCode(double item) { return item.GetHashCode(); }
514
515
516     /// <summary>
517     /// Check if two doubles are equal
518     /// </summary>
519     /// <param name="item1">first double</param>
520     /// <param name="item2">second double</param>
521     /// <returns>True if equal</returns>
522     [Tested]
523     public bool Equals(double item1, double item2) { return item1 == item2; }
524   }
525   #endregion
526
527   #region decimal comparer and equality comparer
528   [Serializable]
529   class DecimalComparer : SCG.IComparer<decimal>
530   {
531     [Tested]
532     public int Compare(decimal item1, decimal item2)
533     {
534       return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
535     }
536   }
537
538   /// <summary>
539   /// An equality comparer for type decimal, also known as System.Decimal. 
540   /// <para>This class is a singleton and the instance can be accessed
541   /// via the static <see cref="P:C5.DecimalEqualityComparer.Default"/> property</para>
542   /// </summary>
543   [Serializable]
544   public class DecimalEqualityComparer : SCG.IEqualityComparer<decimal>
545   {
546     static DecimalEqualityComparer cached;
547     DecimalEqualityComparer() { }
548     /// <summary>
549     /// 
550     /// </summary>
551     /// <value></value>
552     [Tested]
553     public static DecimalEqualityComparer Default { get { return cached ?? (cached = new DecimalEqualityComparer()); } }
554     /// <summary>
555     /// Get the hash code of this decimal.
556     /// </summary>
557     /// <param name="item">The decimal</param>
558     /// <returns>The hash code</returns>
559     [Tested]
560     public int GetHashCode(decimal item) { return item.GetHashCode(); }
561
562
563     /// <summary>
564     /// Determine whether two decimals are equal
565     /// </summary>
566     /// <param name="item1">first decimal</param>
567     /// <param name="item2">second decimal</param>
568     /// <returns>True if equal</returns>
569     [Tested]
570     public bool Equals(decimal item1, decimal item2) { return item1 == item2; }
571   }
572
573   #endregion
574 }