New test.
[mono.git] / mcs / class / Mono.C5 / C5 / Records.cs
1 #if NET_2_0
2 /*\r
3  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
4  Permission is hereby granted, free of charge, to any person obtaining a copy\r
5  of this software and associated documentation files (the "Software"), to deal\r
6  in the Software without restriction, including without limitation the rights\r
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
8  copies of the Software, and to permit persons to whom the Software is\r
9  furnished to do so, subject to the following conditions:\r
10  \r
11  The above copyright notice and this permission notice shall be included in\r
12  all copies or substantial portions of the Software.\r
13  \r
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
20  SOFTWARE.\r
21 */\r
22 \r
23 using C5;\r
24 using System;\r
25 using System.Reflection;\r
26 using System.Reflection.Emit;\r
27 using System.Diagnostics;\r
28 \r
29 namespace C5\r
30 {\r
31   struct RecConst\r
32   {\r
33     public const int HASHFACTOR = 387281;\r
34   }\r
35   /// <summary>\r
36   /// A generic record type with two fields. \r
37   /// <para>\r
38   /// Equality is defined field by field, using the <code>Equals</code> method \r
39   /// inherited from <code>System.Object</code> (i.e. using <see cref="T:C5.NaturalEqualityComparer`1"/>).\r
40   /// </para>\r
41   /// <para>\r
42   /// This type is similar to <see cref="T:C5.KeyValuePair`2"/>, but the latter\r
43   /// uses <see cref="P:C5.EqualityComparer`1.Default"/> to define field equality instead of <see cref="T:C5.NaturalEqualityComparer`1"/>.\r
44   /// </para>\r
45   /// </summary>\r
46   /// <typeparam name="T1"></typeparam>\r
47   /// <typeparam name="T2"></typeparam>\r
48   public struct Rec<T1, T2> : IEquatable<Rec<T1, T2>>, IShowable\r
49   {\r
50     /// <summary>\r
51     /// \r
52     /// </summary>\r
53     public readonly T1 X1;\r
54     /// <summary>\r
55     /// \r
56     /// </summary>\r
57     public readonly T2 X2;\r
58 \r
59     /// <summary>\r
60     /// \r
61     /// </summary>\r
62     /// <param name="x1"></param>\r
63     /// <param name="x2"></param>\r
64     [Tested]\r
65     public Rec(T1 x1, T2 x2)\r
66     {\r
67       this.X1 = x1; this.X2 = x2;\r
68     }\r
69 \r
70     /// <summary>\r
71     /// \r
72     /// </summary>\r
73     /// <param name="other"></param>\r
74     /// <returns></returns>\r
75     [Tested]\r
76     public bool Equals(Rec<T1, T2> other)\r
77     {\r
78       return\r
79         (X1 == null ? other.X1 == null : X1.Equals(other.X1)) &&\r
80         (X2 == null ? other.X2 == null : X2.Equals(other.X2))\r
81         ;\r
82     }\r
83     /// <summary>\r
84     /// \r
85     /// </summary>\r
86     /// <param name="obj"></param>\r
87     /// <returns></returns>\r
88     [Tested]\r
89     public override bool Equals(object obj)\r
90     {\r
91       return obj is Rec<T1, T2> ? Equals((Rec<T1, T2>)obj) : false;\r
92     }\r
93     /// <summary>\r
94     /// \r
95     /// </summary>\r
96     /// <param name="record1"></param>\r
97     /// <param name="record2"></param>\r
98     /// <returns></returns>\r
99     [Tested]\r
100     public static bool operator ==(Rec<T1, T2> record1, Rec<T1, T2> record2)\r
101     {\r
102       return record1.Equals(record2);\r
103     }\r
104     /// <summary>\r
105     /// \r
106     /// </summary>\r
107     /// <param name="record1"></param>\r
108     /// <param name="record2"></param>\r
109     /// <returns></returns>\r
110     [Tested]\r
111     public static bool operator !=(Rec<T1, T2> record1, Rec<T1, T2> record2)\r
112     {\r
113       return !record1.Equals(record2);\r
114     }\r
115     /// <summary>\r
116     /// \r
117     /// </summary>\r
118     /// <returns></returns>\r
119     [Tested]\r
120     public override int GetHashCode()\r
121     {\r
122       //TODO: don't use 0 as hashcode for null, but something else!\r
123       int hashcode = X1 == null ? 0 : X1.GetHashCode();\r
124       hashcode = hashcode * RecConst.HASHFACTOR + (X2 == null ? 0 : X2.GetHashCode());\r
125       return hashcode;\r
126     }\r
127 \r
128     /// <summary>\r
129     /// \r
130     /// </summary>\r
131     /// <returns></returns>\r
132     public override string ToString()\r
133     {\r
134       return String.Format("({0}, {1})", X1, X2);\r
135     }\r
136 \r
137     #region IShowable Members\r
138 \r
139     /// <summary>\r
140     /// \r
141     /// </summary>\r
142     /// <param name="stringbuilder"></param>\r
143     /// <param name="rest"></param>\r
144     /// <param name="formatProvider"></param>\r
145     /// <returns></returns>\r
146     public bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)\r
147     {\r
148       bool incomplete = true;\r
149       stringbuilder.Append("(");\r
150       rest -= 2;\r
151       try\r
152       {\r
153         if (incomplete = !Showing.Show(X1, stringbuilder, ref rest, formatProvider))\r
154           return false;\r
155         stringbuilder.Append(", ");\r
156         rest -= 2;\r
157         if (incomplete = !Showing.Show(X2, stringbuilder, ref rest, formatProvider))\r
158           return false;\r
159       }\r
160       finally\r
161       {\r
162         if (incomplete)\r
163         {\r
164           stringbuilder.Append("...");\r
165           rest -= 3;\r
166         }\r
167         stringbuilder.Append(")");\r
168       }\r
169       return true;\r
170     }\r
171     #endregion\r
172 \r
173     #region IFormattable Members\r
174 \r
175     /// <summary>\r
176     /// \r
177     /// </summary>\r
178     /// <param name="format"></param>\r
179     /// <param name="formatProvider"></param>\r
180     /// <returns></returns>\r
181     public string ToString(string format, IFormatProvider formatProvider)\r
182     {\r
183       return Showing.ShowString(this, format, formatProvider);\r
184     }\r
185 \r
186     #endregion\r
187   }\r
188   /// <summary>\r
189   /// \r
190   /// </summary>\r
191   /// <typeparam name="T1"></typeparam>\r
192   /// <typeparam name="T2"></typeparam>\r
193   /// <typeparam name="T3"></typeparam>\r
194   public struct Rec<T1, T2, T3> : IEquatable<Rec<T1, T2, T3>>, IShowable\r
195   {\r
196     /// <summary>\r
197     /// \r
198     /// </summary>\r
199     public readonly T1 X1;\r
200     /// <summary>\r
201     /// \r
202     /// </summary>\r
203     public readonly T2 X2;\r
204     /// <summary>\r
205     /// \r
206     /// </summary>\r
207     public readonly T3 X3;\r
208     /// <summary>\r
209     /// \r
210     /// </summary>\r
211     /// <param name="x1"></param>\r
212     /// <param name="x2"></param>\r
213     /// <param name="x3"></param>\r
214     [Tested]\r
215     public Rec(T1 x1, T2 x2, T3 x3)\r
216     {\r
217       this.X1 = x1; this.X2 = x2; this.X3 = x3;\r
218     }\r
219     /// <summary>\r
220     /// \r
221     /// </summary>\r
222     /// <param name="other"></param>\r
223     /// <returns></returns>\r
224     [Tested]\r
225     public bool Equals(Rec<T1, T2, T3> other)\r
226     {\r
227       return\r
228         (X1 == null ? other.X1 == null : X1.Equals(other.X1)) &&\r
229         (X2 == null ? other.X2 == null : X2.Equals(other.X2)) &&\r
230         (X3 == null ? other.X3 == null : X3.Equals(other.X3))\r
231         ;\r
232     }\r
233     /// <summary>\r
234     /// \r
235     /// </summary>\r
236     /// <param name="obj"></param>\r
237     /// <returns></returns>\r
238     [Tested]\r
239     public override bool Equals(object obj)\r
240     {\r
241       return obj is Rec<T1, T2, T3> ? Equals((Rec<T1, T2, T3>)obj) : false;\r
242     }\r
243     /// <summary>\r
244     /// \r
245     /// </summary>\r
246     /// <param name="record1"></param>\r
247     /// <param name="record2"></param>\r
248     /// <returns></returns>\r
249     [Tested]\r
250     public static bool operator ==(Rec<T1, T2, T3> record1, Rec<T1, T2, T3> record2)\r
251     {\r
252       return record1.Equals(record2);\r
253     }\r
254     /// <summary>\r
255     /// \r
256     /// </summary>\r
257     /// <param name="record1"></param>\r
258     /// <param name="record2"></param>\r
259     /// <returns></returns>\r
260     [Tested]\r
261     public static bool operator !=(Rec<T1, T2, T3> record1, Rec<T1, T2, T3> record2)\r
262     {\r
263       return !record1.Equals(record2);\r
264     }\r
265     /// <summary>\r
266     /// \r
267     /// </summary>\r
268     /// <returns></returns>\r
269     [Tested]\r
270     public override int GetHashCode()\r
271     {\r
272       //TODO: don't use 0 as hashcode for null, but something else!\r
273       int hashcode = X1 == null ? 0 : X1.GetHashCode();\r
274       hashcode = hashcode * RecConst.HASHFACTOR + (X2 == null ? 0 : X2.GetHashCode());\r
275       hashcode = hashcode * RecConst.HASHFACTOR + (X3 == null ? 0 : X3.GetHashCode());\r
276       return hashcode;\r
277     }\r
278 \r
279     /// <summary>\r
280     /// \r
281     /// </summary>\r
282     /// <returns></returns>\r
283     public override string ToString()\r
284     {\r
285       return String.Format("({0}, {1}, {2})", X1, X2, X3);\r
286     }\r
287     #region IShowable Members\r
288 \r
289     /// <summary>\r
290     /// \r
291     /// </summary>\r
292     /// <param name="stringbuilder"></param>\r
293     /// <param name="rest"></param>\r
294     /// <param name="formatProvider"></param>\r
295     /// <returns></returns>\r
296     public bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)\r
297     {\r
298       bool incomplete = true;\r
299       stringbuilder.Append("(");\r
300       rest -= 2;\r
301       try\r
302       {\r
303         if (incomplete = !Showing.Show(X1, stringbuilder, ref rest, formatProvider))\r
304           return false;\r
305         stringbuilder.Append(", ");\r
306         rest -= 2;\r
307         if (incomplete = !Showing.Show(X2, stringbuilder, ref rest, formatProvider))\r
308           return false;\r
309         stringbuilder.Append(", ");\r
310         rest -= 2;\r
311         if (incomplete = !Showing.Show(X3, stringbuilder, ref rest, formatProvider))\r
312           return false;\r
313       }\r
314       finally\r
315       {\r
316         if (incomplete)\r
317         {\r
318           stringbuilder.Append("...");\r
319           rest -= 3;\r
320         }\r
321         stringbuilder.Append(")");\r
322       }\r
323       return true;\r
324     }\r
325     #endregion\r
326 \r
327     #region IFormattable Members\r
328 \r
329     /// <summary>\r
330     /// \r
331     /// </summary>\r
332     /// <param name="format"></param>\r
333     /// <param name="formatProvider"></param>\r
334     /// <returns></returns>\r
335     public string ToString(string format, IFormatProvider formatProvider)\r
336     {\r
337       return Showing.ShowString(this, format, formatProvider);\r
338     }\r
339 \r
340     #endregion\r
341   }\r
342 \r
343   /// <summary>\r
344   /// \r
345   /// </summary>\r
346   /// <typeparam name="T1"></typeparam>\r
347   /// <typeparam name="T2"></typeparam>\r
348   /// <typeparam name="T3"></typeparam>\r
349   /// <typeparam name="T4"></typeparam>\r
350   public struct Rec<T1, T2, T3, T4> : IEquatable<Rec<T1, T2, T3, T4>>, IShowable\r
351   {\r
352     /// <summary>\r
353     /// \r
354     /// </summary>\r
355     public readonly T1 X1;\r
356     /// <summary>\r
357     /// \r
358     /// </summary>\r
359     public readonly T2 X2;\r
360     /// <summary>\r
361     /// \r
362     /// </summary>\r
363     public readonly T3 X3;\r
364     /// <summary>\r
365     /// \r
366     /// </summary>\r
367     public readonly T4 X4;\r
368     /// <summary>\r
369     /// \r
370     /// </summary>\r
371     /// <param name="x1"></param>\r
372     /// <param name="x2"></param>\r
373     /// <param name="x3"></param>\r
374     /// <param name="x4"></param>\r
375     [Tested]\r
376     public Rec(T1 x1, T2 x2, T3 x3, T4 x4)\r
377     {\r
378       this.X1 = x1; this.X2 = x2; this.X3 = x3; this.X4 = x4;\r
379     }\r
380     /// <summary>\r
381     /// \r
382     /// </summary>\r
383     /// <param name="other"></param>\r
384     /// <returns></returns>\r
385     [Tested]\r
386     public bool Equals(Rec<T1, T2, T3, T4> other)\r
387     {\r
388       return\r
389         (X1 == null ? other.X1 == null : X1.Equals(other.X1)) &&\r
390         (X2 == null ? other.X2 == null : X2.Equals(other.X2)) &&\r
391         (X3 == null ? other.X3 == null : X3.Equals(other.X3)) &&\r
392         (X4 == null ? other.X4 == null : X4.Equals(other.X4))\r
393         ;\r
394     }\r
395     /// <summary>\r
396     /// \r
397     /// </summary>\r
398     /// <param name="obj"></param>\r
399     /// <returns></returns>\r
400     [Tested]\r
401     public override bool Equals(object obj)\r
402     {\r
403       return obj is Rec<T1, T2, T3, T4> ? Equals((Rec<T1, T2, T3, T4>)obj) : false;\r
404     }\r
405 \r
406     /// <summary>\r
407     /// \r
408     /// </summary>\r
409     /// <param name="record1"></param>\r
410     /// <param name="record2"></param>\r
411     /// <returns></returns>\r
412     [Tested]\r
413     public static bool operator ==(Rec<T1, T2, T3, T4> record1, Rec<T1, T2, T3, T4> record2)\r
414     {\r
415       return record1.Equals(record2);\r
416     }\r
417     /// <summary>\r
418     /// \r
419     /// </summary>\r
420     /// <param name="record1"></param>\r
421     /// <param name="record2"></param>\r
422     /// <returns></returns>\r
423     [Tested]\r
424     public static bool operator !=(Rec<T1, T2, T3, T4> record1, Rec<T1, T2, T3, T4> record2)\r
425     {\r
426       return !record1.Equals(record2);\r
427     }\r
428 \r
429     /// <summary>\r
430     /// \r
431     /// </summary>\r
432     /// <returns></returns>\r
433     [Tested]\r
434     public override int GetHashCode()\r
435     {\r
436       //TODO: don't use 0 as hashcode for null, but something else!\r
437       int hashcode = X1 == null ? 0 : X1.GetHashCode();\r
438       hashcode = hashcode * RecConst.HASHFACTOR + (X2 == null ? 0 : X2.GetHashCode());\r
439       hashcode = hashcode * RecConst.HASHFACTOR + (X3 == null ? 0 : X3.GetHashCode());\r
440       hashcode = hashcode * RecConst.HASHFACTOR + (X4 == null ? 0 : X4.GetHashCode());\r
441       return hashcode;\r
442     }\r
443 \r
444     /// <summary>\r
445     /// \r
446     /// </summary>\r
447     /// <returns></returns>\r
448     public override string ToString()\r
449     {\r
450       return String.Format("({0}, {1}, {2}, {3})", X1, X2, X3, X4);\r
451     }\r
452     #region IShowable Members\r
453 \r
454     /// <summary>\r
455     /// \r
456     /// </summary>\r
457     /// <param name="stringbuilder"></param>\r
458     /// <param name="rest"></param>\r
459     /// <param name="formatProvider"></param>\r
460     /// <returns></returns>\r
461     public bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)\r
462     {\r
463       bool incomplete = true;\r
464       stringbuilder.Append("(");\r
465       rest -= 2;\r
466       try\r
467       {\r
468         if (incomplete = !Showing.Show(X1, stringbuilder, ref rest, formatProvider))\r
469           return false;\r
470         stringbuilder.Append(", ");\r
471         rest -= 2;\r
472         if (incomplete = !Showing.Show(X2, stringbuilder, ref rest, formatProvider))\r
473           return false;\r
474         stringbuilder.Append(", ");\r
475         rest -= 2;\r
476         if (incomplete = !Showing.Show(X3, stringbuilder, ref rest, formatProvider))\r
477           return false;\r
478         stringbuilder.Append(", ");\r
479         rest -= 2;\r
480         if (incomplete = !Showing.Show(X4, stringbuilder, ref rest, formatProvider))\r
481           return false;\r
482       }\r
483       finally\r
484       {\r
485         if (incomplete)\r
486         {\r
487           stringbuilder.Append("...");\r
488           rest -= 3;\r
489         }\r
490         stringbuilder.Append(")");\r
491       }\r
492       return true;\r
493     }\r
494     #endregion\r
495 \r
496     #region IFormattable Members\r
497 \r
498     /// <summary>\r
499     /// \r
500     /// </summary>\r
501     /// <param name="format"></param>\r
502     /// <param name="formatProvider"></param>\r
503     /// <returns></returns>\r
504     public string ToString(string format, IFormatProvider formatProvider)\r
505     {\r
506       return Showing.ShowString(this, format, formatProvider);\r
507     }\r
508 \r
509     #endregion\r
510   }\r
511 }\r
512
513 #endif