/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using C5;
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Diagnostics;
using SCG = System.Collections.Generic;
namespace C5
{
///
/// A default item comparer for an item type that is either generic (IComparable<T>)
/// or ordinarily (System.IComparable) comparable.
///
public static class Comparer
{
readonly static Type naturalComparerO = typeof(NaturalComparerO<>);
readonly static Type naturalComparer = typeof(NaturalComparer<>);
static SCG.IComparer cachedComparer = null;
///
/// Create a default comparer.
/// The IComparer[T] object is constructed when this class is initialised, i.e.
/// its static constructors called. Thus, the property will be the same object
/// for the duration of an invocation of the runtime, but a value serialized in
/// another invocation and deserialized here will not be the same object.
///
/// If T is not comparable
/// The comparer
[Tested]
public static SCG.IComparer Default
{
get
{
if (cachedComparer != null)
return cachedComparer;
Type t = typeof(T);
if (t.IsValueType)
{
if (t.Equals(typeof(char)))
return cachedComparer = (SCG.IComparer)(new CharComparer());
if (t.Equals(typeof(sbyte)))
return cachedComparer = (SCG.IComparer)(new SByteComparer());
if (t.Equals(typeof(byte)))
return cachedComparer = (SCG.IComparer)(new ByteComparer());
if (t.Equals(typeof(short)))
return cachedComparer = (SCG.IComparer)(new ShortComparer());
if (t.Equals(typeof(ushort)))
return cachedComparer = (SCG.IComparer)(new UShortComparer());
if (t.Equals(typeof(int)))
return cachedComparer = (SCG.IComparer)(new IntComparer());
if (t.Equals(typeof(uint)))
return cachedComparer = (SCG.IComparer)(new UIntComparer());
if (t.Equals(typeof(long)))
return cachedComparer = (SCG.IComparer)(new LongComparer());
if (t.Equals(typeof(ulong)))
return cachedComparer = (SCG.IComparer)(new ULongComparer());
if (t.Equals(typeof(float)))
return cachedComparer = (SCG.IComparer)(new FloatComparer());
if (t.Equals(typeof(double)))
return cachedComparer = (SCG.IComparer)(new DoubleComparer());
if (t.Equals(typeof(decimal)))
return cachedComparer = (SCG.IComparer)(new DecimalComparer());
}
if (typeof(IComparable).IsAssignableFrom(t))
{
Type c = naturalComparer.MakeGenericType(new Type[] { t });
return cachedComparer = (SCG.IComparer)(c.GetConstructor(System.Type.EmptyTypes).Invoke(null));
}
if (t.GetInterface("System.IComparable") != null)
{
Type c = naturalComparerO.MakeGenericType(new Type[] { t });
return cachedComparer = (SCG.IComparer)(c.GetConstructor(System.Type.EmptyTypes).Invoke(null));
}
throw new NotComparableException(String.Format("Cannot make comparer for type {0}", t));
}
}
}
///
/// A natural generic IComparer for an IComparable<T> item type
///
///
[Serializable]
public class NaturalComparer : SCG.IComparer
where T : IComparable
{
///
/// Compare two items
///
/// First item
/// Second item
/// item1 <=> item2
[Tested]
public int Compare(T item1, T item2) { return item1 != null ? item1.CompareTo(item2) : item2 != null ? -1 : 0; }
}
///
/// A natural generic IComparer for a System.IComparable item type
///
///
[Serializable]
public class NaturalComparerO : SCG.IComparer where T : System.IComparable
{
///
/// Compare two items
///
/// First item
/// Second item
/// item1 <=> item2
[Tested]
public int Compare(T item1, T item2) { return item1 != null ? item1.CompareTo(item2) : item2 != null ? -1 : 0; }
}
///
/// A generic comparer for type T based on a Comparison[T] delegate
///
///
[Serializable]
public class DelegateComparer : SCG.IComparer
{
readonly Comparison cmp;
///
///
///
///
public DelegateComparer(Comparison comparison)
{
if (comparison == null)
throw new NullReferenceException("Comparison cannot be null");
this.cmp = comparison;
}
///
///
///
/// First item
/// Second item
/// item1 <=> item2
public int Compare(T item1, T item2) { return cmp(item1, item2); }
}
}