2 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
\r
3 Permission is hereby granted, free of charge, to any person obtaining a copy
\r
4 of this software and associated documentation files (the "Software"), to deal
\r
5 in the Software without restriction, including without limitation the rights
\r
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
7 copies of the Software, and to permit persons to whom the Software is
\r
8 furnished to do so, subject to the following conditions:
\r
10 The above copyright notice and this permission notice shall be included in
\r
11 all copies or substantial portions of the Software.
\r
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
22 using System.Diagnostics;
\r
23 using SCG = System.Collections.Generic;
\r
26 #region char comparer and equality comparer
\r
27 class CharComparer : SCG.IComparer<char>
\r
29 public int Compare(char item1, char item2) {
\r
30 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
35 /// An equality comparer for type char, also known as System.Char.
\r
37 public class CharEqualityComparer : SCG.IEqualityComparer<char>
\r
39 static CharEqualityComparer cached = new CharEqualityComparer();
\r
40 CharEqualityComparer() { }
\r
45 public static CharEqualityComparer Default { get { return cached ?? (cached = new CharEqualityComparer()); } }
\r
48 /// Get the hash code of this char
\r
50 /// <param name="item">The char</param>
\r
51 /// <returns>The same</returns>
\r
52 public int GetHashCode(char item) { return item.GetHashCode(); }
\r
56 /// Check if two chars are equal
\r
58 /// <param name="item1">first char</param>
\r
59 /// <param name="item2">second char</param>
\r
60 /// <returns>True if equal</returns>
\r
61 public bool Equals(char item1, char item2) { return item1 == item2; }
\r
65 #region sbyte comparer and equality comparer
\r
67 class SByteComparer : SCG.IComparer<sbyte>
\r
70 public int Compare(sbyte item1, sbyte item2) {
\r
71 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
76 /// An equality comparer for type sbyte, also known as System.SByte.
\r
77 /// <para>This class is a singleton and the instance can be accessed
\r
78 /// via the static <see cref="P:C5.SByteEqualityComparer.Default"/> property</para>
\r
81 public class SByteEqualityComparer : SCG.IEqualityComparer<sbyte>
\r
83 static SByteEqualityComparer cached;
\r
84 SByteEqualityComparer() { }
\r
91 public static SByteEqualityComparer Default { get { return cached ?? (cached = new SByteEqualityComparer()); } }
\r
93 /// Get the hash code of this sbyte, that is, itself
\r
95 /// <param name="item">The sbyte</param>
\r
96 /// <returns>The same</returns>
\r
98 public int GetHashCode(sbyte item) { return item.GetHashCode(); }
\r
102 /// Determine whether two sbytes are equal
\r
104 /// <param name="item1">first sbyte</param>
\r
105 /// <param name="item2">second sbyte</param>
\r
106 /// <returns>True if equal</returns>
\r
108 public bool Equals(sbyte item1, sbyte item2) { return item1 == item2; }
\r
113 #region byte comparer and equality comparer
\r
114 class ByteComparer : SCG.IComparer<byte>
\r
116 public int Compare(byte item1, byte item2) {
\r
117 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
122 /// An equality comparer for type byte, also known as System.Byte.
\r
123 /// <para>This class is a singleton and the instance can be accessed
\r
124 /// via the <see cref="P:C5.ByteEqualityComparer.Default"/> property</para>
\r
126 public class ByteEqualityComparer : SCG.IEqualityComparer<byte>
\r
128 static ByteEqualityComparer cached = new ByteEqualityComparer();
\r
129 ByteEqualityComparer() { }
\r
133 /// <value></value>
\r
134 public static ByteEqualityComparer Default { get { return cached ?? (cached = new ByteEqualityComparer()); } }
\r
136 /// Get the hash code of this byte, i.e. itself
\r
138 /// <param name="item">The byte</param>
\r
139 /// <returns>The same</returns>
\r
140 public int GetHashCode(byte item) { return item.GetHashCode(); }
\r
143 /// Check if two bytes are equal
\r
145 /// <param name="item1">first byte</param>
\r
146 /// <param name="item2">second byte</param>
\r
147 /// <returns>True if equal</returns>
\r
148 public bool Equals(byte item1, byte item2) { return item1 == item2; }
\r
152 #region short comparer and equality comparer
\r
154 class ShortComparer : SCG.IComparer<short>
\r
157 public int Compare(short item1, short item2) {
\r
158 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
163 /// An equality comparer for type short, also known as System.Int16.
\r
164 /// <para>This class is a singleton and the instance can be accessed
\r
165 /// via the static <see cref="P:C5.ShortEqualityComparer.Default"/> property</para>
\r
168 public class ShortEqualityComparer : SCG.IEqualityComparer<short>
\r
170 static ShortEqualityComparer cached;
\r
171 ShortEqualityComparer() { }
\r
176 /// <value></value>
\r
178 public static ShortEqualityComparer Default { get { return cached ?? (cached = new ShortEqualityComparer()); } }
\r
180 /// Get the hash code of this short, that is, itself
\r
182 /// <param name="item">The short</param>
\r
183 /// <returns>The same</returns>
\r
185 public int GetHashCode(short item) { return item.GetHashCode(); }
\r
189 /// Determine whether two shorts are equal
\r
191 /// <param name="item1">first short</param>
\r
192 /// <param name="item2">second short</param>
\r
193 /// <returns>True if equal</returns>
\r
195 public bool Equals(short item1, short item2) { return item1 == item2; }
\r
200 #region ushort comparer and equality comparer
\r
202 class UShortComparer : SCG.IComparer<ushort>
\r
205 public int Compare(ushort item1, ushort item2)
\r
207 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
212 /// An equality comparer for type ushort, also known as System.UInt16.
\r
213 /// <para>This class is a singleton and the instance can be accessed
\r
214 /// via the static <see cref="P:C5.UShortEqualityComparer.Default"/> property</para>
\r
217 public class UShortEqualityComparer : SCG.IEqualityComparer<ushort>
\r
219 static UShortEqualityComparer cached;
\r
220 UShortEqualityComparer() { }
\r
225 /// <value></value>
\r
227 public static UShortEqualityComparer Default { get { return cached ?? (cached = new UShortEqualityComparer()); } }
\r
229 /// Get the hash code of this ushort, that is, itself
\r
231 /// <param name="item">The ushort</param>
\r
232 /// <returns>The same</returns>
\r
234 public int GetHashCode(ushort item) { return item.GetHashCode(); }
\r
238 /// Determine whether two ushorts are equal
\r
240 /// <param name="item1">first ushort</param>
\r
241 /// <param name="item2">second ushort</param>
\r
242 /// <returns>True if equal</returns>
\r
244 public bool Equals(ushort item1, ushort item2) { return item1 == item2; }
\r
249 #region int comparer and equality comparer
\r
251 class IntComparer : SCG.IComparer<int>
\r
254 public int Compare(int item1, int item2) {
\r
255 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
260 /// An equality comparer for type int, also known as System.Int32.
\r
261 /// <para>This class is a singleton and the instance can be accessed
\r
262 /// via the static <see cref="P:C5.IntEqualityComparer.Default"/> property</para>
\r
265 public class IntEqualityComparer : SCG.IEqualityComparer<int>
\r
267 static IntEqualityComparer cached;
\r
268 IntEqualityComparer() { }
\r
272 /// <value></value>
\r
274 public static IntEqualityComparer Default { get { return cached ?? (cached = new IntEqualityComparer()); } }
\r
276 /// Get the hash code of this integer, that is, itself
\r
278 /// <param name="item">The integer</param>
\r
279 /// <returns>The same</returns>
\r
281 public int GetHashCode(int item) { return item; }
\r
285 /// Determine whether two integers are equal
\r
287 /// <param name="item1">first integer</param>
\r
288 /// <param name="item2">second integer</param>
\r
289 /// <returns>True if equal</returns>
\r
291 public bool Equals(int item1, int item2) { return item1 == item2; }
\r
296 #region uint comparer and equality comparer
\r
298 class UIntComparer : SCG.IComparer<uint>
\r
301 public int Compare(uint item1, uint item2)
\r
303 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
308 /// An equality comparer for type uint, also known as System.UInt32.
\r
309 /// <para>This class is a singleton and the instance can be accessed
\r
310 /// via the static <see cref="P:C5.UIntEqualityComparer.Default"/> property</para>
\r
313 public class UIntEqualityComparer : SCG.IEqualityComparer<uint>
\r
315 static UIntEqualityComparer cached;
\r
316 UIntEqualityComparer() { }
\r
320 /// <value></value>
\r
322 public static UIntEqualityComparer Default { get { return cached ?? (cached = new UIntEqualityComparer()); } }
\r
324 /// Get the hash code of this unsigned integer
\r
326 /// <param name="item">The integer</param>
\r
327 /// <returns>The same bit pattern as a signed integer</returns>
\r
329 public int GetHashCode(uint item) { return item.GetHashCode(); }
\r
333 /// Determine whether two unsigned integers are equal
\r
335 /// <param name="item1">first unsigned integer</param>
\r
336 /// <param name="item2">second unsigned integer</param>
\r
337 /// <returns>True if equal</returns>
\r
339 public bool Equals(uint item1, uint item2) { return item1 == item2; }
\r
344 #region long comparer and equality comparer
\r
346 class LongComparer : SCG.IComparer<long>
\r
349 public int Compare(long item1, long item2)
\r
351 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
356 /// An equality comparer for type long, also known as System.Int64.
\r
357 /// <para>This class is a singleton and the instance can be accessed
\r
358 /// via the static <see cref="P:C5.LongEqualityComparer.Default"/> property</para>
\r
361 public class LongEqualityComparer : SCG.IEqualityComparer<long>
\r
363 static LongEqualityComparer cached;
\r
364 LongEqualityComparer() { }
\r
368 /// <value></value>
\r
370 public static LongEqualityComparer Default { get { return cached ?? (cached = new LongEqualityComparer()); } }
\r
372 /// Get the hash code of this long integer
\r
374 /// <param name="item">The long integer</param>
\r
375 /// <returns>The hash code</returns>
\r
377 public int GetHashCode(long item) { return item.GetHashCode(); }
\r
381 /// Determine whether two long integers are equal
\r
383 /// <param name="item1">first long integer</param>
\r
384 /// <param name="item2">second long integer</param>
\r
385 /// <returns>True if equal</returns>
\r
387 public bool Equals(long item1, long item2) { return item1 == item2; }
\r
392 #region ulong comparer and equality comparer
\r
394 class ULongComparer : SCG.IComparer<ulong>
\r
397 public int Compare(ulong item1, ulong item2)
\r
399 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
404 /// An equality comparer for type uint, also known as System.UInt64.
\r
405 /// <para>This class is a singleton and the instance can be accessed
\r
406 /// via the static <see cref="P:C5.ULongEqualityComparer.Default"/> property</para>
\r
409 public class ULongEqualityComparer : SCG.IEqualityComparer<ulong>
\r
411 static ULongEqualityComparer cached;
\r
412 ULongEqualityComparer() { }
\r
416 /// <value></value>
\r
418 public static ULongEqualityComparer Default { get { return cached ?? (cached = new ULongEqualityComparer()); } }
\r
420 /// Get the hash code of this unsigned long integer
\r
422 /// <param name="item">The unsigned long integer</param>
\r
423 /// <returns>The hash code</returns>
\r
425 public int GetHashCode(ulong item) { return item.GetHashCode(); }
\r
429 /// Determine whether two unsigned long integers are equal
\r
431 /// <param name="item1">first unsigned long integer</param>
\r
432 /// <param name="item2">second unsigned long integer</param>
\r
433 /// <returns>True if equal</returns>
\r
435 public bool Equals(ulong item1, ulong item2) { return item1 == item2; }
\r
440 #region float comparer and equality comparer
\r
441 class FloatComparer : SCG.IComparer<float>
\r
443 public int Compare(float item1, float item2)
\r
445 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
450 /// An equality comparer for type float, also known as System.Single.
\r
451 /// <para>This class is a singleton and the instance can be accessed
\r
452 /// via the static <see cref="P:C5.FloatEqualityComparer.Default"/> property</para>
\r
454 public class FloatEqualityComparer : SCG.IEqualityComparer<float>
\r
456 static FloatEqualityComparer cached;
\r
457 FloatEqualityComparer() { }
\r
461 /// <value></value>
\r
463 public static FloatEqualityComparer Default { get { return cached ?? (cached = new FloatEqualityComparer()); } }
\r
465 /// Get the hash code of this float
\r
467 /// <param name="item">The float</param>
\r
468 /// <returns>The same</returns>
\r
470 public int GetHashCode(float item) { return item.GetHashCode(); }
\r
474 /// Check if two floats are equal
\r
476 /// <param name="item1">first float</param>
\r
477 /// <param name="item2">second float</param>
\r
478 /// <returns>True if equal</returns>
\r
480 public bool Equals(float item1, float item2) { return item1 == item2; }
\r
484 #region double comparer and equality comparer
\r
485 class DoubleComparer : SCG.IComparer<double>
\r
487 public int Compare(double item1, double item2) {
\r
488 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
493 /// An equality comparer for type double, also known as System.Double.
\r
494 /// <para>This class is a singleton and the instance can be accessed
\r
495 /// via the static <see cref="P:C5.DoubleEqualityComparer.Default"/> property</para>
\r
497 public class DoubleEqualityComparer : SCG.IEqualityComparer<double>
\r
499 static DoubleEqualityComparer cached;
\r
500 DoubleEqualityComparer() { }
\r
504 /// <value></value>
\r
506 public static DoubleEqualityComparer Default { get { return cached ?? (cached = new DoubleEqualityComparer()); } }
\r
508 /// Get the hash code of this double
\r
510 /// <param name="item">The double</param>
\r
511 /// <returns>The same</returns>
\r
513 public int GetHashCode(double item) { return item.GetHashCode(); }
\r
517 /// Check if two doubles are equal
\r
519 /// <param name="item1">first double</param>
\r
520 /// <param name="item2">second double</param>
\r
521 /// <returns>True if equal</returns>
\r
523 public bool Equals(double item1, double item2) { return item1 == item2; }
\r
527 #region decimal comparer and equality comparer
\r
529 class DecimalComparer : SCG.IComparer<decimal>
\r
532 public int Compare(decimal item1, decimal item2)
\r
534 return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
\r
539 /// An equality comparer for type decimal, also known as System.Decimal.
\r
540 /// <para>This class is a singleton and the instance can be accessed
\r
541 /// via the static <see cref="P:C5.DecimalEqualityComparer.Default"/> property</para>
\r
544 public class DecimalEqualityComparer : SCG.IEqualityComparer<decimal>
\r
546 static DecimalEqualityComparer cached;
\r
547 DecimalEqualityComparer() { }
\r
551 /// <value></value>
\r
553 public static DecimalEqualityComparer Default { get { return cached ?? (cached = new DecimalEqualityComparer()); } }
\r
555 /// Get the hash code of this decimal.
\r
557 /// <param name="item">The decimal</param>
\r
558 /// <returns>The hash code</returns>
\r
560 public int GetHashCode(decimal item) { return item.GetHashCode(); }
\r
564 /// Determine whether two decimals are equal
\r
566 /// <param name="item1">first decimal</param>
\r
567 /// <param name="item2">second decimal</param>
\r
568 /// <returns>True if equal</returns>
\r
570 public bool Equals(decimal item1, decimal item2) { return item1 == item2; }
\r