-
Notifications
You must be signed in to change notification settings - Fork 4
/
Tuple.cs
87 lines (76 loc) · 1.97 KB
/
Tuple.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
using System;
using System.Collections;
using System.Collections.Generic;
namespace StripSymmetry
{
static class Tuple
{
public static Tuple<T1, T2> Create<T1, T2>(T1 a, T2 b)
{
return new Tuple<T1, T2>(a, b);
}
internal static int CombineHashCodes(int h1, int h2)
{
return (h1 << 5) + h1 ^ h2;
}
}
public struct Tuple<T1, T2> : IComparable
{
public T1 Item1
{
get;
set;
}
public T2 Item2
{
get;
set;
}
public Tuple(T1 a, T2 b)
: this()
{
Item1 = a;
Item2 = b;
}
public override bool Equals(object obj)
{
return Equals(obj, Comparer<object>.Default);
}
public bool Equals(object obj, IEqualityComparer cmp)
{
if (!(obj is Tuple<T1, T2>))
{
return false;
}
var other = (Tuple<T1, T2>)obj;
return cmp.Equals(Item1, other.Item1) && cmp.Equals(Item2, other.Item2);
}
public override int GetHashCode()
{
return GetHashCode(EqualityComparer<object>.Default);
}
public int GetHashCode(IEqualityComparer cmp)
{
return Tuple.CombineHashCodes(cmp.GetHashCode(Item1), cmp.GetHashCode(Item2));
}
public int CompareTo(object obj)
{
return CompareTo(obj, Comparer<object>.Default);
}
public int CompareTo(object obj, IComparer cmp)
{
if (obj == null)
{
return 1;
}
var other = (Tuple<T1, T2>)obj;
var result = cmp.Compare(Item1, other.Item1);
if (result != 0)
{
return result;
}
result = cmp.Compare(Item2, other.Item2);
return result;
}
}
}