mirror of
https://github.com/golang/go
synced 2024-11-18 01:34:44 -07:00
sort: add gen_sort_variants support for x/exp/slices
Also add a go:generate command to the standard library slices package. For #61374 Change-Id: I7aae8e451b7c6c4390e0344257478d1a96a14189 Reviewed-on: https://go-review.googlesource.com/c/go/+/511660 Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Eli Bendersky <eliben@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
6f597a8a93
commit
862fa6d099
@ -2,6 +2,8 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
//go:generate go run $GOROOT/src/sort/gen_sort_variants.go -generic
|
||||||
|
|
||||||
package slices
|
package slices
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -68,51 +68,9 @@ type Variant struct {
|
|||||||
Funcs template.FuncMap
|
Funcs template.FuncMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
var (
|
||||||
genGeneric := flag.Bool("generic", false, "generate generic versions")
|
traditionalVariants = []Variant{
|
||||||
flag.Parse()
|
Variant{
|
||||||
|
|
||||||
if *genGeneric {
|
|
||||||
generate(&Variant{
|
|
||||||
Name: "generic_ordered",
|
|
||||||
Path: "zsortordered.go",
|
|
||||||
Package: "slices",
|
|
||||||
Imports: "import \"cmp\"\n",
|
|
||||||
FuncSuffix: "Ordered",
|
|
||||||
TypeParam: "[E cmp.Ordered]",
|
|
||||||
ExtraParam: "",
|
|
||||||
ExtraArg: "",
|
|
||||||
DataType: "[]E",
|
|
||||||
Funcs: template.FuncMap{
|
|
||||||
"Less": func(name, i, j string) string {
|
|
||||||
return fmt.Sprintf("cmp.Less(%s[%s], %s[%s])", name, i, name, j)
|
|
||||||
},
|
|
||||||
"Swap": func(name, i, j string) string {
|
|
||||||
return fmt.Sprintf("%s[%s], %s[%s] = %s[%s], %s[%s]", name, i, name, j, name, j, name, i)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
generate(&Variant{
|
|
||||||
Name: "generic_func",
|
|
||||||
Path: "zsortanyfunc.go",
|
|
||||||
Package: "slices",
|
|
||||||
FuncSuffix: "CmpFunc",
|
|
||||||
TypeParam: "[E any]",
|
|
||||||
ExtraParam: ", cmp func(a, b E) int",
|
|
||||||
ExtraArg: ", cmp",
|
|
||||||
DataType: "[]E",
|
|
||||||
Funcs: template.FuncMap{
|
|
||||||
"Less": func(name, i, j string) string {
|
|
||||||
return fmt.Sprintf("(cmp(%s[%s], %s[%s]) < 0)", name, i, name, j)
|
|
||||||
},
|
|
||||||
"Swap": func(name, i, j string) string {
|
|
||||||
return fmt.Sprintf("%s[%s], %s[%s] = %s[%s], %s[%s]", name, i, name, j, name, j, name, i)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
generate(&Variant{
|
|
||||||
Name: "interface",
|
Name: "interface",
|
||||||
Path: "zsortinterface.go",
|
Path: "zsortinterface.go",
|
||||||
Package: "sort",
|
Package: "sort",
|
||||||
@ -130,9 +88,8 @@ func main() {
|
|||||||
return fmt.Sprintf("%s.Swap(%s, %s)", name, i, j)
|
return fmt.Sprintf("%s.Swap(%s, %s)", name, i, j)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
},
|
||||||
|
Variant{
|
||||||
generate(&Variant{
|
|
||||||
Name: "func",
|
Name: "func",
|
||||||
Path: "zsortfunc.go",
|
Path: "zsortfunc.go",
|
||||||
Package: "sort",
|
Package: "sort",
|
||||||
@ -150,7 +107,105 @@ func main() {
|
|||||||
return fmt.Sprintf("%s.Swap(%s, %s)", name, i, j)
|
return fmt.Sprintf("%s.Swap(%s, %s)", name, i, j)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
genericVariants = []Variant{
|
||||||
|
Variant{
|
||||||
|
Name: "generic_ordered",
|
||||||
|
Path: "zsortordered.go",
|
||||||
|
Package: "slices",
|
||||||
|
Imports: "import \"cmp\"\n",
|
||||||
|
FuncSuffix: "Ordered",
|
||||||
|
TypeParam: "[E cmp.Ordered]",
|
||||||
|
ExtraParam: "",
|
||||||
|
ExtraArg: "",
|
||||||
|
DataType: "[]E",
|
||||||
|
Funcs: template.FuncMap{
|
||||||
|
"Less": func(name, i, j string) string {
|
||||||
|
return fmt.Sprintf("cmp.Less(%s[%s], %s[%s])", name, i, name, j)
|
||||||
|
},
|
||||||
|
"Swap": func(name, i, j string) string {
|
||||||
|
return fmt.Sprintf("%s[%s], %s[%s] = %s[%s], %s[%s]", name, i, name, j, name, j, name, i)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Variant{
|
||||||
|
Name: "generic_func",
|
||||||
|
Path: "zsortanyfunc.go",
|
||||||
|
Package: "slices",
|
||||||
|
FuncSuffix: "CmpFunc",
|
||||||
|
TypeParam: "[E any]",
|
||||||
|
ExtraParam: ", cmp func(a, b E) int",
|
||||||
|
ExtraArg: ", cmp",
|
||||||
|
DataType: "[]E",
|
||||||
|
Funcs: template.FuncMap{
|
||||||
|
"Less": func(name, i, j string) string {
|
||||||
|
return fmt.Sprintf("(cmp(%s[%s], %s[%s]) < 0)", name, i, name, j)
|
||||||
|
},
|
||||||
|
"Swap": func(name, i, j string) string {
|
||||||
|
return fmt.Sprintf("%s[%s], %s[%s] = %s[%s], %s[%s]", name, i, name, j, name, j, name, i)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expVariants = []Variant{
|
||||||
|
Variant{
|
||||||
|
Name: "exp_ordered",
|
||||||
|
Path: "zsortordered.go",
|
||||||
|
Package: "slices",
|
||||||
|
Imports: "import \"golang.org/x/exp/constraints\"\n",
|
||||||
|
FuncSuffix: "Ordered",
|
||||||
|
TypeParam: "[E constraints.Ordered]",
|
||||||
|
ExtraParam: "",
|
||||||
|
ExtraArg: "",
|
||||||
|
DataType: "[]E",
|
||||||
|
Funcs: template.FuncMap{
|
||||||
|
"Less": func(name, i, j string) string {
|
||||||
|
return fmt.Sprintf("cmpLess(%s[%s], %s[%s])", name, i, name, j)
|
||||||
|
},
|
||||||
|
"Swap": func(name, i, j string) string {
|
||||||
|
return fmt.Sprintf("%s[%s], %s[%s] = %s[%s], %s[%s]", name, i, name, j, name, j, name, i)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Variant{
|
||||||
|
Name: "exp_func",
|
||||||
|
Path: "zsortanyfunc.go",
|
||||||
|
Package: "slices",
|
||||||
|
FuncSuffix: "CmpFunc",
|
||||||
|
TypeParam: "[E any]",
|
||||||
|
ExtraParam: ", cmp func(a, b E) int",
|
||||||
|
ExtraArg: ", cmp",
|
||||||
|
DataType: "[]E",
|
||||||
|
Funcs: template.FuncMap{
|
||||||
|
"Less": func(name, i, j string) string {
|
||||||
|
return fmt.Sprintf("(cmp(%s[%s], %s[%s]) < 0)", name, i, name, j)
|
||||||
|
},
|
||||||
|
"Swap": func(name, i, j string) string {
|
||||||
|
return fmt.Sprintf("%s[%s], %s[%s] = %s[%s], %s[%s]", name, i, name, j, name, j, name, i)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
genGeneric := flag.Bool("generic", false, "generate generic versions")
|
||||||
|
genExp := flag.Bool("exp", false, "generate x/exp/slices versions")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
var variants []Variant
|
||||||
|
if *genExp {
|
||||||
|
variants = expVariants
|
||||||
|
} else if *genGeneric {
|
||||||
|
variants = genericVariants
|
||||||
|
} else {
|
||||||
|
variants = traditionalVariants
|
||||||
|
}
|
||||||
|
for i := range variants {
|
||||||
|
generate(&variants[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user