Skip to content

Commit 302865e

Browse files
ianlancetaylorgopherbot
authored andcommitted
slices: update to current standard library version
Update x/exp/slices to the current standard library slices package, while retaining the ability to use it with Go 1.18 through Go 1.20. Note that this changes some of the sorting functions to use a comparison function rather than a less function. We don't promise backward compatibility in x/exp packages. Being compatible with the Go 1.21 package seems more useful for people not yet using 1.21, as it will make the transition to 1.21 easier. The generated files were built using "go generate" with a GOROOT that included CL 511660. Fixes golang/go#61374 Change-Id: I4abfd9db92d553f554aec83d60f0c13fa56c1d8e Reviewed-on: https://go-review.googlesource.com/c/exp/+/511895 TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Eli Bendersky <[email protected]>
1 parent d98519c commit 302865e

8 files changed

+942
-229
lines changed

slices/cmp.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package slices
6+
7+
import "golang.org/x/exp/constraints"
8+
9+
// min is a version of the predeclared function from the Go 1.21 release.
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+
// max is a version of the predeclared function from the Go 1.21 release.
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+
// cmpLess is a copy of cmp.Less from the Go 1.21 release.
26+
func cmpLess[T constraints.Ordered](x, y T) bool {
27+
return (isNaN(x) && !isNaN(y)) || x < y
28+
}
29+
30+
// cmpCompare is a copy of cmp.Compare from the Go 1.21 release.
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+
}

0 commit comments

Comments
 (0)