Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / UserGuideExamples / ComparerPatterns.cs @ 3

1
/*
2
 Copyright (c) 2003-2008 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

    
22
// C5 example: Comparer patterns
23

    
24
// Compile with 
25
//   csc /r:C5.dll ComparerPatterns.cs 
26

    
27
using System;
28
using C5;
29
using SCG = System.Collections.Generic;
30

    
31
class MyTest {
32
  public static void Main(String[] args) {
33
    SCG.IComparer<Rec<string,int>> lexico1 = new Lexico();
34
    SCG.IComparer<Rec<string,int>> lexico2 =
35
      new DelegateComparer<Rec<string,int>>(
36
          delegate(Rec<string,int> item1, Rec<string,int> item2) { 
37
            int major = item1.X1.CompareTo(item2.X1);
38
            return major != 0 ? major : item1.X2.CompareTo(item2.X2);
39
          });
40
    Rec<String,int> r1 = new Rec<String,int>("Carsten", 1962);
41
    Rec<String,int> r2 = new Rec<String,int>("Carsten", 1964);
42
    Rec<String,int> r3 = new Rec<String,int>("Christian", 1932);
43
    Console.WriteLine(lexico1.Compare(r1, r1) == 0);
44
    Console.WriteLine(lexico1.Compare(r1, r2) < 0);
45
    Console.WriteLine(lexico1.Compare(r2, r3) < 0);
46
    Console.WriteLine(lexico2.Compare(r1, r1) == 0);
47
    Console.WriteLine(lexico2.Compare(r1, r2) < 0);
48
    Console.WriteLine(lexico2.Compare(r2, r3) < 0);
49
    
50
    SCG.IComparer<String> rev 
51
      = ReverseComparer<String>(Comparer<String>.Default);
52
    Console.WriteLine(rev.Compare("A", "A") == 0);
53
    Console.WriteLine(rev.Compare("A", "B") > 0);
54
    Console.WriteLine(rev.Compare("B", "A") < 0);
55
  }
56

    
57
  class Lexico : SCG.IComparer<Rec<String,int>> {
58
    public int Compare(Rec<String,int> item1, Rec<String,int> item2) {
59
      int major = item1.X1.CompareTo(item2.X1);
60
      return major != 0 ? major : item1.X2.CompareTo(item2.X2);
61
     }
62
  }
63

    
64
  class Lexico3 : SCG.IComparer<Rec<String,int,double>> {
65
    public int Compare(Rec<String,int,double> item1, Rec<String,int,double> item2) {
66
      int major1 = item1.X1.CompareTo(item2.X1);
67
      int major2 = major1 != 0 ? major1 : item1.X2.CompareTo(item2.X2);
68
      return major2 != 0 ? major2 : item1.X2.CompareTo(item2.X2);
69
     }
70
  }
71

    
72
  public static SCG.IComparer<T> ReverseComparer<T>(SCG.IComparer<T> cmp) {
73
    return new DelegateComparer<T>(
74
       delegate(T item1, T item2) { return cmp.Compare(item2, item1); });
75
  }
76
}