2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / corlib / System / Tuples.cs
1 //
2 // Tuples.cs
3 //
4 // Authors:
5 //  Zoltan Varga (vargaz@gmail.com)
6 //
7 // Copyright (C) 2009 Novell
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_4_0
30
31 using System;
32 using System.Collections;
33
34 namespace System
35 {
36         public class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> : IStructuralEquatable, IStructuralComparable, IComparable
37         {
38                 T1 item1;
39                 T2 item2;
40                 T3 item3;
41                 T4 item4;
42                 T5 item5;
43                 T6 item6;
44                 T7 item7;
45                 TRest rest;
46
47                 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest) {
48                         this.item1 = item1;
49                         this.item2 = item2;
50                         this.item3 = item3;
51                         this.item4 = item4;
52                         this.item5 = item5;
53                         this.item6 = item6;
54                         this.item7 = item7;
55                         this.rest = rest;
56
57                         bool ok = true;
58                         if (!typeof (TRest).IsGenericType)
59                                 ok = false;
60                         if (ok) {
61                                 Type t = typeof (TRest).GetGenericTypeDefinition ();
62                                 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<,,,,,,,>)))
63                                         ok = false;
64                         }
65                         if (!ok)
66                                 throw new ArgumentException ("The last element of an eight element tuple must be a Tuple.");
67                 }
68
69                 public T1 Item1 {
70                         get {
71                                 return item1;
72                         }
73                 }
74
75                 public T2 Item2 {
76                         get {
77                                 return item2;
78                         }
79                 }
80
81                 public T3 Item3 {
82                         get {
83                                 return item3;
84                         }
85                 }
86
87                 public T4 Item4 {
88                         get {
89                                 return item4;
90                         }
91                 }
92
93                 public T5 Item5 {
94                         get {
95                                 return item5;
96                         }
97                 }
98
99                 public T6 Item6 {
100                         get {
101                                 return item6;
102                         }
103                 }
104
105                 public T7 Item7 {
106                         get {
107                                 return item7;
108                         }
109                 }
110
111                 public TRest Rest {
112                         get {
113                                 return rest;
114                         }
115                 }
116
117                 int IComparable.CompareTo (object other) { throw new NotImplementedException (); }
118                 int IStructuralComparable.CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
119                 bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer) { throw new NotImplementedException (); }
120                 int IStructuralEquatable.GetHashCode (IEqualityComparer comparer) { throw new NotImplementedException (); }
121
122                 [MonoTODO]
123                 public override string ToString () {
124                         // FIXME: Merge the items from rest into the parent
125                         return String.Format ("({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})", Item1, Item2, Item3, Item4, Item5, Item6, Item7, Rest);
126                 }
127         }
128
129         /* The rest is generated by the script at the bottom */
130
131         public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable
132         {
133                 T1 item1;
134
135                 public Tuple (T1 item1) {
136                          this.item1 = item1;
137                 }
138                 public T1 Item1 {
139                         get {
140                                 return item1;
141                         }
142                 }
143                 [MonoTODO] int IComparable.CompareTo (object other) { throw new NotImplementedException (); }
144                 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
145                 [MonoTODO] bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer) { throw new NotImplementedException (); }
146                 [MonoTODO] int IStructuralEquatable.GetHashCode (IEqualityComparer comparer) { throw new NotImplementedException (); }
147                 public override string ToString () {
148                         return String.Format ("({0})", Item1);
149                 }
150         }
151
152         public class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable
153         {
154                 T1 item1;
155                 T2 item2;
156
157                 public Tuple (T1 item1, T2 item2) {
158                          this.item1 = item1;
159                          this.item2 = item2;
160                 }
161                 public T1 Item1 {
162                         get {
163                                 return item1;
164                         }
165                 }
166                 public T2 Item2 {
167                         get {
168                                 return item2;
169                         }
170                 }
171                 [MonoTODO] int IComparable.CompareTo (object other) { throw new NotImplementedException (); }
172                 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
173                 [MonoTODO] bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer) { throw new NotImplementedException (); }
174                 [MonoTODO] int IStructuralEquatable.GetHashCode (IEqualityComparer comparer) { throw new NotImplementedException (); }
175                 public override string ToString () {
176                         return String.Format ("({0}, {1})", Item1, Item2);
177                 }
178         }
179
180         public class Tuple<T1, T2, T3> : IStructuralEquatable, IStructuralComparable, IComparable
181         {
182                 T1 item1;
183                 T2 item2;
184                 T3 item3;
185
186                 public Tuple (T1 item1, T2 item2, T3 item3) {
187                          this.item1 = item1;
188                          this.item2 = item2;
189                          this.item3 = item3;
190                 }
191                 public T1 Item1 {
192                         get {
193                                 return item1;
194                         }
195                 }
196                 public T2 Item2 {
197                         get {
198                                 return item2;
199                         }
200                 }
201                 public T3 Item3 {
202                         get {
203                                 return item3;
204                         }
205                 }
206                 [MonoTODO] int IComparable.CompareTo (object other) { throw new NotImplementedException (); }
207                 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
208                 [MonoTODO] bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer) { throw new NotImplementedException (); }
209                 [MonoTODO] int IStructuralEquatable.GetHashCode (IEqualityComparer comparer) { throw new NotImplementedException (); }
210                 public override string ToString () {
211                         return String.Format ("({0}, {1}, {2})", Item1, Item2, Item3);
212                 }
213         }
214
215         public class Tuple<T1, T2, T3, T4> : IStructuralEquatable, IStructuralComparable, IComparable
216         {
217                 T1 item1;
218                 T2 item2;
219                 T3 item3;
220                 T4 item4;
221
222                 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4) {
223                          this.item1 = item1;
224                          this.item2 = item2;
225                          this.item3 = item3;
226                          this.item4 = item4;
227                 }
228                 public T1 Item1 {
229                         get {
230                                 return item1;
231                         }
232                 }
233                 public T2 Item2 {
234                         get {
235                                 return item2;
236                         }
237                 }
238                 public T3 Item3 {
239                         get {
240                                 return item3;
241                         }
242                 }
243                 public T4 Item4 {
244                         get {
245                                 return item4;
246                         }
247                 }
248                 [MonoTODO] int IComparable.CompareTo (object other) { throw new NotImplementedException (); }
249                 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
250                 [MonoTODO] bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer) { throw new NotImplementedException (); }
251                 [MonoTODO] int IStructuralEquatable.GetHashCode (IEqualityComparer comparer) { throw new NotImplementedException (); }
252                 public override string ToString () {
253                         return String.Format ("({0}, {1}, {2}, {3})", Item1, Item2, Item3, Item4);
254                 }
255         }
256
257         public class Tuple<T1, T2, T3, T4, T5> : IStructuralEquatable, IStructuralComparable, IComparable
258         {
259                 T1 item1;
260                 T2 item2;
261                 T3 item3;
262                 T4 item4;
263                 T5 item5;
264
265                 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5) {
266                          this.item1 = item1;
267                          this.item2 = item2;
268                          this.item3 = item3;
269                          this.item4 = item4;
270                          this.item5 = item5;
271                 }
272                 public T1 Item1 {
273                         get {
274                                 return item1;
275                         }
276                 }
277                 public T2 Item2 {
278                         get {
279                                 return item2;
280                         }
281                 }
282                 public T3 Item3 {
283                         get {
284                                 return item3;
285                         }
286                 }
287                 public T4 Item4 {
288                         get {
289                                 return item4;
290                         }
291                 }
292                 public T5 Item5 {
293                         get {
294                                 return item5;
295                         }
296                 }
297                 [MonoTODO] int IComparable.CompareTo (object other) { throw new NotImplementedException (); }
298                 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
299                 [MonoTODO] bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer) { throw new NotImplementedException (); }
300                 [MonoTODO] int IStructuralEquatable.GetHashCode (IEqualityComparer comparer) { throw new NotImplementedException (); }
301                 public override string ToString () {
302                         return String.Format ("({0}, {1}, {2}, {3}, {4})", Item1, Item2, Item3, Item4, Item5);
303                 }
304         }
305
306         public class Tuple<T1, T2, T3, T4, T5, T6> : IStructuralEquatable, IStructuralComparable, IComparable
307         {
308                 T1 item1;
309                 T2 item2;
310                 T3 item3;
311                 T4 item4;
312                 T5 item5;
313                 T6 item6;
314
315                 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6) {
316                          this.item1 = item1;
317                          this.item2 = item2;
318                          this.item3 = item3;
319                          this.item4 = item4;
320                          this.item5 = item5;
321                          this.item6 = item6;
322                 }
323                 public T1 Item1 {
324                         get {
325                                 return item1;
326                         }
327                 }
328                 public T2 Item2 {
329                         get {
330                                 return item2;
331                         }
332                 }
333                 public T3 Item3 {
334                         get {
335                                 return item3;
336                         }
337                 }
338                 public T4 Item4 {
339                         get {
340                                 return item4;
341                         }
342                 }
343                 public T5 Item5 {
344                         get {
345                                 return item5;
346                         }
347                 }
348                 public T6 Item6 {
349                         get {
350                                 return item6;
351                         }
352                 }
353                 [MonoTODO] int IComparable.CompareTo (object other) { throw new NotImplementedException (); }
354                 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
355                 [MonoTODO] bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer) { throw new NotImplementedException (); }
356                 [MonoTODO] int IStructuralEquatable.GetHashCode (IEqualityComparer comparer) { throw new NotImplementedException (); }
357                 public override string ToString () {
358                         return String.Format ("({0}, {1}, {2}, {3}, {4}, {5})", Item1, Item2, Item3, Item4, Item5, Item6);
359                 }
360         }
361
362         public class Tuple<T1, T2, T3, T4, T5, T6, T7> : IStructuralEquatable, IStructuralComparable, IComparable
363         {
364                 T1 item1;
365                 T2 item2;
366                 T3 item3;
367                 T4 item4;
368                 T5 item5;
369                 T6 item6;
370                 T7 item7;
371
372                 public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7) {
373                          this.item1 = item1;
374                          this.item2 = item2;
375                          this.item3 = item3;
376                          this.item4 = item4;
377                          this.item5 = item5;
378                          this.item6 = item6;
379                          this.item7 = item7;
380                 }
381                 public T1 Item1 {
382                         get {
383                                 return item1;
384                         }
385                 }
386                 public T2 Item2 {
387                         get {
388                                 return item2;
389                         }
390                 }
391                 public T3 Item3 {
392                         get {
393                                 return item3;
394                         }
395                 }
396                 public T4 Item4 {
397                         get {
398                                 return item4;
399                         }
400                 }
401                 public T5 Item5 {
402                         get {
403                                 return item5;
404                         }
405                 }
406                 public T6 Item6 {
407                         get {
408                                 return item6;
409                         }
410                 }
411                 public T7 Item7 {
412                         get {
413                                 return item7;
414                         }
415                 }
416                 [MonoTODO] int IComparable.CompareTo (object other) { throw new NotImplementedException (); }
417                 [MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }
418                 [MonoTODO] bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer) { throw new NotImplementedException (); }
419                 [MonoTODO] int IStructuralEquatable.GetHashCode (IEqualityComparer comparer) { throw new NotImplementedException (); }
420                 public override string ToString () {
421                         return String.Format ("({0}, {1}, {2}, {3}, {4}, {5}, {6})", Item1, Item2, Item3, Item4, Item5, Item6, Item7);
422                 }
423         }
424
425
426 }
427         
428 #endif
429
430 #if FALSE
431
432 //
433 // generator script
434 //
435
436 using System;
437
438 public class TupleGen
439 {
440         public static void Main () {
441                 for (int arity = 1; arity < 8; ++arity) {
442                         Console.Write ("\tpublic class Tuple<");
443                         for (int i = 1; i <= arity; ++i) {
444                                 Console.Write (String.Format ("T{0}", i));
445                                 if (i < arity)
446                                         Console.Write (", ");
447                         }
448                         Console.WriteLine ("> : IStructuralEquatable, IStructuralComparable, IComparable");
449                         Console.WriteLine ("\t{");
450                         for (int i = 1; i <= arity; ++i)
451                                 Console.WriteLine (String.Format ("\t\tT{0} item{0};", i));
452                         Console.WriteLine ();
453                         Console.Write ("\t\tpublic Tuple (");
454                         for (int i = 1; i <= arity; ++i) {
455                                 Console.Write (String.Format ("T{0} item{0}", i));
456                                 if (i < arity)
457                                         Console.Write (", ");
458                         }
459                         Console.WriteLine (") {");
460                         for (int i = 1; i <= arity; ++i)
461                                 Console.WriteLine (String.Format ("\t\t\t this.item{0} = item{0};", i));
462                         Console.WriteLine ("\t\t}");
463
464                         for (int i = 1; i <= arity; ++i) {
465                                 Console.WriteLine (String.Format ("\t\tpublic T{0} Item{0} {{", i));
466                                 Console.WriteLine ("\t\t\tget {");
467                                 Console.WriteLine (String.Format ("\t\t\t\treturn item{0};", i));
468                                 Console.WriteLine ("\t\t\t}");
469                                 Console.WriteLine ("\t\t}");
470                         }
471
472                         Console.WriteLine ("\t\t[MonoTODO] int IComparable.CompareTo (object other) { throw new NotImplementedException (); }");
473                         Console.WriteLine ("\t\t[MonoTODO] int IStructuralComparable.CompareTo (object other, IComparer comparer) { throw new NotImplementedException (); }");
474                         Console.WriteLine ("\t\t[MonoTODO] bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer) { throw new NotImplementedException (); }");
475                         Console.WriteLine ("\t\t[MonoTODO] int IStructuralEquatable.GetHashCode (IEqualityComparer comparer) { throw new NotImplementedException (); }");
476
477                         Console.WriteLine ("\t\tpublic override string ToString () {");
478                         Console.Write ("\t\t\treturn String.Format (\"(");
479                         for (int i = 1; i <= arity; ++i) {
480                                 Console.Write ("{" + (i - 1) + "}");
481                                 if (i < arity)
482                                         Console.Write (", ");
483                         }
484                         Console.Write (")\", ");
485                         for (int i = 1; i <= arity; ++i) {
486                                 Console.Write ("Item" + i);
487                                 if (i < arity)
488                                         Console.Write (", ");
489                         }
490                         Console.WriteLine (");");
491                         Console.WriteLine ("\t\t}");
492
493                         Console.WriteLine ("\t}\n");
494                 }
495         }
496 }
497
498 #endif