-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistributionElementComparer.cs
141 lines (123 loc) · 5.02 KB
/
DistributionElementComparer.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace RoundedDistributionComparison5
{
internal readonly struct DistributionElementComparer<T> : IComparer<DistributionElement<T>>
where T : unmanaged
{
public static readonly Comparison<DistributionElement<T>> PercentAsc = (x, y) =>
{
var percent = x.Percent.CompareTo(y.Percent);
var rem = x.Remain.CompareTo(y.Remain);
return percent != 0
? percent
: rem != 0
? rem
: x.GetHashCode().CompareTo(y.GetHashCode());
};
public static readonly Comparison<DistributionElement<T>> PercentDesc = (x, y) =>
{
var percent = y.Percent.CompareTo(x.Percent);
var rem = y.Remain.CompareTo(x.Remain);
return percent != 0
? percent
: rem != 0
? rem
: y.Key.GetHashCode().CompareTo(x.Key.GetHashCode());
};
public static readonly Comparison<DistributionElement<T>> RemAsc = (x, y) =>
{
var rem = x.Remain.CompareTo(y.Remain);
var percent = y.Percent.CompareTo(x.Percent);
return rem != 0
? rem
: percent != 0
? percent
: x.GetHashCode().CompareTo(y.GetHashCode());
};
public static readonly Comparison<DistributionElement<T>> RemDsc = (x, y) =>
{
var rem = y.Remain.CompareTo(x.Remain);
var percent = x.Percent.CompareTo(y.Percent);
return rem != 0
? rem
: percent != 0
? percent
: x.GetHashCode().CompareTo(y.GetHashCode());
};
private readonly Comparison<DistributionElement<T>> _comparer;
private DistributionElementComparer(Comparison<DistributionElement<T>> comparer)
{
_comparer = comparer;
}
public int Compare(DistributionElement<T> x, DistributionElement<T> y)
{
return _comparer(x, y);
}
public static IComparer<DistributionElement<T>> PercentAscendingComparer { get; } = new DistributionElementComparer<T>(PercentAsc);
public static IComparer<DistributionElement<T>> PercentDescendingComparer { get; } = new DistributionElementComparer<T>(PercentDesc);
public static IComparer<DistributionElement<T>> RemainAscendingComparer { get; } = new DistributionElementComparer<T>(RemAsc);
public static IComparer<DistributionElement<T>> RemainDescendingComparer { get; } = new DistributionElementComparer<T>(RemDsc);
}
internal struct DistributionElementPercentAscendingComparer<T> : IComparer<DistributionElement<T>>
where T : unmanaged
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Compare(DistributionElement<T> x, DistributionElement<T> y)
{
var percent = x.Percent.CompareTo(y.Percent);
var rem = x.Remain.CompareTo(y.Remain);
return percent != 0
? percent
: rem != 0
? rem
: x.GetHashCode().CompareTo(y.GetHashCode());
}
}
internal struct DistributionElementPercentDescendingComparer<T> : IComparer<DistributionElement<T>>
where T : unmanaged
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Compare(DistributionElement<T> x, DistributionElement<T> y)
{
var percent = y.Percent.CompareTo(x.Percent);
var rem = y.Remain.CompareTo(x.Remain);
return percent != 0
? percent
: rem != 0
? rem
: y.Key.GetHashCode().CompareTo(x.Key.GetHashCode());
}
}
internal struct DistributionElementRemainAscendingComparer<T> : IComparer<DistributionElement<T>>
where T : unmanaged
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Compare(DistributionElement<T> x, DistributionElement<T> y)
{
var rem = x.Remain.CompareTo(y.Remain);
var percent = x.Percent.CompareTo(y.Percent);
return rem != 0
? rem
: percent != 0
? percent
: x.GetHashCode().CompareTo(y.GetHashCode());
}
}
internal struct DistributionElementRemainDescendingComparer<T> : IComparer<DistributionElement<T>>
where T : unmanaged
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Compare(DistributionElement<T> x, DistributionElement<T> y)
{
var rem = y.Remain.CompareTo(x.Remain);
var percent = x.Percent.CompareTo(y.Percent);
return rem != 0
? rem
: percent != 0
? percent
: x.GetHashCode().CompareTo(y.GetHashCode());
}
}
}