5 // Zoltan Varga (vargaz@gmail.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright (C) 2009 Novell
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Generic;
38 public partial class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>
40 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest)
52 if (!typeof (TRest).IsGenericType)
55 Type t = typeof (TRest).GetGenericTypeDefinition ();
56 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<,,,,,,,>)))
60 throw new ArgumentException ("The last element of an eight element tuple must be a Tuple.");
64 /* The rest is generated by the script at the bottom */
67 public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable
71 public Tuple (T1 item1)
80 int IComparable.CompareTo (object obj)
82 return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
85 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
87 throw new NotImplementedException ();
90 public override bool Equals (object obj)
92 return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
95 bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
97 var t = other as Tuple<T1>;
99 if (other == null) return false;
100 throw new ArgumentException ();
103 return comparer.Equals (item1, t.item1);
106 public override int GetHashCode ()
108 return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
111 int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
113 return comparer.GetHashCode (item1);
116 public override string ToString ()
118 return String.Format ("({0})", item1);
123 public class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable
128 public Tuple (T1 item1, T2 item2)
135 get { return item1; }
139 get { return item2; }
142 int IComparable.CompareTo (object obj)
144 return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
147 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
149 throw new NotImplementedException ();
152 public override bool Equals (object obj)
154 return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
157 bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
159 var t = other as Tuple<T1, T2>;
161 if (other == null) return false;
162 throw new ArgumentException ();
165 return comparer.Equals (item1, t.item1) &&
166 comparer.Equals (item2, t.item2);
169 public override int GetHashCode ()
171 return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
174 int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
176 int h = comparer.GetHashCode (item1);
177 h = (h << 5) - h + comparer.GetHashCode (item2);
181 public override string ToString ()
183 return String.Format ("({0}, {1})", item1, item2);
188 public class Tuple<T1, T2, T3> : IStructuralEquatable, IStructuralComparable, IComparable
194 public Tuple (T1 item1, T2 item2, T3 item3)
202 get { return item1; }
206 get { return item2; }
210 get { return item3; }
213 int IComparable.CompareTo (object obj)
215 return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
218 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
220 throw new NotImplementedException ();
223 public override bool Equals (object obj)
225 return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
228 bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
230 var t = other as Tuple<T1, T2, T3>;
232 if (other == null) return false;
233 throw new ArgumentException ();
236 return comparer.Equals (item1, t.item1) &&
237 comparer.Equals (item2, t.item2) &&
238 comparer.Equals (item3, t.item3);
241 public override int GetHashCode ()
243 return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
246 int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
248 int h = comparer.GetHashCode (item1);
249 h = (h << 5) - h + comparer.GetHashCode (item2);
250 h = (h << 5) - h + comparer.GetHashCode (item3);
254 public override string ToString ()
256 return String.Format ("({0}, {1}, {2})", item1, item2, item3);
261 public class Tuple<T1, T2, T3, T4> : IStructuralEquatable, IStructuralComparable, IComparable
268 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4)
277 get { return item1; }
281 get { return item2; }
285 get { return item3; }
289 get { return item4; }
292 int IComparable.CompareTo (object obj)
294 return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
297 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
299 throw new NotImplementedException ();
302 public override bool Equals (object obj)
304 return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
307 bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
309 var t = other as Tuple<T1, T2, T3, T4>;
311 if (other == null) return false;
312 throw new ArgumentException ();
315 return comparer.Equals (item1, t.item1) &&
316 comparer.Equals (item2, t.item2) &&
317 comparer.Equals (item3, t.item3) &&
318 comparer.Equals (item4, t.item4);
321 public override int GetHashCode ()
323 return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
326 int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
328 int h = comparer.GetHashCode (item1);
329 h = (h << 5) - h + comparer.GetHashCode (item2);
330 h = (h << 5) - h + comparer.GetHashCode (item3);
331 h = (h << 5) - h + comparer.GetHashCode (item4);
335 public override string ToString ()
337 return String.Format ("({0}, {1}, {2}, {3})", item1, item2, item3, item4);
342 public class Tuple<T1, T2, T3, T4, T5> : IStructuralEquatable, IStructuralComparable, IComparable
350 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
360 get { return item1; }
364 get { return item2; }
368 get { return item3; }
372 get { return item4; }
376 get { return item5; }
379 int IComparable.CompareTo (object obj)
381 return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
384 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
386 throw new NotImplementedException ();
389 public override bool Equals (object obj)
391 return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
394 bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
396 var t = other as Tuple<T1, T2, T3, T4, T5>;
398 if (other == null) return false;
399 throw new ArgumentException ();
402 return comparer.Equals (item1, t.item1) &&
403 comparer.Equals (item2, t.item2) &&
404 comparer.Equals (item3, t.item3) &&
405 comparer.Equals (item4, t.item4) &&
406 comparer.Equals (item5, t.item5);
409 public override int GetHashCode ()
411 return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
414 int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
416 int h = comparer.GetHashCode (item1);
417 h = (h << 5) - h + comparer.GetHashCode (item2);
418 h = (h << 5) - h + comparer.GetHashCode (item3);
419 h = (h << 5) - h + comparer.GetHashCode (item4);
420 h = (h << 5) - h + comparer.GetHashCode (item5);
424 public override string ToString ()
426 return String.Format ("({0}, {1}, {2}, {3}, {4})", item1, item2, item3, item4, item5);
431 public class Tuple<T1, T2, T3, T4, T5, T6> : IStructuralEquatable, IStructuralComparable, IComparable
440 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
451 get { return item1; }
455 get { return item2; }
459 get { return item3; }
463 get { return item4; }
467 get { return item5; }
471 get { return item6; }
474 int IComparable.CompareTo (object obj)
476 return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
479 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
481 throw new NotImplementedException ();
484 public override bool Equals (object obj)
486 return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
489 bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
491 var t = other as Tuple<T1, T2, T3, T4, T5, T6>;
493 if (other == null) return false;
494 throw new ArgumentException ();
497 return comparer.Equals (item1, t.item1) &&
498 comparer.Equals (item2, t.item2) &&
499 comparer.Equals (item3, t.item3) &&
500 comparer.Equals (item4, t.item4) &&
501 comparer.Equals (item5, t.item5) &&
502 comparer.Equals (item6, t.item6);
505 public override int GetHashCode ()
507 return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
510 int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
512 int h = comparer.GetHashCode (item1);
513 h = (h << 5) - h + comparer.GetHashCode (item2);
514 h = (h << 5) - h + comparer.GetHashCode (item3);
515 h = (h << 5) - h + comparer.GetHashCode (item4);
516 h = (h << 5) - h + comparer.GetHashCode (item5);
517 h = (h << 5) - h + comparer.GetHashCode (item6);
521 public override string ToString ()
523 return String.Format ("({0}, {1}, {2}, {3}, {4}, {5})", item1, item2, item3, item4, item5, item6);
528 public class Tuple<T1, T2, T3, T4, T5, T6, T7> : IStructuralEquatable, IStructuralComparable, IComparable
538 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
550 get { return item1; }
554 get { return item2; }
558 get { return item3; }
562 get { return item4; }
566 get { return item5; }
570 get { return item6; }
574 get { return item7; }
577 int IComparable.CompareTo (object obj)
579 return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
582 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
584 throw new NotImplementedException ();
587 public override bool Equals (object obj)
589 return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
592 bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
594 var t = other as Tuple<T1, T2, T3, T4, T5, T6, T7>;
596 if (other == null) return false;
597 throw new ArgumentException ();
600 return comparer.Equals (item1, t.item1) &&
601 comparer.Equals (item2, t.item2) &&
602 comparer.Equals (item3, t.item3) &&
603 comparer.Equals (item4, t.item4) &&
604 comparer.Equals (item5, t.item5) &&
605 comparer.Equals (item6, t.item6) &&
606 comparer.Equals (item7, t.item7);
609 public override int GetHashCode ()
611 return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
614 int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
616 int h = comparer.GetHashCode (item1);
617 h = (h << 5) - h + comparer.GetHashCode (item2);
618 h = (h << 5) - h + comparer.GetHashCode (item3);
619 h = (h << 5) - h + comparer.GetHashCode (item4);
620 h = (h << 5) - h + comparer.GetHashCode (item5);
621 h = (h << 5) - h + comparer.GetHashCode (item6);
622 h = (h << 5) - h + comparer.GetHashCode (item7);
626 public override string ToString ()
628 return String.Format ("({0}, {1}, {2}, {3}, {4}, {5}, {6})", item1, item2, item3, item4, item5, item6, item7);
633 public partial class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> : IStructuralEquatable, IStructuralComparable, IComparable
645 get { return item1; }
649 get { return item2; }
653 get { return item3; }
657 get { return item4; }
661 get { return item5; }
665 get { return item6; }
669 get { return item7; }
676 int IComparable.CompareTo (object obj)
678 return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
681 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)
683 throw new NotImplementedException ();
686 public override bool Equals (object obj)
688 return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
691 bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
693 var t = other as Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>;
695 if (other == null) return false;
696 throw new ArgumentException ();
699 return comparer.Equals (item1, t.item1) &&
700 comparer.Equals (item2, t.item2) &&
701 comparer.Equals (item3, t.item3) &&
702 comparer.Equals (item4, t.item4) &&
703 comparer.Equals (item5, t.item5) &&
704 comparer.Equals (item6, t.item6) &&
705 comparer.Equals (item7, t.item7) &&
706 comparer.Equals (rest, t.rest);
709 public override int GetHashCode ()
711 return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
714 int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
716 int h = comparer.GetHashCode (item1);
717 h = (h << 5) - h + comparer.GetHashCode (item2);
718 h = (h << 5) - h + comparer.GetHashCode (item3);
719 h = (h << 5) - h + comparer.GetHashCode (item4);
720 h = (h << 5) - h + comparer.GetHashCode (item5);
721 h = (h << 5) - h + comparer.GetHashCode (item6);
722 h = (h << 5) - h + comparer.GetHashCode (item7);
723 h = (h << 5) - h + comparer.GetHashCode (rest);
727 public override string ToString ()
729 return String.Format ("({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})", item1, item2, item3, item4, item5, item6, item7, rest);
746 public class TupleGen
748 public static void Main () {
749 for (int arity = 1; arity < 9; ++arity) {
750 string type_name = GetTypeName (arity);
752 Console.WriteLine ("\t[Serializable]");
753 Console.Write ("\tpublic {0}class ", arity < 8 ? null : "partial ");
754 Console.Write (type_name);
755 Console.WriteLine (" : IStructuralEquatable, IStructuralComparable, IComparable");
756 Console.WriteLine ("\t{");
757 for (int i = 1; i <= arity; ++i)
758 Console.WriteLine ("\t\t{0} {1};", GetItemTypeName (i), GetItemName (i));
761 Console.WriteLine ();
762 Console.Write ("\t\tpublic Tuple (");
763 for (int i = 1; i <= arity; ++i) {
764 Console.Write ("{0} {1}", GetItemTypeName (i), GetItemName (i));
766 Console.Write (", ");
768 Console.WriteLine (")");
769 Console.WriteLine ("\t\t{");
770 for (int i = 1; i <= arity; ++i)
771 Console.WriteLine ("\t\t\t this.{0} = {0};", GetItemName (i));
772 Console.WriteLine ("\t\t}");
775 for (int i = 1; i <= arity; ++i) {
776 Console.WriteLine ();
777 Console.WriteLine ("\t\tpublic {0} {1} {{", GetItemTypeName (i),
778 System.Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase (GetItemName (i)));
779 Console.Write ("\t\t\tget { ");
780 Console.WriteLine ("return {0}; }}", GetItemName (i));
781 Console.WriteLine ("\t\t}");
784 Console.WriteLine ();
785 Console.WriteLine ("\t\tint IComparable.CompareTo (object obj)");
786 Console.WriteLine ("\t\t{");
787 Console.WriteLine ("\t\t\treturn ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);");
788 Console.WriteLine ("\t\t}");
790 Console.WriteLine ();
791 Console.WriteLine ("\t\t[MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer)");
792 Console.WriteLine ("\t\t{");
793 Console.WriteLine ("\t\t\tthrow new NotImplementedException ();");
794 Console.WriteLine ("\t\t}");
796 Console.WriteLine ();
797 Console.WriteLine ("\t\tpublic override bool Equals (object obj)");
798 Console.WriteLine ("\t\t{");
799 Console.WriteLine ("\t\t\treturn ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);");
800 Console.WriteLine ("\t\t}");
802 Console.WriteLine ();
803 Console.WriteLine ("\t\tbool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)");
804 Console.WriteLine ("\t\t{");
805 Console.WriteLine ("\t\t\tvar t = other as {0};", type_name);
806 Console.WriteLine ("\t\t\tif (t == null) {");
807 Console.WriteLine ("\t\t\t\tif (other == null) return false;");
808 Console.WriteLine ("\t\t\t\tthrow new ArgumentException ();");
809 Console.WriteLine ("\t\t\t}");
810 Console.WriteLine ();
811 Console.Write ("\t\t\treturn");
813 for (int i = 1; i <= arity; ++i) {
817 Console.Write ("\t\t\t\t");
819 Console.Write ("comparer.Equals ({0}, t.{0})", GetItemName (i));
821 Console.WriteLine (" &&");
823 Console.WriteLine (";");
825 Console.WriteLine ("\t\t}");
827 Console.WriteLine ();
828 Console.WriteLine ("\t\tpublic override int GetHashCode ()");
829 Console.WriteLine ("\t\t{");
830 Console.WriteLine ("\t\t\treturn GetHashCode (EqualityComparer<object>.Default);");
831 Console.WriteLine ("\t\t}");
833 Console.WriteLine ();
834 Console.WriteLine ("\t\tint IStructuralEquatable.GetHashCode (IEqualityComparer comparer)");
835 Console.WriteLine ("\t\t{");
837 Console.WriteLine ("\t\t\treturn comparer.GetHashCode ({0});", GetItemName (arity));
839 Console.WriteLine ("\t\t\tint h = comparer.GetHashCode ({0});", GetItemName (1));
840 for (int i = 2; i <= arity; ++i)
841 Console.WriteLine ("\t\t\th = (h << 5) - h + comparer.GetHashCode ({0});", GetItemName (i));
842 Console.WriteLine ("\t\t\treturn h;");
845 Console.WriteLine ("\t\t}");
847 Console.WriteLine ();
848 Console.WriteLine ("\t\tpublic override string ToString ()");
849 Console.WriteLine ("\t\t{");
850 Console.Write ("\t\t\treturn String.Format (\"(");
851 for (int i = 1; i <= arity; ++i) {
852 Console.Write ("{" + (i - 1) + "}");
854 Console.Write (", ");
856 Console.Write (")\", ");
857 for (int i = 1; i <= arity; ++i) {
858 Console.Write (GetItemName (i));
860 Console.Write (", ");
862 Console.WriteLine (");");
863 Console.WriteLine ("\t\t}");
865 Console.WriteLine ("\t}\n");
869 static string GetTypeName (int arity)
871 StringBuilder sb = new StringBuilder ();
872 sb.Append ("Tuple<");
873 for (int i = 1; i <= arity; ++i) {
874 sb.Append (GetItemTypeName (i));
880 return sb.ToString ();
883 static string GetItemName (int arity)
885 return arity < 8 ? "item" + arity.ToString () : "rest";
888 static string GetItemTypeName (int arity)
890 return arity < 8 ? "T" + arity.ToString () : "TRest";