...
1
2
3
4
5 package slices
6
7 import "golang.org/x/exp/constraints"
8
9
10 func min[T constraints.Ordered](a, b T) T {
11 if a < b || isNaN(a) {
12 return a
13 }
14 return b
15 }
16
17
18 func max[T constraints.Ordered](a, b T) T {
19 if a > b || isNaN(a) {
20 return a
21 }
22 return b
23 }
24
25
26 func cmpLess[T constraints.Ordered](x, y T) bool {
27 return (isNaN(x) && !isNaN(y)) || x < y
28 }
29
30
31 func cmpCompare[T constraints.Ordered](x, y T) int {
32 xNaN := isNaN(x)
33 yNaN := isNaN(y)
34 if xNaN && yNaN {
35 return 0
36 }
37 if xNaN || x < y {
38 return -1
39 }
40 if yNaN || x > y {
41 return +1
42 }
43 return 0
44 }
45
View as plain text