Merge pull request #820 from brendanzagaeski/master
[mono.git] / mcs / class / corlib / System / Tuples.cs
index 27604d5307ba623babaf0cd419ae2125de7eca7f..a9631dbbdaf01de52c7fe1d8c201fbc60bc1d72f 100644 (file)
@@ -48,23 +48,20 @@ namespace System
                        this.item7 = item7;
                        this.rest = rest;
 
-                       bool ok = true;
-                       if (!typeof (TRest).IsGenericType)
-                               ok = false;
-                       if (ok) {
-                               Type t = typeof (TRest).GetGenericTypeDefinition ();
-                               if (!(t == typeof (Tuple<>) || t == typeof (Tuple<,>) || t == typeof (Tuple<,,>) || t == typeof (Tuple<,,,>) || t == typeof (Tuple<,,,,>) || t == typeof (Tuple <,,,,,>) || t == typeof (Tuple<,,,,,,>) || t == typeof (Tuple<,,,,,,,>)))
-                                       ok = false;
-                       }
-                       if (!ok)
+                       if (!(rest is ITuple))
                                throw new ArgumentException ("rest", "The last element of an eight element tuple must be a Tuple.");
                }
        }
 
+       interface ITuple
+       {
+               string ToString ();
+       }
+
        /* The rest is generated by the script at the bottom */
 
        [Serializable]
-       public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable
+       public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
        {
                T1 item1;
 
@@ -117,14 +114,19 @@ namespace System
                        return comparer.GetHashCode (item1);
                }
 
+               string ITuple.ToString ()
+               {
+                       return String.Format ("{0}", item1);
+               }
+
                public override string ToString ()
                {
-                       return String.Format ("({0})", item1);
+                       return "(" + ((ITuple) this).ToString () + ")";
                }
        }
 
        [Serializable]
-       public class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable
+       public class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
        {
                T1 item1;
                T2 item2;
@@ -189,14 +191,19 @@ namespace System
                        return h0;
                }
 
+               string ITuple.ToString ()
+               {
+                       return String.Format ("{0}, {1}", item1, item2);
+               }
+
                public override string ToString ()
                {
-                       return String.Format ("({0}, {1})", item1, item2);
+                       return "(" + ((ITuple) this).ToString () + ")";
                }
        }
 
        [Serializable]
-       public class Tuple<T1, T2, T3> : IStructuralEquatable, IStructuralComparable, IComparable
+       public class Tuple<T1, T2, T3> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
        {
                T1 item1;
                T2 item2;
@@ -271,14 +278,19 @@ namespace System
                        return h0;
                }
 
+               string ITuple.ToString ()
+               {
+                       return String.Format ("{0}, {1}, {2}", item1, item2, item3);
+               }
+
                public override string ToString ()
                {
-                       return String.Format ("({0}, {1}, {2})", item1, item2, item3);
+                       return "(" + ((ITuple) this).ToString () + ")";
                }
        }
 
        [Serializable]
-       public class Tuple<T1, T2, T3, T4> : IStructuralEquatable, IStructuralComparable, IComparable
+       public class Tuple<T1, T2, T3, T4> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
        {
                T1 item1;
                T2 item2;
@@ -364,14 +376,19 @@ namespace System
                        return h0;
                }
 
+               string ITuple.ToString ()
+               {
+                       return String.Format ("{0}, {1}, {2}, {3}", item1, item2, item3, item4);
+               }
+
                public override string ToString ()
                {
-                       return String.Format ("({0}, {1}, {2}, {3})", item1, item2, item3, item4);
+                       return "(" + ((ITuple) this).ToString () + ")";
                }
        }
 
        [Serializable]
-       public class Tuple<T1, T2, T3, T4, T5> : IStructuralEquatable, IStructuralComparable, IComparable
+       public class Tuple<T1, T2, T3, T4, T5> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
        {
                T1 item1;
                T2 item2;
@@ -467,14 +484,19 @@ namespace System
                        return h0;
                }
 
+               string ITuple.ToString ()
+               {
+                       return String.Format ("{0}, {1}, {2}, {3}, {4}", item1, item2, item3, item4, item5);
+               }
+
                public override string ToString ()
                {
-                       return String.Format ("({0}, {1}, {2}, {3}, {4})", item1, item2, item3, item4, item5);
+                       return "(" + ((ITuple) this).ToString () + ")";
                }
        }
 
        [Serializable]
-       public class Tuple<T1, T2, T3, T4, T5, T6> : IStructuralEquatable, IStructuralComparable, IComparable
+       public class Tuple<T1, T2, T3, T4, T5, T6> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
        {
                T1 item1;
                T2 item2;
@@ -581,14 +603,19 @@ namespace System
                        return h0;
                }
 
+               string ITuple.ToString ()
+               {
+                       return String.Format ("{0}, {1}, {2}, {3}, {4}, {5}", item1, item2, item3, item4, item5, item6);
+               }
+
                public override string ToString ()
                {
-                       return String.Format ("({0}, {1}, {2}, {3}, {4}, {5})", item1, item2, item3, item4, item5, item6);
+                       return "(" + ((ITuple) this).ToString () + ")";
                }
        }
 
        [Serializable]
-       public class Tuple<T1, T2, T3, T4, T5, T6, T7> : IStructuralEquatable, IStructuralComparable, IComparable
+       public class Tuple<T1, T2, T3, T4, T5, T6, T7> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
        {
                T1 item1;
                T2 item2;
@@ -705,14 +732,19 @@ namespace System
                        return h0;
                }
 
+               string ITuple.ToString ()
+               {
+                       return String.Format ("{0}, {1}, {2}, {3}, {4}, {5}, {6}", item1, item2, item3, item4, item5, item6, item7);
+               }
+
                public override string ToString ()
                {
-                       return String.Format ("({0}, {1}, {2}, {3}, {4}, {5}, {6})", item1, item2, item3, item4, item5, item6, item7);
+                       return "(" + ((ITuple) this).ToString () + ")";
                }
        }
 
        [Serializable]
-       public partial class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> : IStructuralEquatable, IStructuralComparable, IComparable
+       public partial class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> : IStructuralEquatable, IStructuralComparable, IComparable, ITuple
        {
                T1 item1;
                T2 item2;
@@ -828,9 +860,14 @@ namespace System
                        return h0;
                }
 
+               string ITuple.ToString ()
+               {
+                       return String.Format ("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}", item1, item2, item3, item4, item5, item6, item7, ((ITuple)rest).ToString ());
+               }
+
                public override string ToString ()
                {
-                       return String.Format ("({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})", item1, item2, item3, item4, item5, item6, item7, rest);
+                       return "(" + ((ITuple) this).ToString () + ")";
                }
        }
 
@@ -856,11 +893,11 @@ public class TupleGen
                        Console.WriteLine ("\t[Serializable]");
                        Console.Write ("\tpublic {0}class ", arity < 8 ? null : "partial ");
                        Console.Write (type_name);
-                       Console.WriteLine (" : IStructuralEquatable, IStructuralComparable, IComparable");
+                       Console.WriteLine (" : IStructuralEquatable, IStructuralComparable, IComparable, ITuple");
                        Console.WriteLine ("\t{");
                        for (int i = 1; i <= arity; ++i)
                                Console.WriteLine ("\t\t{0} {1};", GetItemTypeName (i), GetItemName (i));
-                               
+
                        if (arity < 8) {
                                Console.WriteLine ();
                                Console.Write ("\t\tpublic Tuple (");
@@ -890,7 +927,7 @@ public class TupleGen
                        Console.WriteLine ("\t\t{");
                        Console.WriteLine ("\t\t\treturn ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);");
                        Console.WriteLine ("\t\t}");
-                       
+
                        Console.WriteLine ();
                        Console.WriteLine ("\t\tint IStructuralComparable.CompareTo (object other, IComparer comparer)");
                        Console.WriteLine ("\t\t{");
@@ -900,7 +937,7 @@ public class TupleGen
                        Console.WriteLine ("\t\t\t\tthrow new ArgumentException (\"other\");");
                        Console.WriteLine ("\t\t\t}");
                        Console.WriteLine ();
-                       
+
                        for (int i = 1; i < arity; ++i) {
                                Console.Write ("\t\t\t");
                                if (i == 1)
@@ -910,14 +947,14 @@ public class TupleGen
                                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 ("\t\t}");
+
                        Console.WriteLine ();
                        Console.WriteLine ("\t\tpublic override bool Equals (object obj)");
                        Console.WriteLine ("\t\t{");
                        Console.WriteLine ("\t\t\treturn ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);");
                        Console.WriteLine ("\t\t}");
-                       
+
                        Console.WriteLine ();
                        Console.WriteLine ("\t\tbool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)");
                        Console.WriteLine ("\t\t{");
@@ -926,7 +963,7 @@ public class TupleGen
                        Console.WriteLine ("\t\t\t\treturn false;");
                        Console.WriteLine ();
                        Console.Write ("\t\t\treturn");
-                       
+
                        for (int i = 1; i <= arity; ++i) {
                                if (i == 1)
                                        Console.Write (" ");
@@ -940,26 +977,26 @@ public class TupleGen
                                        Console.WriteLine (";");
                        }
                        Console.WriteLine ("\t\t}");
-                       
+
                        Console.WriteLine ();
                        Console.WriteLine ("\t\tpublic override int GetHashCode ()");
                        Console.WriteLine ("\t\t{");
                        Console.WriteLine ("\t\t\treturn ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);");
                        Console.WriteLine ("\t\t}");
-                       
+
                        Console.WriteLine ();
                        Console.WriteLine ("\t\tint IStructuralEquatable.GetHashCode (IEqualityComparer comparer)");
                        Console.WriteLine ("\t\t{");
                        if (arity == 1) {
                                Console.WriteLine ("\t\t\treturn comparer.GetHashCode ({0});", GetItemName (arity));
                        } else {
-                               int varnum = IntLog2(arity);
+                               int varnum = IntLog2 (arity);
                                Console.Write ("\t\t\tint h0");
                                for (int i = 1; i < varnum; ++i)
                                        Console.Write (", h{0}", i);
                                Console.WriteLine (";");
 
-                               WriteHash(0, 1, arity);
+                               WriteHash (0, 1, arity);
 
                                Console.WriteLine ("\t\t\treturn h0;");
                        }
@@ -967,23 +1004,30 @@ public class TupleGen
                        Console.WriteLine ("\t\t}");
 
                        Console.WriteLine ();
-                       Console.WriteLine ("\t\tpublic override string ToString ()");
+                       Console.WriteLine ("\t\tstring ITuple.ToString ()");
                        Console.WriteLine ("\t\t{");
-                       Console.Write ("\t\t\treturn String.Format (\"(");
+                       Console.Write ("\t\t\treturn String.Format (\"");
                        for (int i = 1; i <= arity; ++i) {
                                Console.Write ("{" + (i - 1) + "}");
                                if (i < arity)
                                        Console.Write (", ");
                        }
-                       Console.Write (")\", ");
+                       Console.Write ("\", ");
                        for (int i = 1; i <= arity; ++i) {
-                               Console.Write (GetItemName (i));
+                               var item_name = GetItemName (i);
+                               Console.Write (i == 8 ? "((ITuple){0}).ToString ()" : "{0}", item_name);
                                if (i < arity)
                                        Console.Write (", ");
                        }
                        Console.WriteLine (");");
                        Console.WriteLine ("\t\t}");
 
+                       Console.WriteLine ();
+                       Console.WriteLine ("\t\tpublic override string ToString ()");
+                       Console.WriteLine ("\t\t{");
+                       Console.WriteLine ("\t\t\treturn \"(\" + ((ITuple) this).ToString () + \")\";");
+                       Console.WriteLine ("\t\t}");
+
                        Console.WriteLine ("\t}\n");
                }
        }
@@ -1005,15 +1049,15 @@ public class TupleGen
                if (count == 1) {
                        Console.WriteLine ("\t\t\th{0} = comparer.GetHashCode ({1});", destVar, GetItemName (start));
                } else {
-                       int subCount = 1 << IntLog2(count-1);
-                       computeHash(destVar, start, subCount);
+                       int subCount = 1 << IntLog2 (count - 1);
+                       WriteHash (destVar, start, subCount);
                        start += subCount;
                        count -= subCount;
                        if (count == 1) {
                                Console.WriteLine ("\t\t\th{0} = (h{0} << 5) + h{0} ^ comparer.GetHashCode ({1});", destVar, GetItemName (start));
                        } else {
-                               WriteHash(destVar+1, start, count);
-                               Console.WriteLine ("\t\t\th{0} = (h{0} << 5) + h{0} ^ h{1};", destVar, destVar+1);
+                               WriteHash (destVar + 1, start, count);
+                               Console.WriteLine ("\t\t\th{0} = (h{0} << 5) + h{0} ^ h{1};", destVar, destVar + 1);
                        }
                }
        }
@@ -1028,19 +1072,20 @@ public class TupleGen
                                sb.Append (", ");
                }
                sb.Append (">");
-               
+
                return sb.ToString ();
        }
-       
+
        static string GetItemName (int arity)
        {
                return arity < 8 ? "item" + arity.ToString () : "rest";
        }
-       
+
        static string GetItemTypeName (int arity)
        {
                return arity < 8 ? "T" + arity.ToString () : "TRest";
        }
 }
 
+
 #endif