[641146] Implemented IStructuralComparable.CompareTo
authorMarek Safar <marek.safar@gmail.com>
Thu, 23 Sep 2010 15:19:09 +0000 (16:19 +0100)
committerMarek Safar <marek.safar@gmail.com>
Thu, 23 Sep 2010 15:22:11 +0000 (16:22 +0100)
mcs/class/corlib/System/Tuples.cs

index e5e8f6ec2199da93443b365eaecda01fafe6bbfb..96e7d638715bcbd3e954d7b139057f88dac82cf6 100644 (file)
@@ -82,9 +82,15 @@ namespace System
                        return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
                }
 
-               [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
+               int IStructuralComparable.CompareTo (object other, IComparer comparer)
                {
-                       throw new NotImplementedException ();
+                       var t = other as Tuple<T1>;
+                       if (t == null) {
+                               if (other == null) return 1;
+                               throw new ArgumentException ();
+                       }
+
+                       return comparer.Compare (item1, t.item1);
                }
 
                public override bool Equals (object obj)
@@ -144,9 +150,17 @@ namespace System
                        return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
                }
 
-               [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
+               int IStructuralComparable.CompareTo (object other, IComparer comparer)
                {
-                       throw new NotImplementedException ();
+                       var t = other as Tuple<T1, T2>;
+                       if (t == null) {
+                               if (other == null) return 1;
+                               throw new ArgumentException ();
+                       }
+
+                       int res = comparer.Compare (item1, t.item1);
+                       if (res != 0) return res;
+                       return comparer.Compare (item2, t.item2);
                }
 
                public override bool Equals (object obj)
@@ -215,9 +229,19 @@ namespace System
                        return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
                }
 
-               [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
+               int IStructuralComparable.CompareTo (object other, IComparer comparer)
                {
-                       throw new NotImplementedException ();
+                       var t = other as Tuple<T1, T2, T3>;
+                       if (t == null) {
+                               if (other == null) return 1;
+                               throw new ArgumentException ();
+                       }
+
+                       int res = comparer.Compare (item1, t.item1);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item2, t.item2);
+                       if (res != 0) return res;
+                       return comparer.Compare (item3, t.item3);
                }
 
                public override bool Equals (object obj)
@@ -294,9 +318,21 @@ namespace System
                        return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
                }
 
-               [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
+               int IStructuralComparable.CompareTo (object other, IComparer comparer)
                {
-                       throw new NotImplementedException ();
+                       var t = other as Tuple<T1, T2, T3, T4>;
+                       if (t == null) {
+                               if (other == null) return 1;
+                               throw new ArgumentException ();
+                       }
+
+                       int res = comparer.Compare (item1, t.item1);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item2, t.item2);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item3, t.item3);
+                       if (res != 0) return res;
+                       return comparer.Compare (item4, t.item4);
                }
 
                public override bool Equals (object obj)
@@ -381,9 +417,23 @@ namespace System
                        return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
                }
 
-               [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
+               int IStructuralComparable.CompareTo (object other, IComparer comparer)
                {
-                       throw new NotImplementedException ();
+                       var t = other as Tuple<T1, T2, T3, T4, T5>;
+                       if (t == null) {
+                               if (other == null) return 1;
+                               throw new ArgumentException ();
+                       }
+
+                       int res = comparer.Compare (item1, t.item1);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item2, t.item2);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item3, t.item3);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item4, t.item4);
+                       if (res != 0) return res;
+                       return comparer.Compare (item5, t.item5);
                }
 
                public override bool Equals (object obj)
@@ -476,9 +526,25 @@ namespace System
                        return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
                }
 
-               [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
+               int IStructuralComparable.CompareTo (object other, IComparer comparer)
                {
-                       throw new NotImplementedException ();
+                       var t = other as Tuple<T1, T2, T3, T4, T5, T6>;
+                       if (t == null) {
+                               if (other == null) return 1;
+                               throw new ArgumentException ();
+                       }
+
+                       int res = comparer.Compare (item1, t.item1);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item2, t.item2);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item3, t.item3);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item4, t.item4);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item5, t.item5);
+                       if (res != 0) return res;
+                       return comparer.Compare (item6, t.item6);
                }
 
                public override bool Equals (object obj)
@@ -579,9 +645,27 @@ namespace System
                        return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
                }
 
-               [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
+               int IStructuralComparable.CompareTo (object other, IComparer comparer)
                {
-                       throw new NotImplementedException ();
+                       var t = other as Tuple<T1, T2, T3, T4, T5, T6, T7>;
+                       if (t == null) {
+                               if (other == null) return 1;
+                               throw new ArgumentException ();
+                       }
+
+                       int res = comparer.Compare (item1, t.item1);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item2, t.item2);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item3, t.item3);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item4, t.item4);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item5, t.item5);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item6, t.item6);
+                       if (res != 0) return res;
+                       return comparer.Compare (item7, t.item7);
                }
 
                public override bool Equals (object obj)
@@ -678,9 +762,29 @@ namespace System
                        return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
                }
 
-               [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
+               int IStructuralComparable.CompareTo (object other, IComparer comparer)
                {
-                       throw new NotImplementedException ();
+                       var t = other as Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>;
+                       if (t == null) {
+                               if (other == null) return 1;
+                               throw new ArgumentException ();
+                       }
+
+                       int res = comparer.Compare (item1, t.item1);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item2, t.item2);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item3, t.item3);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item4, t.item4);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item5, t.item5);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item6, t.item6);
+                       if (res != 0) return res;
+                       res = comparer.Compare (item7, t.item7);
+                       if (res != 0) return res;
+                       return comparer.Compare (rest, t.rest);
                }
 
                public override bool Equals (object obj)
@@ -788,10 +892,25 @@ public class TupleGen
                        Console.WriteLine ("\t\t}");
                        
                        Console.WriteLine ();
-                       Console.WriteLine ("\t\t[MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)");
+                       Console.WriteLine ("\t\tint IStructuralComparable.CompareTo (object other, IComparer comparer)");
                        Console.WriteLine ("\t\t{");
-                       Console.WriteLine ("\t\t\tthrow new NotImplementedException ();");
-                       Console.WriteLine ("\t\t}");
+                       Console.WriteLine ("\t\t\tvar t = other as {0};", type_name);
+                       Console.WriteLine ("\t\t\tif (t == null) {");
+                       Console.WriteLine ("\t\t\t\tif (other == null) return 1;");
+                       Console.WriteLine ("\t\t\t\tthrow new ArgumentException ();");
+                       Console.WriteLine ("\t\t\t}");
+                       Console.WriteLine ();
+                       
+                       for (int i = 1; i < arity; ++i) {
+                               Console.Write ("\t\t\t");
+                               if (i == 1)
+                                       Console.Write ("int ");
+
+                               Console.WriteLine ("res = comparer.Compare ({0}, t.{0});", GetItemName (i));
+                               Console.WriteLine ("\t\t\tif (res != 0) return res;");
+                       }
+                       Console.WriteLine ("\t\t\treturn comparer.Compare ({0}, t.{0});", GetItemName (arity));
+                       Console.WriteLine ("\t\t}");                    
                        
                        Console.WriteLine ();
                        Console.WriteLine ("\t\tpublic override bool Equals (object obj)");
@@ -827,7 +946,7 @@ public class TupleGen
                        Console.WriteLine ();
                        Console.WriteLine ("\t\tpublic override int GetHashCode ()");
                        Console.WriteLine ("\t\t{");
-                       Console.WriteLine ("\t\t\treturn GetHashCode (EqualityComparer<object>.Default);");
+                       Console.WriteLine ("\t\t\treturn ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);");
                        Console.WriteLine ("\t\t}");
                        
                        Console.WriteLine ();