-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tuple4.cs
124 lines (110 loc) · 3.91 KB
/
Tuple4.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// ----------------------------------------------------------------------------
// Tuple structs for use in .NET Not-Quite-3.5 (e.g. Unity3D).
//
// Used Chapter 3 in http://functional-programming.net/ as a starting point.
//
// Note: .NET 4.0 Tuples are immutable classes so they're *slightly* different.
// ----------------------------------------------------------------------------
using System;
namespace Binocle
{
/// <summary>
/// Represents a functional tuple that can be used to store
/// two values of different types inside one object.
/// </summary>
/// <typeparam name="T1">The type of the first element</typeparam>
/// <typeparam name="T2">The type of the second element</typeparam>
/// <typeparam name="T3">The type of the third element</typeparam>
/// <typeparam name="T4">The type of the fourth element</typeparam>
public sealed class Tuple<T1, T2, T3, T4>
{
private readonly T1 item1;
private readonly T2 item2;
private readonly T3 item3;
private readonly T4 item4;
/// <summary>
/// Retyurns the first element of the tuple
/// </summary>
public T1 Item1
{
get { return item1; }
}
/// <summary>
/// Returns the second element of the tuple
/// </summary>
public T2 Item2
{
get { return item2; }
}
/// <summary>
/// Returns the second element of the tuple
/// </summary>
public T3 Item3
{
get { return item3; }
}
/// <summary>
/// Returns the second element of the tuple
/// </summary>
public T4 Item4
{
get { return item4; }
}
/// <summary>
/// Create a new tuple value
/// </summary>
/// <param name="item1">First element of the tuple</param>
/// <param name="second">Second element of the tuple</param>
/// <param name="third">Third element of the tuple</param>
/// <param name="fourth">Fourth element of the tuple</param>
public Tuple(T1 item1, T2 item2, T3 item3, T4 item4)
{
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
this.item4 = item4;
}
public override int GetHashCode()
{
int hash = 17;
hash = hash * 23 + (item1 == null ? 0 : item1.GetHashCode());
hash = hash * 23 + (item2 == null ? 0 : item2.GetHashCode());
hash = hash * 23 + (item3 == null ? 0 : item3.GetHashCode());
hash = hash * 23 + (item4 == null ? 0 : item4.GetHashCode());
return hash;
}
public override bool Equals(object o)
{
if (o.GetType() != typeof(Tuple<T1, T2, T3, T4>))
{
return false;
}
var other = (Tuple<T1, T2, T3, T4>)o;
return this == other;
}
public static bool operator ==(Tuple<T1, T2, T3, T4> a, Tuple<T1, T2, T3, T4> b)
{
if (object.ReferenceEquals(a, null))
{
return object.ReferenceEquals(b, null);
}
if (a.item1 == null && b.item1 != null) return false;
if (a.item2 == null && b.item2 != null) return false;
if (a.item3 == null && b.item3 != null) return false;
if (a.item4 == null && b.item4 != null) return false;
return
a.item1.Equals(b.item1) &&
a.item2.Equals(b.item2) &&
a.item3.Equals(b.item3) &&
a.item4.Equals(b.item4);
}
public static bool operator !=(Tuple<T1, T2, T3, T4> a, Tuple<T1, T2, T3, T4> b)
{
return !(a == b);
}
public void Unpack(Action<T1, T2, T3, T4> unpackerDelegate)
{
unpackerDelegate(Item1, Item2, Item3, Item4);
}
}
}