/* Copyright (c) 2003-2008 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. */ // C5 example: Comparer patterns // Compile with // csc /r:C5.dll ComparerPatterns.cs using System; using C5; using SCG = System.Collections.Generic; class MyTest { public static void Main(String[] args) { SCG.IComparer> lexico1 = new Lexico(); SCG.IComparer> lexico2 = new DelegateComparer>( delegate(Rec item1, Rec item2) { int major = item1.X1.CompareTo(item2.X1); return major != 0 ? major : item1.X2.CompareTo(item2.X2); }); Rec r1 = new Rec("Carsten", 1962); Rec r2 = new Rec("Carsten", 1964); Rec r3 = new Rec("Christian", 1932); Console.WriteLine(lexico1.Compare(r1, r1) == 0); Console.WriteLine(lexico1.Compare(r1, r2) < 0); Console.WriteLine(lexico1.Compare(r2, r3) < 0); Console.WriteLine(lexico2.Compare(r1, r1) == 0); Console.WriteLine(lexico2.Compare(r1, r2) < 0); Console.WriteLine(lexico2.Compare(r2, r3) < 0); SCG.IComparer rev = ReverseComparer(Comparer.Default); Console.WriteLine(rev.Compare("A", "A") == 0); Console.WriteLine(rev.Compare("A", "B") > 0); Console.WriteLine(rev.Compare("B", "A") < 0); } class Lexico : SCG.IComparer> { public int Compare(Rec item1, Rec item2) { int major = item1.X1.CompareTo(item2.X1); return major != 0 ? major : item1.X2.CompareTo(item2.X2); } } class Lexico3 : SCG.IComparer> { public int Compare(Rec item1, Rec item2) { int major1 = item1.X1.CompareTo(item2.X1); int major2 = major1 != 0 ? major1 : item1.X2.CompareTo(item2.X2); return major2 != 0 ? major2 : item1.X2.CompareTo(item2.X2); } } public static SCG.IComparer ReverseComparer(SCG.IComparer cmp) { return new DelegateComparer( delegate(T item1, T item2) { return cmp.Compare(item2, item1); }); } }