-
Notifications
You must be signed in to change notification settings - Fork 85
/
cmp.go
33 lines (29 loc) · 844 Bytes
/
cmp.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
package util
import "golang.org/x/exp/constraints"
// Compare exists as a temporary measure to provide a simple cmp()
// implementation for any ordered types. This was implemented to
// enable us to handle the exp/slices update to SortFunc() which
// switched from expecting a less() function to a cmp() function.
//
// Once we upgrade to Go 1.21 we can remove this and use the builtin
// cmp package - see https://pkg.go.dev/cmp
type Compare[T constraints.Ordered] struct{}
// Cmp returns -1 if a < b, 0 if a == b, and 1 if a > b.
func (c Compare[T]) Cmp(a, b T) int {
if a < b {
return -1
} else if a > b {
return 1
}
return 0
}
// CmpRev returns -1 if a > b, 0 if a == b, and 1 if a < b.
// This is the reverse of Cmp
func (c Compare[T]) CmpRev(a, b T) int {
if a < b {
return 1
} else if a > b {
return -1
}
return 0
}