Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Comparer.cs « C5 « Mono.C5 « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7d760131d9fc4728872f43d63991b64d3c1f302d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#if NET_2_0
/*
 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
{ 
/// <summary>
/// A default item comparer for an item type that is either generic (IComparable&lt;T&gt;)
/// or ordinarily (System.IComparable) comparable.
/// </summary>
  public static class Comparer<T>
  {
    readonly static Type naturalComparerO = typeof(NaturalComparerO<>);

    readonly static Type naturalComparer = typeof(NaturalComparer<>);

    static SCG.IComparer<T> cachedComparer = null;

    /// <summary>
    /// Create a default comparer. 
    /// <para>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.</para>
    /// </summary>
    /// <exception cref="NotComparableException">If T is not comparable</exception>
    /// <value>The comparer</value>
    [Tested]
    public static SCG.IComparer<T> Default
    {
      get
      {
        if (cachedComparer != null)
          return cachedComparer;

        Type t = typeof(T);

        if (t.IsValueType)
        {
          if (t.Equals(typeof(int)))
            return cachedComparer = (SCG.IComparer<T>)(new IntComparer());

          if (t.Equals(typeof(double)))
            return cachedComparer = (SCG.IComparer<T>)(new DoubleComparer());

          if (t.Equals(typeof(byte)))
            return cachedComparer = (SCG.IComparer<T>)(new ByteComparer());
        }

        if (typeof(IComparable<T>).IsAssignableFrom(t))
        {
          Type c = naturalComparer.MakeGenericType(new Type[] { t });

          return cachedComparer = (SCG.IComparer<T>)(c.GetConstructor(System.Type.EmptyTypes).Invoke(null));
        }

        if (t.GetInterface("System.IComparable") != null)
        {
          Type c = naturalComparerO.MakeGenericType(new Type[] { t });

          return cachedComparer = (SCG.IComparer<T>)(c.GetConstructor(System.Type.EmptyTypes).Invoke(null));
        }

        throw new NotComparableException(String.Format("Cannot make comparer for type {0}", t));
      }
    }
  }

  /// <summary>
  /// A natural generic IComparer for an IComparable&lt;T&gt; item type
  /// </summary>
  /// <typeparam name="T"></typeparam>
  public class NaturalComparer<T> : SCG.IComparer<T>
      where T : IComparable<T>
  {
    /// <summary>
    /// Compare two items
    /// </summary>
    /// <param name="item1">First item</param>
    /// <param name="item2">Second item</param>
    /// <returns>item1 &lt;=&gt; item2</returns>
    [Tested]
    public int Compare(T item1, T item2) { return item1 != null ? item1.CompareTo(item2) : item2 != null ? -1 : 0; }
  }

  /// <summary>
  /// A natural generic IComparer for a System.IComparable item type
  /// </summary>
  /// <typeparam name="T"></typeparam>
  public class NaturalComparerO<T> : SCG.IComparer<T>
      where T : System.IComparable
  {
    /// <summary>
    /// Compare two items
    /// </summary>
    /// <param name="item1">First item</param>
    /// <param name="item2">Second item</param>
    /// <returns>item1 &lt;=&gt; item2</returns>
    [Tested]
    public int Compare(T item1, T item2) { return item1 != null ? item1.CompareTo(item2) : item2 != null ? -1 : 0; }
  }

  /// <summary>
  /// A generic comparer for type T based on a Comparison[T] delegate
  /// </summary>
  /// <typeparam name="T"></typeparam>
  public class DelegateComparer<T> : SCG.IComparer<T>
  {
    readonly Comparison<T> cmp;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="comparison"></param>
    public DelegateComparer(Comparison<T> comparison)
    {
      if (comparison == null)
        throw new NullReferenceException("Comparison cannot be null");
      this.cmp = comparison;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="item1">First item</param>
    /// <param name="item2">Second item</param>
    /// <returns>item1 &lt;=&gt; item2</returns>
    public int Compare(T item1, T item2) { return cmp(item1, item2); }
  }
}
#endif