-
Notifications
You must be signed in to change notification settings - Fork 16
/
MyExtensions.cs
200 lines (185 loc) · 5.92 KB
/
MyExtensions.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.CompilerServices;
namespace ExtensionMethods;
public static class MyExtensions
{
public static IEnumerable<(int, T)> Enumerate<T>(this IEnumerable<T> input, int start = 0)
{
int i = start;
foreach (var t in input)
yield return (i++, t);
}
public static int IndexOfMax<T>(this IEnumerable<T> sequence)
where T : IComparable<T>
{
T maxValue = default; // Immediately overwritten anyway
int ans = 0;
int index = 0;
foreach (T value in sequence)
{
int compareResult = value.CompareTo(maxValue);
if (compareResult > 0 || index == 0)
{
ans = index;
maxValue = value;
}
index++;
}
return ans;
}
public static int IndexOfMax<T, R>(this IEnumerable<T> sequence, Func<T, R> mapping)
where R : IComparable<R>
{
R maxValue = default; // Immediately overwritten anyway
int ans = 0;
int index = 0;
foreach (T value in sequence)
{
R res = mapping(value);
int compareResult = res.CompareTo(maxValue);
if (compareResult > 0 || index == 0)
{
ans = index;
maxValue = res;
}
index++;
}
return ans;
}
/// <summary>
/// Like Python's max(x, key=func)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="R"></typeparam>
/// <param name="sequence"></param>
/// <param name="mapping"></param>
/// <returns></returns>
public static T MaxByKeyFunc<T, R>(this IEnumerable<T> sequence, Func<T, R> mapping)
where R : IComparable<R>
{
R maxValue = default; // Immediately overwritten anyway
T ans = default;
int index = 0;
foreach (T value in sequence)
{
R res = mapping(value);
int compareResult = res.CompareTo(maxValue);
if (compareResult > 0 || index == 0)
{
ans = value;
maxValue = res;
}
index++;
}
return ans;
}
/// <summary>
/// Based on MaxIndex By Jon Skeet: http://stackoverflow.com/questions/462699/how-do-i-get-the-index-of-the-highest-value-in-an-array-using-linq
/// </summary>
/// <param name="sequence"></param>
/// <returns></returns>
public static IList<int> IndicesOfMax<T>(this IEnumerable<T> sequence)
where T : IComparable<T>
{
var ans = new List<int>(4);
T maxValue = default; // Immediately overwritten anyway
int index = 0;
foreach (T value in sequence)
{
int compareResult = value.CompareTo(maxValue);
if (compareResult > 0 || ans.Count == 0)
{
ans.Clear();
ans.Add(index);
maxValue = value;
}
if (compareResult == 0)
ans.Add(index);
index++;
}
return ans;
}
public static IList<int> IndicesOfMax<T, R>(this IEnumerable<T> sequence, Func<T, R> mapping)
where R : IComparable<R>
{
var ans = new List<int>(4);
R maxRes = default; // Immediately overwritten anyway
int index = 0;
foreach (T value in sequence)
{
R res = mapping(value);
int compareResult = res.CompareTo(maxRes);
if (compareResult > 0 || ans.Count == 0)
{
ans.Clear();
ans.Add(index);
maxRes = res;
}
if (compareResult == 0)
ans.Add(index);
index++;
}
return ans;
}
public static IList<T> AllMax<T>(this IEnumerable<T> sequence)
where T : IComparable<T>
{
var ans = new List<T>();
T maxVal = default; // Immediately overwritten anyway
foreach (T item in sequence)
{
int compareResult = item.CompareTo(maxVal);
if (compareResult > 0 || ans.Count == 0)
{
ans.Clear();
ans.Add(item);
maxVal = item;
}
if (compareResult == 0)
ans.Add(item);
}
return ans;
}
public static IList<T> AllMax<T, R>(this IEnumerable<T> sequence, Func<T, R> mapping)
where R : IComparable<R>
{
var ans = new List<T>();
R maxResult = default; // Immediately overwritten anyway
foreach (T item in sequence)
{
R mappingResult = mapping(item);
int compareResult = mappingResult.CompareTo(maxResult);
if (compareResult > 0 || ans.Count == 0)
{
ans.Clear();
ans.Add(item);
maxResult = mappingResult;
}
if (compareResult == 0)
ans.Add(item);
}
return ans;
}
public static IList<K> KeysOfMaxValue<K, V>(this IEnumerable<KeyValuePair<K, V>> mapping)
where V : IComparable<V>
{
var ans = new List<K>();
V maxValue = default; // Immediately overwritten anyway
foreach (KeyValuePair<K, V> kvp in mapping)
{
int compareResult = kvp.Value.CompareTo(maxValue);
if (compareResult > 0 || ans.Count == 0)
{
ans.Clear();
ans.Add(kvp.Key);
maxValue = kvp.Value;
}
if (compareResult == 0)
ans.Add(kvp.Key);
}
return ans;
}
}