mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-12 13:48:01 +00:00
all: update exp/slices and fix call sites
slices.SortFunc suffered a late-in-cycle API breakage. Updates #cleanup Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:

committed by
Dave Anderson

parent
90a7d3066c
commit
52212f4323
@@ -20,3 +20,40 @@ func Or[T comparable](list ...T) T {
|
||||
}
|
||||
return zero
|
||||
}
|
||||
|
||||
// Ordered is cmp.Ordered from Go 1.21.
|
||||
type Ordered interface {
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64 |
|
||||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
|
||||
~float32 | ~float64 |
|
||||
~string
|
||||
}
|
||||
|
||||
// Compare returns
|
||||
//
|
||||
// -1 if x is less than y,
|
||||
// 0 if x equals y,
|
||||
// +1 if x is greater than y.
|
||||
//
|
||||
// For floating-point types, a NaN is considered less than any non-NaN,
|
||||
// a NaN is considered equal to a NaN, and -0.0 is equal to 0.0.
|
||||
func Compare[T Ordered](x, y T) int {
|
||||
xNaN := isNaN(x)
|
||||
yNaN := isNaN(y)
|
||||
if xNaN && yNaN {
|
||||
return 0
|
||||
}
|
||||
if xNaN || x < y {
|
||||
return -1
|
||||
}
|
||||
if yNaN || x > y {
|
||||
return +1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// isNaN reports whether x is a NaN without requiring the math package.
|
||||
// This will always return false if T is not floating-point.
|
||||
func isNaN[T Ordered](x T) bool {
|
||||
return x != x
|
||||
}
|
||||
|
Reference in New Issue
Block a user