-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathminmax.go
248 lines (233 loc) · 8.95 KB
/
minmax.go
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package go2linq
import (
"cmp"
"iter"
"github.com/solsw/errorhelper"
"github.com/solsw/generichelper"
)
// 'selector' projects each element of 'source'
// 'less' compares the projected values
// if 'min', function searches for minimum, otherwise - for maximum
// returns: element of sequence, corresponding projected value, count of 'source' elements
func minMaxPrim[Source, Result any](source iter.Seq[Source], selector func(Source) Result,
less func(Result, Result) bool, min bool) (Source, Result, int) {
count := 0
first := true
var rs Source
var rr Result
for s := range source {
count++
if first {
first = false
rs = s
rr = selector(s)
continue
}
r := selector(s)
if (min && less(r, rr)) || (!min && less(rr, r)) {
rs = s
rr = r
}
}
return rs, rr, count
}
// [Min] returns the minimum value in a sequence.
//
// [Min]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.min
func Min[Source cmp.Ordered](source iter.Seq[Source]) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
r, err := MinSel(source, Identity[Source])
if err != nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(err)
}
return r, nil
}
// [MinLs] returns the minimum value in a sequence using a specified less.
//
// [MinLs]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.min
func MinLs[Source any](source iter.Seq[Source], less func(Source, Source) bool) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
if less == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilLess)
}
r, err := MinSelLs(source, Identity[Source], less)
if err != nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(err)
}
return r, nil
}
// [MinSel] invokes a transform function on each element of a sequence and returns the minimum resulting value.
//
// [MinSel]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.min
func MinSel[Source any, Result cmp.Ordered](source iter.Seq[Source], selector func(Source) Result) (Result, error) {
if source == nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrNilSource)
}
if selector == nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrNilSelector)
}
r, err := MinSelLs(source, selector, cmp.Less[Result])
if err != nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(err)
}
return r, nil
}
// [MinSelLs] invokes a transform function on each element of a sequence
// and returns the minimum resulting value using a specified less.
//
// [MinSelLs]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.min
func MinSelLs[Source, Result any](source iter.Seq[Source], selector func(Source) Result, less func(Result, Result) bool) (Result, error) {
if source == nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrNilSource)
}
if selector == nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrNilSelector)
}
if less == nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrNilLess)
}
_, min, count := minMaxPrim(source, selector, less, true)
if count == 0 {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrEmptySource)
}
return min, nil
}
// [MinBySel] returns the value in a sequence that produces the minimum key according to a key selector function.
//
// [MinBySel]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.minby
func MinBySel[Source any, Key cmp.Ordered](source iter.Seq[Source], selector func(Source) Key) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
if selector == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSelector)
}
r, err := MinBySelLs(source, selector, cmp.Less[Key])
if err != nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(err)
}
return r, nil
}
// [MinBySelLs] returns the value in a sequence that produces the minimum key according to a key selector function and a key less.
//
// [MinBySelLs]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.minby
func MinBySelLs[Source, Key any](source iter.Seq[Source], selector func(Source) Key, less func(Key, Key) bool) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
if selector == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSelector)
}
if less == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilLess)
}
min, _, count := minMaxPrim(source, selector, less, true)
if count == 0 {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrEmptySource)
}
return min, nil
}
// [Max] returns the maximum value in a sequence.
//
// [Max]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.max
func Max[Source cmp.Ordered](source iter.Seq[Source]) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
r, err := MaxSel(source, Identity[Source])
if err != nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(err)
}
return r, nil
}
// [MaxLs] returns the maximum value in a sequence using a specified less.
//
// [MaxLs]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.max
func MaxLs[Source any](source iter.Seq[Source], less func(Source, Source) bool) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
if less == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilLess)
}
r, err := MaxSelLs(source, Identity[Source], less)
if err != nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(err)
}
return r, nil
}
// [MaxSel] invokes a transform function on each element of a sequence and returns the maximum resulting value.
//
// [MaxSel]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.max
func MaxSel[Source any, Result cmp.Ordered](source iter.Seq[Source], selector func(Source) Result) (Result, error) {
if source == nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrNilSource)
}
if selector == nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrNilSelector)
}
r, err := MaxSelLs(source, selector, cmp.Less[Result])
if err != nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(err)
}
return r, nil
}
// [MaxSelLs] invokes a transform function on each element of a sequence
// and returns the maximum resulting value using a specified less.
//
// [MaxSelLs]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.max
func MaxSelLs[Source, Result any](source iter.Seq[Source], selector func(Source) Result, less func(Result, Result) bool) (Result, error) {
if source == nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrNilSource)
}
if selector == nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrNilSelector)
}
if less == nil {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrNilLess)
}
_, max, count := minMaxPrim(source, selector, less, false)
if count == 0 {
return generichelper.ZeroValue[Result](), errorhelper.CallerError(ErrEmptySource)
}
return max, nil
}
// [MaxBySel] returns the value in a sequence that produces the maximum key according to a key selector function.
//
// [MaxBySel]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.maxby
func MaxBySel[Source any, Key cmp.Ordered](source iter.Seq[Source], selector func(Source) Key) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
if selector == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSelector)
}
r, err := MaxBySelLs(source, selector, cmp.Less[Key])
if err != nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(err)
}
return r, nil
}
// [MaxBySelLs] returns the value in a sequence that produces the maximum key according to a key selector function and a key less.
//
// [MaxBySelLs]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.maxby
func MaxBySelLs[Source, Key any](source iter.Seq[Source], selector func(Source) Key, less func(Key, Key) bool) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
if selector == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSelector)
}
if less == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilLess)
}
max, _, count := minMaxPrim(source, selector, less, false)
if count == 0 {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrEmptySource)
}
return max, nil
}