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 public int CompareTo (object obj)
82 return CompareTo (obj, Comparer<object>.Default);
85 [MonoTODO] public int CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
87 public override bool Equals (object obj)
89 return Equals (obj, EqualityComparer<object>.Default);
92 public bool Equals (object other, IEqualityComparer comparer)
94 var t = other as Tuple<T1>;
96 if (other == null) return false;
97 throw new ArgumentException ();
100 return comparer.Equals (item1, t.item1);
103 public override int GetHashCode ()
105 return GetHashCode (EqualityComparer<object>.Default);
108 public int GetHashCode (IEqualityComparer comparer)
110 return comparer.GetHashCode (item1);
113 public override string ToString ()
115 return String.Format ("({0})", item1);
120 public class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable
125 public Tuple (T1 item1, T2 item2)
132 get { return item1; }
136 get { return item2; }
139 public int CompareTo (object obj)
141 return CompareTo (obj, Comparer<object>.Default);
144 [MonoTODO] public int CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
146 public override bool Equals (object obj)
148 return Equals (obj, EqualityComparer<object>.Default);
151 public bool Equals (object other, IEqualityComparer comparer)
153 var t = other as Tuple<T1, T2>;
155 if (other == null) return false;
156 throw new ArgumentException ();
159 return comparer.Equals (item1, t.item1) &&
160 comparer.Equals (item2, t.item2);
163 public override int GetHashCode ()
165 return GetHashCode (EqualityComparer<object>.Default);
168 public int GetHashCode (IEqualityComparer comparer)
170 int h = comparer.GetHashCode (item1);
171 h = (h << 5) - h + comparer.GetHashCode (item2);
175 public override string ToString ()
177 return String.Format ("({0}, {1})", item1, item2);
182 public class Tuple<T1, T2, T3> : IStructuralEquatable, IStructuralComparable, IComparable
188 public Tuple (T1 item1, T2 item2, T3 item3)
196 get { return item1; }
200 get { return item2; }
204 get { return item3; }
207 public int CompareTo (object obj)
209 return CompareTo (obj, Comparer<object>.Default);
212 [MonoTODO] public int CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
214 public override bool Equals (object obj)
216 return Equals (obj, EqualityComparer<object>.Default);
219 public bool Equals (object other, IEqualityComparer comparer)
221 var t = other as Tuple<T1, T2, T3>;
223 if (other == null) return false;
224 throw new ArgumentException ();
227 return comparer.Equals (item1, t.item1) &&
228 comparer.Equals (item2, t.item2) &&
229 comparer.Equals (item3, t.item3);
232 public override int GetHashCode ()
234 return GetHashCode (EqualityComparer<object>.Default);
237 public int GetHashCode (IEqualityComparer comparer)
239 int h = comparer.GetHashCode (item1);
240 h = (h << 5) - h + comparer.GetHashCode (item2);
241 h = (h << 5) - h + comparer.GetHashCode (item3);
245 public override string ToString ()
247 return String.Format ("({0}, {1}, {2})", item1, item2, item3);
252 public class Tuple<T1, T2, T3, T4> : IStructuralEquatable, IStructuralComparable, IComparable
259 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4)
268 get { return item1; }
272 get { return item2; }
276 get { return item3; }
280 get { return item4; }
283 public int CompareTo (object obj)
285 return CompareTo (obj, Comparer<object>.Default);
288 [MonoTODO] public int CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
290 public override bool Equals (object obj)
292 return Equals (obj, EqualityComparer<object>.Default);
295 public bool Equals (object other, IEqualityComparer comparer)
297 var t = other as Tuple<T1, T2, T3, T4>;
299 if (other == null) return false;
300 throw new ArgumentException ();
303 return comparer.Equals (item1, t.item1) &&
304 comparer.Equals (item2, t.item2) &&
305 comparer.Equals (item3, t.item3) &&
306 comparer.Equals (item4, t.item4);
309 public override int GetHashCode ()
311 return GetHashCode (EqualityComparer<object>.Default);
314 public int GetHashCode (IEqualityComparer comparer)
316 int h = comparer.GetHashCode (item1);
317 h = (h << 5) - h + comparer.GetHashCode (item2);
318 h = (h << 5) - h + comparer.GetHashCode (item3);
319 h = (h << 5) - h + comparer.GetHashCode (item4);
323 public override string ToString ()
325 return String.Format ("({0}, {1}, {2}, {3})", item1, item2, item3, item4);
330 public class Tuple<T1, T2, T3, T4, T5> : IStructuralEquatable, IStructuralComparable, IComparable
338 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
348 get { return item1; }
352 get { return item2; }
356 get { return item3; }
360 get { return item4; }
364 get { return item5; }
367 public int CompareTo (object obj)
369 return CompareTo (obj, Comparer<object>.Default);
372 [MonoTODO] public int CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
374 public override bool Equals (object obj)
376 return Equals (obj, EqualityComparer<object>.Default);
379 public bool Equals (object other, IEqualityComparer comparer)
381 var t = other as Tuple<T1, T2, T3, T4, T5>;
383 if (other == null) return false;
384 throw new ArgumentException ();
387 return comparer.Equals (item1, t.item1) &&
388 comparer.Equals (item2, t.item2) &&
389 comparer.Equals (item3, t.item3) &&
390 comparer.Equals (item4, t.item4) &&
391 comparer.Equals (item5, t.item5);
394 public override int GetHashCode ()
396 return GetHashCode (EqualityComparer<object>.Default);
399 public int GetHashCode (IEqualityComparer comparer)
401 int h = comparer.GetHashCode (item1);
402 h = (h << 5) - h + comparer.GetHashCode (item2);
403 h = (h << 5) - h + comparer.GetHashCode (item3);
404 h = (h << 5) - h + comparer.GetHashCode (item4);
405 h = (h << 5) - h + comparer.GetHashCode (item5);
409 public override string ToString ()
411 return String.Format ("({0}, {1}, {2}, {3}, {4})", item1, item2, item3, item4, item5);
416 public class Tuple<T1, T2, T3, T4, T5, T6> : IStructuralEquatable, IStructuralComparable, IComparable
425 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
436 get { return item1; }
440 get { return item2; }
444 get { return item3; }
448 get { return item4; }
452 get { return item5; }
456 get { return item6; }
459 public int CompareTo (object obj)
461 return CompareTo (obj, Comparer<object>.Default);
464 [MonoTODO] public int CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
466 public override bool Equals (object obj)
468 return Equals (obj, EqualityComparer<object>.Default);
471 public bool Equals (object other, IEqualityComparer comparer)
473 var t = other as Tuple<T1, T2, T3, T4, T5, T6>;
475 if (other == null) return false;
476 throw new ArgumentException ();
479 return comparer.Equals (item1, t.item1) &&
480 comparer.Equals (item2, t.item2) &&
481 comparer.Equals (item3, t.item3) &&
482 comparer.Equals (item4, t.item4) &&
483 comparer.Equals (item5, t.item5) &&
484 comparer.Equals (item6, t.item6);
487 public override int GetHashCode ()
489 return GetHashCode (EqualityComparer<object>.Default);
492 public int GetHashCode (IEqualityComparer comparer)
494 int h = comparer.GetHashCode (item1);
495 h = (h << 5) - h + comparer.GetHashCode (item2);
496 h = (h << 5) - h + comparer.GetHashCode (item3);
497 h = (h << 5) - h + comparer.GetHashCode (item4);
498 h = (h << 5) - h + comparer.GetHashCode (item5);
499 h = (h << 5) - h + comparer.GetHashCode (item6);
503 public override string ToString ()
505 return String.Format ("({0}, {1}, {2}, {3}, {4}, {5})", item1, item2, item3, item4, item5, item6);
510 public class Tuple<T1, T2, T3, T4, T5, T6, T7> : IStructuralEquatable, IStructuralComparable, IComparable
520 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
532 get { return item1; }
536 get { return item2; }
540 get { return item3; }
544 get { return item4; }
548 get { return item5; }
552 get { return item6; }
556 get { return item7; }
559 public int CompareTo (object obj)
561 return CompareTo (obj, Comparer<object>.Default);
564 [MonoTODO] public int CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
566 public override bool Equals (object obj)
568 return Equals (obj, EqualityComparer<object>.Default);
571 public bool Equals (object other, IEqualityComparer comparer)
573 var t = other as Tuple<T1, T2, T3, T4, T5, T6, T7>;
575 if (other == null) return false;
576 throw new ArgumentException ();
579 return comparer.Equals (item1, t.item1) &&
580 comparer.Equals (item2, t.item2) &&
581 comparer.Equals (item3, t.item3) &&
582 comparer.Equals (item4, t.item4) &&
583 comparer.Equals (item5, t.item5) &&
584 comparer.Equals (item6, t.item6) &&
585 comparer.Equals (item7, t.item7);
588 public override int GetHashCode ()
590 return GetHashCode (EqualityComparer<object>.Default);
593 public int GetHashCode (IEqualityComparer comparer)
595 int h = comparer.GetHashCode (item1);
596 h = (h << 5) - h + comparer.GetHashCode (item2);
597 h = (h << 5) - h + comparer.GetHashCode (item3);
598 h = (h << 5) - h + comparer.GetHashCode (item4);
599 h = (h << 5) - h + comparer.GetHashCode (item5);
600 h = (h << 5) - h + comparer.GetHashCode (item6);
601 h = (h << 5) - h + comparer.GetHashCode (item7);
605 public override string ToString ()
607 return String.Format ("({0}, {1}, {2}, {3}, {4}, {5}, {6})", item1, item2, item3, item4, item5, item6, item7);
612 public partial class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> : IStructuralEquatable, IStructuralComparable, IComparable
624 get { return item1; }
628 get { return item2; }
632 get { return item3; }
636 get { return item4; }
640 get { return item5; }
644 get { return item6; }
648 get { return item7; }
655 public int CompareTo (object obj)
657 return CompareTo (obj, Comparer<object>.Default);
660 [MonoTODO] public int CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
662 public override bool Equals (object obj)
664 return Equals (obj, EqualityComparer<object>.Default);
667 public bool Equals (object other, IEqualityComparer comparer)
669 var t = other as Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>;
671 if (other == null) return false;
672 throw new ArgumentException ();
675 return comparer.Equals (item1, t.item1) &&
676 comparer.Equals (item2, t.item2) &&
677 comparer.Equals (item3, t.item3) &&
678 comparer.Equals (item4, t.item4) &&
679 comparer.Equals (item5, t.item5) &&
680 comparer.Equals (item6, t.item6) &&
681 comparer.Equals (item7, t.item7) &&
682 comparer.Equals (rest, t.rest);
685 public override int GetHashCode ()
687 return GetHashCode (EqualityComparer<object>.Default);
690 public int GetHashCode (IEqualityComparer comparer)
692 int h = comparer.GetHashCode (item1);
693 h = (h << 5) - h + comparer.GetHashCode (item2);
694 h = (h << 5) - h + comparer.GetHashCode (item3);
695 h = (h << 5) - h + comparer.GetHashCode (item4);
696 h = (h << 5) - h + comparer.GetHashCode (item5);
697 h = (h << 5) - h + comparer.GetHashCode (item6);
698 h = (h << 5) - h + comparer.GetHashCode (item7);
699 h = (h << 5) - h + comparer.GetHashCode (rest);
703 public override string ToString ()
705 return String.Format ("({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})", item1, item2, item3, item4, item5, item6, item7, rest);
721 public class TupleGen
723 public static void Main () {
724 for (int arity = 1; arity < 9; ++arity) {
725 string type_name = GetTypeName (arity);
727 Console.WriteLine ("\t[Serializable]");
728 Console.Write ("\tpublic {0}class ", arity < 8 ? null : "partial ");
729 Console.Write (type_name);
730 Console.WriteLine (" : IStructuralEquatable, IStructuralComparable, IComparable");
731 Console.WriteLine ("\t{");
732 for (int i = 1; i <= arity; ++i)
733 Console.WriteLine ("\t\t{0} {1};", GetItemTypeName (i), GetItemName (i));
736 Console.WriteLine ();
737 Console.Write ("\t\tpublic Tuple (");
738 for (int i = 1; i <= arity; ++i) {
739 Console.Write ("{0} {1}", GetItemTypeName (i), GetItemName (i));
741 Console.Write (", ");
743 Console.WriteLine (")");
744 Console.WriteLine ("\t\t{");
745 for (int i = 1; i <= arity; ++i)
746 Console.WriteLine ("\t\t\t this.{0} = {0};", GetItemName (i));
747 Console.WriteLine ("\t\t}");
750 for (int i = 1; i <= arity; ++i) {
751 Console.WriteLine ();
752 Console.WriteLine ("\t\tpublic {0} {1} {{", GetItemTypeName (i),
753 System.Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase (GetItemName (i)));
754 Console.Write ("\t\t\tget { ");
755 Console.WriteLine ("return {0}; }}", GetItemName (i));
756 Console.WriteLine ("\t\t}");
759 Console.WriteLine ();
760 Console.WriteLine ("\t\tpublic int CompareTo (object obj)");
761 Console.WriteLine ("\t\t{");
762 Console.WriteLine ("\t\t\treturn CompareTo (obj, Comparer<object>.Default);");
763 Console.WriteLine ("\t\t}");
765 Console.WriteLine ();
766 Console.WriteLine ("\t\t[MonoTODO] public int CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }");
768 Console.WriteLine ();
769 Console.WriteLine ("\t\tpublic override bool Equals (object obj)");
770 Console.WriteLine ("\t\t{");
771 Console.WriteLine ("\t\t\treturn Equals (obj, EqualityComparer<object>.Default);");
772 Console.WriteLine ("\t\t}");
774 Console.WriteLine ();
775 Console.WriteLine ("\t\tpublic bool Equals (object other, IEqualityComparer comparer)");
776 Console.WriteLine ("\t\t{");
777 Console.WriteLine ("\t\t\tvar t = other as {0};", type_name);
778 Console.WriteLine ("\t\t\tif (t == null) {");
779 Console.WriteLine ("\t\t\t\tif (other == null) return false;");
780 Console.WriteLine ("\t\t\t\tthrow new ArgumentException ();");
781 Console.WriteLine ("\t\t\t}");
782 Console.WriteLine ();
783 Console.Write ("\t\t\treturn");
785 for (int i = 1; i <= arity; ++i) {
789 Console.Write ("\t\t\t\t");
791 Console.Write ("comparer.Equals ({0}, t.{0})", GetItemName (i));
793 Console.WriteLine (" &&");
795 Console.WriteLine (";");
797 Console.WriteLine ("\t\t}");
799 Console.WriteLine ();
800 Console.WriteLine ("\t\tpublic override int GetHashCode ()");
801 Console.WriteLine ("\t\t{");
802 Console.WriteLine ("\t\t\treturn GetHashCode (EqualityComparer<object>.Default);");
803 Console.WriteLine ("\t\t}");
805 Console.WriteLine ();
806 Console.WriteLine ("\t\tpublic int GetHashCode (IEqualityComparer comparer)");
807 Console.WriteLine ("\t\t{");
809 Console.WriteLine ("\t\t\treturn comparer.GetHashCode ({0});", GetItemName (arity));
811 Console.WriteLine ("\t\t\tint h = comparer.GetHashCode ({0});", GetItemName (1));
812 for (int i = 2; i <= arity; ++i)
813 Console.WriteLine ("\t\t\th = (h << 5) - h + comparer.GetHashCode ({0});", GetItemName (i));
814 Console.WriteLine ("\t\t\treturn h;");
817 Console.WriteLine ("\t\t}");
819 Console.WriteLine ();
820 Console.WriteLine ("\t\tpublic override string ToString ()");
821 Console.WriteLine ("\t\t{");
822 Console.Write ("\t\t\treturn String.Format (\"(");
823 for (int i = 1; i <= arity; ++i) {
824 Console.Write ("{" + (i - 1) + "}");
826 Console.Write (", ");
828 Console.Write (")\", ");
829 for (int i = 1; i <= arity; ++i) {
830 Console.Write (GetItemName (i));
832 Console.Write (", ");
834 Console.WriteLine (");");
835 Console.WriteLine ("\t\t}");
837 Console.WriteLine ("\t}\n");
841 static string GetTypeName (int arity)
843 StringBuilder sb = new StringBuilder ();
844 sb.Append ("Tuple<");
845 for (int i = 1; i <= arity; ++i) {
846 sb.Append (GetItemTypeName (i));
852 return sb.ToString ();
855 static string GetItemName (int arity)
857 return arity < 8 ? "item" + arity.ToString () : "rest";
860 static string GetItemTypeName (int arity)
862 return arity < 8 ? "T" + arity.ToString () : "TRest";