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:
10 The above copyright notice and this permission notice shall be included in
11 all copies or substantial portions of the Software.
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
22 using System.Diagnostics;
23 using SCG = System.Collections.Generic;
26 #region char comparer and equality comparer
27 class CharComparer : SCG.IComparer<char>
29 public int Compare(char item1, char item2) {
30 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
35 /// An equality comparer for type char, also known as System.Char.
37 public class CharEqualityComparer : SCG.IEqualityComparer<char>
39 static CharEqualityComparer cached = new CharEqualityComparer();
40 CharEqualityComparer() { }
45 public static CharEqualityComparer Default { get { return cached ?? (cached = new CharEqualityComparer()); } }
48 /// Get the hash code of this char
50 /// <param name="item">The char</param>
51 /// <returns>The same</returns>
52 public int GetHashCode(char item) { return item.GetHashCode(); }
56 /// Check if two chars are equal
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; }
65 #region sbyte comparer and equality comparer
67 class SByteComparer : SCG.IComparer<sbyte>
70 public int Compare(sbyte item1, sbyte item2) {
71 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
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>
81 public class SByteEqualityComparer : SCG.IEqualityComparer<sbyte>
83 static SByteEqualityComparer cached;
84 SByteEqualityComparer() { }
91 public static SByteEqualityComparer Default { get { return cached ?? (cached = new SByteEqualityComparer()); } }
93 /// Get the hash code of this sbyte, that is, itself
95 /// <param name="item">The sbyte</param>
96 /// <returns>The same</returns>
98 public int GetHashCode(sbyte item) { return item.GetHashCode(); }
102 /// Determine whether two sbytes are equal
104 /// <param name="item1">first sbyte</param>
105 /// <param name="item2">second sbyte</param>
106 /// <returns>True if equal</returns>
108 public bool Equals(sbyte item1, sbyte item2) { return item1 == item2; }
113 #region byte comparer and equality comparer
114 class ByteComparer : SCG.IComparer<byte>
116 public int Compare(byte item1, byte item2) {
117 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
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>
126 public class ByteEqualityComparer : SCG.IEqualityComparer<byte>
128 static ByteEqualityComparer cached = new ByteEqualityComparer();
129 ByteEqualityComparer() { }
134 public static ByteEqualityComparer Default { get { return cached ?? (cached = new ByteEqualityComparer()); } }
136 /// Get the hash code of this byte, i.e. itself
138 /// <param name="item">The byte</param>
139 /// <returns>The same</returns>
140 public int GetHashCode(byte item) { return item.GetHashCode(); }
143 /// Check if two bytes are equal
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; }
152 #region short comparer and equality comparer
154 class ShortComparer : SCG.IComparer<short>
157 public int Compare(short item1, short item2) {
158 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
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>
168 public class ShortEqualityComparer : SCG.IEqualityComparer<short>
170 static ShortEqualityComparer cached;
171 ShortEqualityComparer() { }
178 public static ShortEqualityComparer Default { get { return cached ?? (cached = new ShortEqualityComparer()); } }
180 /// Get the hash code of this short, that is, itself
182 /// <param name="item">The short</param>
183 /// <returns>The same</returns>
185 public int GetHashCode(short item) { return item.GetHashCode(); }
189 /// Determine whether two shorts are equal
191 /// <param name="item1">first short</param>
192 /// <param name="item2">second short</param>
193 /// <returns>True if equal</returns>
195 public bool Equals(short item1, short item2) { return item1 == item2; }
200 #region ushort comparer and equality comparer
202 class UShortComparer : SCG.IComparer<ushort>
205 public int Compare(ushort item1, ushort item2)
207 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
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>
217 public class UShortEqualityComparer : SCG.IEqualityComparer<ushort>
219 static UShortEqualityComparer cached;
220 UShortEqualityComparer() { }
227 public static UShortEqualityComparer Default { get { return cached ?? (cached = new UShortEqualityComparer()); } }
229 /// Get the hash code of this ushort, that is, itself
231 /// <param name="item">The ushort</param>
232 /// <returns>The same</returns>
234 public int GetHashCode(ushort item) { return item.GetHashCode(); }
238 /// Determine whether two ushorts are equal
240 /// <param name="item1">first ushort</param>
241 /// <param name="item2">second ushort</param>
242 /// <returns>True if equal</returns>
244 public bool Equals(ushort item1, ushort item2) { return item1 == item2; }
249 #region int comparer and equality comparer
251 class IntComparer : SCG.IComparer<int>
254 public int Compare(int item1, int item2) {
255 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
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>
265 public class IntEqualityComparer : SCG.IEqualityComparer<int>
267 static IntEqualityComparer cached;
268 IntEqualityComparer() { }
274 public static IntEqualityComparer Default { get { return cached ?? (cached = new IntEqualityComparer()); } }
276 /// Get the hash code of this integer, that is, itself
278 /// <param name="item">The integer</param>
279 /// <returns>The same</returns>
281 public int GetHashCode(int item) { return item; }
285 /// Determine whether two integers are equal
287 /// <param name="item1">first integer</param>
288 /// <param name="item2">second integer</param>
289 /// <returns>True if equal</returns>
291 public bool Equals(int item1, int item2) { return item1 == item2; }
296 #region uint comparer and equality comparer
298 class UIntComparer : SCG.IComparer<uint>
301 public int Compare(uint item1, uint item2)
303 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
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>
313 public class UIntEqualityComparer : SCG.IEqualityComparer<uint>
315 static UIntEqualityComparer cached;
316 UIntEqualityComparer() { }
322 public static UIntEqualityComparer Default { get { return cached ?? (cached = new UIntEqualityComparer()); } }
324 /// Get the hash code of this unsigned integer
326 /// <param name="item">The integer</param>
327 /// <returns>The same bit pattern as a signed integer</returns>
329 public int GetHashCode(uint item) { return item.GetHashCode(); }
333 /// Determine whether two unsigned integers are equal
335 /// <param name="item1">first unsigned integer</param>
336 /// <param name="item2">second unsigned integer</param>
337 /// <returns>True if equal</returns>
339 public bool Equals(uint item1, uint item2) { return item1 == item2; }
344 #region long comparer and equality comparer
346 class LongComparer : SCG.IComparer<long>
349 public int Compare(long item1, long item2)
351 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
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>
361 public class LongEqualityComparer : SCG.IEqualityComparer<long>
363 static LongEqualityComparer cached;
364 LongEqualityComparer() { }
370 public static LongEqualityComparer Default { get { return cached ?? (cached = new LongEqualityComparer()); } }
372 /// Get the hash code of this long integer
374 /// <param name="item">The long integer</param>
375 /// <returns>The hash code</returns>
377 public int GetHashCode(long item) { return item.GetHashCode(); }
381 /// Determine whether two long integers are equal
383 /// <param name="item1">first long integer</param>
384 /// <param name="item2">second long integer</param>
385 /// <returns>True if equal</returns>
387 public bool Equals(long item1, long item2) { return item1 == item2; }
392 #region ulong comparer and equality comparer
394 class ULongComparer : SCG.IComparer<ulong>
397 public int Compare(ulong item1, ulong item2)
399 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
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>
409 public class ULongEqualityComparer : SCG.IEqualityComparer<ulong>
411 static ULongEqualityComparer cached;
412 ULongEqualityComparer() { }
418 public static ULongEqualityComparer Default { get { return cached ?? (cached = new ULongEqualityComparer()); } }
420 /// Get the hash code of this unsigned long integer
422 /// <param name="item">The unsigned long integer</param>
423 /// <returns>The hash code</returns>
425 public int GetHashCode(ulong item) { return item.GetHashCode(); }
429 /// Determine whether two unsigned long integers are equal
431 /// <param name="item1">first unsigned long integer</param>
432 /// <param name="item2">second unsigned long integer</param>
433 /// <returns>True if equal</returns>
435 public bool Equals(ulong item1, ulong item2) { return item1 == item2; }
440 #region float comparer and equality comparer
441 class FloatComparer : SCG.IComparer<float>
443 public int Compare(float item1, float item2)
445 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
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>
454 public class FloatEqualityComparer : SCG.IEqualityComparer<float>
456 static FloatEqualityComparer cached;
457 FloatEqualityComparer() { }
463 public static FloatEqualityComparer Default { get { return cached ?? (cached = new FloatEqualityComparer()); } }
465 /// Get the hash code of this float
467 /// <param name="item">The float</param>
468 /// <returns>The same</returns>
470 public int GetHashCode(float item) { return item.GetHashCode(); }
474 /// Check if two floats are equal
476 /// <param name="item1">first float</param>
477 /// <param name="item2">second float</param>
478 /// <returns>True if equal</returns>
480 public bool Equals(float item1, float item2) { return item1 == item2; }
484 #region double comparer and equality comparer
485 class DoubleComparer : SCG.IComparer<double>
487 public int Compare(double item1, double item2) {
488 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
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>
497 public class DoubleEqualityComparer : SCG.IEqualityComparer<double>
499 static DoubleEqualityComparer cached;
500 DoubleEqualityComparer() { }
506 public static DoubleEqualityComparer Default { get { return cached ?? (cached = new DoubleEqualityComparer()); } }
508 /// Get the hash code of this double
510 /// <param name="item">The double</param>
511 /// <returns>The same</returns>
513 public int GetHashCode(double item) { return item.GetHashCode(); }
517 /// Check if two doubles are equal
519 /// <param name="item1">first double</param>
520 /// <param name="item2">second double</param>
521 /// <returns>True if equal</returns>
523 public bool Equals(double item1, double item2) { return item1 == item2; }
527 #region decimal comparer and equality comparer
529 class DecimalComparer : SCG.IComparer<decimal>
532 public int Compare(decimal item1, decimal item2)
534 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
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>
544 public class DecimalEqualityComparer : SCG.IEqualityComparer<decimal>
546 static DecimalEqualityComparer cached;
547 DecimalEqualityComparer() { }
553 public static DecimalEqualityComparer Default { get { return cached ?? (cached = new DecimalEqualityComparer()); } }
555 /// Get the hash code of this decimal.
557 /// <param name="item">The decimal</param>
558 /// <returns>The hash code</returns>
560 public int GetHashCode(decimal item) { return item.GetHashCode(); }
564 /// Determine whether two decimals are equal
566 /// <param name="item1">first decimal</param>
567 /// <param name="item2">second decimal</param>
568 /// <returns>True if equal</returns>
570 public bool Equals(decimal item1, decimal item2) { return item1 == item2; }